rllib  1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
rltime.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rltime.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.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 rlTime::rlTime(int Year, int Month, int Day, int Hour, int Minute, int Second, int Millisecond)
63 {
64  year = Year;
65  month = Month;
66  day = Day;
67  hour = Hour;
68  minute = Minute;
69  second = Second;
70  millisecond = Millisecond;
71 }
72 
74 {
75 }
76 
77 void rlTime::setTimeFromString(const char *time_string)
78 {
79  year = 0;
80  month = 0;
81  day = 0;
82  hour = 0;
83  minute = 0;
84  second = 0;
85  millisecond = 0;
86  sscanf(time_string,"%d-%d-%d %d:%d:%d %d",&year,&month,&day, &hour,&minute,&second, &millisecond);
87 }
88 
89 void rlTime::setTimeFromIsoString(const char *iso_time_string)
90 {
91  year = 0;
92  month = 0;
93  day = 0;
94  hour = 0;
95  minute = 0;
96  second = 0;
97  millisecond = 0;
98  sscanf(iso_time_string,"%d-%d-%dT%d:%d:%d.%d",&year,&month,&day, &hour,&minute,&second, &millisecond);
99 }
100 
102 {
103  sprintf(time_string,"%04d-%02d-%02d %02d:%02d:%02d %04d",year,month,day, hour,minute,second, millisecond);
104  return time_string;
105 }
106 
108 {
109  sprintf(time_string,"%04d-%02d-%02dT%02d:%02d:%02d.%d",year,month,day, hour,minute,second, millisecond);
110  return time_string;
111 }
112 
114 {
115 #ifdef RLUNIX
116  time_t t;
117  struct tm *tms;
118  struct timeval tv;
119  struct timezone tz;
120 
121  time(&t);
122  tms = localtime(&t);
123  gettimeofday(&tv, &tz);
124 
125  /* adjust year and month */
126  tms->tm_year += 1900;
127  tms->tm_mon += 1;
128 
129  millisecond = (int)tv.tv_usec / 1000;
130  second = (int)tms->tm_sec;
131  minute = (int)tms->tm_min;
132  hour = (int)tms->tm_hour;
133  day = (int)tms->tm_mday;
134  month = (int)tms->tm_mon;
135  year = (int)tms->tm_year;
136 #endif
137 
138 #ifdef __VMS
139  TDS tds;
140  sys$numtim(&tds, 0);
141  millisecond = (int)tds.hth * 10;
142  second = (int)tds.sec;
143  minute = (int)tds.min;
144  hour = (int)tds.hour;
145  day = (int)tds.day;
146  month = (int)tds.month;
147  year = (int)tds.year;
148 #endif
149 
150 #ifdef RLWIN32
151  SYSTEMTIME st;
152  GetLocalTime(&st);
153  millisecond = st.wMilliseconds;
154  second = st.wSecond;
155  minute = st.wMinute;
156  hour = st.wHour;
157  day = st.wDay;
158  month = st.wMonth;
159  year = st.wYear;
160 #endif
161 }
162 
163 int rlTime::getFileModificationTime(const char *filename)
164 {
165  struct stat statbuf;
166  struct tm *tms;
167 
168 #ifdef RLUNIX
169  if(lstat(filename,&statbuf)) return -1;
170 #else
171  if(stat(filename,&statbuf)) return -1;
172 #endif
173  tms = localtime(&statbuf.st_mtime);
174 
175  /* adjust year and month */
176  tms->tm_year += 1900;
177  tms->tm_mon += 1;
178 
179  millisecond = 0;
180  second = (int)tms->tm_sec;
181  minute = (int)tms->tm_min;
182  hour = (int)tms->tm_hour;
183  day = (int)tms->tm_mday;
184  month = (int)tms->tm_mon;
185  year = (int)tms->tm_year;
186 
187  return 0;
188 }
189 
191 {
192 #ifdef RLUNIX
193  struct timeval tv;
194  struct tm t;
195 
196  t.tm_mday = day;
197  t.tm_mon = month - 1;
198  t.tm_year = year - 1900;
199  t.tm_hour = hour;
200  t.tm_min = minute;
201  t.tm_sec = second;
202  tv.tv_sec = mktime(&t);
203  tv.tv_usec = 1000 * millisecond;
204  settimeofday(&tv,NULL);
205 #endif
206 
207 #ifdef __VMS
208  VAX_BIN_TIME vbt;
209  struct dsc$descriptor_s d_time;
210  char smonth[12][4],buf[64];
211 
212  // Initialize month array
213  memset (smonth , 0, sizeof(smonth));
214  memcpy (smonth [0], "JAN", 3);
215  memcpy (smonth [1], "FEB", 3);
216  memcpy (smonth [2], "MAR", 3);
217  memcpy (smonth [3], "APR", 3);
218  memcpy (smonth [4], "MAY", 3);
219  memcpy (smonth [5], "JUN", 3);
220  memcpy (smonth [6], "JUL", 3);
221  memcpy (smonth [7], "AUG", 3);
222  memcpy (smonth [8], "SEP", 3);
223  memcpy (smonth [9], "OCT", 3);
224  memcpy (smonth [10], "NOV", 3);
225  memcpy (smonth [11], "DEC", 3);
226  // Create time buffer
227  sprintf(buf, "%02d-%3.3s-%04d %02d:%02d:%02d.%02d",
228  day,
229  smonth[month-1],
230  year,
231  hour,
232  minute,
233  second,
234  millisecond / 10);
235 
236  // Fill string descriptor
237  d_time.dsc$w_length = strlen(buf);
238  d_time.dsc$b_dtype = DSC$K_DTYPE_T;
239  d_time.dsc$b_class = DSC$K_CLASS_S;
240  d_time.dsc$a_pointer = buf;
241  // Convert time buf to VAX bin time
242  sys$bintim(&d_time, &vbt);
243  // Set the system time
244  sys$setime(&vbt);
245 #endif
246 
247 #ifdef RLWIN32
248  SYSTEMTIME st;
249  st.wDay = day;
250  st.wMonth = month;
251  st.wYear = year;
252  st.wHour = hour;
253  st.wMinute = minute;
254  st.wSecond = second;
255  st.wMilliseconds = millisecond;
256  SetSystemTime(&st);
257 #endif
258 }
259 
261 {
262  rlTime t;
263  t = *this + time;
264  *this = t;
265  return *this;
266 }
267 
269 {
270  rlTime t;
271  t = *this - time;
272  *this = t;
273  return *this;
274 }
275 
277 {
278  int maxmonth,y,m;
279  rlTime t;
280 
281  t.year = year + time.year;
282  t.month = month + time.month;
283  t.day = day + time.day;
284  t.hour = hour + time.hour;
285  t.minute = minute + time.minute;
286  t.second = second + time.second;
288 
289  y = t.year;
290  if(t.month > 12 || (t.month==12 && t.day==31 && t.hour>=24)) y++;
291  m = t.month;
292  if(t.month > 12 || (t.month==12 && t.day==31 && t.hour>=24)) m = 1;
293 
294  switch(m % 12)
295  {
296  case 1: // january
297  maxmonth = 31;
298  break;
299  case 2: // february
300  maxmonth = 28;
301  // Annus bisextilis (calendario Gregoriano)
302  if(y%4==0)
303  {
304  maxmonth = 29;
305  int hth = y % 100;
306  int special = y % 400; // 1900-+-2100-2200-2300-+-2500-2600-2700
307  if(hth == 0 && special != 0) maxmonth = 28;
308  }
309  break;
310  case 3: // march
311  maxmonth = 31;
312  break;
313  case 4: // april
314  maxmonth = 30;
315  break;
316  case 5: // may
317  maxmonth = 31;
318  break;
319  case 6: // june
320  maxmonth = 30;
321  break;
322  case 7: // july
323  maxmonth = 31;
324  break;
325  case 8: // august
326  maxmonth = 31;
327  break;
328  case 9: // september
329  maxmonth = 30;
330  break;
331  case 10: // october
332  maxmonth = 31;
333  break;
334  case 11: // november
335  maxmonth = 30;
336  break;
337  case 12: // december
338  maxmonth = 31;
339  break;
340  default:
341  maxmonth = 31;
342  break;
343  }
344 
345  if(t.millisecond >= 1000) { t.second++; t.millisecond -= 1000; }
346  if(t.second >= 60) { t.minute++; t.second -= 60; }
347  if(t.minute >= 60) { t.hour++, t.minute -= 60; }
348  if(t.hour >= 24) { t.day++; t.hour -= 24; }
349  if(t.day > maxmonth) { t.month++; t.day -= maxmonth; }
350  if(t.month > 12) { t.year++; t.month -= 12; }
351  return t;
352 }
353 
355 {
356  int maxmonth,y,m;
357  rlTime t;
358 
359  y = 0;
360  t.year = year - time.year;
361  t.month = month - time.month;
362  t.day = day - time.day;
363  t.hour = hour - time.hour;
364  t.minute = minute - time.minute;
365  t.second = second - time.second;
367 
368  if(t.millisecond < 0) { t.second--; t.millisecond += 1000; }
369  if(t.second < 0) { t.minute--; t.second += 60; }
370  if(t.minute < 0) { t.hour--, t.minute += 60; }
371  if(t.hour < 0) { t.day--; t.hour += 24; }
372 
373  if(t.day < 0)
374  {
375  t.month--;
376  y = t.year;
377  m = t.month;
378  if(m <= 0) { m += 12; y--; }
379  switch(m % 12)
380  {
381  case 1: // january
382  maxmonth = 31;
383  break;
384  case 2: // february
385  maxmonth = 28;
386  // Annus bisextilis (calendario Gregoriano)
387  if(y%4==0)
388  {
389  maxmonth = 29;
390  int hth = y % 100;
391  int special = y % 400; // 1900-+-2100-2200-2300-+-2500-2600-2700
392  if(hth == 0 && special != 0) maxmonth = 28;
393  }
394  break;
395  case 3: // march
396  maxmonth = 31;
397  break;
398  case 4: // april
399  maxmonth = 30;
400  break;
401  case 5: // may
402  maxmonth = 31;
403  break;
404  case 6: // june
405  maxmonth = 30;
406  break;
407  case 7: // july
408  maxmonth = 31;
409  break;
410  case 8: // august
411  maxmonth = 31;
412  break;
413  case 9: // september
414  maxmonth = 30;
415  break;
416  case 10: // october
417  maxmonth = 31;
418  break;
419  case 11: // november
420  maxmonth = 30;
421  break;
422  case 12: // december
423  maxmonth = 31;
424  break;
425  default:
426  maxmonth = 31;
427  break;
428  }
429  t.day += maxmonth;
430  }
431 
432  if(y >= 0)
433  {
434  //printf("after christ was born. thus everything is ok.\n");
435  }
436  else
437  {
438  //printf("before christ was born. now also ok\n");
439  { t.month++; t.day -= 30; }
440  if(t.day < 30) { t.day++; t.hour -= 24; }
441  if(t.hour < 0 ) { t.hour++; t.minute -= 60; }
442  if(t.minute < 0 ) { t.minute++; t.second -= 60; }
443  if(t.second < 0 ) { t.second++; t.millisecond -= 1000; }
444  }
445 
446  return t;
447 }
448 
450 {
451  if(year != time.year) return 0;
452  if(month != time.month) return 0;
453  if(day != time.day) return 0;
454  if(hour != time.hour) return 0;
455  if(minute != time.minute) return 0;
456  if(second != time.second) return 0;
457  if(millisecond != time.millisecond) return 0;
458 
459  return 1;
460 }
461 
463 {
464  rlTime diff,t1;
465 
466  t1.year = year;
467  t1.month = month;
468  t1.day = day;
469  t1.hour = hour;
470  t1.minute = minute;
471  t1.second = second;
473  //printf("<t1=%s\n",t1.getTimeString());
474  //printf("<time=%s\n",time.getTimeString());
475  diff = t1 - time;
476  //printf("<diff=%s\n",diff.getTimeString());
477  if(diff.year < 0) return 1;
478  if(diff.month < 0) return 1;
479  if(diff.day < 0) return 1;
480  if(diff.hour < 0) return 1;
481  if(diff.minute < 0) return 1;
482  if(diff.second < 0) return 1;
483  if(diff.millisecond < 0) return 1;
484  return 0;
485 }
486 
488 {
489  if((*this) == time) return 1;
490  if((*this) < time) return 1;
491  return 0;
492 }
493 
495 {
496  rlTime diff,t1;
497 
498  t1.year = year;
499  t1.month = month;
500  t1.day = day;
501  t1.hour = hour;
502  t1.minute = minute;
503  t1.second = second;
505  //printf(">t1=%s\n",t1.getTimeString());
506  //printf(">time=%s\n",time.getTimeString());
507  diff = time - t1;
508  //printf(">diff=%s\n",diff.getTimeString());
509  if(diff.year < 0) return 1;
510  if(diff.month < 0) return 1;
511  if(diff.day < 0) return 1;
512  if(diff.hour < 0) return 1;
513  if(diff.minute < 0) return 1;
514  if(diff.second < 0) return 1;
515  if(diff.millisecond < 0) return 1;
516  return 0;
517 }
518 
520 {
521  if((*this) == time) return 1;
522  if((*this) > time) return 1;
523  return 0;
524 }
525 
527 {
528  struct tm begin;
529  struct tm test;
530 
531  memset(&begin,0,sizeof(tm));
532  memset(&test,0,sizeof(tm));
533 
534  begin.tm_year = 70;
535  begin.tm_mon = 0;
536  begin.tm_mday = 1;
537  begin.tm_hour = 0;
538  begin.tm_min = 0;
539  begin.tm_sec = 0;
540 
541  test.tm_year = year - 1900;
542  test.tm_mon = month - 1;
543  test.tm_mday = day;
544  test.tm_hour = hour;
545  test.tm_min = minute;
546  test.tm_sec = second;
547 
548  time_t t0 = mktime(&begin);
549  time_t t1 = mktime(&test);
550 
551  return difftime(t1,t0) + (((double) millisecond) / 1000);
552 }
553