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