rllib  1
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
ns_rltime_v2::rlTime Class Reference

#include <rltime_v2.h>

Public Types

enum  FormatLargestUnit {
  MinutesSecondsFraction = 0, HoursMinutesSecondsFraction, DaysHoursMinutesSecondsFraction, WeeksDaysHoursMinutesSecondsFraction,
  MinutesSeconds, HoursMinutesSeconds, DaysHoursMinutesSeconds, WeeksDaysHoursMinutesSeconds
}
 

Public Member Functions

 rlTime (int Year=0, int Month=0, int Day=0, int Hour=0, int Minute=0, int Second=0, int Millisecond=0)
 
virtual ~rlTime ()
 
const char * version ()
 
const char * getTimeString ()
 
const char * getIsoTimeString ()
 
const char * toString (const char *format)
 
void getLocalTime ()
 
int getFileModificationTime (const char *filename)
 
void setTimeFromString (const char *time_string)
 
void setTimeFromIsoString (const char *iso_time_string)
 
void setTimeFromSeconds (double const seconds)
 
void setLocalTime ()
 
double secondsSinceEpoche () const
 
double seconds () const
 
rlTimeoperator+= (const rlTime &time)
 
rlTimeoperator-= (const rlTime &time)
 
rlTime operator+ (const rlTime &time) const
 
rlTime operator- (const rlTime &time) const
 
rlTimeoperator+= (double seconds)
 
rlTimeoperator-= (double seconds)
 
rlTime operator+ (double seconds) const
 
rlTime operator- (double seconds) const
 
rlTimeoperator+= (time_t seconds)
 adjust absolute time by number of seconds More...
 
rlTimeoperator-= (time_t seconds)
 adjust absolute time by number of seconds More...
 
rlTime operator+ (time_t seconds) const
 make new object with absolute time adjusted by number of seconds More...
 
rlTime operator- (time_t seconds) const
 make new object with absolute time adjusted by number of seconds More...
 
int operator== (const rlTime &time) const
 
int operator< (const rlTime &time) const
 
int operator<= (const rlTime &time) const
 
int operator> (const rlTime &time) 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. More...
 
const char * formatTimeDiffString (const rlTime &t1, const rlTime &t2, enum FormatLargestUnit=HoursMinutesSecondsFraction)
 
void normalizeAsDate ()
 

Static Public Member Functions

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[]) More...
 
static const char * formatTimeDiff (const rlTime &t1, const rlTime &t2, enum FormatLargestUnit=HoursMinutesSecondsFraction, unsigned bufferLength=32, char *buffer=0)
 Caller chooses formatting template, default is Hours:Minutes:Seconds.Milliseconds, returned object manages string memory. More...
 
static time_t timegm (struct tm *tm_)
 

Public Attributes

int year
 
int month
 
int day
 
int hour
 
int minute
 
int second
 
int millisecond
 

Private Member Functions

 rlTime (double &)
 

Private Attributes

char time_string [32 *2]
 
char iso_time_string [32]
 

Detailed Description

class for handling time.

Definition at line 27 of file rltime_v2.h.

Member Enumeration Documentation

◆ FormatLargestUnit

Enumerator
MinutesSecondsFraction 

"0:00.000", needs at least 9 bytes buffer

HoursMinutesSecondsFraction 

"0:00:00.000", needs at least 12 bytes buffer

DaysHoursMinutesSecondsFraction 

"0:00:00:00.000", needs at least 15 bytes buffer

WeeksDaysHoursMinutesSecondsFraction 

"0:0:00:00:00.000", needs at least 17 bytes buffer

MinutesSeconds 

"0:00", needs at least 5 bytes buffer

HoursMinutesSeconds 

"0:00:00", needs at least 8 bytes buffer

DaysHoursMinutesSeconds 

"0:00:00:00", needs at least 11 bytes buffer

WeeksDaysHoursMinutesSeconds 

"0:0:00:00:00", needs at least 13 bytes buffer

Definition at line 92 of file rltime_v2.h.

93  {
102  };
"0:00:00:00.000", needs at least 15 bytes buffer
Definition: rltime_v2.h:96
"0:0:00:00:00.000", needs at least 17 bytes buffer
Definition: rltime_v2.h:97
"0:00", needs at least 5 bytes buffer
Definition: rltime_v2.h:98
"0:00.000", needs at least 9 bytes buffer
Definition: rltime_v2.h:94
"0:00:00", needs at least 8 bytes buffer
Definition: rltime_v2.h:99
"0:0:00:00:00", needs at least 13 bytes buffer
Definition: rltime_v2.h:101
"0:00:00.000", needs at least 12 bytes buffer
Definition: rltime_v2.h:95
"0:00:00:00", needs at least 11 bytes buffer
Definition: rltime_v2.h:100

Constructor & Destructor Documentation

◆ rlTime() [1/2]

ns_rltime_v2::rlTime::rlTime ( int  Year = 0,
int  Month = 0,
int  Day = 0,
int  Hour = 0,
int  Minute = 0,
int  Second = 0,
int  Millisecond = 0 
)

Definition at line 63 of file rltime.cpp.

64 {
65  year = Year;
66  month = Month;
67  day = Day;
68  hour = Hour;
69  minute = Minute;
70  second = Second;
71  millisecond = Millisecond;
72 }

◆ ~rlTime()

ns_rltime_v2::rlTime::~rlTime ( )
virtual

Definition at line 74 of file rltime.cpp.

75 {
76 }

◆ rlTime() [2/2]

ns_rltime_v2::rlTime::rlTime ( double &  )
explicitprivate

Member Function Documentation

◆ formatTimeDiff() [1/2]

const char * ns_rltime_v2::rlTime::formatTimeDiff ( double  tdiff,
enum FormatLargestUnit  fmt = HoursMinutesSecondsFraction,
unsigned  bufferLength = 32,
char *  buffer = 0 
)
static

