~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/temporal.cc

  • Committer: Brian Aker
  • Date: 2010-12-31 21:25:11 UTC
  • mto: (2045.1.1 clean)
  • mto: This revision was merged to the branch mainline in revision 2046.
  • Revision ID: brian@tangent.org-20101231212511-bhlyaw71ij4v15au
Make more time usage private.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 * their single parameter.
35
35
 */
36
36
 
37
 
#include <config.h>
 
37
#include "config.h"
38
38
 
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/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"
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;
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
1078
void Time::to_decimal(type::Decimal *to) const
1291
1289
  struct tm broken_time;
1292
1290
  struct tm *result;
1293
1291
 
1294
 
  result= util::gmtime(from, &broken_time);
 
1292
  result= gmtime_r(&from, &broken_time);
1295
1293
  if (result != NULL)
1296
1294
  {
1297
1295
    _years= 0;
1315
1313
  struct tm broken_time;
1316
1314
  struct tm *result;
1317
1315
 
1318
 
  result= util::gmtime(from, &broken_time);
 
1316
  result= gmtime_r(&from, &broken_time);
1319
1317
  if (result != NULL)
1320
1318
  {
1321
1319
    _years= 1900 + broken_time.tm_year;
1334
1332
    return false;
1335
1333
}
1336
1334
 
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
1335
bool DateTime::from_time_t(const time_t from)
1364
1336
{
1365
1337
  struct tm broken_time;
1366
1338
  struct tm *result;
1367
1339
 
1368
 
  result= util::gmtime(from, &broken_time);
 
1340
  result= gmtime_r(&from, &broken_time);
1369
1341
  if (result != NULL)
1370
1342
  {
1371
1343
    _years= 1900 + broken_time.tm_year;
1381
1353
    return is_valid();
1382
1354
  }
1383
1355
  else 
1384
 
  {
1385
1356
    return false;
1386
 
  }
1387
1357
}
1388
1358
 
1389
1359
void Date::to_time_t(time_t &to) const
1403
1373
  to= _epoch_seconds;
1404
1374
}
1405
1375
 
1406
 
void MicroTimestamp::to_timeval(struct timeval &to) const
 
1376
void MicroTimestamp::to_timeval(struct timeval *to) const
1407
1377
{
1408
 
  to.tv_sec= _epoch_seconds;
1409
 
  to.tv_usec= _useconds;
 
1378
  to->tv_sec= _epoch_seconds;
 
1379
  to->tv_usec= _useconds;
1410
1380
}
1411
1381
 
1412
1382
void NanoTimestamp::to_timespec(struct timespec *to) const
1418
1388
bool Date::is_valid() const
1419
1389
{
1420
1390
  return (_years >= DRIZZLE_MIN_YEARS_SQL && _years <= DRIZZLE_MAX_YEARS_SQL)
1421
 
      && (_months >= 1 && _months <= DRIZZLE_MAX_MONTHS)
 
1391
      && (_months >= 1 && _months <= 12)
1422
1392
      && (_days >= 1 && _days <= days_in_gregorian_year_month(_years, _months));
1423
1393
}
1424
1394
 
1427
1397
  return (_years == 0)
1428
1398
      && (_months == 0)
1429
1399
      && (_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... */
 
1400
      && (_hours <= 23)
 
1401
      && (_minutes <= 59)
 
1402
      && (_seconds <= 59); /* No Leap second... TIME is for elapsed time... */
1433
1403
}
1434
1404
 
1435
1405
bool Time::is_fuzzy_valid() const
1438
1408
    return true;
1439
1409
 
1440
1410
  return (_years >= DRIZZLE_MIN_YEARS_SQL && _years <= DRIZZLE_MAX_YEARS_SQL)
1441
 
      && (_months >= 1 && _months <= DRIZZLE_MAX_MONTHS)
 
1411
      && (_months >= 1 && _months <= 12)
1442
1412
      && (_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... */
 
1413
      && (_hours <= 23)
 
1414
      && (_minutes <= 59)
 
1415
      && (_seconds <= 59); /* No Leap second... TIME is for elapsed time... */
1446
1416
}
1447
1417
 
1448
1418
bool DateTime::is_valid() const
1449
1419
{
1450
1420
  return (_years >= DRIZZLE_MIN_YEARS_SQL && _years <= DRIZZLE_MAX_YEARS_SQL)
1451
 
      && (_months >= 1 && _months <= DRIZZLE_MAX_MONTHS)
 
1421
      && (_months >= 1 && _months <= 12)
1452
1422
      && (_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... */
 
1423
      && (_hours <= 23)
 
1424
      && (_minutes <= 59)
 
1425
      && (_seconds <= 61); /* Leap second... */
1456
1426
}
1457
1427
 
1458
1428
bool Timestamp::is_valid() const
1459
1429
{
1460
 
  return DateTime::is_valid()
 
1430
  return DateTime::is_valid() 
1461
1431
      && in_unix_epoch_range(_years, _months, _days, _hours, _minutes, _seconds)
1462
 
      && (_seconds <= DRIZZLE_MAX_SECONDS);
 
1432
      && (_seconds <= 59);
1463
1433
}
1464
1434
 
1465
1435
bool MicroTimestamp::is_valid() const