1
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
4
* Copyright (C) 2008 Sun Microsystems, Inc.
34
34
* their single parameter.
39
#include "drizzled/charset_info.h"
40
#include "drizzled/decimal.h"
41
#include "drizzled/calendar.h"
42
#include "drizzled/temporal.h"
43
#include "drizzled/temporal_format.h"
44
#include "drizzled/time_functions.h"
39
#include <drizzled/charset_info.h>
40
#include <drizzled/type/decimal.h>
41
#include <drizzled/calendar.h>
42
#include <drizzled/temporal.h>
43
#include <drizzled/temporal_format.h>
44
#include <drizzled/time_functions.h>
47
#include <drizzled/util/gmtime.h>
102
103
// Get the gmtime based on the local seconds since the Epoch
103
gm_time = gmtime_r(&local_secs, &gm__rec);
104
gm_time = util::gmtime(local_secs, &gm__rec);
104
105
gm_time->tm_isdst = 0;
106
107
// Interpret gmtime as the local time and convert it to seconds since the Epoch
153
154
TemporalFormat *current_format;
154
155
std::vector<TemporalFormat *>::iterator current= known_date_formats.begin();
157
_useconds= 0; // We may not match on it, so we need to make sure we zero it out.
156
158
while (current != known_date_formats.end())
158
160
current_format= *current;
1069
1071
int MicroTimestamp::to_string(char *to, size_t to_len) const
1071
1073
return snprintf(to, to_len,
1072
"%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32
1073
" %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ".%06" PRIu32,
1074
_years, _months, _days,
1075
_hours, _minutes, _seconds, _useconds);
1074
"%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32
1075
" %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ".%06" PRIu32,
1076
_years, _months, _days,
1077
_hours, _minutes, _seconds, _useconds);
1078
void Time::to_decimal(my_decimal *to) const
1080
void Time::to_decimal(type::Decimal *to) const
1080
1082
int64_t time_portion= (((_hours * 100L) + _minutes) * 100L) + _seconds;
1081
(void) int2my_decimal(E_DEC_FATAL_ERROR, time_portion, false, to);
1083
(void) int2_class_decimal(E_DEC_FATAL_ERROR, time_portion, false, to);
1082
1084
if (_useconds > 0)
1084
1086
to->buf[(to->intg-1) / 9 + 1]= _useconds * 1000;
1089
void Date::to_decimal(my_decimal *to) const
1091
void Date::to_decimal(type::Decimal *to) const
1091
1093
int64_t date_portion= (((_years * 100L) + _months) * 100L) + _days;
1092
(void) int2my_decimal(E_DEC_FATAL_ERROR, date_portion, false, to);
1094
(void) int2_class_decimal(E_DEC_FATAL_ERROR, date_portion, false, to);
1095
void DateTime::to_decimal(my_decimal *to) const
1097
void DateTime::to_decimal(type::Decimal *to) const
1097
1099
int64_t date_portion= (((_years * 100L) + _months) * 100L) + _days;
1098
1100
int64_t time_portion= (((((date_portion * 100L) + _hours) * 100L) + _minutes) * 100L) + _seconds;
1099
(void) int2my_decimal(E_DEC_FATAL_ERROR, time_portion, false, to);
1101
(void) int2_class_decimal(E_DEC_FATAL_ERROR, time_portion, false, to);
1100
1102
if (_useconds > 0)
1102
1104
to->buf[(to->intg-1) / 9 + 1]= _useconds * 1000;
1337
bool DateTime::from_timeval(struct timeval &timeval_arg)
1339
struct tm broken_time;
1342
result= util::gmtime(timeval_arg.tv_sec, &broken_time);
1345
_years= 1900 + broken_time.tm_year;
1346
_months= 1 + broken_time.tm_mon; /* Month is NOT ordinal for struct tm! */
1347
_days= broken_time.tm_mday; /* Day IS ordinal for struct tm */
1348
_hours= broken_time.tm_hour;
1349
_minutes= broken_time.tm_min;
1350
_seconds= broken_time.tm_sec;
1351
_epoch_seconds= timeval_arg.tv_sec;
1352
/* Set hires precision to zero */
1353
_useconds= timeval_arg.tv_usec;
1327
1363
bool DateTime::from_time_t(const time_t from)
1329
1365
struct tm broken_time;
1330
1366
struct tm *result;
1332
result= gmtime_r(&from, &broken_time);
1368
result= util::gmtime(from, &broken_time);
1333
1369
if (result != NULL)
1335
1371
_years= 1900 + broken_time.tm_year;
1345
1381
return is_valid();
1351
void Date::to_time_t(time_t *to) const
1389
void Date::to_time_t(time_t &to) const
1353
1391
if (in_unix_epoch())
1355
*to= _epoch_seconds;
1361
void Timestamp::to_time_t(time_t *to) const
1363
*to= _epoch_seconds;
1366
void MicroTimestamp::to_timeval(struct timeval *to) const
1368
to->tv_sec= _epoch_seconds;
1369
to->tv_usec= _useconds;
1401
void Timestamp::to_time_t(time_t &to) const
1406
void MicroTimestamp::to_timeval(struct timeval &to) const
1408
to.tv_sec= _epoch_seconds;
1409
to.tv_usec= _useconds;
1372
1412
void NanoTimestamp::to_timespec(struct timespec *to) const
1378
1418
bool Date::is_valid() const
1380
1420
return (_years >= DRIZZLE_MIN_YEARS_SQL && _years <= DRIZZLE_MAX_YEARS_SQL)
1381
&& (_months >= 1 && _months <= 12)
1421
&& (_months >= 1 && _months <= DRIZZLE_MAX_MONTHS)
1382
1422
&& (_days >= 1 && _days <= days_in_gregorian_year_month(_years, _months));
1387
1427
return (_years == 0)
1388
1428
&& (_months == 0)
1389
1429
&& (_days == 0)
1392
&& (_seconds <= 59); /* No Leap second... TIME is for elapsed time... */
1430
&& (_hours <= DRIZZLE_MAX_HOURS)
1431
&& (_minutes <= DRIZZLE_MAX_MINUTES)
1432
&& (_seconds <= DRIZZLE_MAX_SECONDS); /* No Leap second... TIME is for elapsed time... */
1435
bool Time::is_fuzzy_valid() const
1440
return (_years >= DRIZZLE_MIN_YEARS_SQL && _years <= DRIZZLE_MAX_YEARS_SQL)
1441
&& (_months >= 1 && _months <= DRIZZLE_MAX_MONTHS)
1442
&& (_days >= 1 && _days <= days_in_gregorian_year_month(_years, _months))
1443
&& (_hours <= DRIZZLE_MAX_HOURS)
1444
&& (_minutes <= DRIZZLE_MAX_MINUTES)
1445
&& (_seconds <= DRIZZLE_MAX_SECONDS); /* No Leap second... TIME is for elapsed time... */
1395
1448
bool DateTime::is_valid() const
1397
1450
return (_years >= DRIZZLE_MIN_YEARS_SQL && _years <= DRIZZLE_MAX_YEARS_SQL)
1398
&& (_months >= 1 && _months <= 12)
1451
&& (_months >= 1 && _months <= DRIZZLE_MAX_MONTHS)
1399
1452
&& (_days >= 1 && _days <= days_in_gregorian_year_month(_years, _months))
1402
&& (_seconds <= 61); /* Leap second... */
1453
&& (_hours <= DRIZZLE_MAX_HOURS)
1454
&& (_minutes <= DRIZZLE_MAX_MINUTES)
1455
&& (_seconds <= DRIZZLE_MAX_SECONDS_WITH_LEAP); /* Leap second... */
1405
1458
bool Timestamp::is_valid() const
1407
return DateTime::is_valid()
1460
return DateTime::is_valid()
1408
1461
&& in_unix_epoch_range(_years, _months, _days, _hours, _minutes, _seconds)
1409
&& (_seconds <= 59);
1462
&& (_seconds <= DRIZZLE_MAX_SECONDS);
1412
1465
bool MicroTimestamp::is_valid() const