Caller chooses formatting template, default is Hours:Minutes:Seconds.Milliseconds, caller provides buffer, or buffer must be deleted (delete[])

Definition at line 1068 of file rltime_v2.cpp.

1069 {
1070  if(0 < bufferLength)
1071  {
1072  if(not buffer) buffer = new char[bufferLength];
1073 
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;
1079 
1080  minutes = lldiv(seconds, 60);
1081 
1082  const char* fmtString = "%s%d:%02d.%03d";
1083  switch (fmt)
1084  {
1086  fmtString = "%s%d:%02d.%03d";
1087  snprintf(buffer, bufferLength, fmtString, (isNegative ? "-" : ""), minutes.quot, minutes.rem, milliseconds);
1088  break;
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);
1093  break;
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);
1099  break;
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);
1106  break;
1107  case MinutesSeconds:
1108  fmtString = "%s%d:%02d";
1109  snprintf(buffer, bufferLength, fmtString, (isNegative ? "-" : ""), minutes.quot, minutes.rem);
1110  break;
1111  case HoursMinutesSeconds:
1112  fmtString = "%s%d:%02d:%02d";
1113  hours = lldiv(minutes.quot, 60);
1114  snprintf(buffer, bufferLength, fmtString, (isNegative ? "-" : ""), hours.quot, hours.rem, minutes.rem);
1115  break;
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);
1121  break;
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);
1128  break;
1129  }
1130  }
1131 
1132  return buffer;
1133 }
"0:00:00:00.000", needs at least 15 bytes buffer
Definition: rltime_v2.h:96
"0:0:00:00:00.000", needs at least 17 bytes buffer
Definition: rltime_v2.h:97
"0:00", needs at least 5 bytes buffer
Definition: rltime_v2.h:98
"0:00.000", needs at least 9 bytes buffer
Definition: rltime_v2.h:94
"0:00:00", needs at least 8 bytes buffer
Definition: rltime_v2.h:99
"0:0:00:00:00", needs at least 13 bytes buffer
Definition: rltime_v2.h:101
"0:00:00.000", needs at least 12 bytes buffer
Definition: rltime_v2.h:95
double seconds() const
Definition: rltime_v2.cpp:1062
"0:00:00:00", needs at least 11 bytes buffer
Definition: rltime_v2.h:100

◆ formatTimeDiff() [2/2]

const char * ns_rltime_v2::rlTime::formatTimeDiff ( const rlTime t1,
const rlTime t2,
enum FormatLargestUnit  fmt = HoursMinutesSecondsFraction,
unsigned  bufferLength = 32,
char *  buffer = 0 
)
static

Caller chooses formatting template, default is Hours:Minutes:Seconds.Milliseconds, returned object manages string memory.

Definition at line 1135 of file rltime_v2.cpp.

1136 {
1137  rlTime tdiff;
1138  tdiff = t2 - t1;
1139  return formatTimeDiff(tdiff.seconds(), fmt, bufferLength, buffer);
1140 }
rlTime(int Year=0, int Month=0, int Day=0, int Hour=0, int Minute=0, int Second=0, int Millisecond=0)
Definition: rltime.cpp:63
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[])
Definition: rltime_v2.cpp:1068

◆ formatTimeDiffString() [1/2]

const char * ns_rltime_v2::rlTime::formatTimeDiffString ( double  tdiff,
enum FormatLargestUnit  fmt = HoursMinutesSecondsFraction 
)

Caller chooses formatting template, default is Hours:Minutes:Seconds.Milliseconds, returned object manages string memory.

Definition at line 1142 of file rltime_v2.cpp.

1143 {
1144  char strBuffer[32];
1145 
1146  const char* result = formatTimeDiff(tdiff, fmt, sizeof(strBuffer), strBuffer);
1147  int len = strlen(result);
1148  time_string[0] = '\0';
1149  if(len+1 < (int) sizeof(time_string)) strcpy(time_string, result);
1150  return time_string;
1151 }
char time_string[32 *2]
Definition: rltime_v2.h:123
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[])
Definition: rltime_v2.cpp:1068

◆ formatTimeDiffString() [2/2]

const char * ns_rltime_v2::rlTime::formatTimeDiffString ( const rlTime t1,
const rlTime t2,
enum FormatLargestUnit  fmt = HoursMinutesSecondsFraction 
)

Definition at line 1153 of file rltime_v2.cpp.

1154 {
1155  return formatTimeDiffString(t2 - t1, fmt);
1156 }
const char * formatTimeDiffString(double, enum FormatLargestUnit=HoursMinutesSecondsFraction)
Caller chooses formatting template, default is Hours:Minutes:Seconds.Milliseconds, returned object manages string memory.
Definition: rltime_v2.cpp:1142

◆ getFileModificationTime()

int ns_rltime_v2::rlTime::getFileModificationTime ( const char *  filename)

Definition at line 164 of file rltime.cpp.

165 {
166  struct stat statbuf;
167  struct tm *tms;
168 
169 #ifdef RLUNIX
170  if(lstat(filename,&statbuf)) return -1;
171 #else
172  if(stat(filename,&statbuf)) return -1;
173 #endif
174  tms = localtime(&statbuf.st_mtime);
175 
176  /* adjust year and month */
177  tms->tm_year += 1900;
178  tms->tm_mon += 1;
179 
180  millisecond = 0;
181  second = (int)tms->tm_sec;
182  minute = (int)tms->tm_min;
183  hour = (int)tms->tm_hour;
184  day = (int)tms->tm_mday;
185  month = (int)tms->tm_mon;
186  year = (int)tms->tm_year;
187 
188  return 0;
189 }

◆ getIsoTimeString()

const char * ns_rltime_v2::rlTime::getIsoTimeString ( )

Definition at line 108 of file rltime.cpp.

