xtime.cpp 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /****************************************************************************\
  19. xtime Neal Kettler
  20. Improved version of the Wtime library (V->W->X...)
  21. Handles signed times better and dates long
  22. long long after you'll be dead.
  23. \****************************************************************************/
  24. #include <ctype.h>
  25. #include <time.h>
  26. #ifndef _WINDOWS
  27. #include <sys/time.h>
  28. #endif
  29. #include "xtime.h"
  30. static char *DAYS[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
  31. static char *FULLDAYS[]={"Sunday","Monday","Tuesday","Wednesday","Thursday",
  32. "Friday","Saturday"};
  33. static char *MONTHS[]={"Jan","Feb","Mar","Apr","May","Jun","Jul",
  34. "Aug","Sep","Oct","Nov","Dec"};
  35. static char *FULLMONTHS[]={"January","February","March","April","May","June",
  36. "July","August","September","October","November","December"};
  37. #define IS_LEAP(y) ((y) % 4) == 0 && (! ((y) % 100) == 0 || ((y) % 400) == 0)
  38. #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
  39. /////////////// Utility functions ///////////////////
  40. //
  41. // Return the daycount since year 0 for the specified date.
  42. // month = 1-12, day = 1-31 year = 0...
  43. //
  44. static sint32 Get_Day(int month, int day, int year)
  45. {
  46. time_t days;
  47. static int DaysAtMonth[] =
  48. {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  49. /* Add up number of straight days + number of leap days + days */
  50. /* up to the month + the day of month. */
  51. days = (year * 365) + (year / 4) - (year / 100) + (year / 400) +
  52. DaysAtMonth[month-1] + day;
  53. /* If we haven't hit Feb 29, and this is a leap year, we need to */
  54. /* subtract out the leap day that was added above for this year */
  55. if (month < 3 && IS_LEAP(year))
  56. --days;
  57. return(days);
  58. }
  59. //
  60. // Get the year from a daycount since year 0
  61. // Also get the daycount since the start of the year
  62. //
  63. // Ayecarumba what a pain in the ass!
  64. //
  65. static bit8 Get_Date_From_Day(sint32 days, OUT sint32 &year, OUT sint32 &yday)
  66. {
  67. //register long int rem;
  68. register long int y;
  69. //register const unsigned short int *ip;
  70. if (days <= 365)
  71. {
  72. year=0;
  73. yday=days+1; // 1 based
  74. return(TRUE);
  75. }
  76. y = 1;
  77. days-=365;
  78. days--; // zero based
  79. //
  80. // As far as I can tell there's no non-iteritive way to
  81. // do this...
  82. //
  83. while (days < 0 || days >= (IS_LEAP (y) ? 366 : 365))
  84. {
  85. /* Guess a corrected year, assuming 365 days per year. */
  86. long int yg = y + days / 365 - (days % 365 < 0);
  87. /* Adjust DAYS and Y to match the guessed year. */
  88. days -= ((yg - y) * 365
  89. + LEAPS_THRU_END_OF (yg - 1)
  90. - LEAPS_THRU_END_OF (y - 1));
  91. y = yg;
  92. }
  93. year=y;
  94. yday=days+1; // 1 based
  95. return(TRUE);
  96. }
  97. //
  98. // Get the max day of a given month in a given year
  99. //
  100. int Max_Day(int month, int year)
  101. {
  102. static char dayTable[2][13] = {
  103. {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  104. {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  105. };
  106. bit8 isleap=IS_LEAP(year);
  107. return(dayTable[isleap][month]);
  108. }
  109. /**********************************************************
  110. int main(int argc, char *argv[])
  111. {
  112. Xtime wtime;
  113. int month,mday,year,hour,minute,second,day;
  114. int i;
  115. int dayoffset;
  116. int yr;
  117. int yday;
  118. int ydaycount;
  119. wtime.set(719528,0);
  120. ***********************************************
  121. for (year=0; year<10000; year++)
  122. {
  123. ydaycount=1;
  124. for (month=1; month<=12; month++)
  125. {
  126. for (day=1; day<=Max_Day(month,year); day++)
  127. {
  128. dayoffset=Get_Day(month,day,year);
  129. assert (GetDateFromDay(dayoffset,yr,yday)==TRUE);
  130. //printf("Yday=%d YdayCount=%d\n",yday,ydaycount);
  131. if (yr!=year)
  132. {
  133. printf("MO=%d DAY=%d YEAR=%d YR=%d\n",month,day,year,yr);
  134. assert(0);
  135. }
  136. if (yday != ydaycount)
  137. {
  138. printf("MO=%d DAY=%d YEAR=%d YR=%d\n",month,day,year,yr);
  139. printf("Yday=%d YdayCount=%d\n",yday,ydaycount);
  140. assert(0);
  141. }
  142. ydaycount++;
  143. }
  144. }
  145. printf("(%d) ",year);
  146. }
  147. ***************************************
  148. ///////wtime.addSeconds((60*60*24)-(60*60*8));
  149. ////////wtime.addSeconds(-(60*60*8)); // timezone delta
  150. dayoffset=Get_Day(1,1,1970);
  151. printf("DAYOFFSET = %d\n",dayoffset);
  152. wtime.getTime(month, mday, year, hour, minute, second);
  153. printf("\n%s %d %d %d:%02d:%02d\n\n",
  154. MONTHS[month-1],mday,year,hour,minute,second);
  155. struct timeval unixtime;
  156. struct timezone unixtzone;
  157. time_t ttime;
  158. tm tmtime;
  159. memset(&tmtime,0,sizeof(tmtime));
  160. ttime=0;
  161. unixtime.tv_sec=0;
  162. unixtime.tv_usec=0;
  163. //gettimeofday(&unixtime,&unixtzone);
  164. //ttime=time(NULL);
  165. tmtime=*gmtime(&ttime);
  166. printf("TIME->CTIME = %s\n",ctime(&ttime));
  167. printf("GTOD->CTIE = %s\n",ctime(&(unixtime.tv_sec)));
  168. printf("TIME->GMT->ASCTIME = %s\n",asctime(&tmtime));
  169. }
  170. ***************************************************************/
  171. //
  172. // Construct with current clock time
  173. //
  174. Xtime::Xtime(void)
  175. {
  176. update();
  177. }
  178. //
  179. // Copy constructor
  180. //
  181. Xtime::Xtime( Xtime &other )
  182. {
  183. day_=other.day_;
  184. msec_=other.msec_;
  185. }
  186. //
  187. // Set to a time_t (1970-2038)
  188. //
  189. Xtime::Xtime( time_t other )
  190. {
  191. day_=719528; // days from year 0 to Jan1, 1970
  192. // Add seconds from time_t
  193. addSeconds(other);
  194. msec_=0;
  195. }
  196. Xtime::~Xtime()
  197. {
  198. }
  199. //
  200. // Add some number of seconds to the time (seconds can be negative)
  201. //
  202. void Xtime::addSeconds(sint32 seconds)
  203. {
  204. // Add to day counter first
  205. day_+=(seconds/86400);
  206. msec_+=(seconds % 86400)*1000;
  207. // Now normalize in case msec is > 1 days worth
  208. normalize();
  209. }
  210. //
  211. // If msec is > 1 days worth, adjust the day count
  212. // & decrement the msec counter until OK.
  213. //
  214. void Xtime::normalize(void)
  215. {
  216. day_+=(msec_/86400000);
  217. msec_%=86400000;
  218. while (msec_ < 0)
  219. {
  220. day_--;
  221. msec_+=86400000;
  222. }
  223. }
  224. //
  225. // Update time to hold the current clock time
  226. // (breaks in 2038 :-)
  227. //
  228. void Xtime::update(void)
  229. {
  230. day_=719528; // day_s from year 0 to Jan1, 1970
  231. msec_=0;
  232. #ifdef _WINDOWS
  233. struct _timeb wintime;
  234. _ftime(&wintime);
  235. addSeconds(wintime.time);
  236. msec_+=wintime.millitm;
  237. #endif
  238. #ifndef _WINDOWS
  239. struct timeval unixtime;
  240. struct timezone unixtzone;
  241. gettimeofday(&unixtime,&unixtzone);
  242. addSeconds(unixtime.tv_sec);
  243. msec_+=(unixtime.tv_usec/1000);
  244. #endif
  245. // Now normalize in case msec is > 1 days worth
  246. normalize();
  247. }
  248. // This takes the standard Microsoft time formatting string
  249. // make sure the out string is big enough
  250. // An example format would be "mm/dd/yy hh:mm:ss"
  251. // CHANGE: Joe Howes 06/30/99
  252. // To specify 12-hour format, use "aa" instead of "hh".
  253. // The hours will be 12 hour and the string will be
  254. // appended with " AM" or " PM".
  255. bit8 Xtime::FormatTime(char *out, char *format)
  256. {
  257. int lastWasH=0;
  258. int ampmflag = 0;
  259. out[0]=0;
  260. char *ptr=format;
  261. if (*ptr=='"') ptr++; // skip past open quote if exists
  262. while (*ptr!=0)
  263. {
  264. if (lastWasH>0)
  265. lastWasH--;
  266. if (isspace(*ptr))
  267. {
  268. if (lastWasH==1) lastWasH=2;
  269. sprintf(out+strlen(out),"%c",*ptr);
  270. ptr+=1;
  271. }
  272. else if (strncmp(ptr,"\"",1)==0)
  273. {
  274. break;
  275. }
  276. else if (strncmp(ptr,":",1)==0)
  277. {
  278. if (lastWasH==1) lastWasH=2;
  279. sprintf(out+strlen(out),":");
  280. ptr+=1;
  281. }
  282. else if (strncmp(ptr,"/",1)==0)
  283. {
  284. sprintf(out+strlen(out),"/");
  285. ptr+=1;
  286. }
  287. else if (strncmp(ptr,"c",1)==0)
  288. {
  289. sprintf(out+strlen(out),"%ld/%ld/%02ld %ld:%02ld:%02ld",getMonth(),
  290. getMDay(),getYear()%100,getHour(),getMinute(),getSecond());
  291. ptr+=1;
  292. }
  293. else if (strncmp(ptr,"dddddd",6)==0)
  294. {
  295. sprintf(out+strlen(out),"%s %02ld, %ld",FULLMONTHS[getMonth()-1],
  296. getMDay(),getYear());
  297. ptr+=6;
  298. }
  299. else if (strncmp(ptr,"ddddd",5)==0)
  300. {
  301. sprintf(out+strlen(out),"%ld/%ld/%02ld",getMonth(),getMDay(),
  302. getYear()%100);
  303. ptr+=5;
  304. }
  305. /*else if (strncmp(ptr,"dddd",4)==0)
  306. {
  307. sprintf(out+strlen(out),"%s",FULLDAYS[getWDay()-1]);
  308. ptr+=4;
  309. }
  310. else if (strncmp(ptr,"ddd",3)==0)
  311. {
  312. sprintf(out+strlen(out),"%s",DAYS[getWDay()-1]);
  313. ptr+=3;
  314. }*/
  315. else if (strncmp(ptr,"dd",2)==0)
  316. {
  317. sprintf(out+strlen(out),"%02ld",getMDay());
  318. ptr+=2;
  319. }
  320. else if (strncmp(ptr,"d",1)==0)
  321. {
  322. sprintf(out+strlen(out),"%ld",getMDay());
  323. ptr+=1;
  324. }
  325. /*else if (strncmp(ptr,"ww",2)==0)
  326. {
  327. sprintf(out+strlen(out),"%02ld",getYWeek());
  328. ptr+=2;
  329. }*/
  330. /*else if (strncmp(ptr,"w",1)==0)
  331. {
  332. sprintf(out+strlen(out),"%ld",getWDay());
  333. ptr+=1;
  334. }*/
  335. else if (strncmp(ptr,"mmmm",4)==0)
  336. {
  337. sprintf(out+strlen(out),"%s",FULLMONTHS[getMonth()-1]);
  338. ptr+=4;
  339. }
  340. else if (strncmp(ptr,"mmm",3)==0)
  341. {
  342. sprintf(out+strlen(out),"%s",MONTHS[getMonth()-1]);
  343. ptr+=3;
  344. }
  345. else if (strncmp(ptr,"mm",2)==0)
  346. {
  347. if (lastWasH==1)
  348. sprintf(out+strlen(out),"%02ld",getMinute());
  349. else
  350. sprintf(out+strlen(out),"%02ld",getMonth());
  351. ptr+=2;
  352. }
  353. else if (strncmp(ptr,"m",1)==0)
  354. {
  355. if (lastWasH==1)
  356. sprintf(out+strlen(out),"%ld",getMinute());
  357. else
  358. sprintf(out+strlen(out),"%ld",getMonth());
  359. ptr+=1;
  360. }
  361. else if (strncmp(ptr,"q",1)==0)
  362. {
  363. sprintf(out+strlen(out),"%ld",((getMonth()-1)/4)+1); // GetQuarter
  364. ptr+=1;
  365. }
  366. else if (strncmp(ptr,"yyyy",4)==0)
  367. {
  368. sprintf(out+strlen(out),"%ld",getYear());
  369. ptr+=4;
  370. }
  371. else if (strncmp(ptr,"yy",2)==0)
  372. {
  373. sprintf(out+strlen(out),"%02ld",getYear()%100);
  374. ptr+=2;
  375. }
  376. /*else if (strncmp(ptr,"y",1)==0)
  377. {
  378. sprintf(out+strlen(out),"%ld",getYDay());
  379. ptr+=1;
  380. }*/
  381. else if (strncmp(ptr,"hh",2)==0)
  382. {
  383. sprintf(out+strlen(out),"%02ld",getHour());
  384. lastWasH=2; // needs to be 1 after top of loop decs it
  385. ptr+=2;
  386. }
  387. else if (strncmp(ptr,"h",1)==0)
  388. {
  389. sprintf(out+strlen(out),"%ld",getHour());
  390. lastWasH=2; // needs to be 1 after top of loop decs it
  391. ptr+=1;
  392. }
  393. else if (strncmp(ptr,"nn",2)==0)
  394. {
  395. sprintf(out+strlen(out),"%02ld",getMinute());
  396. ptr+=2;
  397. }
  398. else if (strncmp(ptr,"n",1)==0)
  399. {
  400. sprintf(out+strlen(out),"%ld",getMinute());
  401. ptr+=1;
  402. }
  403. else if (strncmp(ptr,"ss",2)==0)
  404. {
  405. sprintf(out+strlen(out),"%02ld",getSecond());
  406. ptr+=2;
  407. }
  408. else if (strncmp(ptr,"s",1)==0)
  409. {
  410. sprintf(out+strlen(out),"%ld",getSecond());
  411. ptr+=1;
  412. }
  413. else if (strncmp(ptr,"ttttt",5)==0)
  414. {
  415. sprintf(out+strlen(out),"%ld:%02ld:%02ld",getHour(),getMinute(),
  416. getSecond());
  417. ptr+=5;
  418. }
  419. else if (strncmp(ptr,"aa",2)==0)
  420. {
  421. uint32 tmp = (getHour() <= 12) ? getHour() : getHour() - 12;
  422. sprintf(out+strlen(out),"%02ld", tmp);
  423. lastWasH=2; // needs to be 1 after top of loop decs it
  424. ptr+=2;
  425. ampmflag = 1;
  426. }
  427. else // an unknown char, move to next
  428. ptr++;
  429. }
  430. if(ampmflag)
  431. {
  432. char ampm[4];
  433. if( getHour() < 12 )
  434. strcpy(ampm, " AM");
  435. else
  436. strcpy(ampm, " PM");
  437. sprintf(out+strlen(out), "%s", ampm);
  438. }
  439. return(TRUE);
  440. }
  441. /**************************************
  442. REPLACE THIS CRAP
  443. // Parses a date string that's in modified RFC 1123 format
  444. // Can have a +minutes after the normal time
  445. // eg: Thu, 20 Jun 1996 17:33:49 +100
  446. // Returns true if successfully parsed, false otherwise
  447. bit8 Xtime::ParseDate(char *in)
  448. {
  449. int i;
  450. uint32 minOffset;
  451. struct tm t;
  452. char *ptr=in;
  453. while ((!isgraph(*ptr))&&(*ptr!=0)) ptr++; // skip to start of string
  454. if (*ptr==0) return(FALSE);
  455. t.tm_wday_=-1;
  456. for (i=0; i<7; i++) // parse day_ of week
  457. if (strncmp(ptr,DAYS[i],strlen(DAYS[i]))==0)
  458. t.tm_wday_=i;
  459. if (t.tm_wday_==-1)
  460. return(FALSE);
  461. while ((!isdigit(*ptr))&&(*ptr!=0)) ptr++; // skip to day_ of month
  462. if (*ptr==0) return(FALSE);
  463. t.tm_mday_=atoi(ptr);
  464. while ((!isalpha(*ptr))&&(*ptr!=0)) ptr++; // skip to month
  465. if (*ptr==0) return(FALSE);
  466. t.tm_mon=-1;
  467. for (i=0; i<12; i++) // match month
  468. if (strncmp(ptr,MONTHS[i],strlen(MONTHS[i]))==0) t.tm_mon=i;
  469. if (t.tm_mon==-1) return(FALSE);
  470. while ((!isdigit(*ptr))&&(*ptr!=0)) ptr++;
  471. if (*ptr==0) return(FALSE);
  472. t.tm_year=atoi(ptr);
  473. if (t.tm_year<70) // if they specify a 2 digit year, we'll be nice
  474. t.tm_year+=2000;
  475. else if (t.tm_year<100)
  476. t.tm_year+=1900;
  477. if (t.tm_year>2200) // I doubt my code will be around for another 203 years
  478. return(FALSE);
  479. while ((isdigit(*ptr))&&(*ptr!=0)) ptr++; // skip to end of year
  480. if (*ptr==0) return(FALSE);
  481. while ((!isgraph(*ptr))&&(*ptr!=0)) ptr++; // skip to start of time
  482. if (*ptr==0) return(FALSE);
  483. t.tm_hour=atoi(ptr);
  484. while ((*ptr!=':')&&(*ptr!=0)) ptr++;
  485. ptr++; // skip past colon
  486. if (*ptr==0) return(FALSE);
  487. t.tm_min=atoi(ptr);
  488. while ((*ptr!=':')&&(*ptr!=0)) ptr++;
  489. ptr++; // skip past colon
  490. if (*ptr==0) return(FALSE);
  491. t.tm_sec=atoi(ptr);
  492. t.tm_year%=100; // 1996 is stored as 96, not 1996
  493. t.tm_isdst=-1; // day_light savings info isn't available
  494. sec=(uint32)(mktime(&t));
  495. if ((sint32)sec==-1)
  496. return(FALSE);
  497. // The next part of the time is OPTIONAL (+minutes)
  498. // first skip past the seconds
  499. while ((isdigit(*ptr))&&(*ptr!=0)) ptr++;
  500. if (*ptr==0) return(TRUE);
  501. // skip past any spaces
  502. while ((isspace(*ptr))&&(*ptr!=0)) ptr++;
  503. if (*ptr!='+')
  504. {
  505. //printf("\nNOPE ptr was '%s'\n",ptr);
  506. return(TRUE);
  507. }
  508. ptr++;
  509. if (*ptr==0)
  510. {
  511. //printf("\nPTR WAS 0\n");
  512. return(TRUE);
  513. }
  514. minOffset=atol(ptr);
  515. //printf("\n\nAdding %d minutes!\n\n",minOffset);
  516. sec+=minOffset*60; // add the minutes as seconds
  517. return(TRUE);
  518. }
  519. // In addition to PrintTime & PrintDate there is the 'Print' function
  520. // which prints both in RFC 1123 format
  521. void Xtime::PrintTime(FILE *out) const
  522. {
  523. char string[80];
  524. PrintTime(string);
  525. fprintf(out,"%s",string);
  526. }
  527. void Xtime::PrintTime(char *out) const
  528. {
  529. sprintf(out," %02lu:%02lu:%02lu",GetHour(),GetMinute(),GetSecond());
  530. }
  531. void Xtime::PrintDate(FILE *out) const
  532. {
  533. char string[80];
  534. PrintDate(string);
  535. fprintf(out,"%s",string);
  536. }
  537. void Xtime::PrintDate(char *out) const
  538. {
  539. sprintf(out,"%s, %lu %s %lu",DAYS[GetWDay()-1],GetMDay(),MONTHS[GetMonth()-1],
  540. GetYear());
  541. }
  542. ********************************************/
  543. // Get day_s since year 0
  544. sint32 Xtime::getDay(void) const
  545. {
  546. return(day_);
  547. }
  548. // Get msecs since start of day
  549. sint32 Xtime::getMsec(void) const
  550. {
  551. return(msec_);
  552. }
  553. // Set days since year 0
  554. void Xtime::setDay(sint32 newday)
  555. {
  556. day_=newday;
  557. }
  558. // Set msec since start of this day
  559. void Xtime::setMsec(sint32 newmsec)
  560. {
  561. msec_=newmsec;
  562. }
  563. // Set both
  564. void Xtime::set(sint32 newday, sint32 newmsec)
  565. {
  566. day_=newday;
  567. msec_=newmsec;
  568. }
  569. //
  570. // Get a timeval ptr from a Xtime class
  571. // May fail if timeval can't hold a year this big or small
  572. //
  573. bit8 Xtime::getTimeval(struct timeval &tv)
  574. {
  575. // A timeval can only hold dates from 1970-2038
  576. if ((day_ < 719528) || (day_ >= 719528+24855))
  577. return(FALSE);
  578. // Compute seconds since Jan 1, 1970
  579. uint32 seconds=day_-719528;
  580. seconds*=(60*60*24);
  581. seconds+=(msec_/1000);
  582. tv.tv_sec=seconds;
  583. tv.tv_usec=(msec_%1000)*1000;
  584. return(TRUE);
  585. }
  586. //
  587. // Set the time
  588. //
  589. bit8 Xtime::setTime(int month, int mday, int year, int hour, int minute, int second)
  590. {
  591. day_=Get_Day(month,mday,year);
  592. msec_=(hour*1000*60*60)+(minute*1000*60)+(second*1000);
  593. return(TRUE);
  594. }
  595. int Xtime::getYDay(void) const // Day of Year (1-366) (366 = leap yr)
  596. {
  597. int year;
  598. sint32 dayofyear;
  599. if (Get_Date_From_Day(day_,year,dayofyear)==FALSE)
  600. return(-1);
  601. return dayofyear;
  602. }
  603. //
  604. // Get all the components of the time in the usual normalized format.
  605. //
  606. // Most of the uglyness is in Get_Date_From_Day()
  607. //
  608. bit8 Xtime::getTime(int &month, int &mday, int &year, int &hour, int &minute, int &second) const
  609. {
  610. int i;
  611. sint32 dayofyear;
  612. if (Get_Date_From_Day(day_,year,dayofyear)==FALSE)
  613. return(FALSE);
  614. static int DaysAtMonth[2][12] = {
  615. {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}, // normal
  616. {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335} // leap year
  617. };
  618. month=0;
  619. bit8 isleap=IS_LEAP(year);
  620. for (i=0; i<12; i++)
  621. {
  622. if (DaysAtMonth[isleap][i] >= dayofyear)
  623. break;
  624. month=i;
  625. }
  626. month++; // 1 based
  627. mday=dayofyear-DaysAtMonth[isleap][month-1];
  628. // Whew! Now all we have to do is figure out H/M/S from the msec!
  629. hour=(msec_/3600000)%24; // 1000*60*60
  630. minute=(msec_/60000)%60; // 1000*60
  631. second=(msec_/ 1000)%60; // 1000
  632. return(TRUE);
  633. }
  634. //
  635. // These are for getting components of the time in the
  636. // standard ranges. Like Day 1-31, Second 0-59, etc...
  637. //
  638. int Xtime::getSecond(void) const
  639. {
  640. int month,mday,year,hour,minute,second;
  641. getTime(month, mday, year, hour, minute, second);
  642. return(second);
  643. }
  644. int Xtime::getMinute(void) const
  645. {
  646. int month,mday,year,hour,minute,second;
  647. getTime(month, mday, year, hour, minute, second);
  648. return(minute);
  649. }
  650. int Xtime::getHour(void) const
  651. {
  652. int month,mday,year,hour,minute,second;
  653. getTime(month, mday, year, hour, minute, second);
  654. return(hour);
  655. }
  656. int Xtime::getMDay(void) const
  657. {
  658. int month,mday,year,hour,minute,second;
  659. getTime(month, mday, year, hour, minute, second);
  660. return(mday);
  661. }
  662. int Xtime::getMonth(void) const
  663. {
  664. int month,mday,year,hour,minute,second;
  665. getTime(month, mday, year, hour, minute, second);
  666. return(month);
  667. }
  668. // based at year 0 (real 0, not 1970)
  669. int Xtime::getYear(void) const
  670. {
  671. int month,mday,year,hour,minute,second;
  672. getTime(month, mday, year, hour, minute, second);
  673. return(year);
  674. }
  675. //
  676. // Set the seconds value (0-59)
  677. //
  678. bit8 Xtime::setSecond(sint32 sec)
  679. {
  680. sint32 second=(msec_/ 1000)%60;
  681. msec_-=(second*1000);
  682. msec_+=(sec*1000);
  683. return(TRUE);
  684. }
  685. //
  686. // Set the minutes value (0-59)
  687. //
  688. bit8 Xtime::setMinute(sint32 min)
  689. {
  690. sint32 minute=(msec_/60000)%60; // 1000*60
  691. msec_-=(minute*60000);
  692. msec_+=(min*60000);
  693. return(TRUE);
  694. }
  695. //
  696. // Set the minutes value (0-23)
  697. //
  698. bit8 Xtime::setHour(sint32 hour)
  699. {
  700. hour=(msec_/3600000)%24; // 1000*60*60
  701. msec_-=(hour*3600000);
  702. msec_+=(hour*3600000);
  703. return(TRUE);
  704. }
  705. //
  706. // Set the year value
  707. // Results are undefined if you're moving from Feb 29, to a non leap year
  708. //
  709. bit8 Xtime::setYear(sint32 _year)
  710. {
  711. // extract the date
  712. int month,mday,year,hour,min,sec;
  713. getTime(month,mday,year,hour,min,sec);
  714. // modify & rebuild
  715. year=_year;
  716. day_=Get_Day(month,mday,year);
  717. return(TRUE);
  718. }
  719. //
  720. // Modify the month
  721. //
  722. bit8 Xtime::setMonth(sint32 _month)
  723. {
  724. // extract the date
  725. int month,mday,year,hour,min,sec;
  726. getTime(month,mday,year,hour,min,sec);
  727. // modify & rebuild
  728. month=_month;
  729. day_=Get_Day(month,mday,year);
  730. return(TRUE);
  731. }
  732. //
  733. // Modify the day of the month
  734. //
  735. bit8 Xtime::setMDay(sint32 _mday)
  736. {
  737. // extract the date
  738. int month,mday,year,hour,min,sec;
  739. getTime(month,mday,year,hour,min,sec);
  740. // modify & rebuild
  741. mday=_mday;
  742. day_=Get_Day(month,mday,year);
  743. return(TRUE);
  744. }
  745. //
  746. // Compare two times. The time better be normalized
  747. // which it would be unless you've put bad stuff in it!
  748. //
  749. // 1 = *this > other
  750. //-1 = *this < other
  751. // 0 = *this == other
  752. int Xtime::compare(const Xtime &other) const
  753. {
  754. if ((day_==other.day_)&&(msec_==other.msec_))
  755. return(0); // equal
  756. else if (day_>other.day_)
  757. return(1);
  758. else if (day_<other.day_)
  759. return(-1);
  760. else if (msec_>other.msec_)
  761. return(1);
  762. else
  763. return(-1);
  764. }
  765. bit8 Xtime::operator == ( const Xtime &other ) const
  766. {
  767. bit8 retval=compare(other);
  768. if (retval==0)
  769. return(TRUE);
  770. else
  771. return(FALSE);
  772. }
  773. bit8 Xtime::operator != ( const Xtime &other ) const
  774. {
  775. bit8 retval=compare(other);
  776. if (retval==0)
  777. return(FALSE);
  778. else
  779. return(TRUE);
  780. }
  781. bit8 Xtime::operator < ( const Xtime &other ) const
  782. {
  783. int retval=compare(other);
  784. if (retval==-1)
  785. return(TRUE);
  786. else
  787. return(FALSE);
  788. }
  789. bit8 Xtime::operator > ( const Xtime &other ) const
  790. {
  791. int retval=compare(other);
  792. if (retval==1)
  793. return(TRUE);
  794. else
  795. return(FALSE);
  796. }
  797. bit8 Xtime::operator <= ( const Xtime &other ) const
  798. {
  799. int retval=compare(other);
  800. if ((retval==-1)||(retval==0))
  801. return(TRUE);
  802. else
  803. return(FALSE);
  804. }
  805. bit8 Xtime::operator >= ( const Xtime &other ) const
  806. {
  807. int retval=compare(other);
  808. if ((retval==1)||(retval==0))
  809. return(TRUE);
  810. else
  811. return(FALSE);
  812. }
  813. Xtime &Xtime::operator += (const Xtime &other)
  814. {
  815. day_+=other.day_;
  816. msec_+=other.msec_;
  817. normalize();
  818. return *this;
  819. }
  820. Xtime &Xtime::operator -= (const Xtime &other)
  821. {
  822. day_-=other.day_;
  823. msec_-=other.msec_;
  824. normalize();
  825. return *this;
  826. }
  827. Xtime Xtime::operator - (Xtime &other)
  828. {
  829. Xtime temp(*this);
  830. temp-=other;
  831. return(temp);
  832. }
  833. Xtime Xtime::operator + (Xtime &other)
  834. {
  835. Xtime temp(*this);
  836. temp+=other;
  837. return(temp);
  838. }
  839. Xtime &Xtime::operator = (const Xtime &other)
  840. {
  841. day_=other.day_;
  842. msec_=other.msec_;
  843. return *this;
  844. }
  845. Xtime &Xtime::operator += (const time_t other)
  846. {
  847. addSeconds(other);
  848. return *this;
  849. }
  850. Xtime &Xtime::operator -= (const time_t other)
  851. {
  852. addSeconds(-((sint32)other));
  853. return *this;
  854. }
  855. Xtime Xtime::operator - (time_t other)
  856. {
  857. Xtime temp(*this);
  858. temp-=other;
  859. return(temp);
  860. }
  861. Xtime Xtime::operator + (time_t other)
  862. {
  863. Xtime temp(*this);
  864. temp+=other;
  865. return(temp);
  866. }
  867. Xtime &Xtime::operator = (const time_t other)
  868. {
  869. msec_=0;
  870. day_=719528; // Jan 1, 1970
  871. addSeconds(other);
  872. return *this;
  873. }