00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "eyedb/syscls.h"
00025 #include "ClockConverter.h"
00026 #include "datelib.h"
00027
00028
00029
00030
00031
00032 namespace eyedb {
00033
00034 eyedblib::int64
00035 gmt2local_time(eyedblib::int64 gmt_time, eyedblib::int16 time_zone)
00036 {
00037
00038 eyedblib::int64 local_time = gmt_time + (eyedblib::int64)(time_zone) * USEC_OF_MINUTE;
00039
00040 local_time = local_time % USEC_OF_DAY;
00041
00042 if( local_time < 0 )
00043 {
00044 local_time = USEC_OF_DAY + local_time;
00045 }
00046
00047 return local_time;
00048 }
00049
00050
00051 void parse_time(const char * time, eyedblib::int64 * gmt_usec, eyedblib::int16 * timezone)
00052 {
00053
00054
00055 char * s = strdup(time);
00056
00057 char * s_usec = strtok(s, " ");
00058 char * s_tz = strtok(0, " ");
00059
00060
00061
00062 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00063
00064 eyedblib::int64 local_usec = clock->ascii2usec(s_usec);
00065
00066 if( s_tz == 0 )
00067 {
00068 *timezone = clock->local_timezone();
00069 }
00070 else
00071 {
00072 *timezone = clock->ascii2tz(s_tz);
00073 }
00074
00075
00076
00077
00078 *gmt_usec = local_usec - ((eyedblib::int64)(*timezone) * USEC_OF_MINUTE);
00079
00080
00081 *gmt_usec = *gmt_usec % USEC_OF_DAY;
00082
00083 if( *gmt_usec < 0 )
00084 {
00085 *gmt_usec = USEC_OF_DAY + *gmt_usec;
00086 }
00087
00088
00089
00090 free(s);
00091 }
00092
00093
00094
00095
00096
00097
00098
00099 void
00100 Time::userCopy(const Object &)
00101 {
00102 setClientData();
00103 }
00104
00105 void
00106 Time::userInitialize()
00107 {
00108 setClientData();
00109 }
00110
00111 void
00112 Time::setClientData()
00113 {
00114 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00115
00116 char * s_clock = clock->usec2ascii( gmt2local_time(getUsecs(), getTz()) );
00117 char * s_zone = clock->tz2ascii(getTz());
00118
00119 this->string_time[0] = '\0';
00120 strcat(this->string_time, s_clock);
00121 strcat(this->string_time, " ");
00122 strcat(this->string_time, s_zone);
00123
00124 delete [] s_clock;
00125 delete [] s_zone;
00126 }
00127
00128
00129
00130 eyedblib::int16
00131 Time::hour() const
00132 {
00133 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00134
00135 eyedblib::int16 hour = 0;
00136 eyedblib::int64 local_time = gmt2local_time(getUsecs(), getTz());
00137 clock->usec2clock( local_time, &hour);
00138
00139 return hour;
00140 }
00141
00142 eyedblib::int16
00143 Time::minute() const
00144 {
00145 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00146
00147 eyedblib::int16 min = 0;
00148 eyedblib::int64 local_time = gmt2local_time(getUsecs(), getTz());
00149 clock->usec2clock( local_time, 0, &min);
00150
00151 return min;
00152 }
00153
00154 eyedblib::int16
00155 Time::second() const
00156 {
00157 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00158
00159 eyedblib::int16 sec = 0;
00160 clock->usec2clock(getUsecs(), 0, 0, &sec);
00161
00162 return sec;
00163 }
00164
00165 eyedblib::int16
00166 Time::millisecond() const
00167 {
00168 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00169
00170 eyedblib::int16 ms = 0;
00171 clock->usec2clock(getUsecs(), 0, 0, 0, &ms);
00172
00173 return ms;
00174 }
00175
00176 eyedblib::int16
00177 Time::tz_hour() const
00178 {
00179
00180 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00181
00182 eyedblib::int16 hour = 0;
00183 clock->tz2clock(getTz(), &hour, 0);
00184
00185 return hour;
00186 }
00187
00188 eyedblib::int16
00189 Time::tz_minute() const
00190 {
00191 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00192
00193 eyedblib::int16 min = 0;
00194 clock->tz2clock(getTz(), 0, &min);
00195
00196 return min;
00197
00198 }
00199
00200 Bool
00201 Time::is_equal(const Time & t) const
00202 {
00203 if( getUsecs() == t.getUsecs() )
00204 {
00205 return True;
00206 }
00207 else
00208 {
00209 return False;
00210 }
00211 }
00212
00213 Bool
00214 Time::is_greater(const Time & t) const
00215 {
00216 if( getUsecs() > t.getUsecs() )
00217 {
00218 return True;
00219 }
00220 else
00221 {
00222 return False;
00223 }
00224 }
00225
00226 Bool
00227 Time::is_greater_or_equal(const Time & t) const
00228 {
00229 if( getUsecs() >= t.getUsecs() )
00230 {
00231 return True;
00232 }
00233 else
00234 {
00235 return False;
00236 }
00237 }
00238
00239 Bool
00240 Time::is_less(const Time & t) const
00241 {
00242 if( getUsecs() < t.getUsecs() )
00243 {
00244 return True;
00245 }
00246 else
00247 {
00248 return False;
00249 }
00250 }
00251
00252 Bool
00253 Time::is_less_or_equal(const Time & t) const
00254 {
00255 if( getUsecs() <= t.getUsecs() )
00256 {
00257 return True;
00258 }
00259 else
00260 {
00261 return False;
00262 }
00263
00264 }
00265
00266 Bool
00267 Time::is_between(const Time & t1, const Time &t2) const
00268 {
00269 eyedblib::int64 usecs = getUsecs();
00270
00271 if( ( usecs > t1.getUsecs()
00272 && usecs < t2.getUsecs() )
00273 || ( usecs < t1.getUsecs()
00274 && usecs > t2.getUsecs() ) )
00275 {
00276 return True;
00277 }
00278 else
00279 {
00280 return False;
00281 }
00282 }
00283
00284 Time &
00285 Time::add_interval(const TimeInterval & i)
00286 {
00287 eyedblib::int64 usecs = getUsecs() + i.getUsecs();
00288
00289 usecs = usecs % USEC_OF_DAY;
00290
00291 if( usecs < 0 )
00292 {
00293 usecs = USEC_OF_DAY + usecs;
00294 }
00295
00296 set_usecs(usecs, getTz());
00297
00298
00299 return *this;
00300 }
00301
00302 Time &
00303 Time::substract_interval(const TimeInterval & i)
00304 {
00305 eyedblib::int64 usecs = getUsecs() - i.getUsecs();
00306
00307 usecs = usecs % USEC_OF_DAY;
00308
00309 if( usecs < 0 )
00310 {
00311 usecs = USEC_OF_DAY + usecs;
00312 }
00313
00314 set_usecs(usecs, getTz());
00315
00316 return *this;
00317 }
00318
00319 TimeInterval *
00320 Time::substract_time(const Time & t)
00321 {
00322 eyedblib::int64 interval = getUsecs() - t.getUsecs();
00323
00324 if( interval < 0 )
00325 {
00326 interval = USEC_OF_DAY + interval;
00327 }
00328
00329 return TimeInterval::time_interval(getDatabase(), interval);
00330 }
00331
00332 eyedblib::int16
00333 Time::gmt_hour() const
00334 {
00335 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00336
00337 eyedblib::int16 hour = 0;
00338 clock->usec2clock(getUsecs(), &hour);
00339
00340 return hour;
00341 }
00342
00343 eyedblib::int16
00344 Time::gmt_minute() const
00345 {
00346 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00347
00348 eyedblib::int16 min = 0;
00349 clock->usec2clock(getUsecs(), 0, &min);
00350
00351 return min;
00352 }
00353
00354 eyedblib::int16
00355 Time::microsecond() const
00356 {
00357 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00358
00359 eyedblib::int16 us = 0;
00360 clock->usec2clock(getUsecs(), 0, 0, 0, 0, &us);
00361
00362 return us;
00363 }
00364
00365 Status
00366 Time::set_usecs(eyedblib::int64 usecs, eyedblib::int16 tz)
00367 {
00368 if(usecs < 0
00369 || usecs >= USEC_OF_DAY)
00370 {
00371 return Exception::make(IDB_ERROR, "time out of range");
00372 }
00373 if(tz < MIN_TZ
00374 || tz > MAX_TZ)
00375 {
00376 return Exception::make(IDB_ERROR, "time_zone out of range");
00377 }
00378
00379 Status s;
00380 s = setUsecs(usecs);
00381
00382
00383 setClientData();
00384
00385
00386 if(s)
00387 {
00388 return s;
00389 }
00390
00391 s = setTz(tz);
00392
00393
00394 setClientData();
00395
00396 return s;
00397 }
00398
00399 void
00400 Time::get_local_time_zone(eyedblib::int16 * tz_hour, eyedblib::int16 * tz_min)
00401 {
00402 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00403
00404 clock->tz2clock( clock->local_timezone(), tz_hour, tz_min);
00405 }
00406
00407 Time *
00408 Time::gmt_time(Database * db)
00409 {
00410 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00411
00412 Time * t = new Time(db);
00413 t->set_usecs(clock->current_time(), 0);
00414
00415 return t;
00416 }
00417
00418 Time *
00419 Time::local_time(Database * db)
00420 {
00421 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00422
00423 Time * t = new Time(db);
00424 t->set_usecs(clock->current_time(), clock->local_timezone());
00425
00426 return t;
00427 }
00428
00429 Time *
00430 Time::time(Database * db, const Time & time)
00431 {
00432 Time * t = new Time(db);
00433 t->set_usecs(time.getUsecs(), time.getTz());
00434
00435 return t;
00436 }
00437
00438 Time *
00439 Time::time(Database * db, eyedblib::int64 usec)
00440 {
00441 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00442
00443 Time * t = new Time(db);
00444 t->set_usecs(usec, clock->local_timezone());
00445
00446 return t;
00447 }
00448
00449 Time *
00450 Time::time(Database * db, eyedblib::int64 usec, eyedblib::int16 tz)
00451 {
00452 Time * t = new Time(db);
00453 t->set_usecs(usec, tz);
00454
00455 return t;
00456 }
00457
00458 Time *
00459 Time::time(Database * db, eyedblib::int16 hours, eyedblib::int16 min, eyedblib::int16 sec, eyedblib::int16 msec)
00460 {
00461 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00462
00463 Time * t = new Time(db);
00464
00465 eyedblib::int64 usec_time = 0;
00466 clock->clock2usec(&usec_time, hours, min, sec, msec, 0);
00467 t->set_usecs(usec_time, clock->local_timezone());
00468
00469 return t;
00470
00471 }
00472
00473
00474 Time *
00475 Time::time(Database * db, eyedblib::int16 hours, eyedblib::int16 min, eyedblib::int16 sec, eyedblib::int16 msec, eyedblib::int16 usec)
00476 {
00477 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00478
00479 Time * t = new Time(db);
00480
00481 eyedblib::int64 usec_time = 0;
00482 clock->clock2usec(&usec_time, hours, min, sec, msec, usec);
00483 t->set_usecs(usec_time, clock->local_timezone());
00484
00485 return t;
00486
00487 }
00488
00489
00490
00491 Time *
00492 Time::time(Database * db, eyedblib::int16 hours, eyedblib::int16 min, eyedblib::int16 sec, eyedblib::int16 msec, eyedblib::int16 tz_hour, eyedblib::int16 tz_minute)
00493 {
00494 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00495
00496 Time * t = new Time(db);
00497
00498 eyedblib::int64 usec_time = 0;
00499 eyedblib::int16 tz = 0;
00500 clock->clock2usec(&usec_time, hours, min, sec, msec, 0);
00501 clock->clock2tz(&tz, tz_hour, tz_minute);
00502
00503 t->set_usecs(usec_time, tz);
00504
00505 return t;
00506
00507 }
00508
00509 Time *
00510 Time::time(Database * db, eyedblib::int16 hours, eyedblib::int16 min, eyedblib::int16 sec, eyedblib::int16 msec, eyedblib::int16 usec, eyedblib::int16 tz_hour, eyedblib::int16 tz_minute)
00511 {
00512 ClockConverter * clock = DateAlgorithmRepository::getDefaultClockConverter();
00513
00514 Time * t = new Time(db);
00515
00516 eyedblib::int64 usec_time = 0;
00517 eyedblib::int16 tz = 0;
00518 clock->clock2usec(&usec_time, hours, min, sec, msec, usec);
00519 clock->clock2tz(&tz, tz_hour, tz_minute);
00520
00521 t->set_usecs(usec_time, tz);
00522
00523 return t;
00524
00525 }
00526
00527 Time *
00528 Time::time(Database * db, const char * s)
00529 {
00530 eyedblib::int64 gmt_usec = 0;
00531 eyedblib::int16 timezone = 0;
00532
00533 parse_time(s, &gmt_usec, &timezone);
00534
00535 Time * t = new Time(db);
00536 t->set_usecs(gmt_usec, timezone);
00537
00538 return t;
00539 }
00540
00541 const char *
00542 Time::toString() const
00543 {
00544 return this->string_time;
00545 }
00546
00547 eyedblib::int64
00548 Time::usec_time(const char * t)
00549 {
00550 eyedblib::int64 gmt_usec;
00551 eyedblib::int16 timezone;
00552
00553 parse_time(t, &gmt_usec, &timezone);
00554
00555 return gmt_usec;
00556 }
00557
00558 }