109 {
110  sprintf(iso_time_string,"%04d-%02d-%02dT%02d:%02d:%02d.%03d",year, month, day, hour, minute, second, millisecond);
111  return iso_time_string;
112 }
char iso_time_string[32]
Definition: rltime_v2.h:124

◆ getLocalTime()

void ns_rltime_v2::rlTime::getLocalTime ( )

Definition at line 114 of file rltime.cpp.

115 {
116 #ifdef RLUNIX
117  time_t t;
118  struct tm *tms;
119  struct timeval tv;
120  struct timezone tz;
121 
122  time(&t);
123  tms = localtime(&t);
124  gettimeofday(&tv, &tz);
125 
126  /* adjust year and month */
127  tms->tm_year += 1900;
128  tms->tm_mon += 1;
129 
130  millisecond = (int)tv.tv_usec / 1000;
131  second = (int)tms->tm_sec;
132  minute = (int)tms->tm_min;
133  hour = (int)tms->tm_hour;
134  day = (int)tms->tm_mday;
135  month = (int)tms->tm_mon;
136  year = (int)tms->tm_year;
137 #endif
138 
139 #ifdef __VMS
140  TDS tds;
141  sys$numtim(&tds, 0);
142  millisecond = (int)tds.hth * 10;
143  second = (int)tds.sec;
144  minute = (int)tds.min;
145  hour = (int)tds.hour;
146  day = (int)tds.day;
147  month = (int)tds.month;
148  year = (int)tds.year;
149 #endif
150 
151 #ifdef RLWIN32
152  SYSTEMTIME st;
153  GetLocalTime(&st);
154  millisecond = st.wMilliseconds;
155  second = st.wSecond;
156  minute = st.wMinute;
157  hour = st.wHour;
158  day = st.wDay;
159  month = st.wMonth;
160  year = st.wYear;
161 #endif
162 }
#define RLWIN32
Definition: rldefine.h:42
Definition: rltime.cpp:39

◆ getTimeString()

const char * ns_rltime_v2::rlTime::getTimeString ( )

Definition at line 102 of file rltime.cpp.

103 {
104  sprintf(time_string,"%04d-%02d-%02d %02d:%02d:%02d %03d",year, month, day, hour, minute, second, millisecond);
105  return time_string;
106 }
char time_string[32 *2]
Definition: rltime_v2.h:123

◆ normalizeAsDate()

void ns_rltime_v2::rlTime::normalizeAsDate ( )

normalizes odd constructions of time and date, such 2014-03-36 is normalized to 2014-04-05,

Definition at line 98 of file rltime_v2.cpp.

99 {
100  if (year)
101  {
102  // read members
103  tm t;
104  memset(&t, 0, sizeof(t));
105  t.tm_year = year - 1900;
106  t.tm_mon = month - 1;
107  t.tm_mday = day;
108  t.tm_hour = hour;
109  t.tm_min = minute;
110  t.tm_sec = second;
111 
112  // normalize milliseconds
113  while (millisecond > 1000)
114  {
115  ++t.tm_sec;
116  millisecond -= 1000;
117  }
118 
119  // normalize time structure
120  time_t ep = timegm(&t);
121  gmtime_r(&ep, &t);
122 
123  // set members
124  year = 1900 + t.tm_year;
125  month = 1 + t.tm_mon;
126  day = t.tm_mday;
127  hour = t.tm_hour;
128  minute = t.tm_min;
129  second = t.tm_sec;
130  }
131 }
static time_t timegm(struct tm *tm_)
Definition: rltime_v2.cpp:1017
static struct tm * gmtime_r(const time_t *t, struct tm *r)
Definition: rltime_v2.cpp:65

◆ operator+() [1/3]

rlTime ns_rltime_v2::rlTime::operator+ ( const rlTime time) const

Definition at line 616 of file rltime_v2.cpp.

617 {
618  int maxmonth,y,m;
619  rlTime t;
620 
621  t.year = year + time.year;
622  t.month = month + time.month;
623  t.day = day + time.day;
624  t.hour = hour + time.hour;
625  t.minute = minute + time.minute;
626  t.second = second + time.second;
627  t.millisecond = millisecond + time.millisecond;
628 
629  y = t.year;
630  if(t.month > 12 || (t.month==12 && t.day==31 && t.hour>=24)) y++;
631  m = t.month;
632  if(t.month > 12 || (t.month==12 && t.day==31 && t.hour>=24)) m = 1;
633 
634  switch(m % 12)
635  {
636  case 1: // january
637  maxmonth = 31;
638  break;
639  case 2: // february
640  maxmonth = 28;
641  // Annus bisextilis (calendario Gregoriano)
642  if(y%4==0)
643  {
644  maxmonth = 29;
645  int hth = y % 100;
646  int special = y % 400; // 1900-+-2100-2200-2300-+-2500-2600-2700
647  if(hth == 0 && special != 0) maxmonth = 28;
648  }
649  break;
650  case 3: // march
651  maxmonth = 31;
652  break;
653  case 4: // april
654  maxmonth = 30;
655  break;
656  case 5: // may
657  maxmonth = 31;
658  break;
659  case 6: // june
660  maxmonth = 30;
661  break;
662  case 7: // july
663  maxmonth = 31;
664  break;
665  case 8: // august
666  maxmonth = 31;
667  break;
668  case 9: // september
669  maxmonth = 30;
670  break;
671  case 10: // october
672  maxmonth = 31;
673  break;
674  case 11: // november
675  maxmonth = 30;
676  break;
677  case 12: // december
678  maxmonth = 31;
679  break;
680  default:
681  maxmonth = 31;
682  break;
683  }
684 
685  if(t.millisecond >= 1000) { t.second++; t.millisecond -= 1000; }
686  if(t.second >= 60) { t.minute++; t.second -= 60; }
687  if(t.minute >= 60) { t.hour++, t.minute -= 60; }
688  if(t.hour >= 24) { t.day++; t.hour -= 24; }
689  if(t.day > maxmonth) { t.month++; t.day -= maxmonth; }
690  if(t.month > 12) { t.year++; t.month -= 12; }
691  return t;
692 }
rlTime(int Year=0, int Month=0, int Day=0, int Hour=0, int Minute=0, int Second=0, int Millisecond=0)
Definition: rltime.cpp:63

