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

#include <rltime_v1.h>

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 ()
 
void getLocalTime ()
 
int getFileModificationTime (const char *filename)
 
void setTimeFromString (const char *time_string)
 
void setTimeFromIsoString (const char *iso_time_string)
 
void setLocalTime ()
 
double secondsSinceEpoche ()
 
rlTimeoperator+= (rlTime &time)
 
rlTimeoperator-= (rlTime &time)
 
rlTime operator+ (rlTime &time)
 
rlTime operator- (rlTime &time)
 
int operator== (rlTime &time)
 
int operator< (rlTime &time)
 
int operator<= (rlTime &time)
 
int operator> (rlTime &time)
 
int operator>= (rlTime &time)
 

Public Attributes

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

Private Attributes

char time_string [32]
 
char iso_time_string [32]
 

Detailed Description

class for handling time.

Definition at line 28 of file rltime_v1.h.

Constructor & Destructor Documentation

◆ rlTime()

ns_rltime_v1::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 65 of file rltime_v1.cpp.

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

◆ ~rlTime()

ns_rltime_v1::rlTime::~rlTime ( )
virtual

Definition at line 76 of file rltime_v1.cpp.

77 {
78 }

Member Function Documentation

◆ getFileModificationTime()

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

Definition at line 171 of file rltime_v1.cpp.

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

◆ getIsoTimeString()

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

Definition at line 115 of file rltime_v1.cpp.

116 {
117  sprintf(iso_time_string,"%04d-%02d-%02dT%02d:%02d:%02d.%03d",year, month, day, hour, minute, second, millisecond);
118  return iso_time_string;
119 }
char iso_time_string[32]
Definition: rltime_v1.h:64

◆ getLocalTime()

void ns_rltime_v1::rlTime::getLocalTime ( )

Definition at line 121 of file rltime_v1.cpp.

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

◆ getTimeString()

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

Definition at line 109 of file rltime_v1.cpp.

110 {
111  sprintf(time_string,"%04d-%02d-%02d %02d:%02d:%02d %03d",year, month, day, hour, minute, second, millisecond);
112  return time_string;
113 }
char time_string[32]
Definition: rltime_v1.h:63

◆ operator+()

rlTime ns_rltime_v1::rlTime::operator+ ( rlTime time)

Definition at line 277 of file rltime.cpp.

278 {
279  int maxmonth,y,m;
280  rlTime t;
281 
282  t.year = year + time.year;
283  t.month = month + time.month;
284  t.day = day + time.day;
285  t.hour = hour + time.hour;
286  t.minute = minute + time.minute;
287  t.second = second + time.second;
289 
290  y = t.year;
291  if(t.month > 12 || (t.month==12 && t.day==31 && t.hour>=24)) y++;
292  m = t.month;
293  if(t.month > 12 || (t.month==12 && t.day==31 && t.hour>=24)) m = 1;
294 
295  switch(m % 12)
296  {
297  case 1: // january
298  maxmonth = 31;
299  break;
300  case 2: // february
301  maxmonth = 28;
302  // Annus bisextilis (calendario Gregoriano)
303  if(y%4==0)
304  {
305  maxmonth = 29;
306  int hth = y % 100;
307  int special = y % 400; // 1900-+-2100-2200-2300-+-2500-2600-2700
308  if(hth == 0 && special != 0) maxmonth = 28;
309  }
310  break;
311  case 3: // march
312  maxmonth = 31;
313  break;
314  case 4: // april
315  maxmonth = 30;
316  break;
317  case 5: // may
318  maxmonth = 31;
319  break;
320  case 6: // june
321  maxmonth = 30;
322  break;
323  case 7: // july
324  maxmonth = 31;
325  break;
326  case 8: // august
327  maxmonth = 31;
328  break;
329  case 9: // september
330  maxmonth = 30;
331  break;
332  case 10: // october
333  maxmonth = 31;
334  break;
335  case 11: // november
336  maxmonth = 30;
337  break;
338  case 12: // december
339  maxmonth = 31;
340  break;
341  default:
342  maxmonth = 31;
343  break;
344  }
345 
346  if(t.millisecond >= 1000) { t.second++; t.millisecond -= 1000; }
347  if(t.second >= 60) { t.minute++; t.second -= 60; }
348  if(t.minute >= 60) { t.hour++, t.minute -= 60; }
349  if(t.hour >= 24) { t.day++; t.hour -= 24; }
350  if(t.day > maxmonth) { t.month++; t.day -= maxmonth; }
351  if(t.month > 12) { t.year++; t.month -= 12; }
352  return t;
353 }

◆ operator+=()

rlTime & ns_rltime_v1::rlTime::operator+= ( rlTime time)

Definition at line 268 of file rltime_v1.cpp.

