wtime.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*
  2. ** Command & Conquer Generals(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. wtime Neal Kettler
  20. \****************************************************************************/
  21. #include <ctype.h>
  22. #include "wtime.h"
  23. static char *DAYS[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
  24. static char *FULLDAYS[]={"Sunday","Monday","Tuesday","Wednesday","Thursday",
  25. "Friday","Saturday"};
  26. static char *MONTHS[]={"Jan","Feb","Mar","Apr","May","Jun","Jul",
  27. "Aug","Sep","Oct","Nov","Dec"};
  28. static char *FULLMONTHS[]={"January","February","March","April","May","June",
  29. "July","August","September","October","November","December"};
  30. // MDC: Windows doesn't provide a localtime_r, so make our own...
  31. #ifdef _WINDOWS
  32. #ifdef _REENTRANT
  33. #include "critsec.h"
  34. static CritSec localtime_critsec;
  35. #undef localtime
  36. _CRTIMP struct tm *localtime(const time_t *clockval);
  37. #endif // _REENTRANT
  38. static struct tm *localtime_r(const time_t *clockval, struct tm *res) {
  39. #ifdef _REENTRANT
  40. localtime_critsec.lock();
  41. #endif
  42. struct tm *static_tm = localtime(clockval);
  43. res = (struct tm *)memcpy(res, static_tm, sizeof(tm));
  44. #ifdef _REENTRANT
  45. localtime_critsec.unlock();
  46. #endif
  47. return res;
  48. }
  49. #endif // _WINDOWS
  50. Wtime::Wtime(void)
  51. {
  52. Update();
  53. }
  54. Wtime::Wtime( Wtime &other )
  55. {
  56. sign=other.sign;
  57. sec=other.sec;
  58. usec=other.usec;
  59. }
  60. Wtime::Wtime( uint32 other )
  61. {
  62. sign=POSITIVE;
  63. sec=other;
  64. usec=0;
  65. }
  66. Wtime::~Wtime()
  67. {
  68. }
  69. void Wtime::Update(void)
  70. {
  71. sign=POSITIVE;
  72. #ifdef _WINDOWS
  73. struct _timeb wintime;
  74. _ftime(&wintime);
  75. sec=wintime.time;
  76. usec=(wintime.millitm)*1000;
  77. #endif
  78. #ifndef _WINDOWS
  79. struct timeval unixtime;
  80. struct timezone unixtzone;
  81. gettimeofday(&unixtime,&unixtzone);
  82. sec=unixtime.tv_sec;
  83. usec=unixtime.tv_usec;
  84. #endif
  85. }
  86. // Parses a date string that's in modified RFC 1123 format
  87. // Can have a +minutes after the normal time
  88. // eg: Thu, 20 Jun 1996 17:33:49 +100
  89. // Returns true if successfully parsed, false otherwise
  90. bit8 Wtime::ParseDate(char *in)
  91. {
  92. int i;
  93. uint32 minOffset;
  94. struct tm t;
  95. char *ptr=in;
  96. while ((!isgraph(*ptr))&&(*ptr!=0)) ptr++; // skip to start of string
  97. if (*ptr==0) return(FALSE);
  98. t.tm_wday=-1;
  99. for (i=0; i<7; i++) // parse day of week
  100. if (strncmp(ptr,DAYS[i],strlen(DAYS[i]))==0)
  101. t.tm_wday=i;
  102. if (t.tm_wday==-1)
  103. return(FALSE);
  104. while ((!isdigit(*ptr))&&(*ptr!=0)) ptr++; // skip to day of month
  105. if (*ptr==0) return(FALSE);
  106. t.tm_mday=atoi(ptr);
  107. while ((!isalpha(*ptr))&&(*ptr!=0)) ptr++; // skip to month
  108. if (*ptr==0) return(FALSE);
  109. t.tm_mon=-1;
  110. for (i=0; i<12; i++) // match month
  111. if (strncmp(ptr,MONTHS[i],strlen(MONTHS[i]))==0) t.tm_mon=i;
  112. if (t.tm_mon==-1) return(FALSE);
  113. while ((!isdigit(*ptr))&&(*ptr!=0)) ptr++;
  114. if (*ptr==0) return(FALSE);
  115. t.tm_year=atoi(ptr);
  116. if (t.tm_year<70) // if they specify a 2 digit year, we'll be nice
  117. t.tm_year+=2000;
  118. else if (t.tm_year<100)
  119. t.tm_year+=1900;
  120. if (t.tm_year>2200) // I doubt my code will be around for another 203 years
  121. return(FALSE);
  122. while ((isdigit(*ptr))&&(*ptr!=0)) ptr++; // skip to end of year
  123. if (*ptr==0) return(FALSE);
  124. while ((!isgraph(*ptr))&&(*ptr!=0)) ptr++; // skip to start of time
  125. if (*ptr==0) return(FALSE);
  126. t.tm_hour=atoi(ptr);
  127. while ((*ptr!=':')&&(*ptr!=0)) ptr++;
  128. ptr++; // skip past colon
  129. if (*ptr==0) return(FALSE);
  130. t.tm_min=atoi(ptr);
  131. while ((*ptr!=':')&&(*ptr!=0)) ptr++;
  132. ptr++; // skip past colon
  133. if (*ptr==0) return(FALSE);
  134. t.tm_sec=atoi(ptr);
  135. t.tm_year%=100; // 1996 is stored as 96, not 1996
  136. t.tm_isdst=-1; // daylight savings info isn't available
  137. sec=(uint32)(mktime(&t));
  138. if ((sint32)sec==-1)
  139. return(FALSE);
  140. // The next part of the time is OPTIONAL (+minutes)
  141. // first skip past the seconds
  142. while ((isdigit(*ptr))&&(*ptr!=0)) ptr++;
  143. if (*ptr==0) return(TRUE);
  144. // skip past any spaces
  145. while ((isspace(*ptr))&&(*ptr!=0)) ptr++;
  146. if (*ptr!='+')
  147. {
  148. //printf("\nNOPE ptr was '%s'\n",ptr);
  149. return(TRUE);
  150. }
  151. ptr++;
  152. if (*ptr==0)
  153. {
  154. //printf("\nPTR WAS 0\n");
  155. return(TRUE);
  156. }
  157. minOffset=atol(ptr);
  158. //printf("\n\nAdding %d minutes!\n\n",minOffset);
  159. sec+=minOffset*60; // add the minutes as seconds
  160. return(TRUE);
  161. }
  162. // This takes the standard Microsoft time formatting string
  163. // make sure the out string is big enough
  164. // An example format would be "mm/dd/yy hh:mm:ss"
  165. // CHANGE: Joe Howes 06/30/99
  166. // To specify 12-hour format, use "aa" instead of "hh".
  167. // The hours will be 12 hour and the string will be
  168. // appended with " AM" or " PM".
  169. bit8 Wtime::FormatTime(char *out, char *format)
  170. {
  171. int lastWasH=0;
  172. int ampmflag = 0;
  173. out[0]=0;
  174. char *ptr=format;
  175. if (*ptr=='"') ptr++; // skip past open quote if exists
  176. while (*ptr!=0)
  177. {
  178. if (lastWasH>0)
  179. lastWasH--;
  180. if (isspace(*ptr))
  181. {
  182. if (lastWasH==1) lastWasH=2;
  183. sprintf(out+strlen(out),"%c",*ptr);
  184. ptr+=1;
  185. }
  186. else if (strncmp(ptr,"\"",1)==0)
  187. {
  188. break;
  189. }
  190. else if (strncmp(ptr,":",1)==0)
  191. {
  192. if (lastWasH==1) lastWasH=2;
  193. sprintf(out+strlen(out),":");
  194. ptr+=1;
  195. }
  196. else if (strncmp(ptr,"/",1)==0)
  197. {
  198. sprintf(out+strlen(out),"/");
  199. ptr+=1;
  200. }
  201. else if (strncmp(ptr,"c",1)==0)
  202. {
  203. sprintf(out+strlen(out),"%ld/%ld/%02ld %ld:%02ld:%02ld",GetMonth(),
  204. GetMDay(),GetYear()%100,GetHour(),GetMinute(),GetSecond());
  205. ptr+=1;
  206. }
  207. else if (strncmp(ptr,"dddddd",6)==0)
  208. {
  209. sprintf(out+strlen(out),"%s %02ld, %ld",FULLMONTHS[GetMonth()-1],
  210. GetMDay(),GetYear());
  211. ptr+=6;
  212. }
  213. else if (strncmp(ptr,"ddddd",5)==0)
  214. {
  215. sprintf(out+strlen(out),"%ld/%ld/%02ld",GetMonth(),GetMDay(),
  216. GetYear()%100);
  217. ptr+=5;
  218. }
  219. else if (strncmp(ptr,"dddd",4)==0)
  220. {
  221. sprintf(out+strlen(out),"%s",FULLDAYS[GetWDay()-1]);
  222. ptr+=4;
  223. }
  224. else if (strncmp(ptr,"ddd",3)==0)
  225. {
  226. sprintf(out+strlen(out),"%s",DAYS[GetWDay()-1]);
  227. ptr+=3;
  228. }
  229. else if (strncmp(ptr,"dd",2)==0)
  230. {
  231. sprintf(out+strlen(out),"%02ld",GetMDay());
  232. ptr+=2;
  233. }
  234. else if (strncmp(ptr,"d",1)==0)
  235. {
  236. sprintf(out+strlen(out),"%ld",GetMDay());
  237. ptr+=1;
  238. }
  239. else if (strncmp(ptr,"ww",2)==0)
  240. {
  241. sprintf(out+strlen(out),"%02ld",GetYWeek());
  242. ptr+=2;
  243. }
  244. else if (strncmp(ptr,"w",1)==0)
  245. {
  246. sprintf(out+strlen(out),"%ld",GetWDay());
  247. ptr+=1;
  248. }
  249. else if (strncmp(ptr,"mmmm",4)==0)
  250. {
  251. sprintf(out+strlen(out),"%s",FULLMONTHS[GetMonth()-1]);
  252. ptr+=4;
  253. }
  254. else if (strncmp(ptr,"mmm",3)==0)
  255. {
  256. sprintf(out+strlen(out),"%s",MONTHS[GetMonth()-1]);
  257. ptr+=3;
  258. }
  259. else if (strncmp(ptr,"mm",2)==0)
  260. {
  261. if (lastWasH==1)
  262. sprintf(out+strlen(out),"%02ld",GetMinute());
  263. else
  264. sprintf(out+strlen(out),"%02ld",GetMonth());
  265. ptr+=2;
  266. }
  267. else if (strncmp(ptr,"m",1)==0)
  268. {
  269. if (lastWasH==1)
  270. sprintf(out+strlen(out),"%ld",GetMinute());
  271. else
  272. sprintf(out+strlen(out),"%ld",GetMonth());
  273. ptr+=1;
  274. }
  275. else if (strncmp(ptr,"q",1)==0)
  276. {
  277. sprintf(out+strlen(out),"%ld",((GetMonth()-1)/4)+1); // GetQuarter
  278. ptr+=1;
  279. }
  280. else if (strncmp(ptr,"yyyy",4)==0)
  281. {
  282. sprintf(out+strlen(out),"%ld",GetYear());
  283. ptr+=4;
  284. }
  285. else if (strncmp(ptr,"yy",2)==0)
  286. {
  287. sprintf(out+strlen(out),"%02ld",GetYear()%100);
  288. ptr+=2;
  289. }
  290. else if (strncmp(ptr,"y",1)==0)
  291. {
  292. sprintf(out+strlen(out),"%ld",GetYDay());
  293. ptr+=1;
  294. }
  295. else if (strncmp(ptr,"hh",2)==0)
  296. {
  297. sprintf(out+strlen(out),"%02ld",GetHour());
  298. lastWasH=2; // needs to be 1 after top of loop decs it
  299. ptr+=2;
  300. }
  301. else if (strncmp(ptr,"h",1)==0)
  302. {
  303. sprintf(out+strlen(out),"%ld",GetHour());
  304. lastWasH=2; // needs to be 1 after top of loop decs it
  305. ptr+=1;
  306. }
  307. else if (strncmp(ptr,"nn",2)==0)
  308. {
  309. sprintf(out+strlen(out),"%02ld",GetMinute());
  310. ptr+=2;
  311. }
  312. else if (strncmp(ptr,"n",1)==0)
  313. {
  314. sprintf(out+strlen(out),"%ld",GetMinute());
  315. ptr+=1;
  316. }
  317. else if (strncmp(ptr,"ss",2)==0)
  318. {
  319. sprintf(out+strlen(out),"%02ld",GetSecond());
  320. ptr+=2;
  321. }
  322. else if (strncmp(ptr,"s",1)==0)
  323. {
  324. sprintf(out+strlen(out),"%ld",GetSecond());
  325. ptr+=1;
  326. }
  327. else if (strncmp(ptr,"ttttt",5)==0)
  328. {
  329. sprintf(out+strlen(out),"%ld:%02ld:%02ld",GetHour(),GetMinute(),
  330. GetSecond());
  331. ptr+=5;
  332. }
  333. else if (strncmp(ptr,"aa",2)==0)
  334. {
  335. uint32 tmp = (GetHour() <= 12) ? GetHour() : GetHour() - 12;
  336. sprintf(out+strlen(out),"%02ld", tmp);
  337. lastWasH=2; // needs to be 1 after top of loop decs it
  338. ptr+=2;
  339. ampmflag = 1;
  340. }
  341. else // an unknown char, move to next
  342. ptr++;
  343. }
  344. if(ampmflag)
  345. {
  346. char ampm[4];
  347. if( GetHour() < 12 )
  348. strcpy(ampm, " AM");
  349. else
  350. strcpy(ampm, " PM");
  351. sprintf(out+strlen(out), "%s", ampm);
  352. }
  353. return(TRUE);
  354. }
  355. // In addition to PrintTime & PrintDate there is the 'Print' function
  356. // which prints both in RFC 1123 format
  357. void Wtime::PrintTime(FILE *out) const
  358. {
  359. char string[80];
  360. PrintTime(string);
  361. fprintf(out,"%s",string);
  362. }
  363. void Wtime::PrintTime(char *out) const
  364. {
  365. sprintf(out," %02lu:%02lu:%02lu",GetHour(),GetMinute(),GetSecond());
  366. }
  367. void Wtime::PrintDate(FILE *out) const
  368. {
  369. char string[80];
  370. PrintDate(string);
  371. fprintf(out,"%s",string);
  372. }
  373. void Wtime::PrintDate(char *out) const
  374. {
  375. sprintf(out,"%s, %lu %s %lu",DAYS[GetWDay()-1],GetMDay(),MONTHS[GetMonth()-1],
  376. GetYear());
  377. }
  378. uint32 Wtime::GetSec(void) const
  379. {
  380. return(sec);
  381. }
  382. uint32 Wtime::GetUsec(void) const
  383. {
  384. return(usec);
  385. }
  386. void Wtime::SetSec(uint32 newsec)
  387. {
  388. sec=newsec;
  389. }
  390. void Wtime::SetUsec(uint32 newusec)
  391. {
  392. usec=newusec;
  393. }
  394. void Wtime::Set(uint32 newsec, uint32 newusec)
  395. {
  396. sec=newsec;
  397. usec=newusec;
  398. }
  399. // Get a timeval ptr from a Wtime class
  400. struct timeval *Wtime::GetTimeval(void)
  401. {
  402. static struct timeval tv;
  403. tv.tv_sec=sec;
  404. tv.tv_usec=usec;
  405. return(&tv);
  406. }
  407. // Get a timeval ptr from a Wtime class
  408. void Wtime::GetTimevalMT(struct timeval &tv)
  409. {
  410. tv.tv_sec=sec;
  411. tv.tv_usec=usec;
  412. }
  413. uint32 Wtime::GetSecond(void) const
  414. {
  415. struct tm t;
  416. struct tm *tptr;
  417. tptr=localtime_r((time_t *)&sec,&t);
  418. return(tptr->tm_sec);
  419. }
  420. uint32 Wtime::GetMinute(void) const
  421. {
  422. struct tm t;
  423. struct tm *tptr;
  424. tptr=localtime_r((time_t *)&sec,&t);
  425. return(tptr->tm_min);
  426. }
  427. uint32 Wtime::GetHour(void) const
  428. {
  429. struct tm t;
  430. struct tm *tptr;
  431. tptr=localtime_r((time_t *)&sec,&t);
  432. return(tptr->tm_hour);
  433. }
  434. uint32 Wtime::GetMDay(void) const
  435. {
  436. struct tm t;
  437. struct tm *tptr;
  438. tptr=localtime_r((time_t *)&sec,&t);
  439. return(tptr->tm_mday);
  440. }
  441. uint32 Wtime::GetWDay(void) const
  442. {
  443. struct tm t;
  444. struct tm *tptr;
  445. tptr=localtime_r((time_t *)&sec,&t);
  446. return(tptr->tm_wday+1);
  447. }
  448. uint32 Wtime::GetYDay(void) const
  449. {
  450. struct tm t;
  451. struct tm *tptr;
  452. tptr=localtime_r((time_t *)&sec,&t);
  453. return(tptr->tm_yday+1);
  454. }
  455. uint32 Wtime::GetYWeek(void) const
  456. {
  457. uint32 yweek;
  458. uint32 yday=GetYDay();
  459. uint32 wday=GetWDay();
  460. //phase holds the first weekday of the year. If (Jan 1 = Sun) phase = 0
  461. sint32 phase=((wday-yday)%7);
  462. if (phase<0) phase+=7;
  463. yweek=((yday+phase-1)/7)+1;
  464. return(yweek);
  465. }
  466. uint32 Wtime::GetMonth(void) const
  467. {
  468. struct tm t;
  469. struct tm *tptr;
  470. tptr=localtime_r((time_t *)&sec,&t);
  471. return(tptr->tm_mon+1);
  472. }
  473. uint32 Wtime::GetYear(void) const
  474. {
  475. struct tm t;
  476. struct tm *tptr;
  477. tptr=localtime_r((time_t *)&sec,&t);
  478. if ((tptr->tm_year)>=70)
  479. return((tptr->tm_year)+1900);
  480. else
  481. return((tptr->tm_year)+2000);
  482. }
  483. bit8 Wtime::GetSign(void) const
  484. {
  485. return(sign);
  486. }
  487. // 1 = *this > other
  488. //-1 = *this < other
  489. // 0 = *this == other
  490. int Wtime::Compare(const Wtime &other) const
  491. {
  492. if ((sec==other.sec)&&(usec==other.usec))
  493. return(0); // equal
  494. else if (sec>other.sec)
  495. return(1);
  496. else if (sec<other.sec)
  497. return(-1);
  498. else if (usec>other.usec)
  499. return(1);
  500. else
  501. return(-1);
  502. }
  503. bit8 Wtime::operator == ( const Wtime &other ) const
  504. {
  505. bit8 retval=Compare(other);
  506. if (retval==0)
  507. return(TRUE);
  508. else
  509. return(FALSE);
  510. }
  511. bit8 Wtime::operator != ( const Wtime &other ) const
  512. {
  513. bit8 retval=Compare(other);
  514. if (retval==0)
  515. return(FALSE);
  516. else
  517. return(TRUE);
  518. }
  519. bit8 Wtime::operator < ( const Wtime &other ) const
  520. {
  521. int retval=Compare(other);
  522. if (retval==-1)
  523. return(TRUE);
  524. else
  525. return(FALSE);
  526. }
  527. bit8 Wtime::operator > ( const Wtime &other ) const
  528. {
  529. int retval=Compare(other);
  530. if (retval==1)
  531. return(TRUE);
  532. else
  533. return(FALSE);
  534. }
  535. bit8 Wtime::operator <= ( const Wtime &other ) const
  536. {
  537. int retval=Compare(other);
  538. if ((retval==-1)||(retval==0))
  539. return(TRUE);
  540. else
  541. return(FALSE);
  542. }
  543. bit8 Wtime::operator >= ( const Wtime &other ) const
  544. {
  545. int retval=Compare(other);
  546. if ((retval==1)||(retval==0))
  547. return(TRUE);
  548. else
  549. return(FALSE);
  550. }
  551. // None of the operators pay attention to sign
  552. // only the functions that begin with 'Signed'
  553. void Wtime::SignedAdd(const Wtime &other)
  554. {
  555. Wtime temp;
  556. if ((sign==POSITIVE)&&(other.sign==POSITIVE))
  557. {
  558. *this+=other;
  559. sign=POSITIVE;
  560. }
  561. else if ((sign==POSITIVE)&&(other.sign==NEGATIVE))
  562. {
  563. if (*this>other)
  564. {
  565. *this-=other;
  566. sign=POSITIVE;
  567. }
  568. else
  569. {
  570. temp=other;
  571. temp-=*this;
  572. *this=temp;
  573. sign=NEGATIVE;
  574. }
  575. }
  576. else if ((sign==NEGATIVE)&&(other.sign==POSITIVE))
  577. {
  578. if (*this<other)
  579. {
  580. temp=other;
  581. temp-=*this;
  582. *this=temp;
  583. sign=POSITIVE;
  584. }
  585. else
  586. {
  587. *this-=other;
  588. sign=NEGATIVE;
  589. }
  590. }
  591. else if ((sign==NEGATIVE)&&(other.sign==NEGATIVE))
  592. {
  593. *this+=other;
  594. sign=NEGATIVE;
  595. }
  596. }
  597. // None of the operators pay attention to sign
  598. // only the functions that begin with 'Signed'
  599. void Wtime::SignedSubtract(const Wtime &other)
  600. {
  601. Wtime temp;
  602. if ((sign==POSITIVE)&&(other.sign==NEGATIVE))
  603. {
  604. *this+=other;
  605. sign=POSITIVE;
  606. }
  607. else if ((sign==POSITIVE)&&(other.sign==POSITIVE))
  608. {
  609. if (*this>other)
  610. {
  611. *this-=other;
  612. sign=POSITIVE;
  613. }
  614. else
  615. {
  616. temp=other;
  617. temp-=*this;
  618. *this=temp;
  619. sign=NEGATIVE;
  620. }
  621. }
  622. else if ((sign==NEGATIVE)&&(other.sign==NEGATIVE))
  623. {
  624. if (*this<other)
  625. {
  626. temp=other;
  627. temp-=*this;
  628. *this=temp;
  629. sign=POSITIVE;
  630. }
  631. else
  632. {
  633. *this-=other;
  634. sign=NEGATIVE;
  635. }
  636. }
  637. else if ((sign==NEGATIVE)&&(other.sign==POSITIVE))
  638. {
  639. *this+=other;
  640. sign=NEGATIVE;
  641. }
  642. }
  643. Wtime &Wtime::operator += (const Wtime &other)
  644. {
  645. sec+=other.sec;
  646. usec+=other.usec;
  647. if (usec>1000000)
  648. {
  649. sec++;
  650. usec-=1000000;
  651. }
  652. return *this;
  653. }
  654. Wtime &Wtime::operator -= (const Wtime &other)
  655. {
  656. sint32 temp;
  657. if (Compare(other)==-1)
  658. {
  659. sec=0; // can't handle negative time
  660. usec=0;
  661. return *this;
  662. }
  663. sec-=other.sec;
  664. temp=(sint32)usec;
  665. temp-=(sint32)other.usec;
  666. if (temp<0)
  667. {
  668. sec--;
  669. temp+=1000000;
  670. }
  671. usec=temp;
  672. return *this;
  673. }
  674. Wtime Wtime::operator - (Wtime &other)
  675. {
  676. Wtime temp(*this);
  677. temp-=other;
  678. return(temp);
  679. }
  680. Wtime Wtime::operator + (Wtime &other)
  681. {
  682. Wtime temp(*this);
  683. temp+=other;
  684. return(temp);
  685. }
  686. Wtime &Wtime::operator = (const Wtime &other)
  687. {
  688. sign=other.sign;
  689. sec=other.sec;
  690. usec=other.usec;
  691. return *this;
  692. }
  693. Wtime &Wtime::operator += (const uint32 other)
  694. {
  695. sec+=other;
  696. return *this;
  697. }
  698. Wtime &Wtime::operator -= (const uint32 other)
  699. {
  700. sec-=other;
  701. return *this;
  702. }
  703. Wtime Wtime::operator - (uint32 other)
  704. {
  705. Wtime temp(*this);
  706. temp-=other;
  707. return(temp);
  708. }
  709. Wtime Wtime::operator + (uint32 other)
  710. {
  711. Wtime temp(*this);
  712. temp+=other;
  713. return(temp);
  714. }
  715. Wtime &Wtime::operator = (const uint32 other)
  716. {
  717. sign=POSITIVE;
  718. sec=other;
  719. usec=0;
  720. return *this;
  721. }