◆ operator+() [2/3]

rlTime ns_rltime_v2::rlTime::operator+ ( double  seconds) const

Definition at line 1001 of file rltime_v2.cpp.

1002 {
1003  rlTime t, sec;
1004  sec.setTimeFromSeconds(seconds);
1005  t = *this + sec;
1006  return t;
1007 }
rlTime(int Year=0, int Month=0, int Day=0, int Hour=0, int Minute=0, int Second=0, int Millisecond=0)
Definition: rltime.cpp:63
double seconds() const
Definition: rltime_v2.cpp:1062

◆ operator+() [3/3]

rlTime ns_rltime_v2::rlTime::operator+ ( time_t  seconds) const

make new object with absolute time adjusted by number of seconds

Definition at line 961 of file rltime_v2.cpp.

962 {
963  rlTime t(*this);
964 
965  t += seconds;
966  return t;
967 }
rlTime(int Year=0, int Month=0, int Day=0, int Hour=0, int Minute=0, int Second=0, int Millisecond=0)
Definition: rltime.cpp:63
double seconds() const
Definition: rltime_v2.cpp:1062

◆ operator+=() [1/3]

rlTime & ns_rltime_v2::rlTime::operator+= ( const rlTime time)

Definition at line 600 of file rltime_v2.cpp.

601 {
602  rlTime t;
603  t = *this + time;
604  *this = t;
605  return *this;
606 }
rlTime(int Year=0, int Month=0, int Day=0, int Hour=0, int Minute=0, int Second=0, int Millisecond=0)
Definition: rltime.cpp:63

◆ operator+=() [2/3]

rlTime & ns_rltime_v2::rlTime::operator+= ( double  seconds)

Definition at line 983 of file rltime_v2.cpp.

984 {
985  rlTime t, sec;
986  sec.setTimeFromSeconds(seconds);
987  t = *this + sec;
988  *this = t;
989  return *this;
990 }
rlTime(int Year=0, int Month=0, int Day=0, int Hour=0, int Minute=0, int Second=0, int Millisecond=0)
Definition: rltime.cpp:63
double seconds() const
Definition: rltime_v2.cpp:1062

◆ operator+=() [3/3]

rlTime & ns_rltime_v2::rlTime::operator+= ( time_t  seconds)

adjust absolute time by number of seconds

Definition at line 876 of file rltime_v2.cpp.

877 {
878  if(0 > seconds) return this->operator -=(-seconds);
879  lldiv_t d = lldiv(seconds, time_t(60));
880  second += d.rem;
881  if(d.quot)
882  {
883  d = lldiv(d.quot, time_t(60));
884  minute += d.rem;
885  if(d.quot)
886  {
887  d = lldiv(d.quot, time_t(24));
888  hour += d.rem;
889  if (d.quot)
890  {
891  d = lldiv(d.quot, time_t(31));
892  day += d.rem;
893  if(d.quot)
894  {
895  d = lldiv(d.quot, time_t(12));
896  month += d.rem;
897  year += d.quot;
898  }
899  }
900  }
901  }
902  this->normalizeAsDate();
903  return *this;
904 }
rlTime & operator-=(const rlTime &time)
Definition: rltime_v2.cpp:608
double seconds() const
Definition: rltime_v2.cpp:1062

◆ operator-() [1/3]

rlTime ns_rltime_v2::rlTime::operator- ( const rlTime time) const

Definition at line 694 of file rltime_v2.cpp.

695 {
696  int maxmonth,y,m;
697  rlTime t;
698 
699  y = 0;
700  t.year = year - time.year;
701  t.month = month - time.month;
702  t.day = day - time.day;
703  t.hour = hour - time.hour;
704  t.minute = minute - time.minute;
705  t.second = second - time.second;
706  t.millisecond = millisecond - time.millisecond;
707 
708  if(t.millisecond < 0) { t.second--; t.millisecond += 1000; }
709  if(t.second < 0) { t.minute--; t.second += 60; }
710  if(t.minute < 0) { t.hour--, t.minute += 60; }
711  if(t.hour < 0) { t.day--; t.hour += 24; }
712 
713  if(t.day < 0)
714  {
715  t.month--;
716  y = t.year;
717  m = t.month;
718  if(m <= 0) { m += 12; y--; }
719  switch(m % 12)
720  {
721  case 1: // january
722  maxmonth = 31;
723  break;
724  case 2: // february
725  maxmonth = 28;
726  // Annus bisextilis (calendario Gregoriano)
727  if(y%4==0)
728  {
729  maxmonth = 29;
730  int hth = y % 100;
731  int special = y % 400; // 1900-+-2100-2200-2300-+-2500-2600-2700
732  if(hth == 0 && special != 0) maxmonth = 28;
733  }
734  break;
735  case 3: // march
736  maxmonth = 31;
737  break;
738  case 4: // april
739  maxmonth = 30;
740  break;
741  case 5: // may
742  maxmonth = 31;
743  break;
744  case 6: // june
745  maxmonth = 30;
746  break;
747  case 7: // july
748  maxmonth = 31;
749  break;
750  case 8: // august
751  maxmonth = 31;
752  break;
753  case 9: // september
754  maxmonth = 30;
755  break;
756  case 10: // october
757  maxmonth = 31;
758  break;
759  case 11: // november
760  maxmonth = 30;
761  break;
762  case 12: // december
763  maxmonth = 31;
764  break;
765  default:
766  maxmonth = 31;
767  break;
768  }
769  t.day += maxmonth;
770  }
771 
772  if(y >= 0)
773  {
774  //printf("after christ was born. thus everything is ok.\n");
775  }
776  else
777  {
778  //printf("before christ was born. now also ok\n");
779  { t.month++; t.day -= 30; }
780  if(t.day < 30) { t.day++; t.hour -= 24; }
781  if(t.hour < 0 ) { t.hour++; t.minute -= 60; }
782  if(t.minute < 0 ) { t.minute++; t.second -= 60; }
783  if(t.second < 0 ) { t.second++; t.millisecond -= 1000; }
784  }
785 
786  return t;
787 }
rlTime(int Year=0, int Month=0, int Day=0, int Hour=0, int Minute=0, int Second=0, int Millisecond=0)
Definition: rltime.cpp:63