269 {
270  rlTime t;
271  t = *this + time;
272  *this = t;
273  return *this;
274 }
rlTime(int Year=0, int Month=0, int Day=0, int Hour=0, int Minute=0, int Second=0, int Millisecond=0)
Definition: rltime_v1.cpp:65

◆ operator-()

rlTime ns_rltime_v1::rlTime::operator- ( rlTime time)

Definition at line 355 of file rltime.cpp.

356 {
357  int maxmonth,y,m;
358  rlTime t;
359 
360  y = 0;
361  t.year = year - time.year;
362  t.month = month - time.month;
363  t.day = day - time.day;
364  t.hour = hour - time.hour;
365  t.minute = minute - time.minute;
366  t.second = second - time.second;
368 
369  if(t.millisecond < 0) { t.second--; t.millisecond += 1000; }
370  if(t.second < 0) { t.minute--; t.second += 60; }
371  if(t.minute < 0) { t.hour--, t.minute += 60; }
372  if(t.hour < 0) { t.day--; t.hour += 24; }
373 
374  if(t.day < 0)
375  {
376  t.month--;
377  y = t.year;
378  m = t.month;
379  if(m <= 0) { m += 12; y--; }
380  switch(m % 12)
381  {
382  case 1: // january
383  maxmonth = 31;
384  break;
385  case 2: // february
386  maxmonth = 28;
387  // Annus bisextilis (calendario Gregoriano)
388  if(y%4==0)
389  {
390  maxmonth = 29;
391  int hth = y % 100;
392  int special = y % 400; // 1900-+-2100-2200-2300-+-2500-2600-2700
393  if(hth == 0 && special != 0) maxmonth = 28;
394  }
395  break;
396  case 3: // march
397  maxmonth = 31;
398  break;
399  case 4: // april
400  maxmonth = 30;
401  break;
402  case 5: // may
403  maxmonth = 31;
404  break;
405  case 6: // june
406  maxmonth = 30;
407  break;
408  case 7: // july
409  maxmonth = 31;
410  break;
411  case 8: // august
412  maxmonth = 31;
413  break;
414  case 9: // september
415  maxmonth = 30;
416  break;
417  case 10: // october
418  maxmonth = 31;
419  break;
420  case 11: // november
421  maxmonth = 30;
422  break;
423  case 12: // december
424  maxmonth = 31;
425  break;
426  default:
427  maxmonth = 31;
428  break;
429  }
430  t.day += maxmonth;
431  }
432 
433  if(y >= 0)
434  {
435  //printf("after christ was born. thus everything is ok.\n");
436  }
437  else
438  {
439  //printf("before christ was born. now also ok\n");
440  { t.month++; t.day -= 30; }
441  if(t.day < 30) { t.day++; t.hour -= 24; }
442  if(t.hour < 0 ) { t.hour++; t.minute -= 60; }
443  if(t.minute < 0 ) { t.minute++; t.second -= 60; }
444  if(t.second < 0 ) { t.second++; t.millisecond -= 1000; }
445  }
446 
447  return t;
448 }

◆ operator-=()

rlTime & ns_rltime_v1::rlTime::operator-= ( rlTime time)

Definition at line 276 of file rltime_v1.cpp.

277 {
278  rlTime t;
279  t = *this - time;
280  *this = t;
281  return *this;
282 }
rlTime(int Year=0, int Month=0, int Day=0, int Hour=0, int Minute=0, int Second=0, int Millisecond=0)
Definition: rltime_v1.cpp:65

◆ operator<()

int ns_rltime_v1::rlTime::operator< ( rlTime time)

Definition at line 463 of file rltime.cpp.

464 {
465  rlTime diff,t1;
466 
467  t1.year = year;
468  t1.month = month;
469  t1.day = day;
470  t1.hour = hour;
471  t1.minute = minute;
472  t1.second = second;
474  //printf("<t1=%s\n",t1.getTimeString());
475  //printf("<time=%s\n",time.getTimeString());
476  diff = t1 - time;
477  //printf("<diff=%s\n",diff.getTimeString());
478  if(diff.year < 0) return 1;
479  if(diff.month < 0) return 1;
480  if(diff.day < 0) return 1;
481  if(diff.hour < 0) return 1;
482  if(diff.minute < 0) return 1;
483  if(diff.second < 0) return 1;
484  if(diff.millisecond < 0) return 1;
485  return 0;
486 }

◆ operator<=()

int ns_rltime_v1::rlTime::operator<= ( rlTime time)

Definition at line 488 of file rltime.cpp.

489 {
490  if((*this) == time) return 1;
491  if((*this) < time) return 1;
492  return 0;
493 }

◆ operator==()

int ns_rltime_v1::rlTime::operator== ( rlTime time)

