wtime.cpp 16 KB

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