◆ operator-() [2/3]

rlTime ns_rltime_v2::rlTime::operator- ( double  seconds) const

Definition at line 1009 of file rltime_v2.cpp.

1010 {
1011  rlTime t, sec;
1012  sec.setTimeFromSeconds(seconds);
1013  t = *this - sec;
1014  return t;
1015 }
rlTime(int Year=0, int Month=0, int Day=0, int Hour=0, int Minute=0, int Second=0, int Millisecond=0)
Definition: rltime.cpp:63
double seconds() const
Definition: rltime_v2.cpp:1062

◆ operator-() [3/3]

rlTime ns_rltime_v2::rlTime::operator- ( time_t  seconds) const

make new object with absolute time adjusted by number of seconds

Definition at line 969 of file rltime_v2.cpp.

970 {
971  rlTime t(*this);
972 
973  t -= seconds;
974  return t;
975 }
rlTime(int Year=0, int Month=0, int Day=0, int Hour=0, int Minute=0, int Second=0, int Millisecond=0)
Definition: rltime.cpp:63
double seconds() const
Definition: rltime_v2.cpp:1062

◆ operator-=() [1/3]

rlTime & ns_rltime_v2::rlTime::operator-= ( const rlTime time)

Definition at line 608 of file rltime_v2.cpp.

609 {
610  rlTime t;
611  t = *this - time;
612  *this = t;
613  return *this;
614 }
rlTime(int Year=0, int Month=0, int Day=0, int Hour=0, int Minute=0, int Second=0, int Millisecond=0)
Definition: rltime.cpp:63

◆ operator-=() [2/3]

rlTime & ns_rltime_v2::rlTime::operator-= ( double  seconds)

Definition at line 992 of file rltime_v2.cpp.

993 {
994  rlTime t, sec;
995  sec.setTimeFromSeconds(seconds);
996  t = *this - sec;
997  *this = t;
998  return *this;
999 }
rlTime(int Year=0, int Month=0, int Day=0, int Hour=0, int Minute=0, int Second=0, int Millisecond=0)
Definition: rltime.cpp:63
double seconds() const
Definition: rltime_v2.cpp:1062

◆ operator-=() [3/3]

rlTime & ns_rltime_v2::rlTime::operator-= ( time_t  seconds)

adjust absolute time by number of seconds

Definition at line 906 of file rltime_v2.cpp.

907 {
908  if(0 > seconds) return this->operator +=(-seconds);
909  lldiv_t d = lldiv(seconds, time_t(60));
910  second -= d.rem;
911  if(second < 0)
912  {
913  ++d.quot;
914  second += 60;
915  }
916  if(d.quot)
917  {
918  d = lldiv(d.quot, time_t(60));
919  minute -= d.rem;
920  if(minute < 0)
921  {
922  ++d.quot;
923  minute += 60;
924  }
925  if(d.quot)
926  {
927  d = lldiv(d.quot, time_t(24));
928  hour -= d.rem;
929  if(hour < 0)
930  {
931  ++d.quot;
932  hour += 24;
933  }
934  if(d.quot)
935  {
936  d = lldiv(d.quot, time_t(31));
937  day -= d.rem;
938  if(day < 0)
939  {
940  ++d.quot;
941  day += 31;
942  }
943  if(d.quot)
944  {
945  d = lldiv(d.quot, time_t(12));
946  month -= d.rem;
947  if(month < 0)
948  {
949  ++d.quot;
950  month += 12;
951  }
952  year -= d.quot;
953  }
954  }
955  }
956  }
957  this->normalizeAsDate();
958  return *this;
959 }
rlTime & operator+=(const rlTime &time)
Definition: rltime_v2.cpp:600
double seconds() const
Definition: rltime_v2.cpp:1062

◆ operator<()

int ns_rltime_v2::rlTime::operator< ( const rlTime time) const

Definition at line 802 of file rltime_v2.cpp.

803 {
804  if(this->year < time.year) return 1;
805  else if(this->year == time.year)
806  {
807  if(this->month < time.month) return 1;
808  else if(this->month == time.month)
809  {
810  if(this->day < time.day) return 1;
811  else if(this->day == time.day)
812  {
813  if(this->hour < time.hour) return 1;
814  else if(this->hour == time.hour)
815  {
816  if(this->minute < time.minute) return 1;
817  else if(this->minute == time.minute)
818  {
819  if(this->second < time.second) return 1;
820  else if(this->second == time.second)
821  {
822  if(this->millisecond < time.millisecond) return 1;
823  }
824  }
825  }
826  }
827  }
828  }
829  return 0;
830 }

◆ operator<=()

int ns_rltime_v2::rlTime::operator<= ( const rlTime time) const

Definition at line 832 of file rltime_v2.cpp.

833 {
834  if((*this) == time) return 1;
835  if((*this) < time) return 1;
836  return 0;
837 }

◆ operator==()

int ns_rltime_v2::rlTime::operator== ( const rlTime time) const

Definition at line 789 of file rltime_v2.cpp.

