|
rllib
1
|
00001 /*************************************************************************** 00002 rltime.cpp - description 00003 ------------------- 00004 begin : Tue Jan 02 2001 00005 copyright : (C) 2001 by Rainer Lehrig 00006 email : lehrig@t-online.de 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This library is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as * 00013 * published by the Free Software Foundation * 00014 * * 00015 ***************************************************************************/ 00016 #include "rltime.h" 00017 #include <stdio.h> 00018 #include <string.h> 00019 #include <time.h> 00020 #include <sys/stat.h> 00021 00022 #ifdef RLUNIX 00023 #include <sys/types.h> 00024 #include <sys/time.h> 00025 #include <unistd.h> 00026 #endif 00027 00028 #ifdef __VMS 00029 #include <ssdef.h> 00030 #include <iodef.h> 00031 #include <stdio.h> 00032 #include <string.h> 00033 #include <starlet.h> 00034 #include <descrip.h> 00035 #include <lib$routines.h> 00036 #include <libdef.h> 00037 #include <jpidef.h> 00038 typedef struct 00039 { 00040 short year; 00041 short month; 00042 short day; 00043 short hour; 00044 short min; 00045 short sec; 00046 short hth; 00047 } 00048 TDS; 00049 typedef struct 00050 { 00051 long word_1; 00052 long word_2; 00053 } 00054 VAX_BIN_TIME; 00055 #endif 00056 00057 #ifdef RLWIN32 00058 #include <windows.h> 00059 #include <mmsystem.h> 00060 #endif 00061 00062 rlTime::rlTime(int Year, int Month, int Day, int Hour, int Minute, int Second, int Millisecond) 00063 { 00064 year = Year; 00065 month = Month; 00066 day = Day; 00067 hour = Hour; 00068 minute = Minute; 00069 second = Second; 00070 millisecond = Millisecond; 00071 } 00072 00073 rlTime::~rlTime() 00074 { 00075 } 00076 00077 void rlTime::setTimeFromString(const char *time_string) 00078 { 00079 year = 0; 00080 month = 0; 00081 day = 0; 00082 hour = 0; 00083 minute = 0; 00084 second = 0; 00085 millisecond = 0; 00086 sscanf(time_string,"%d-%d-%d %d:%d:%d %d",&year,&month,&day, &hour,&minute,&second, &millisecond); 00087 } 00088 00089 void rlTime::setTimeFromIsoString(const char *iso_time_string) 00090 { 00091 year = 0; 00092 month = 0; 00093 day = 0; 00094 hour = 0; 00095 minute = 0; 00096 second = 0; 00097 millisecond = 0; 00098 sscanf(iso_time_string,"%d-%d-%dT%d:%d:%d.%d",&year,&month,&day, &hour,&minute,&second, &millisecond); 00099 } 00100 00101 const char *rlTime::getTimeString() 00102 { 00103 sprintf(time_string,"%04d-%02d-%02d %02d:%02d:%02d %04d",year,month,day, hour,minute,second, millisecond); 00104 return time_string; 00105 } 00106 00107 const char *rlTime::getIsoTimeString() 00108 { 00109 sprintf(time_string,"%04d-%02d-%02dT%02d:%02d:%02d.%d",year,month,day, hour,minute,second, millisecond); 00110 return time_string; 00111 } 00112 00113 void rlTime::getLocalTime() 00114 { 00115 #ifdef RLUNIX 00116 time_t t; 00117 struct tm *tms; 00118 struct timeval tv; 00119 struct timezone tz; 00120 00121 time(&t); 00122 tms = localtime(&t); 00123 gettimeofday(&tv, &tz); 00124 00125 /* adjust year and month */ 00126 tms->tm_year += 1900; 00127 tms->tm_mon += 1; 00128 00129 millisecond = (int)tv.tv_usec / 1000; 00130 second = (int)tms->tm_sec; 00131 minute = (int)tms->tm_min; 00132 hour = (int)tms->tm_hour; 00133 day = (int)tms->tm_mday; 00134 month = (int)tms->tm_mon; 00135 year = (int)tms->tm_year; 00136 #endif 00137 00138 #ifdef __VMS 00139 TDS tds; 00140 sys$numtim(&tds, 0); 00141 millisecond = (int)tds.hth * 10; 00142 second = (int)tds.sec; 00143 minute = (int)tds.min; 00144 hour = (int)tds.hour; 00145 day = (int)tds.day; 00146 month = (int)tds.month; 00147 year = (int)tds.year; 00148 #endif 00149 00150 #ifdef RLWIN32 00151 SYSTEMTIME st; 00152 GetLocalTime(&st); 00153 millisecond = st.wMilliseconds; 00154 second = st.wSecond; 00155 minute = st.wMinute; 00156 hour = st.wHour; 00157 day = st.wDay; 00158 month = st.wMonth; 00159 year = st.wYear; 00160 #endif 00161 } 00162 00163 int rlTime::getFileModificationTime(const char *filename) 00164 { 00165 struct stat statbuf; 00166 struct tm *tms; 00167 00168 #ifdef RLUNIX 00169 if(lstat(filename,&statbuf)) return -1; 00170 #else 00171 if(stat(filename,&statbuf)) return -1; 00172 #endif 00173 tms = localtime(&statbuf.st_mtime); 00174 00175 /* adjust year and month */ 00176 tms->tm_year += 1900; 00177 tms->tm_mon += 1; 00178 00179 millisecond = 0; 00180 second = (int)tms->tm_sec; 00181 minute = (int)tms->tm_min; 00182 hour = (int)tms->tm_hour; 00183 day = (int)tms->tm_mday; 00184 month = (int)tms->tm_mon; 00185 year = (int)tms->tm_year; 00186 00187 return 0; 00188 } 00189 00190 void rlTime::setLocalTime() 00191 { 00192 #ifdef RLUNIX 00193 struct timeval tv; 00194 struct tm t; 00195 00196 t.tm_mday = day; 00197 t.tm_mon = month - 1; 00198 t.tm_year = year - 1900; 00199 t.tm_hour = hour; 00200 t.tm_min = minute; 00201 t.tm_sec = second; 00202 tv.tv_sec = mktime(&t); 00203 tv.tv_usec = 1000 * millisecond; 00204 settimeofday(&tv,NULL); 00205 #endif 00206 00207 #ifdef __VMS 00208 VAX_BIN_TIME vbt; 00209 struct dsc$descriptor_s d_time; 00210 char smonth[12][4],buf[64]; 00211 00212 // Initialize month array 00213 memset (smonth , 0, sizeof(smonth)); 00214 memcpy (smonth [0], "JAN", 3); 00215 memcpy (smonth [1], "FEB", 3); 00216 memcpy (smonth [2], "MAR", 3); 00217 memcpy (smonth [3], "APR", 3); 00218 memcpy (smonth [4], "MAY", 3); 00219 memcpy (smonth [5], "JUN", 3); 00220 memcpy (smonth [6], "JUL", 3); 00221 memcpy (smonth [7], "AUG", 3); 00222 memcpy (smonth [8], "SEP", 3); 00223 memcpy (smonth [9], "OCT", 3); 00224 memcpy (smonth [10], "NOV", 3); 00225 memcpy (smonth [11], "DEC", 3); 00226 // Create time buffer 00227 sprintf(buf, "%02d-%3.3s-%04d %02d:%02d:%02d.%02d", 00228 day, 00229 smonth[month-1], 00230 year, 00231 hour, 00232 minute, 00233 second, 00234 millisecond / 10); 00235 00236 // Fill string descriptor 00237 d_time.dsc$w_length = strlen(buf); 00238 d_time.dsc$b_dtype = DSC$K_DTYPE_T; 00239 d_time.dsc$b_class = DSC$K_CLASS_S; 00240 d_time.dsc$a_pointer = buf; 00241 // Convert time buf to VAX bin time 00242 sys$bintim(&d_time, &vbt); 00243 // Set the system time 00244 sys$setime(&vbt); 00245 #endif 00246 00247 #ifdef RLWIN32 00248 SYSTEMTIME st; 00249 st.wDay = day; 00250 st.wMonth = month; 00251 st.wYear = year; 00252 st.wHour = hour; 00253 st.wMinute = minute; 00254 st.wSecond = second; 00255 st.wMilliseconds = millisecond; 00256 SetSystemTime(&st); 00257 #endif 00258 } 00259 00260 rlTime& rlTime::operator+=(rlTime &time) 00261 { 00262 rlTime t; 00263 t = *this + time; 00264 *this = t; 00265 return *this; 00266 } 00267 00268 rlTime& rlTime::operator-=(rlTime &time) 00269 { 00270 rlTime t; 00271 t = *this - time; 00272 *this = t; 00273 return *this; 00274 } 00275 00276 rlTime rlTime::operator+(rlTime &time) 00277 { 00278 int maxmonth,y,m; 00279 rlTime t; 00280 00281 t.year = year + time.year; 00282 t.month = month + time.month; 00283 t.day = day + time.day; 00284 t.hour = hour + time.hour; 00285 t.minute = minute + time.minute; 00286 t.second = second + time.second; 00287 t.millisecond = millisecond + time.millisecond; 00288 00289 y = t.year; 00290 if(t.month > 12 || (t.month==12 && t.day==31 && t.hour>=24)) y++; 00291 m = t.month; 00292 if(t.month > 12 || (t.month==12 && t.day==31 && t.hour>=24)) m = 1; 00293 00294 switch(m % 12) 00295 { 00296 case 1: // january 00297 maxmonth = 31; 00298 break; 00299 case 2: // february 00300 maxmonth = 28; 00301 // Annus bisextilis (calendario Gregoriano) 00302 if(y%4==0) 00303 { 00304 maxmonth = 29; 00305 int hth = y % 100; 00306 int special = y % 400; // 1900-+-2100-2200-2300-+-2500-2600-2700 00307 if(hth == 0 && special != 0) maxmonth = 28; 00308 } 00309 break; 00310 case 3: // march 00311 maxmonth = 31; 00312 break; 00313 case 4: // april 00314 maxmonth = 30; 00315 break; 00316 case 5: // may 00317 maxmonth = 31; 00318 break; 00319 case 6: // june 00320 maxmonth = 30; 00321 break; 00322 case 7: // july 00323 maxmonth = 31; 00324 break; 00325 case 8: // august 00326 maxmonth = 31; 00327 break; 00328 case 9: // september 00329 maxmonth = 30; 00330 break; 00331 case 10: // october 00332 maxmonth = 31; 00333 break; 00334 case 11: // november 00335 maxmonth = 30; 00336 break; 00337 case 12: // december 00338 maxmonth = 31; 00339 break; 00340 default: 00341 maxmonth = 31; 00342 break; 00343 } 00344 00345 if(t.millisecond >= 1000) { t.second++; t.millisecond -= 1000; } 00346 if(t.second >= 60) { t.minute++; t.second -= 60; } 00347 if(t.minute >= 60) { t.hour++, t.minute -= 60; } 00348 if(t.hour >= 24) { t.day++; t.hour -= 24; } 00349 if(t.day > maxmonth) { t.month++; t.day -= maxmonth; } 00350 if(t.month > 12) { t.year++; t.month -= 12; } 00351 return t; 00352 } 00353 00354 rlTime rlTime::operator-(rlTime &time) 00355 { 00356 int maxmonth,y,m; 00357 rlTime t; 00358 00359 t.year = year - time.year; 00360 t.month = month - time.month; 00361 t.day = day - time.day; 00362 t.hour = hour - time.hour; 00363 t.minute = minute - time.minute; 00364 t.second = second - time.second; 00365 t.millisecond = millisecond - time.millisecond; 00366 00367 if(t.millisecond < 0) { t.second--; t.millisecond += 1000; } 00368 if(t.second < 0) { t.minute--; t.second += 60; } 00369 if(t.minute < 0) { t.hour--, t.minute += 60; } 00370 if(t.hour < 0) { t.day--; t.hour += 24; } 00371 00372 if(t.day < 0) 00373 { 00374 t.month--; 00375 y = t.year; 00376 m = t.month; 00377 if(m <= 0) { m += 12; y--; } 00378 switch(m % 12) 00379 { 00380 case 1: // january 00381 maxmonth = 31; 00382 break; 00383 case 2: // february 00384 maxmonth = 28; 00385 // Annus bisextilis (calendario Gregoriano) 00386 if(y%4==0) 00387 { 00388 maxmonth = 29; 00389 int hth = y % 100; 00390 int special = y % 400; // 1900-+-2100-2200-2300-+-2500-2600-2700 00391 if(hth == 0 && special != 0) maxmonth = 28; 00392 } 00393 break; 00394 case 3: // march 00395 maxmonth = 31; 00396 break; 00397 case 4: // april 00398 maxmonth = 30; 00399 break; 00400 case 5: // may 00401 maxmonth = 31; 00402 break; 00403 case 6: // june 00404 maxmonth = 30; 00405 break; 00406 case 7: // july 00407 maxmonth = 31; 00408 break; 00409 case 8: // august 00410 maxmonth = 31; 00411 break; 00412 case 9: // september 00413 maxmonth = 30; 00414 break; 00415 case 10: // october 00416 maxmonth = 31; 00417 break; 00418 case 11: // november 00419 maxmonth = 30; 00420 break; 00421 case 12: // december 00422 maxmonth = 31; 00423 break; 00424 default: 00425 maxmonth = 31; 00426 break; 00427 } 00428 t.day += maxmonth; 00429 } 00430 if(t.month <= 0) { t.year--; t.month += 12; } 00431 00432 return t; 00433 } 00434 00435 int rlTime::operator==(rlTime &time) 00436 { 00437 if(year != time.year) return 0; 00438 if(month != time.month) return 0; 00439 if(day != time.day) return 0; 00440 if(hour != time.hour) return 0; 00441 if(minute != time.minute) return 0; 00442 if(second != time.second) return 0; 00443 if(millisecond != time.millisecond) return 0; 00444 00445 return 1; 00446 } 00447 00448 int rlTime::operator<(rlTime &time) 00449 { 00450 rlTime diff,t1; 00451 00452 t1.year = year; 00453 t1.month = month; 00454 t1.day = day; 00455 t1.hour = hour; 00456 t1.minute = minute; 00457 t1.second = second; 00458 t1.millisecond = millisecond; 00459 //printf("<t1=%s\n",t1.getTimeString()); 00460 //printf("<time=%s\n",time.getTimeString()); 00461 diff = t1 - time; 00462 //printf("<diff=%s\n",diff.getTimeString()); 00463 if(diff.year < 0) return 1; 00464 if(diff.month < 0) return 1; 00465 if(diff.day < 0) return 1; 00466 if(diff.hour < 0) return 1; 00467 if(diff.minute < 0) return 1; 00468 if(diff.second < 0) return 1; 00469 if(diff.millisecond < 0) return 1; 00470 return 0; 00471 } 00472 00473 int rlTime::operator<=(rlTime &time) 00474 { 00475 if((*this) == time) return 1; 00476 if((*this) < time) return 1; 00477 return 0; 00478 } 00479 00480 int rlTime::operator>(rlTime &time) 00481 { 00482 rlTime diff,t1; 00483 00484 t1.year = year; 00485 t1.month = month; 00486 t1.day = day; 00487 t1.hour = hour; 00488 t1.minute = minute; 00489 t1.second = second; 00490 t1.millisecond = millisecond; 00491 //printf(">t1=%s\n",t1.getTimeString()); 00492 //printf(">time=%s\n",time.getTimeString()); 00493 diff = time - t1; 00494 //printf(">diff=%s\n",diff.getTimeString()); 00495 if(diff.year < 0) return 1; 00496 if(diff.month < 0) return 1; 00497 if(diff.day < 0) return 1; 00498 if(diff.hour < 0) return 1; 00499 if(diff.minute < 0) return 1; 00500 if(diff.second < 0) return 1; 00501 if(diff.millisecond < 0) return 1; 00502 return 0; 00503 } 00504 00505 int rlTime::operator>=(rlTime &time) 00506 { 00507 if((*this) == time) return 1; 00508 if((*this) > time) return 1; 00509 return 0; 00510 } 00511 00512 double rlTime::secondsSinceEpoche() 00513 { 00514 struct tm begin; 00515 struct tm test; 00516 00517 memset(&begin,0,sizeof(tm)); 00518 memset(&test,0,sizeof(tm)); 00519 00520 begin.tm_year = 70; 00521 begin.tm_mon = 0; 00522 begin.tm_mday = 1; 00523 begin.tm_hour = 0; 00524 begin.tm_min = 0; 00525 begin.tm_sec = 0; 00526 00527 test.tm_year = year - 1900; 00528 test.tm_mon = month - 1; 00529 test.tm_mday = day; 00530 test.tm_hour = hour; 00531 test.tm_min = minute; 00532 test.tm_sec = second; 00533 00534 time_t t0 = mktime(&begin); 00535 time_t t1 = mktime(&test); 00536 00537 return difftime(t1,t0) + (((double) millisecond) / 1000); 00538 } 00539
1.7.5.1