Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Is Time.h Multi-Platform-Safe?

This thread is locked; no one can reply to it. rss feed Print
Is Time.h Multi-Platform-Safe?
ZoriaRPG
Member #16,714
July 2017
avatar

How strong is the multi-platform support for Time.h?

Do any of you know if it doesn't behave in the same manner on Windows, Linux and OSX; or if there are there any (compiler, or other) quriks with it? I'm compiling with MSVC (C++, not C99 for this), so I hope that it's uniform.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

ZoriaRPG
Member #16,714
July 2017
avatar

Aye, just minimal date and time. TY.

I'd never use that for a precision timer. The intended use is to match the same general 'time of day', both in-game, and in the real world. In theory, it can also be used to measure if the game has been accelerated, or if it is lagging (in either case, considerably against an average of 60fps).

I did this, in the event that anyone else ever want to steal this code:

#SelectExpand
1enum { curyear, curmonth, curday_month, curday_week, curhour, 2 curminute, cursecond, curdayyear, curDST, curTimeLAST }; 3 4 5int FFScript::getTime(int type) 6{ 7 struct tm * tm_struct; 8 time_t sysRTC; 9 time (&sysRTC); 10 tm_struct = localtime (&sysRTC); 11 int rval = -1; 12 13 switch(type) 14 { 15 case curyear: 16 { 17 //Year format starts at 1900, yeat 18 //A raw read of '2018' would be '118', so we add 1900 to it to derive the actual year. 19 rval = tm_struct->tm_year + 1900; break; 20 21 } 22 case curmonth: 23 { 24 //Months start at 0, but we want 1->12 25 //al_trace("The current month is: %d\n",month); 26 rval = tm_struct->tm_mon +1; break; 27 } 28 case curday_month: 29 { 30 rval = tm_struct->tm_mday; break; 31 } 32 case curday_week: 33 { 34 //It seems that weekdays are a value range of 1 to 7. 35 rval = tm_struct->tm_wday; break; 36 } 37 case curhour: 38 { 39 rval = tm_struct->tm_hour; break; 40 } 41 case curminute: 42 { 43 rval = tm_struct->tm_min; break; 44 } 45 case cursecond: 46 { 47 rval = tm_struct->tm_sec; break; 48 } 49 case curdayyear: 50 { 51 //The day (n/365) out of the entire year. 52 rval = tm_struct->tm_yday; break; 53 } 54 case curDST: 55 { 56 //Returns if the user is in a Time Zone with Daylight TIme of some sort. 57 //View the time.h docs for the actual values of this struct element. 58 rval = tm_struct->tm_isdst;; break; 59 } 60 default: 61 { 62 al_trace("Invalid category passed to GetSystemTime(%d)\n",type); 63 rval = -1; break; 64 } 65 66 } 67 return rval; 68}

Go to: