~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/temporal.cc

  • Committer: Brian Aker
  • Date: 2010-12-19 06:20:54 UTC
  • mfrom: (2005.1.1 bug673105)
  • Revision ID: brian@tangent.org-20101219062054-1kt0l3dxs4z2z8md
Merge Dave.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
#include "config.h"
38
38
 
39
39
#include "drizzled/charset_info.h"
40
 
#include "drizzled/type/decimal.h"
 
40
#include "drizzled/decimal.h"
41
41
#include "drizzled/calendar.h"
42
42
#include "drizzled/temporal.h"
43
43
#include "drizzled/temporal_format.h"
44
44
#include "drizzled/time_functions.h"
45
45
#include "time.h"
46
46
 
47
 
#include <drizzled/util/gmtime.h>
48
 
 
49
47
#include <time.h>
50
48
 
51
49
#include <cstdio>
61
59
extern std::vector<TemporalFormat *> known_date_formats;
62
60
extern std::vector<TemporalFormat *> known_time_formats;
63
61
 
64
 
Temporal::Temporal() :
65
 
  _calendar(GREGORIAN),
66
 
  _years(0),
67
 
  _months(0),
68
 
  _days(0),
69
 
  _hours(0),
70
 
  _minutes(0),
71
 
  _seconds(0),
72
 
  _epoch_seconds(0),
73
 
  _useconds(0),
74
 
  _nseconds(0),
75
 
  _overflow(false)
 
62
Temporal::Temporal()
 
63
:
 
64
  _calendar(GREGORIAN)
 
65
, _years(0)
 
66
, _months(0)
 
67
, _days(0)
 
68
, _hours(0)
 
69
, _minutes(0)
 
70
, _seconds(0)
 
71
, _epoch_seconds(0)
 
72
, _useconds(0)
 
73
, _nseconds(0)
 
74
, _overflow(false)
76
75
{}
77
76
 
78
77
uint64_t Temporal::_cumulative_seconds_in_time() const
101
100
        }
102
101
        
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;
106
105
        
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();
156
155
 
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())
159
157
  {
160
158
    current_format= *current;
1027
1025
    current++;
1028
1026
  }
1029
1027
 
1030
 
  if (not matched)
 
1028
  if (! matched)
1031
1029
    return false;
1032
 
 
1033
 
  return is_fuzzy_valid();
 
1030
  else
 
1031
    return is_valid();
1034
1032
}
1035
1033
 
1036
1034
int Time::to_string(char *to, size_t to_len) const
1071
1069
int MicroTimestamp::to_string(char *to, size_t to_len) const
1072
1070
{
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);
1078
1076
}
1079
1077
 
1080
 
void Time::to_decimal(type::Decimal *to) const
 
1078
void Time::to_decimal(my_decimal *to) const
1081
1079
{
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)
1085
1083
  {
1086
1084
    to->buf[(to->intg-1) / 9 + 1]= _useconds * 1000;
1088
1086
  }
1089
1087
}
1090
1088
 
1091
 
void Date::to_decimal(type::Decimal *to) const
 
1089
void Date::to_decimal(my_decimal *to) const
1092
1090
{
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);
1095
1093
}
1096
1094
 
1097
 
void DateTime::to_decimal(type::Decimal *to) const
 
1095
void DateTime::to_decimal(my_decimal *to) const
1098
1096
{
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)
1103
1101
  {
1104
1102
    to->buf[(to->intg-1) / 9 + 1]= _useconds * 1000;
1127
1125
     + _seconds;
1128
1126
}
1129
1127
 
1130
 
// We fill the structure based on just int
1131
 
void Time::to_uint64_t(uint64_t &to) const
1132
 