790 {
791  if(year != time.year) return 0;
792  if(month != time.month) return 0;
793  if(day != time.day) return 0;
794  if(hour != time.hour) return 0;
795  if(minute != time.minute) return 0;
796  if(second != time.second) return 0;
797  if(millisecond != time.millisecond) return 0;
798 
799  return 1;
800 }

◆ operator>()

int ns_rltime_v2::rlTime::operator> ( const rlTime time) const

Definition at line 839 of file rltime_v2.cpp.

840 {
841  if(this->year > time.year) return 1;
842  else if(this->year == time.year)
843  {
844  if(this->month > time.month) return 1;
845  else if(this->month == time.month)
846  {
847  if(this->day > time.day) return 1;
848  else if(this->day == time.day)
849  {
850  if(this->hour > time.hour) return 1;
851  else if(this->hour == time.hour)
852  {
853  if(this->minute > time.minute) return 1;
854  else if(this->minute == time.minute)
855  {
856  if(this->second > time.second) return 1;
857  else if (this->second == time.second)
858  {
859  if(this->millisecond > time.millisecond) return 1;
860  }
861  }
862  }
863  }
864  }
865  }
866  return 0;
867 }

◆ operator>=()

int ns_rltime_v2::rlTime::operator>= ( const rlTime time) const

Definition at line 869 of file rltime_v2.cpp.

870 {
871  if((*this) == time) return 1;
872  if((*this) > time) return 1;
873  return 0;
874 }

◆ seconds()

double ns_rltime_v2::rlTime::seconds ( ) const

seconds := delta time milliseconds within the fraction

Definition at line 1062 of file rltime_v2.cpp.

1063 {
1064  double ret = (((double) millisecond) / 1000) + second + minute*60 + hour*60*60 + month*60*60*30.5 + year*60*60*30.5*12;
1065  return ret;
1066 }

◆ secondsSinceEpoche()

double ns_rltime_v2::rlTime::secondsSinceEpoche ( ) const

Definition at line 1028 of file rltime_v2.cpp.

1029 {
1030  struct tm begin;
1031  struct tm test;
1032 
1033  memset(&begin,0,sizeof(tm));
1034  memset(&test,0,sizeof(tm));
1035 
1036  begin.tm_year = 70;
1037  begin.tm_mon = 0;
1038  begin.tm_mday = 2; // see below
1039  begin.tm_hour = 0;
1040  begin.tm_min = 0;
1041  begin.tm_sec = 0;
1042  begin.tm_isdst = 0;
1043 
1044  test.tm_year = year - 1900;
1045  test.tm_mon = month - 1;
1046  test.tm_mday = day;
1047  test.tm_hour = hour;
1048  test.tm_min = minute;
1049  test.tm_sec = second;
1050  test.tm_isdst = -1;
1051 
1052  time_t t0 = timegm(&begin) - 86400; // a weak workaround: on several platform (especially Windows 7 in Europe Timezones) mktime() is not capable of converting 1970-01-01T00:00:00 into seconds, because they try to convert it into UTC instead of localtime
1053  time_t t1 = mktime(&test); // this might be error prone, see above
1054 
1055  return difftime(t1,t0) + (((double) millisecond) / 1000);
1056 }
static time_t timegm(struct tm *tm_)
Definition: rltime_v2.cpp:1017

◆ setLocalTime()

void ns_rltime_v2::rlTime::setLocalTime ( )

Definition at line 191 of file rltime.cpp.

192 {
193 #ifdef RLUNIX
194  struct timeval tv;
195  struct tm t;
196 
197  t.tm_mday = day;
198  t.tm_mon = month - 1;
199  t.tm_year = year - 1900;
200  t.tm_hour = hour;
201  t.tm_min = minute;
202  t.tm_sec = second;
203  tv.tv_sec = mktime(&t);
204  tv.tv_usec = 1000 * millisecond;
205  settimeofday(&tv,NULL);
206 #endif
207 
208 #ifdef __VMS
209  VAX_BIN_TIME vbt;
210  struct dsc$descriptor_s d_time;
211  char smonth[12][4],buf[64];
212 
213  // Initialize month array
214  memset (smonth , 0, sizeof(smonth));
215  memcpy (smonth [0], "JAN", 3);
216  memcpy (smonth [1], "FEB", 3);
217  memcpy (smonth [2], "MAR", 3);
218  memcpy (smonth [3], "APR", 3);
219  memcpy (smonth [4], "MAY", 3);
220  memcpy (smonth [5], "JUN", 3);
221  memcpy (smonth [6], "JUL", 3);
222  memcpy (smonth [7], "AUG", 3);
223  memcpy (smonth [8], "SEP", 3);
224  memcpy (smonth [9], "OCT", 3);
225  memcpy (smonth [10], "NOV", 3);
226  memcpy (smonth [11], "DEC", 3);
227  // Create time buffer
228  sprintf(buf, "%02d-%3.3s-%04d %02d:%02d:%02d.%02d",
229  day,
230  smonth[month-1],
231  year,
232  hour,
233  minute,
234  second,
235  millisecond / 10);
236 
237  // Fill string descriptor
238  d_time.dsc$w_length = strlen(buf);
239  d_time.dsc$b_dtype = DSC$K_DTYPE_T;
240  d_time.dsc$b_class = DSC$K_CLASS_S;
241  d_time.dsc$a_pointer = buf;
242  // Convert time buf to VAX bin time
243  sys$bintim(&d_time, &vbt);
244  // Set the system time
245  sys$setime(&vbt);
246 #endif
247 
248 #ifdef RLWIN32
249  SYSTEMTIME st;
250  st.wDay = day;
251  st.wMonth = month;
252  st.wYear = year;
253  st.wHour = hour;
254  st.wMinute = minute;
255  st.wSecond = second;
256  st.wMilliseconds = millisecond;
257  SetSystemTime(&st);
258 #endif
259 }

◆ setTimeFromIsoString()

void ns_rltime_v2::rlTime::setTimeFromIsoString ( const char *  iso_time_string)

