25 #include <sys/types.h> 37 #include <lib$routines.h> 65 static struct tm*
gmtime_r(
const time_t* t,
struct tm* r)
68 struct tm *theTm = gmtime(t);
83 struct tm *theTm = localtime(t);
104 memset(&t, 0,
sizeof(t));
105 t.tm_year =
year - 1900;
106 t.tm_mon =
month - 1;
124 year = 1900 + t.tm_year;
125 month = 1 + t.tm_mon;
133 rlTime::rlTime(
int Year,
int Month,
int Day,
int Hour,
int Minute,
int Second,
int Millisecond)
188 double mod = fmod(
seconds*1000, 1000);
272 while(*format !=
'\0')
274 if (strncmp(format,
"dd",2) == 0)
276 sprintf(buf,
"%02d",
day);
281 else if(strncmp(format,
"d",1) == 0)
283 sprintf(buf,
"%d",
day);
288 else if(strncmp(format,
"MMM",3) == 0)
291 if(
month == 1) strcpy(buf,
"Jan");
292 if(
month == 2) strcpy(buf,
"Feb");
293 if(
month == 3) strcpy(buf,
"Mar");
294 if(
month == 4) strcpy(buf,
"Apr");
295 if(
month == 5) strcpy(buf,
"May");
296 if(
month == 6) strcpy(buf,
"Jun");
297 if(
month == 7) strcpy(buf,
"Jul");
298 if(
month == 8) strcpy(buf,
"Aug");
299 if(
month == 9) strcpy(buf,
"Sep");
300 if(
month == 10) strcpy(buf,
"Oct");
301 if(
month == 11) strcpy(buf,
"Nov");
302 if(
month == 12) strcpy(buf,
"Dec");
307 else if(strncmp(format,
"MM",2) == 0)
309 sprintf(buf,
"%02d",
month);
314 else if(strncmp(format,
"M",1) == 0)
316 sprintf(buf,
"%d",
month);
321 else if(strncmp(format,
"yyyy",4) == 0)
323 sprintf(buf,
"%4d",
year);
328 else if(strncmp(format,
"yy",2) == 0)
330 sprintf(buf,
"%4d",
year);
331 strcpy(dest,&buf[2]);
332 dest += strlen(&buf[2]);
335 else if(strncmp(format,
"hh",2) == 0)
337 if (
hour > 12) sprintf(buf,
"%02d",
hour - 12);
338 else if(
hour == 0) sprintf(buf,
"%02d", 12);
339 else sprintf(buf,
"%02d",
hour);
344 else if(strncmp(format,
"h",1) == 0)
346 if (
hour > 12) sprintf(buf,
"%2d",
hour - 12);
347 else if(
hour == 0) sprintf(buf,
"%2d", 12);
348 else sprintf(buf,
"%2d",
hour);
353 else if(strncmp(format,
"HH",2) == 0)
355 sprintf(buf,
"%02d",
hour);
360 else if(strncmp(format,
"H",1) == 0)
362 sprintf(buf,
"%d",
hour);
367 else if(strncmp(format,
"mm",2) == 0)
369 sprintf(buf,
"%02d",
minute);
374 else if(strncmp(format,
"m",1) == 0)
381 else if(strncmp(format,
"ss",2) == 0)
383 sprintf(buf,
"%02d",
second);
388 else if(strncmp(format,
"s",1) == 0)
395 else if(strncmp(format,
"zzz",3) == 0)
402 else if(strncmp(format,
"z",1) == 0)
409 else if(strncmp(format,
"AP",2) == 0)
411 if (
hour == 0) strcpy(dest,
"PM");
412 else if(
hour < 13) strcpy(dest,
"AM");
413 else strcpy(dest,
"PM");
414 dest += strlen(
"AM");
417 else if(strncmp(format,
"ap",2) == 0)
419 if (
hour == 0) strcpy(dest,
"pm");
420 else if(
hour < 13) strcpy(dest,
"am");
421 else strcpy(dest,
"pm");
422 dest += strlen(
"am");
425 else if(strncmp(format,
"A",1) == 0)
427 if (
hour == 0) strcpy(dest,
"PM");
428 else if(
hour < 13) strcpy(dest,
"AM");
429 else strcpy(dest,
"PM");
430 dest += strlen(
"AM");
433 else if(strncmp(format,
"a",1) == 0)
435 if (
hour == 0) strcpy(dest,
"pm");
436 else if(
hour < 13) strcpy(dest,
"am");
437 else strcpy(dest,
"pm");
438 dest += strlen(
"am");
462 gettimeofday(&tv, &tz);
465 tms->tm_year += 1900;
469 second = (
int)tms->tm_sec;
470 minute = (int)tms->tm_min;
471 hour = (
int)tms->tm_hour;
472 day = (int)tms->tm_mday;
473 month = (
int)tms->tm_mon;
474 year = (int)tms->tm_year;
483 hour = (
int)tds.hour;
485 month = (
int)tds.month;
486 year = (int)tds.year;
509 if(lstat(filename,&statbuf))
return -1;
511 if(stat(filename,&statbuf))
return -1;
516 tms->tm_year += 1900;
520 second = (int)tms->tm_sec;
521 minute = (
int)tms->tm_min;
522 hour = (int)tms->tm_hour;
523 day = (
int)tms->tm_mday;
524 month = (int)tms->tm_mon;
525 year = (
int)tms->tm_year;
537 t.tm_mon =
month - 1;
538 t.tm_year =
year - 1900;
542 tv.tv_sec = mktime(&t);
544 settimeofday(&tv,NULL);
549 struct dsc$descriptor_s d_time;
550 char smonth[12][4],buf[64];
553 memset (smonth , 0,
sizeof(smonth));
554 memcpy (smonth [0],
"JAN", 3);
555 memcpy (smonth [1],
"FEB", 3);
556 memcpy (smonth [2],
"MAR", 3);
557 memcpy (smonth [3],
"APR", 3);
558 memcpy (smonth [4],
"MAY", 3);
559 memcpy (smonth [5],
"JUN", 3);
560 memcpy (smonth [6],
"JUL", 3);
561 memcpy (smonth [7],
"AUG", 3);
562 memcpy (smonth [8],
"SEP", 3);
563 memcpy (smonth [9],
"OCT", 3);
564 memcpy (smonth [10],
"NOV", 3);
565 memcpy (smonth [11],
"DEC", 3);
567 sprintf(buf,
"%02d-%3.3s-%04d %02d:%02d:%02d.%02d",
577 d_time.dsc$w_length = strlen(buf);
578 d_time.dsc$b_dtype = DSC$K_DTYPE_T;
579 d_time.dsc$b_class = DSC$K_CLASS_S;
580 d_time.dsc$a_pointer = buf;
582 sys$bintim(&d_time, &vbt);
646 int special = y % 400;
647 if(hth == 0 && special != 0) maxmonth = 28;
689 if(t.
day > maxmonth) { t.
month++; t.
day -= maxmonth; }
718 if(m <= 0) { m += 12; y--; }
731 int special = y % 400;
732 if(hth == 0 && special != 0) maxmonth = 28;
793 if(
day != time.
day)
return 0;
804 if(this->
year < time.
year)
return 1;
810 if(this->
day < time.
day)
return 1;
811 else if(this->
day == time.
day)
813 if(this->
hour < time.
hour)
return 1;
834 if((*
this) == time)
return 1;
835 if((*
this) < time)
return 1;
841 if(this->
year > time.
year)
return 1;
847 if(this->
day > time.
day)
return 1;
848 else if(this->
day == time.
day)
850 if(this->
hour > time.
hour)
return 1;
871 if((*
this) == time)
return 1;
872 if((*
this) > time)
return 1;
879 lldiv_t d = lldiv(
seconds, time_t(60));
883 d = lldiv(d.quot, time_t(60));
887 d = lldiv(d.quot, time_t(24));
891 d = lldiv(d.quot, time_t(31));
895 d = lldiv(d.quot, time_t(12));
909 lldiv_t d = lldiv(
seconds, time_t(60));
918 d = lldiv(d.quot, time_t(60));
927 d = lldiv(d.quot, time_t(24));
936 d = lldiv(d.quot, time_t(31));
945 d = lldiv(d.quot, time_t(12));
1019 time_t t = mktime(tm_);
1023 struct tm *gt =
gmtime_r(&t, >m);
1024 time_t diff = mktime(lt) - mktime(gt);
1033 memset(&begin,0,
sizeof(tm));
1034 memset(&test,0,
sizeof(tm));
1044 test.tm_year =
year - 1900;
1045 test.tm_mon =
month - 1;
1047 test.tm_hour =
hour;
1052 time_t t0 =
timegm(&begin) - 86400;
1053 time_t t1 = mktime(&test);
1055 return difftime(t1,t0) + (((double)
millisecond) / 1000);
1070 if(0 < bufferLength)
1072 if(not buffer) buffer =
new char[bufferLength];
1074 bool isNegative = (tdiff < 0);
1075 tdiff = fabs(tdiff);
1076 int milliseconds = ((int) (tdiff * 1000)) % 1000;
1077 time_t
seconds = (time_t) tdiff;
1078 lldiv_t minutes, hours, days, weeks;
1082 const char* fmtString =
"%s%d:%02d.%03d";
1086 fmtString =
"%s%d:%02d.%03d";
1087 snprintf(buffer, bufferLength, fmtString, (isNegative ?
"-" :
""), minutes.quot, minutes.rem, milliseconds);
1090 fmtString =
"%s%d:%02d:%02d.%03d";
1091 hours = lldiv(minutes.quot, 60);
1092 snprintf(buffer, bufferLength, fmtString, (isNegative ?
"-" :
""), hours.quot, hours.rem, minutes.rem, milliseconds);
1095 fmtString =
"%s%d:%02d:%02d:%02d.%03d";
1096 hours = lldiv(minutes.quot, 60);
1097 days = lldiv(hours.quot, 24);
1098 snprintf(buffer, bufferLength, fmtString, (isNegative ?
"-" :
""), days.quot, days.rem, hours.rem, minutes.rem, milliseconds);
1101 fmtString =
"%s%d:%d:%02d:%02d:%02d.%03d";
1102 hours = lldiv(minutes.quot, 60);
1103 days = lldiv(hours.quot, 24);
1104 weeks = lldiv(days.quot, 7);
1105 snprintf(buffer, bufferLength, fmtString, (isNegative ?
"-" :
""), weeks.quot, weeks.rem, days.rem, hours.rem, minutes.rem, milliseconds);
1108 fmtString =
"%s%d:%02d";
1109 snprintf(buffer, bufferLength, fmtString, (isNegative ?
"-" :
""), minutes.quot, minutes.rem);
1112 fmtString =
"%s%d:%02d:%02d";
1113 hours = lldiv(minutes.quot, 60);
1114 snprintf(buffer, bufferLength, fmtString, (isNegative ?
"-" :
""), hours.quot, hours.rem, minutes.rem);
1117 fmtString =
"%s%d:%02d:%02d:%02d";
1118 hours = lldiv(minutes.quot, 60);
1119 days = lldiv(hours.quot, 24);
1120 snprintf(buffer, bufferLength, fmtString, (isNegative ?
"-" :
""), days.quot, days.rem, hours.rem, minutes.rem);
1123 fmtString =
"%s%d:%d:%02d:%02d:%02d";
1124 hours = lldiv(minutes.quot, 60);
1125 days = lldiv(hours.quot, 24);
1126 weeks = lldiv(days.quot, 7);
1127 snprintf(buffer, bufferLength, fmtString, (isNegative ?
"-" :
""), weeks.quot, weeks.rem, days.rem, hours.rem, minutes.rem);
1146 const char* result =
formatTimeDiff(tdiff, fmt,
sizeof(strBuffer), strBuffer);
1147 int len = strlen(result);
static time_t timegm(struct tm *tm_)
rlTime operator+(const rlTime &time) const
"0:00:00:00.000", needs at least 15 bytes buffer
int operator>=(const rlTime &time) const
int getFileModificationTime(const char *filename)
"0:0:00:00:00.000", needs at least 17 bytes buffer
double secondsSinceEpoche() const
int operator<(const rlTime &time) const
const char * formatTimeDiffString(double, enum FormatLargestUnit=HoursMinutesSecondsFraction)
Caller chooses formatting template, default is Hours:Minutes:Seconds.Milliseconds, returned object manages string memory.
rlTime & operator-=(const rlTime &time)
"0:00", needs at least 5 bytes buffer
void setTimeFromString(const char *time_string)
const char * getIsoTimeString()
int operator==(const rlTime &time) const
"0:00.000", needs at least 9 bytes buffer
rlTime & operator+=(const rlTime &time)
const char * toString(const char *format)
"0:00:00", needs at least 8 bytes buffer
static struct tm * gmtime_r(const time_t *t, struct tm *r)
int operator>(const rlTime &time) const
const char * getTimeString()
rlTime(int Year=0, int Month=0, int Day=0, int Hour=0, int Minute=0, int Second=0, int Millisecond=0)
void setTimeFromIsoString(const char *iso_time_string)
"0:0:00:00:00", needs at least 13 bytes buffer
void setTimeFromSeconds(double const seconds)
int operator<=(const rlTime &time) const
"0:00:00.000", needs at least 12 bytes buffer
static const char * formatTimeDiff(double, enum FormatLargestUnit=HoursMinutesSecondsFraction, unsigned bufferLength=32, char *buffer=0)
Caller chooses formatting template, default is Hours:Minutes:Seconds.Milliseconds, caller provides buffer, or buffer must be deleted (delete[])
"0:00:00:00", needs at least 11 bytes buffer
rlTime operator-(const rlTime &time) const
static struct tm * localtime_r(const time_t *t, struct tm *r)