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, Inc.
4
* Copyright (C) 2008 Sun Microsystems
34
34
* their single parameter.
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>
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"
47
#include <drizzled/util/gmtime.h>
103
102
// Get the gmtime based on the local seconds since the Epoch
104
gm_time = util::gmtime(local_secs, &gm__rec);
103
gm_time = gmtime_r(&local_secs, &gm__rec);
105
104
gm_time->tm_isdst = 0;
107
106
// Interpret gmtime as the local time and convert it to seconds since the Epoch
154
153
TemporalFormat *current_format;
155
154
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.
158
156
while (current != known_date_formats.end())
160
158
current_format= *current;
1071
1069
int MicroTimestamp::to_string(char *to, size_t to_len) const
1073
1071
return snprintf(to, to_len,
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);
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);
1080
void Time::to_decimal(type::Decimal *to) const
1078
void Time::to_decimal(my_decimal *to) const
1082
1080
int64_t time_portion= (((_hours * 100L) + _minutes) * 100L) + _seconds;
1083
(void) int2_class_decimal(E_DEC_FATAL_ERROR, time_portion, false, to);
1081
(void) int2my_decimal(E_DEC_FATAL_ERROR, time_portion, false, to);
1084
1082
if (_useconds > 0)
1086
1084
to->buf[(to->intg-1) / 9 + 1]= _useconds * 1000;
1091
void Date::to_decimal(type::Decimal *to) const
1089
void Date::to_decimal(my_decimal *to) const
1093
1091
int64_t date_portion= (((_years * 100L) + _months) * 100L) + _days;
1094
(void) int2_class_decimal(E_DEC_FATAL_ERROR, date_portion, false, to);
1092
(void) int2my_decimal(E_DEC_FATAL_ERROR, date_portion, false, to);
1097
void DateTime::to_decimal(type::Decimal *to) const
1095
void DateTime::to_decimal(my_decimal *to) const
1099
1097
int64_t date_portion= (((_years * 100L) + _months) * 100L) + _days;
1100
1098
int64_t time_portion= (((((date_portion * 100L) + _hours) * 100L) + _minutes) * 100L) + _seconds;
1101
(void) int2_class_decimal(E_DEC_FATAL_ERROR, time_portion, false, to);
1099
(void) int2my_decimal(E_DEC_FATAL_ERROR, time_portion, false, to);
1102
1100
if (_useconds > 0)
1104
1102
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;
1363
1327
bool DateTime::from_time_t(const time_t from)
1365
1329
struct tm broken_time;
1366
1330
struct tm *result;
1368
result= util::gmtime(from, &broken_time);
1332
result= gmtime_r(&from, &broken_time);
1369
1333
if (result != NULL)
1371
1335
_years= 1900 + broken_time.tm_year;
1381
1345
return is_valid();
1389
void Date::to_time_t(time_t &to) const
1351
void Date::to_time_t(time_t *to) const
1391
1353
if (in_unix_epoch())
1355
*to= _epoch_seconds;
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;
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;
1412
1372
void NanoTimestamp::to_timespec(struct timespec *to) const
1418
1378
bool Date::is_valid() const
1420
1380
return (_years >= DRIZZLE_MIN_YEARS_SQL && _years <= DRIZZLE_MAX_YEARS_SQL)
1421
&& (_months >= 1 && _months <= DRIZZLE_MAX_MONTHS)
1381
&& (_months >= 1 && _months <= 12)
1422
1382
&& (_days >= 1 && _days <= days_in_gregorian_year_month(_years, _months));
1427
1387
return (_years == 0)
1428
1388
&& (_months == 0)
1429
1389
&& (_days == 0)
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... */
1392
&& (_seconds <= 59); /* No Leap second... TIME is for elapsed time... */
1448
1395
bool DateTime::is_valid() const
1450
1397
return (_years >= DRIZZLE_MIN_YEARS_SQL && _years <= DRIZZLE_MAX_YEARS_SQL)
1451
&& (_months >= 1 && _months <= DRIZZLE_MAX_MONTHS)
1398
&& (_months >= 1 && _months <= 12)
1452
1399
&& (_days >= 1 && _days <= days_in_gregorian_year_month(_years, _months))
1453
&& (_hours <= DRIZZLE_MAX_HOURS)
1454
&& (_minutes <= DRIZZLE_MAX_MINUTES)
1455
&& (_seconds <= DRIZZLE_MAX_SECONDS_WITH_LEAP); /* Leap second... */
1402
&& (_seconds <= 61); /* Leap second... */
1458
1405
bool Timestamp::is_valid() const
1460
return DateTime::is_valid()
1407
return DateTime::is_valid()
1461
1408
&& in_unix_epoch_range(_years, _months, _days, _hours, _minutes, _seconds)
1462
&& (_seconds <= DRIZZLE_MAX_SECONDS);
1409
&& (_seconds <= 59);
1465
1412
bool MicroTimestamp::is_valid() const