Definition at line 90 of file rltime.cpp.

91 {
92  year = 0;
93  month = 0;
94  day = 0;
95  hour = 0;
96  minute = 0;
97  second = 0;
98  millisecond = 0;
99  sscanf(iso_time_string,"%d-%d-%dT%d:%d:%d.%d",&year,&month,&day, &hour,&minute,&second, &millisecond);
100 }
char iso_time_string[32]
Definition: rltime_v2.h:124

◆ setTimeFromSeconds()

void ns_rltime_v2::rlTime::setTimeFromSeconds ( double const  seconds)
  seconds := delta time
  milliseconds within the fraction
  calculating with 1 month <=> 30.5 days

Definition at line 186 of file rltime_v2.cpp.

187 { // we assume that the average month has 30.5 days
188  double mod = fmod(seconds*1000, 1000);
189  millisecond = (int) mod;
190 
191  mod = fmod(seconds, 60);
192  second = (int) mod;
193 
194  seconds /= 60;
195  mod = fmod(seconds, 60);
196  minute = (int) mod;
197 
198  seconds /= 60;
199  mod = fmod(seconds, 24);
200  hour = (int) mod;
201 
202  seconds /= 24;
203  mod = fmod(seconds, 30.5);
204  day = (int) mod;
205 
206  seconds /= 30.5;
207  mod = fmod(seconds, 12);
208  month = (int) mod;
209 
210  seconds /= 12;
211  year = (int) seconds;
212 }
double seconds() const
Definition: rltime_v2.cpp:1062

◆ setTimeFromString()

void ns_rltime_v2::rlTime::setTimeFromString ( const char *  time_string)
format: sscanf(time_string,"%d-%d-%d %d:%d:%d %d",&year,&month,&day, &hour,&minute,&second, &millisecond);

Definition at line 78 of file rltime.cpp.

79 {
80  year = 0;
81  month = 0;
82  day = 0;
83  hour = 0;
84  minute = 0;
85  second = 0;
86  millisecond = 0;
87  sscanf(time_string,"%d-%d-%d %d:%d:%d %d",&year,&month,&day, &hour,&minute,&second, &millisecond);
88 }
char time_string[32 *2]
Definition: rltime_v2.h:123

◆ timegm()

time_t ns_rltime_v2::rlTime::timegm ( struct tm *  tm_)
static

emulates the POSIX function, which is i.e. under Windows not available

Definition at line 1017 of file rltime_v2.cpp.

1018 {
1019  time_t t = mktime(tm_);
1020  struct tm ltm;
1021  struct tm gtm;
1022  struct tm *lt = localtime_r(&t, &ltm);
1023  struct tm *gt = gmtime_r(&t, &gtm);
1024  time_t diff = mktime(lt) - mktime(gt);
1025  return t + diff;
1026 }
static struct tm * gmtime_r(const time_t *t, struct tm *r)
Definition: rltime_v2.cpp:65
static struct tm * localtime_r(const time_t *t, struct tm *r)
Definition: rltime_v2.cpp:80

◆ toString()

const char * ns_rltime_v2::rlTime::toString ( const char *  format)
  Returns the datetime as a string. The format parameter determines the format of the result string.
  These expressions may be used for the date:
  Expression  Output
  d the day as number without a leading zero (1 to 31)
  dd  the day as number with a leading zero (01 to 31)
  M the month as number without a leading zero (1-12)
  MM  the month as number with a leading zero (01-12)
  MMM the abbreviated localized month name (e.g. 'Jan' to 'Dec').
  yy  the year as two digit number (00-99)
  yyyy  the year as four digit number
  These expressions may be used for the time:
  Expression  Output
  h the hour without a leading zero (1 to 12 if AM/PM display)
  hh  the hour with a leading zero (01 to 12 if AM/PM display)
  H the hour without a leading zero (0 to 23, even with AM/PM display)
  HH  the hour with a leading zero (00 to 23, even with AM/PM display)
  m the minute without a leading zero (0 to 59)
  mm  the minute with a leading zero (00 to 59)
  s the whole second without a leading zero (0 to 59)
  ss  the whole second with a leading zero where applicable (00 to 59)
  z the fractional part of the second, to go after a decimal point, without trailing zeroes (0 to 999).
  zzz the fractional part of the second, to millisecond precision, including trailing zeroes where applicable (000 to 999).
  AP or A use AM/PM display. A/AP will be replaced by either "AM" or "PM".
  ap or a use am/pm display. a/ap will be replaced by either "am" or "pm".
  All other input characters will be copyed
  Example format strings (assumed that the rlTime is 21 May 2001 14:13:09.120):
  Format  Result
  dd.MM.yyyy      21.05.2001
  ddd MMMM d yy Tue May 21 01
  hh:mm:ss.zzz    14:13:09.120
  hh:mm:ss.z      14:13:09.12
  h:m:s ap        2:13:9 pm

Definition at line 265 of file rltime_v2.cpp.

