Suggestion Change GetDate so it returns milliseconds as well

Martin

Active member
CX Code Contributor
3rd Party Module Dev
Tutorial Author
Joined
Jun 19, 2017
Messages
334
Problem: Apparently the gametarget.cpp GetDate function doesn't return milliseconds. This only happens for the Desktop target, all other are fine.

Solution: My proposal would be to change to change the function void BBGame::GetDate( Array<int> date ) in gametarget.cpp to:

C++:
void BBGame::GetDate( Array<int> date ){
    int n=date.Length();
    if( n>0 ){
        time_t t=time( 0 );
    
        struct timeval tp;
    
#if _WIN32
        static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL);

        SYSTEMTIME  system_time;
        FILETIME    file_time;
        uint64_t    time;

        GetSystemTime( &system_time );
        SystemTimeToFileTime( &system_time, &file_time );
        time =  ((uint64_t)file_time.dwLowDateTime )      ;
        time += ((uint64_t)file_time.dwHighDateTime) << 32;

        tp.tv_sec  = (long) ((time - EPOCH) / 10000000L);
        tp.tv_usec = (long) (system_time.wMilliseconds * 1000);
#else
        gettimeofday(&tp, NULL);
#endif

#if _MSC_VER
        struct tm tii;
        struct tm *ti=&tii;
        localtime_s( ti,&t );
#else
        struct tm *ti=localtime( &t );
#endif

        date[0]=ti->tm_year+1900;
        if( n>1 ){
            date[1]=ti->tm_mon+1;
            if( n>2 ){
                date[2]=ti->tm_mday;
                if( n>3 ){
                    date[3]=ti->tm_hour;
                    if( n>4 ){
                        date[4]=ti->tm_min;
                        if( n>5 ){
                            date[5]=ti->tm_sec;
                            if( n>6 ){
                                date[6]=tp.tv_usec / 1000;
                            }
                        }
                    }
                }
            }
        }
    }
}

Should work for Windows, Linux and hopefully Mac, even though I've just tested the Windows version.
 
Last edited by a moderator:

MikeHart

Administrator
Joined
Jun 19, 2017
Messages
3,165
I can test this on OSX soon. If it works, we will take your version in.
 

Martin

Active member
CX Code Contributor
3rd Party Module Dev
Tutorial Author
Joined
Jun 19, 2017
Messages
334
Oh thanks and please also for Linux. ;)
 
Top Bottom