00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <eyedbconfig.h>
00026
00027 #if TIME_WITH_SYS_TIME
00028 #include <sys/time.h>
00029 #include <time.h>
00030 #else
00031 #if HAVE_SYS_TIME_H
00032 #include <sys/time.h>
00033 #else
00034 #include <time.h>
00035 #endif
00036 #endif
00037 #include <iostream>
00038
00039 #include "ClockConverter.h"
00040
00041
00042
00043
00044
00045 namespace eyedb {
00046
00047 const eyedblib::int64 USEC_OF_DAY = 86400000000LL;
00048 const eyedblib::int64 USEC_OF_HOUR = 3600000000LL;
00049 const eyedblib::int32 USEC_OF_MINUTE = 60000000;
00050 const eyedblib::int32 USEC_OF_SECOND = 1000000;
00051 const eyedblib::int16 USEC_OF_MILLISECOND = 1000;
00052 const eyedblib::int16 MAX_TZ = 720;
00053 const eyedblib::int16 MIN_TZ = -720;
00054
00055 const int MAX_STRING_CLOCK = 32;
00056
00057
00058 const int MAX_STRING_ZONE = 16;
00059
00060
00061
00062
00063
00064
00065
00066
00067 ClockConverter::ClockConverter()
00068 {
00069 this->string_clock = new char[MAX_STRING_CLOCK];
00070 this->string_zone = new char[MAX_STRING_ZONE];
00071 }
00072
00073
00074 ClockConverter::~ClockConverter()
00075 {
00076 delete [] this->string_clock;
00077 delete [] this->string_zone;
00078 }
00079
00080
00081
00082 void
00083 ClockConverter::usec2clock(const eyedblib::int64 usec, eyedblib::int16 * hour, eyedblib::int16 * min, eyedblib::int16 *sec, eyedblib::int16 * ms, eyedblib::int16 *us)
00084 {
00085 eyedblib::int64 usec_res = 0;
00086
00087 if(hour != 0)
00088 {
00089 *hour = (eyedblib::int16) (usec/USEC_OF_HOUR);
00090 }
00091 usec_res = usec % USEC_OF_HOUR;
00092
00093 if(min != 0)
00094 {
00095 *min = (eyedblib::int16) (usec_res/USEC_OF_MINUTE);
00096 }
00097 usec_res = usec_res % USEC_OF_MINUTE;
00098
00099 if(sec != 0)
00100 {
00101 *sec = (eyedblib::int16) (usec_res/USEC_OF_SECOND);
00102 }
00103 usec_res = usec_res % USEC_OF_SECOND;
00104
00105 if(ms != 0)
00106 {
00107 *ms = (eyedblib::int16) (usec_res/USEC_OF_MILLISECOND);
00108 }
00109 usec_res = usec_res % USEC_OF_MILLISECOND;
00110
00111 if(us != 0)
00112 {
00113 *us = (eyedblib::int16) usec_res;
00114 }
00115
00116 }
00117
00118
00119 void
00120 ClockConverter::clock2usec(eyedblib::int64 *usec, const eyedblib::int16 hour, const eyedblib::int16 min, const eyedblib::int16 sec, const eyedblib::int16 ms, const eyedblib::int16 us)
00121 {
00122 *usec = hour * USEC_OF_HOUR +
00123 (eyedblib::int64)min * USEC_OF_MINUTE +
00124 (eyedblib::int64)sec * USEC_OF_SECOND +
00125 (eyedblib::int64)ms * USEC_OF_MILLISECOND +
00126 us;
00127
00128 }
00129
00130
00131 eyedblib::int32
00132 ClockConverter::usec2day(eyedblib::int64 usec)
00133 {
00134 return usec / USEC_OF_DAY;
00135 }
00136
00137
00138
00139
00140
00141 void
00142 ClockConverter::tz2clock(const eyedblib::int16 tz, eyedblib::int16 * hour, eyedblib::int16 * min)
00143 {
00144 if( hour != 0)
00145 {
00146 *hour = (tz/60)%24;
00147 }
00148 if( min != 0 )
00149 {
00150 *min = tz%60;
00151 }
00152 }
00153
00154 void
00155 ClockConverter::clock2tz(eyedblib::int16 * tz, const eyedblib::int16 hour, const eyedblib::int16 min)
00156 {
00157 *tz = (hour%24) * 60 + min;
00158 }
00159
00160
00161
00162
00163
00164 eyedblib::int64
00165 ClockConverter::ascii2usec(const char * t)
00166 {
00167
00168
00169
00170
00171
00172 if(strlen(t) > 16
00173 || strlen(t) < 5)
00174 {
00175 return 0;
00176 }
00177
00178
00179
00180
00181
00182
00183 strcpy(this->string_clock, t);
00184
00185 char * null_string = "";
00186 char * s_hour = null_string;
00187 char * s_min = null_string;
00188 char * s_sec = null_string;
00189 char * s_ms = null_string;
00190 char * s_us = null_string;
00191
00192 switch( strlen(string_clock) )
00193 {
00194 case 16 :
00195 {
00196 string_clock[12] = '\0';
00197 s_us = string_clock + 13;
00198 }
00199 case 12 :
00200 {
00201 string_clock[8] = '\0';
00202 s_ms = string_clock + 9;
00203 }
00204 case 8 :
00205 {
00206 string_clock[5] = '\0';
00207 s_sec = string_clock + 6;
00208 }
00209 case 5 :
00210 {
00211 string_clock[2] = '\0';
00212 s_min = string_clock + 3;
00213 s_hour = string_clock;
00214 }
00215 }
00216
00217
00218
00219
00220
00221
00222 eyedblib::int16 hour = atoi(s_hour);;
00223 eyedblib::int16 min = atoi(s_min);
00224 eyedblib::int16 sec = atoi(s_sec);
00225 eyedblib::int16 ms = atoi(s_ms);
00226 eyedblib::int16 us = atoi(s_us);
00227
00228
00229
00230
00231
00232
00233 eyedblib::int64 usec = 0;
00234 clock2usec(&usec, hour, min, sec, ms, us);
00235
00236
00237 return usec;
00238 }
00239
00240 char *
00241 ClockConverter::usec2ascii(const eyedblib::int64 usec)
00242 {
00243 eyedblib::int16 hour = 0;
00244 eyedblib::int16 min = 0;
00245 eyedblib::int16 sec = 0;
00246 eyedblib::int16 ms = 0;
00247 eyedblib::int16 us = 0;
00248
00249 usec2clock(usec, &hour, &min, &sec, &ms, &us);
00250
00251 char * return_value = new char[MAX_STRING_CLOCK];
00252 sprintf(return_value, "%.2d:%.2d:%.2d,%.3d,%.3d", hour, min, sec, ms, us);
00253
00254 return return_value;
00255 }
00256
00257
00258
00259 eyedblib::int16
00260 ClockConverter::ascii2tz(const char * tz)
00261 {
00262
00263
00264
00265
00266
00267 if(strlen(tz) > 9
00268 || strlen(tz) < 6)
00269 {
00270 return 0;
00271 }
00272
00273
00274
00275
00276
00277
00278 strcpy(this->string_zone, tz);
00279
00280 char * null_string = "";
00281 char * s_hour = null_string;
00282 char * s_min = null_string;
00283 char sign = 0;
00284
00285 switch( strlen(string_zone))
00286 {
00287 case 9 :
00288 {
00289 string_zone[6] = '\0';
00290 s_min = string_zone + 7;
00291 }
00292 case 6 :
00293 {
00294 s_hour = string_zone + 4;
00295 sign = string_zone[3];
00296 break;
00297 }
00298 }
00299
00300
00301
00302
00303
00304
00305
00306 eyedblib::int16 hour = atoi(s_hour);;
00307 eyedblib::int16 min = atoi(s_min);
00308
00309
00310 if( sign == '-' )
00311 {
00312 hour = -hour;
00313 min = -min;
00314 }
00315
00316
00317
00318
00319
00320
00321 eyedblib::int16 timezone = 0;
00322 clock2tz(&timezone, hour, min);
00323
00324 return timezone;
00325 }
00326
00327 char *
00328 ClockConverter::tz2ascii(const eyedblib::int16 tz)
00329 {
00330 eyedblib::int16 hour = 0;
00331 eyedblib::int16 min = 0;
00332 char sign;
00333
00334 if( tz < 0 )
00335 {
00336 sign = '-';
00337 }
00338 else
00339 {
00340 sign = '+';
00341 }
00342
00343
00344 tz2clock(tz, &hour, &min);
00345
00346
00347 char * return_value = new char[MAX_STRING_ZONE];
00348 sprintf(return_value, "GMT%c%.2d:%.2d", sign, abs(hour), abs(min));
00349
00350 return return_value;
00351 }
00352
00353
00354
00355
00356 eyedblib::int64
00357 ClockConverter::current_time()
00358 {
00359 struct timeval now;
00360 gettimeofday(&now, 0);
00361
00362 struct tm * time = gmtime((time_t*)&(now.tv_sec));
00363
00364 eyedblib::int64 usec = 0;
00365 clock2usec(&usec, time->tm_hour, time->tm_min, time->tm_sec, 0, 0);
00366 usec += now.tv_usec;
00367
00368 return usec;
00369 }
00370
00371
00372
00373
00374 eyedblib::int16
00375 ClockConverter::local_timezone()
00376 {
00377 #if defined(HAVE_TZSET) && defined(HAVE_VAR_LONG_TIMEZONE)
00378 tzset();
00379
00380 return -(timezone / 60);
00381 #else
00382 std::cerr << "ClockConverter::local_timezone() not implemented\n";
00383 return 0;
00384 #endif
00385 }
00386
00387 }
00388