266 {
267  // See:
268  // https://doc.qt.io/qt-5/qdatetime.html#toString
269  //
270  char buf[16];
271  char *dest = time_string;
272  while(*format != '\0')
273  {
274  if (strncmp(format,"dd",2) == 0)
275  {
276  sprintf(buf,"%02d",day);
277  strcpy(dest,buf);
278  dest += strlen(buf);
279  format += 2;
280  }
281  else if(strncmp(format,"d",1) == 0)
282  {
283  sprintf(buf,"%d",day);
284  strcpy(dest,buf);
285  dest += strlen(buf);
286  format += 1;
287  }
288  else if(strncmp(format,"MMM",3) == 0)
289  {
290  buf[0] = '\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");
303  strcpy(dest,buf);
304  dest += strlen(buf);
305  format += 3;
306  }
307  else if(strncmp(format,"MM",2) == 0)
308  {
309  sprintf(buf,"%02d",month);
310  strcpy(dest,buf);
311  dest += strlen(buf);
312  format += 2;
313  }
314  else if(strncmp(format,"M",1) == 0)
315  {
316  sprintf(buf,"%d",month);
317  strcpy(dest,buf);
318  dest += strlen(buf);
319  format += 1;
320  }
321  else if(strncmp(format,"yyyy",4) == 0)
322  {
323  sprintf(buf,"%4d",year);
324  strcpy(dest,buf);
325  dest += strlen(buf);
326  format += 4;
327  }
328  else if(strncmp(format,"yy",2) == 0)
329  {
330  sprintf(buf,"%4d",year);
331  strcpy(dest,&buf[2]);
332  dest += strlen(&buf[2]);
333  format += 2;
334  }
335  else if(strncmp(format,"hh",2) == 0)
336  {
337  if (hour > 12) sprintf(buf,"%02d", hour - 12);
338  else if(hour == 0) sprintf(buf,"%02d", 12);
339  else sprintf(buf,"%02d", hour);
340  strcpy(dest,buf);
341  dest += strlen(buf);
342  format += 2;
343  }
344  else if(strncmp(format,"h",1) == 0)
345  {
346  if (hour > 12) sprintf(buf,"%2d", hour - 12);
347  else if(hour == 0) sprintf(buf,"%2d", 12);
348  else sprintf(buf,"%2d", hour);
349  strcpy(dest,buf);
350  dest += strlen(buf);
351  format += 1;
352  }
353  else if(strncmp(format,"HH",2) == 0)
354  {
355  sprintf(buf,"%02d",hour);
356  strcpy(dest,buf);
357  dest += strlen(buf);
358  format += 2;
359  }
360  else if(strncmp(format,"H",1) == 0)
361  {
362  sprintf(buf,"%d",hour);
363  strcpy(dest,buf);
364  dest += strlen(buf);
365  format += 1;
366  }
367  else if(strncmp(format,"mm",2) == 0)
368  {
369  sprintf(buf,"%02d",minute);
370  strcpy(dest,buf);
371  dest += strlen(buf);
372  format += 2;
373  }
374  else if(strncmp(format,"m",1) == 0)
375  {
376  sprintf(buf,"%d",minute);
377  strcpy(dest,buf);
378  dest += strlen(buf);
379  format += 1;
380  }
381  else if(strncmp(format,"ss",2) == 0)
382  {
383  sprintf(buf,"%02d",second);
384  strcpy(dest,buf);
385  dest += strlen(buf);
386  format += 2;
387  }
388  else if(strncmp(format,"s",1) == 0)
389  {
390  sprintf(buf,"%d",second);
391  strcpy(dest,buf);
392  dest += strlen(buf);
393  format += 1;
394  }
395  else if(strncmp(format,"zzz",3) == 0)
396  {
397  sprintf(buf,"%03d",millisecond);
398  strcpy(dest,buf);
399  dest += strlen(buf);
400  format += 3;
401  }
402  else if(strncmp(format,"z",1) == 0)
403  {
404  sprintf(buf,"%d",millisecond);
405  strcpy(dest,buf);
406  dest += strlen(buf);
407  format += 1;
408  }
409  else if(strncmp(format,"AP",2) == 0)
410  {
411  if (hour == 0) strcpy(dest,"PM");
412  else if(hour < 13) strcpy(dest,"AM");
413  else strcpy(dest,"PM");
414  dest += strlen("AM");
415  format += 2;
416  }
417  else if(strncmp(format,"ap",2) == 0)
418  {
419  if (hour == 0) strcpy(dest,"pm");
420  else if(hour < 13) strcpy(dest,"am");
421  else strcpy(dest,"pm");
422  dest += strlen("am");
423  format += 2;
424  }
425  else if(strncmp(format,"A",1) == 0)
426  {
427  if (hour == 0) strcpy(dest,"PM");
428  else if(hour < 13) strcpy(dest,"AM");
429  else strcpy(dest,"PM");
430  dest += strlen("AM");
431  format += 1;
432  }
433  else if(strncmp(format,"a",1) == 0)
434  {
435  if (hour == 0) strcpy(dest,"pm");
436  else if(hour < 13) strcpy(dest,"am");
437  else strcpy(dest,"pm");
438  dest += strlen("am");
439  format += 1;
440  }
441  else
442  {
443  *dest++ = *format++;
444  }
445  if(dest - time_string + 6 > (int) sizeof(time_string)) break;
446  }
447  *dest = '\0';
448  return time_string;
449 }
char time_string[32 *2]
Definition: rltime_v2.h:123

◆ version()

const char * ns_rltime_v2::rlTime::version ( )

Definition at line 150 of file rltime_v2.cpp.

151 {
152  return __FILE__;
153 }

Member Data Documentation

◆ day

int ns_rltime_v2::rlTime::day

Definition at line 86 of file rltime_v2.h.

◆ hour

int ns_rltime_v2::rlTime::hour

Definition at line 87 of file rltime_v2.h.

◆ iso_time_string

char ns_rltime_v2::rlTime::iso_time_string[32]
private

Definition at line 124 of file rltime_v2.h.

◆ millisecond

int ns_rltime_v2::rlTime::millisecond

Definition at line 90 of file rltime_v2.h.

◆ minute

int ns_rltime_v2::rlTime::minute

Definition at line 88 of file rltime_v2.h.

◆ month

int ns_rltime_v2::rlTime::month

Definition at line 85 of file rltime_v2.h.

◆ second

int ns_rltime_v2::rlTime::second

Definition at line 89 of file rltime_v2.h.

◆ time_string

char ns_rltime_v2::rlTime::time_string[32 *2]
private

Definition at line 123 of file rltime_v2.h.

◆ year

int ns_rltime_v2::rlTime::year

Definition at line 84 of file rltime_v2.h.


The documentation for this class was generated from the following files: