~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/temporal.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
 
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
5
5
 *
6
6
 *  Authors:
7
7
 *
37
37
#include "config.h"
38
38
 
39
39
#include "drizzled/charset_info.h"
40
 
#include "drizzled/decimal.h"
 
40
#include "drizzled/type/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
 
47
49
#include <time.h>
48
50
 
49
51
#include <cstdio>
59
61
extern std::vector<TemporalFormat *> known_date_formats;
60
62
extern std::vector<TemporalFormat *> known_time_formats;
61
63
 
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)
 
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)
75
76
{}
76
77
 
77
78
uint64_t Temporal::_cumulative_seconds_in_time() const
100
101
        }
101
102
        
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;
105
106
        
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();
155
156
 
 
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())
157
159
  {
158
160
    current_format= *current;
1025
1027
    current++;
1026
1028
  }
1027
1029
 
1028
 
  if (! matched)
 
1030
  if (not matched)
1029
1031
    return false;
1030
 
  else
1031
 
    return is_valid();
 
1032
 
 
1033
  return is_fuzzy_valid();
1032
1034
}
1033
1035
 
1034
1036
int Time::to_string(char *to, size_t to_len) const
1069
1071
int MicroTimestamp::to_string(char *to, size_t to_len) const
1070
1072
{
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);
1076
1078
}
1077
1079
 
1078
 
void Time::to_decimal(my_decimal *to) const
 
1080
void Time::to_decimal(type::Decimal *to) const
1079
1081
{
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)
1083
1085
  {
1084
1086
    to->buf[(to->intg-1) / 9 + 1]= _useconds * 1000;
1086
1088
  }
1087
1089
}
1088
1090
 
1089
 
void Date::to_decimal(my_decimal *to) const
 
1091
void Date::to_decimal(type::Decimal *to) const
1090
1092
{
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);
1093
1095
}
1094
1096
 
1095
 
void DateTime::to_decimal(my_decimal *to) const
 
1097
void DateTime::to_decimal(type::Decimal *to) const
1096
1098
{
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)
1101
1103
  {
1102
1104
    to->buf[(to->intg-1) / 9 + 1]= _useconds * 1000;
1125
1127
     + _seconds;
1126
1128
}
1127
1129
 
 
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
 
1128
1138
void DateTime::to_int64_t(int64_t *to) const
1129
1139
{
1130
1140
  *to= ((
1281
1291
  struct tm broken_time;
1282
1292
  struct tm *result;
1283
1293
 
1284
 
  result= gmtime_r(&from, &broken_time);
 
1294
  result= util::gmtime(from, &broken_time);
1285
1295
  if (result != NULL)
1286
1296
  {
1287
1297
    _years= 0;
1305
1315
  struct tm broken_time;
1306
1316
  struct tm *result;
1307
1317
 
1308
 
  result= gmtime_r(&from, &broken_time);
 
1318
  result= util::gmtime(from, &broken_time);
1309
1319
  if (result != NULL)
1310
1320
  {
1311
1321
    _years= 1900 + broken_time.tm_year;
1324
1334
    return false;
1325
1335
}
1326
1336
 
 
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
 
1327
1363
bool DateTime::from_time_t(const time_t from)
1328
1364
{
1329
1365
  struct tm broken_time;
1330
1366
  struct tm *result;
1331
1367
 
1332
 
  result= gmtime_r(&from, &broken_time);
 
1368
  result= util::gmtime(from, &broken_time);
1333
1369
  if (result != NULL)
1334
1370
  {
1335
1371
    _years= 1900 + broken_time.tm_year;
1345
1381
    return is_valid();
1346
1382
  }
1347
1383
  else 
 
1384
  {
1348
1385
    return false;
 
1386
  }
1349
1387
}
1350
1388
 
1351
 
void Date::to_time_t(time_t *to) const
 
1389
void Date::to_time_t(time_t &to) const
1352
1390
{
1353
1391
  if (in_unix_epoch())
1354
1392
  {
1355
 
    *to= _epoch_seconds;
 
1393
    to= _epoch_seconds;
1356
1394
  }
1357
1395
  else
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;
 
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;
1370
1410
}
1371
1411
 
1372
1412
void NanoTimestamp::to_timespec(struct timespec *to) const
1392
1432
      && (_seconds <= 59); /* No Leap second... TIME is for elapsed time... */
1393
1433
}
1394
1434
 
 
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
 
1395
1448
bool DateTime::is_valid() const
1396
1449
{
1397
1450
  return (_years >= DRIZZLE_MIN_YEARS_SQL && _years <= DRIZZLE_MAX_YEARS_SQL)