{
1133
 
  to= _hours * 24
1134
 
     + _minutes * 60
1135
 
     + _seconds;
1136
 
}
1137
 
 
1138
1128
void DateTime::to_int64_t(int64_t *to) const
1139
1129
{
1140
1130
  *to= ((
1291
1281
  struct tm broken_time;
1292
1282
  struct tm *result;
1293
1283
 
1294
 
  result= util::gmtime(from, &broken_time);
 
1284
  result= gmtime_r(&from, &broken_time);
1295
1285
  if (result != NULL)
1296
1286
  {
1297
1287
    _years= 0;
1315
1305
  struct tm broken_time;
1316
1306
  struct tm *result;
1317
1307
 
1318
 
  result= util::gmtime(from, &broken_time);
 
1308
  result= gmtime_r(&from, &broken_time);
1319
1309
  if (result != NULL)
1320
1310
  {
1321
1311
    _years= 1900 + broken_time.tm_year;
1334
1324
    return false;
1335
1325
}
1336
1326
 
1337
 
bool DateTime::from_timeval(struct timeval &timeval_arg)
1338
 
{
1339
 
  struct tm broken_time;
1340
 
  struct tm *result;
1341
 
 
1342
 
  result= util::gmtime(timeval_arg.tv_sec, &broken_time);
1343
 
  if (result != NULL)
1344
 
  {
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;
1354
 
    _nseconds= 0;
1355
 
    return is_valid();
1356
 
  }
1357
 
  else 
1358
 
  {
1359
 
    return false;
1360
 
  }
1361
 
}
1362
 
 
1363
1327
bool DateTime::from_time_t(const time_t from)
1364
1328
{
1365
1329
  struct tm broken_time;
1366
1330
  struct tm *result;
1367
1331
 
1368
 
  result= util::gmtime(from, &broken_time);
 
1332
  result= gmtime_r(&from, &broken_time);
1369
1333
  if (result != NULL)
1370
1334
  {
1371
1335
    _years= 1900 + broken_time.tm_year;
1381
1345
    return is_valid();
1382
1346
  }
1383
1347
  else 
1384
 
  {
1385
1348
    return false;
1386
 
  }
1387
1349
}
1388
1350
 
1389
 
void Date::to_time_t(time_t &to) const
 
1351
void Date::to_time_t(time_t *to) const
1390
1352
{
1391
1353
  if (in_unix_epoch())
1392
1354
  {
1393
 
    to= _epoch_seconds;
 
1355
    *to= _epoch_seconds;
1394
1356
  }
1395
1357
  else
1396
 
  {
1397
 
    to= 0;
1398
 
  }
1399
 
}
1400
 
 
1401
 
void Timestamp::to_time_t(time_t &to) const
1402
 
{
1403
 
  to= _epoch_seconds;
1404
 
}
1405
 
 
1406
 
void MicroTimestamp::to_timeval(struct timeval &to) const
1407
 
{
1408
 
  to.tv_sec= _epoch_seconds;
1409
 
  to.tv_usec= _useconds;
 
1358
    *to= 0;
 
1359
}
 
1360
 
 
1361
void Timestamp::to_time_t(time_t *to) const
 
1362
{
 
1363
  *to= _epoch_seconds;
 
1364
}
 
1365
 
 
1366
void MicroTimestamp::to_timeval(struct timeval *to) const
 
1367
{
 
1368
  to->tv_sec= _epoch_seconds;
 
1369
  to->tv_usec= _useconds;
1410
1370
}
1411
1371
 
1412
1372
void NanoTimestamp::to_timespec(struct timespec *to) const
1432
1392
      && (_seconds <= 59); /* No Leap second... TIME is for elapsed time... */
1433
1393
}
1434
1394
 
1435
 
bool Time::is_fuzzy_valid() const
1436
 
{
1437
 
  if (is_valid())
1438
 
    return true;
1439
 
 
1440
 
  return (_years >= DRIZZLE_MIN_YEARS_SQL && _years <= DRIZZLE_MAX_YEARS_SQL)
1441
 
      && (_months >= 1 && _months <= 12)
1442
 
      && (_days >= 1 && _days <= days_in_gregorian_year_month(_years, _months))
1443
 
      && (_hours <= 23)
1444
 
      && (_minutes <= 59)
1445
 
      && (_seconds <= 59); /* No Leap second... TIME is for elapsed time... */
1446
 
}
1447
 
 
1448
1395
bool DateTime::is_valid() const
1449
1396
{
1450
1397
  return (_years >= DRIZZLE_MIN_YEARS_SQL && _years <= DRIZZLE_MAX_YEARS_SQL)