Definition at line 450 of file rltime.cpp.

451 {
452  if(year != time.year) return 0;
453  if(month != time.month) return 0;
454  if(day != time.day) return 0;
455  if(hour != time.hour) return 0;
456  if(minute != time.minute) return 0;
457  if(second != time.second) return 0;
458  if(millisecond != time.millisecond) return 0;
459 
460  return 1;
461 }

◆ operator>()

int ns_rltime_v1::rlTime::operator> ( rlTime time)

Definition at line 495 of file rltime.cpp.

496 {
497  rlTime diff,t1;
498 
499  t1.year = year;
500  t1.month = month;
501  t1.day = day;
502  t1.hour = hour;
503  t1.minute = minute;
504  t1.second = second;
506  //printf(">t1=%s\n",t1.getTimeString());
507  //printf(">time=%s\n",time.getTimeString());
508  diff = time - t1;
509  //printf(">diff=%s\n",diff.getTimeString());
510  if(diff.year < 0) return 1;
511  if(diff.month < 0) return 1;
512  if(diff.day < 0) return 1;
513  if(diff.hour < 0) return 1;
514  if(diff.minute < 0) return 1;
515  if(diff.second < 0) return 1;
516  if(diff.millisecond < 0) return 1;
517  return 0;
518 }

◆ operator>=()

int ns_rltime_v1::rlTime::operator>= ( rlTime time)

Definition at line 520 of file rltime.cpp.

521 {
522  if((*this) == time) return 1;
523  if((*this) > time) return 1;
524  return 0;
525 }

◆ secondsSinceEpoche()

double ns_rltime_v1::rlTime::secondsSinceEpoche ( )

Definition at line 527 of file rltime.cpp.

528 {
529  struct tm begin;
530  struct tm test;
531 
532  memset(&begin,0,sizeof(tm));
533  memset(&test,0,sizeof(tm));
534 
535  begin.tm_year = 70;
536  begin.tm_mon = 0;
537  begin.tm_mday = 1;
538  begin.tm_hour = 0;
539  begin.tm_min = 0;
540  begin.tm_sec = 0;
541 
542  test.tm_year = year - 1900;
543  test.tm_mon = month - 1;
544  test.tm_mday = day;
545  test.tm_hour = hour;
546  test.tm_min = minute;
547  test.tm_sec = second;
548 
549  time_t t0 = mktime(&begin);
550  time_t t1 = mktime(&test);
551 
552  return difftime(t1,t0) + (((double) millisecond) / 1000);
553 }

◆ setLocalTime()

void ns_rltime_v1::rlTime::setLocalTime ( )

Definition at line 198 of file rltime_v1.cpp.

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

◆ setTimeFromIsoString()

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

Definition at line 97 of file rltime_v1.cpp.

98 {
99  year = 0;
100  month = 0;
101  day = 0;
102  hour = 0;
103  minute = 0;
104  second = 0;
105  millisecond = 0;
106  sscanf(iso_time_string,"%d-%d-%dT%d:%d:%d.%d",&year,&month,&day, &hour,&minute,&second, &millisecond);
107 }
char iso_time_string[32]
Definition: rltime_v1.h:64

◆ setTimeFromString()

void ns_rltime_v1::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 85 of file rltime_v1.cpp.

86 {
87  year = 0;
88  month = 0;
89  day = 0;
90  hour = 0;
91  minute = 0;
92  second = 0;
93  millisecond = 0;
94  sscanf(time_string,"%d-%d-%d %d:%d:%d %d",&year,&month,&day, &hour,&minute,&second, &millisecond);
95 }
char time_string[32]
Definition: rltime_v1.h:63

◆ version()

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

Definition at line 80 of file rltime_v1.cpp.

81 {
82  return __FILE__;
83 }

Member Data Documentation

◆ day

int ns_rltime_v1::rlTime::day

Definition at line 57 of file rltime_v1.h.

◆ hour

int ns_rltime_v1::rlTime::hour

Definition at line 58 of file rltime_v1.h.

◆ iso_time_string

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

Definition at line 64 of file rltime_v1.h.

◆ millisecond

int ns_rltime_v1::rlTime::millisecond

Definition at line 61 of file rltime_v1.h.

◆ minute

int ns_rltime_v1::rlTime::minute

Definition at line 59 of file rltime_v1.h.

◆ month

int ns_rltime_v1::rlTime::month

Definition at line 56 of file rltime_v1.h.

◆ second

int ns_rltime_v1::rlTime::second

Definition at line 60 of file rltime_v1.h.

◆ time_string

char ns_rltime_v1::rlTime::time_string[32]
private

Definition at line 63 of file rltime_v1.h.

◆ year

int ns_rltime_v1::rlTime::year

Definition at line 55 of file rltime_v1.h.


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