rllib  1
rltime_v1.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rltime_v1.cpp - description
3  -------------------
4  begin : Tue Jan 02 2001
5  copyright : (C) 2001 by R. Lehrig
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 #include "rltime_v1.h"
17 #include <stdio.h>
18 #include <string.h>
19 #include <time.h>
20 #include <sys/stat.h>
21 
22 #ifdef RLUNIX
23 #include <sys/types.h>
24 #include <sys/time.h>
25 #include <unistd.h>
26 #endif
27 
28 #ifdef __VMS
29 #include <ssdef.h>
30 #include <iodef.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <starlet.h>
34 #include <descrip.h>
35 #include <lib$routines.h>
36 #include <libdef.h>
37 #include <jpidef.h>
38 typedef struct
39 {
40  short year;
41  short month;
42  short day;
43  short hour;
44  short min;
45  short sec;
46  short hth;
47 }
48 TDS;
49 typedef struct
50 {
51  long word_1;
52  long word_2;
53 }
55 #endif
56 
57 #ifdef RLWIN32
58 #include <windows.h>
59 #include <mmsystem.h>
60 #endif
61 
62 namespace ns_rltime_v1
63 {
64 
65 rlTime::rlTime(int Year, int Month, int Day, int Hour, int Minute, int Second, int Millisecond)
66 {
67  year = Year;
68  month = Month;
69  day = Day;
70  hour = Hour;
71  minute = Minute;
72  second = Second;
73  millisecond = Millisecond;
74 }
75 
77 {
78 }
79 
80 const char *rlTime::version()
81 {
82  return __FILE__;
83 }
84 
85 void rlTime::setTimeFromString(const char *time_string)
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 }
96 
97 void rlTime::setTimeFromIsoString(const char *iso_time_string)
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 }
108 
110 {
111  sprintf(time_string,"%04d-%02d-%02d %02d:%02d:%02d %03d",year, month, day, hour, minute, second, millisecond);
112  return time_string;
113 }
114 
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 }
120 
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 }
170 
171 int rlTime::getFileModificationTime(const char *filename)
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 }
197 
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 }
267 
269 {
270  rlTime t;
271  t = *this + time;
272  *this = t;
273  return *this;
274 }
275 
277 {
278  rlTime t;
279  t = *this - time;
280  *this = t;
281  return *this;
282 }
283 
285 {
286  int maxmonth,y,m;
287  rlTime t;
288 
289  t.year = year + time.year;
290  t.month = month + time.month;
291  t.day = day + time.day;
292  t.hour = hour + time.hour;
293  t.minute = minute + time.minute;
294  t.second = second + time.second;
296 
297  y = t.year;
298  if(t.month > 12 || (t.month==12 && t.day==31 && t.hour>=24)) y++;
299  m = t.month;
300  if(t.month > 12 || (t.month==12 && t.day==31 && t.hour>=24)) m = 1;
301 
302  switch(m % 12)
303  {
304  case 1: // january
305  maxmonth = 31;
306  break;
307  case 2: // february
308  maxmonth = 28;
309  // Annus bisextilis (calendario Gregoriano)
310  if(y%4==0)
311  {
312  maxmonth = 29;
313  int hth = y % 100;
314  int special = y % 400; // 1900-+-2100-2200-2300-+-2500-2600-2700
315  if(hth == 0 && special != 0) maxmonth = 28;
316  }
317  break;
318  case 3: // march
319  maxmonth = 31;
320  break;
321  case 4: // april
322  maxmonth = 30;
323  break;
324  case 5: // may
325  maxmonth = 31;
326  break;
327  case 6: // june
328  maxmonth = 30;
329  break;
330  case 7: // july
331  maxmonth = 31;
332  break;
333  case 8: // august
334  maxmonth = 31;
335  break;
336  case 9: // september
337  maxmonth = 30;
338  break;
339  case 10: // october
340  maxmonth = 31;
341  break;
342  case 11: // november
343  maxmonth = 30;
344  break;
345  case 12: // december
346  maxmonth = 31;
347  break;
348  default:
349  maxmonth = 31;
350  break;
351  }
352 
353  if(t.millisecond >= 1000) { t.second++; t.millisecond -= 1000; }
354  if(t.second >= 60) { t.minute++; t.second -= 60; }
355  if(t.minute >= 60) { t.hour++, t.minute -= 60; }
356  if(t.hour >= 24) { t.day++; t.hour -= 24; }
357  if(t.day > maxmonth) { t.month++; t.day -= maxmonth; }
358  if(t.month > 12) { t.year++; t.month -= 12; }
359  return t;
360 }
361 
362 rlTime rlTime::operator-(rlTime &time)
363 {
364  int maxmonth,y,m;
365  rlTime t;
366 
367  y = 0;
368  t.year = year - time.year;
369  t.month = month - time.month;
370  t.day = day - time.day;
371  t.hour = hour - time.hour;
372  t.minute = minute - time.minute;
373  t.second = second - time.second;
374  t.millisecond = millisecond - time.millisecond;
375 
376  if(t.millisecond < 0) { t.second--; t.millisecond += 1000; }
377  if(t.second < 0) { t.minute--; t.second += 60; }
378  if(t.minute < 0) { t.hour--, t.minute += 60; }
379  if(t.hour < 0) { t.day--; t.hour += 24; }
380 
381  if(t.day < 0)
382  {
383  t.month--;
384  y = t.year;
385  m = t.month;
386  if(m <= 0) { m += 12; y--; }
387  switch(m % 12)
388  {
389  case 1: // january
390  maxmonth = 31;
391  break;
392  case 2: // february
393  maxmonth = 28;
394  // Annus bisextilis (calendario Gregoriano)
395  if(y%4==0)
396  {
397  maxmonth = 29;
398  int hth = y % 100;
399  int special = y % 400; // 1900-+-2100-2200-2300-+-2500-2600-2700
400  if(hth == 0 && special != 0) maxmonth = 28;
401  }
402  break;
403  case 3: // march
404  maxmonth = 31;
405  break;
406  case 4: // april
407  maxmonth = 30;
408  break;
409  case 5: // may
410  maxmonth = 31;
411  break;
412  case 6: // june
413  maxmonth = 30;
414  break;
415  case 7: // july
416  maxmonth = 31;
417  break;
418  case 8: // august
419  maxmonth = 31;
420  break;
421  case 9: // september
422  maxmonth = 30;
423  break;
424  case 10: // october
425  maxmonth = 31;
426  break;
427  case 11: // november
428  maxmonth = 30;
429  break;
430  case 12: // december
431  maxmonth = 31;
432  break;
433  default:
434  maxmonth = 31;
435  break;
436  }
437  t.day += maxmonth;
438  }
439 
440  if(y >= 0)
441  {
442  //printf("after christ was born. thus everything is ok.\n");
443  }
444  else
445  {
446  //printf("before christ was born. now also ok\n");
447  { t.month++; t.day -= 30; }
448  if(t.day < 30) { t.day++; t.hour -= 24; }
449  if(t.hour < 0 ) { t.hour++; t.minute -= 60; }
450  if(t.minute < 0 ) { t.minute++; t.second -= 60; }
451  if(t.second < 0 ) { t.second++; t.millisecond -= 1000; }
452  }
453 
454  return t;
455 }
456 
457 int rlTime::operator==(rlTime &time)
458 {
459  if(year != time.year) return 0;
460  if(month != time.month) return 0;
461  if(day != time.day) return 0;
462  if(hour != time.hour) return 0;
463  if(minute != time.minute) return 0;
464  if(second != time.second) return 0;
465  if(millisecond != time.millisecond) return 0;
466 
467  return 1;
468 }
469 
470 int rlTime::operator<(rlTime &time)
471 {
472  rlTime diff,t1;
473 
474  t1.year = year;
475  t1.month = month;
476  t1.day = day;
477  t1.hour = hour;
478  t1.minute = minute;
479  t1.second = second;
480  t1.millisecond = millisecond;
481  //printf("<t1=%s\n",t1.getTimeString());
482  //printf("<time=%s\n",time.getTimeString());
483  diff = t1 - time;
484  //printf("<diff=%s\n",diff.getTimeString());
485  if(diff.year < 0) return 1;
486  if(diff.month < 0) return 1;
487  if(diff.day < 0) return 1;
488  if(diff.hour < 0) return 1;
489  if(diff.minute < 0) return 1;
490  if(diff.second < 0) return 1;
491  if(diff.millisecond < 0) return 1;
492  return 0;
493 }
494 
495 int rlTime::operator<=(rlTime &time)
496 {
497  if((*this) == time) return 1;
498  if((*this) < time) return 1;
499  return 0;
500 }
501 
502 int rlTime::operator>(rlTime &time)
503 {
504  rlTime diff,t1;
505 
506  t1.year = year;
507  t1.month = month;
508  t1.day = day;
509  t1.hour = hour;
510  t1.minute = minute;
511  t1.second = second;
512  t1.millisecond = millisecond;
513  //printf(">t1=%s\n",t1.getTimeString());
514  //printf(">time=%s\n",time.getTimeString());
515  diff = time - t1;
516  //printf(">diff=%s\n",diff.getTimeString());
517  if(diff.year < 0) return 1;
518  if(diff.month < 0) return 1;
519  if(diff.day < 0) return 1;
520  if(diff.hour < 0) return 1;
521  if(diff.minute < 0) return 1;
522  if(diff.second < 0) return 1;
523  if(diff.millisecond < 0) return 1;
524  return 0;
525 }
526 
527 int rlTime::operator>=(rlTime &time)
528 {
529  if((*this) == time) return 1;
530  if((*this) > time) return 1;
531  return 0;
532 }
533 
535 {
536  struct tm begin;
537  struct tm test;
538 
539  memset(&begin,0,sizeof(tm));
540  memset(&test,0,sizeof(tm));
541 
542  begin.tm_year = 70;
543  begin.tm_mon = 0;
544  begin.tm_mday = 1;
545  begin.tm_hour = 0;
546  begin.tm_min = 0;
547  begin.tm_sec = 0;
548 
549  test.tm_year = year - 1900;
550  test.tm_mon = month - 1;
551  test.tm_mday = day;
552  test.tm_hour = hour;
553  test.tm_min = minute;
554  test.tm_sec = second;
555 
556  time_t t0 = mktime(&begin);
557  time_t t1 = mktime(&test);
558 
559  return difftime(t1,t0) + (((double) millisecond) / 1000);
560 }
561 
562 } // end of namespace ns_rltime_v1
563 
int operator>(rlTime &time)
Definition: rltime.cpp:495
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
char iso_time_string[32]
Definition: rltime_v1.h:64
const char * getIsoTimeString()
Definition: rltime_v1.cpp:115
double secondsSinceEpoche()
Definition: rltime.cpp:527
rlTime operator-(rlTime &time)
Definition: rltime.cpp:355
#define RLWIN32
Definition: rldefine.h:42
int operator>=(rlTime &time)
Definition: rltime.cpp:520
rlTime & operator-=(rlTime &time)
Definition: rltime_v1.cpp:276
const char * version()
Definition: rltime_v1.cpp:80
void setTimeFromString(const char *time_string)
Definition: rltime_v1.cpp:85
Definition: rltime.cpp:39
const char * getTimeString()
Definition: rltime_v1.cpp:109
void setTimeFromIsoString(const char *iso_time_string)
Definition: rltime_v1.cpp:97
int operator<(rlTime &time)
Definition: rltime.cpp:463
int operator==(rlTime &time)
Definition: rltime.cpp:450
int operator<=(rlTime &time)
Definition: rltime.cpp:488
rlTime operator+(rlTime &time)
Definition: rltime.cpp:277
int getFileModificationTime(const char *filename)
Definition: rltime_v1.cpp:171
rlTime & operator+=(rlTime &time)
Definition: rltime_v1.cpp:268
char time_string[32]
Definition: rltime_v1.h:63