1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
8
* Jay Pipes <jay.pipes@sun.com>
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28
* Implementation of the server's temporal class API
32
* Move to completed ValueObject API, which would remove the from_xxx() methods
33
* and replace them with constructors which take other ValueObject instances as
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"
58
extern std::vector<TemporalFormat *> known_datetime_formats;
59
extern std::vector<TemporalFormat *> known_date_formats;
60
extern std::vector<TemporalFormat *> known_time_formats;
77
uint64_t Temporal::_cumulative_seconds_in_time() const
79
return (uint64_t) ((_hours * INT64_C(3600))
80
+ (_minutes * INT64_C(60))
84
#if defined(TARGET_OS_SOLARIS)
85
/* @TODO: Replace this with Boost.DateTime */
86
static time_t timegm(struct tm *my_time)
88
time_t local_secs, gm_secs;
89
struct tm gm__rec, *gm_time;
91
// Interpret 't' as the local time and convert it to seconds since the Epoch
92
local_secs = mktime(my_time);
96
local_secs = mktime (my_time);
102
// Get the gmtime based on the local seconds since the Epoch
103
gm_time = gmtime_r(&local_secs, &gm__rec);
104
gm_time->tm_isdst = 0;
106
// Interpret gmtime as the local time and convert it to seconds since the Epoch
107
gm_secs = mktime (gm_time);
111
gm_secs = mktime (gm_time);
117
// Return the local time adjusted by the difference from GM time.
118
return (local_secs - (gm_secs - local_secs));
122
void Temporal::set_epoch_seconds()
125
* If the temporal is in the range of a timestamp, set
126
* the epoch_seconds member variable
128
if (in_unix_epoch_range(_years, _months, _days, _hours, _minutes, _seconds))
131
struct tm broken_time;
133
broken_time.tm_sec= _seconds;
134
broken_time.tm_min= _minutes;
135
broken_time.tm_hour= _hours;
136
broken_time.tm_mday= _days; /* Drizzle format uses ordinal, standard tm does too! */
137
broken_time.tm_mon= _months - 1; /* Drizzle format uses ordinal, standard tm does NOT! */
138
broken_time.tm_year= _years - 1900; /* tm_year expects range of 70 - 38 */
140
result_time= timegm(&broken_time);
142
_epoch_seconds= result_time;
146
bool Date::from_string(const char *from, size_t from_len)
149
* Loop through the known date formats and see if
153
TemporalFormat *current_format;
154
std::vector<TemporalFormat *>::iterator current= known_date_formats.begin();
156
while (current != known_date_formats.end())
158
current_format= *current;
159
if (current_format->matches(from, from_len, this))
174
bool DateTime::from_string(const char *from, size_t from_len)
177
* Loop through the known datetime formats and see if
181
TemporalFormat *current_format;
182
std::vector<TemporalFormat *>::iterator current= known_datetime_formats.begin();
184
while (current != known_datetime_formats.end())
186
current_format= *current;
187
if (current_format->matches(from, from_len, this))
203
* Comparison operators for Time against another Time
204
* are easy. We simply compare the cumulative time
207
bool Time::operator==(const Time& rhs)
211
&& _minutes == rhs._minutes
212
&& _seconds == rhs._seconds
213
&& _useconds == rhs._useconds
214
&& _nseconds == rhs._nseconds
217
bool Time::operator!=(const Time& rhs)
219
return ! (*this == rhs);
221
bool Time::operator<(const Time& rhs)
223
return (_cumulative_seconds_in_time() < rhs._cumulative_seconds_in_time());
225
bool Time::operator<=(const Time& rhs)
227
return (_cumulative_seconds_in_time() <= rhs._cumulative_seconds_in_time());
229
bool Time::operator>(const Time& rhs)
231
return (_cumulative_seconds_in_time() > rhs._cumulative_seconds_in_time());
233
bool Time::operator>=(const Time& rhs)
235
return (_cumulative_seconds_in_time() >= rhs._cumulative_seconds_in_time());
239
* Subtracting one Time value from another can yield
240
* a new Time instance.
242
* This operator is called in the following situation:
246
* lhs.from_string("20:00:00");
248
* rhs.from_string("19:00:00");
250
* Time result= lhs - rhs;
255
* Subtracting a larger time value from a smaller one
256
* should throw an exception at some point. The result
257
* of such an operator should be a TemporalInterval, not
258
* a Time instance, since a negative time is not possible.
260
const Time Time::operator-(const Time& rhs)
264
int64_t second_diff= _cumulative_seconds_in_time() - rhs._cumulative_seconds_in_time();
265
result._hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
266
second_diff%= DRIZZLE_SECONDS_IN_HOUR;
267
result._minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
268
second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
269
result._seconds= (uint32_t) second_diff;
273
const Time Time::operator+(const Time& rhs)
276
int64_t second_diff= _cumulative_seconds_in_time() + rhs._cumulative_seconds_in_time();
277
result._hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
278
second_diff%= DRIZZLE_SECONDS_IN_HOUR;
279
result._minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
280
second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
281
result._seconds= (uint32_t) second_diff;
283
* @TODO Once exceptions are supported, we should raise an error here if
284
* the result Time is not valid?
290
* Variation of + and - operator which returns a reference to the left-hand
291
* side Time object and adds the right-hand side to itself.
293
Time& Time::operator+=(const Time& rhs)
295
int64_t second_diff= _cumulative_seconds_in_time() + rhs._cumulative_seconds_in_time();
296
_hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
297
second_diff%= DRIZZLE_SECONDS_IN_HOUR;
298
_minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
299
second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
300
_seconds= (uint32_t) second_diff;
302
* @TODO Once exceptions are supported, we should raise an error here if
303
* the result Time is not valid?
307
Time& Time::operator-=(const Time& rhs)
309
int64_t second_diff= _cumulative_seconds_in_time() - rhs._cumulative_seconds_in_time();
310
_hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
311
second_diff%= DRIZZLE_SECONDS_IN_HOUR;
312
_minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
313
second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
314
_seconds= (uint32_t) second_diff;
316
* @TODO Once exceptions are supported, we should raise an error here if
317
* the result Time is not valid?
323
* Comparison operators for Date against another Date
324
* are easy. We simply compare the cumulative
327
bool Date::operator==(const Date& rhs)
331
&& _months == rhs._months
332
&& _days == rhs._days
335
bool Date::operator!=(const Date& rhs)
337
return ! (*this == rhs);
339
bool Date::operator<(const Date& rhs)
341
int64_t days_left= julian_day_number_from_gregorian_date(_years, _months, _days);
342
int64_t days_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
343
return (days_left < days_right);
345
bool Date::operator<=(const Date& rhs)
347
int64_t days_left= julian_day_number_from_gregorian_date(_years, _months, _days);
348
int64_t days_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
349
return (days_left <= days_right);
351
bool Date::operator>(const Date& rhs)
353
return ! (*this <= rhs);
355
bool Date::operator>=(const Date& rhs)
357
return ! (*this < rhs);
361
* Comparison operators for DateTime against another DateTime
362
* are easy. We simply compare the cumulative time
365
bool Date::operator==(const DateTime& rhs)
369
&& _months == rhs._months
370
&& _days == rhs._days
371
&& _hours == rhs._hours
372
&& _minutes == rhs._minutes
373
&& _seconds == rhs._seconds
374
&& _useconds == rhs._useconds
375
&& _nseconds == rhs._nseconds
378
bool Date::operator!=(const DateTime& rhs)
380
return ! (*this == rhs);
382
bool Date::operator<(const DateTime& rhs)
384
int64_t days_left= julian_day_number_from_gregorian_date(_years, _months, _days);
385
int64_t days_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
386
if (days_left < days_right)
388
else if (days_left > days_right)
390
/* Here if both dates are the same, so compare times */
391
return (_cumulative_seconds_in_time() < rhs._cumulative_seconds_in_time());
393
bool Date::operator<=(const DateTime& rhs)
395
int64_t days_left= julian_day_number_from_gregorian_date(_years, _months, _days);
396
int64_t days_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
397
if (days_left < days_right)
399
else if (days_left > days_right)
401
/* Here if both dates are the same, so compare times */
402
return (_cumulative_seconds_in_time() <= rhs._cumulative_seconds_in_time());
404
bool Date::operator>(const DateTime& rhs)
406
return ! (*this <= rhs);
408
bool Date::operator>=(const DateTime& rhs)
410
return ! (*this < rhs);
414
* We can add or subtract a Time value to/from a DateTime value
415
* as well...it always produces a DateTime.
417
const Date Date::operator-(const Time& rhs)
422
* First, we set the resulting DATE pieces equal to our
423
* left-hand side DateTime's DATE components. Then, deal with
424
* the time components.
426
result._years= _years;
427
result._months= _months;
430
int64_t second_diff= _cumulative_seconds_in_time() - rhs._cumulative_seconds_in_time();
433
* The resulting diff might be negative. If it is, that means that
434
* we have subtracting a larger time piece from the datetime, like so:
436
* x = DateTime("2007-06-09 09:30:00") - Time("16:30:00");
438
* In these cases, we need to subtract a day from the resulting
444
result._hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
445
second_diff%= DRIZZLE_SECONDS_IN_HOUR;
446
result._minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
447
second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
448
result._seconds= (uint32_t) second_diff;
450
/* Handle the microsecond precision */
451
int64_t microsecond_diff= _useconds - rhs._useconds;
452
if (microsecond_diff < 0)
454
microsecond_diff= (-1 * microsecond_diff);
457
result._useconds= (uint32_t) microsecond_diff;
461
const Date Date::operator+(const Time& rhs)
466
* First, we set the resulting DATE pieces equal to our
467
* left-hand side DateTime's DATE components. Then, deal with
468
* the time components.
470
result._years= _years;
471
result._months= _months;
474
int64_t second_diff= _cumulative_seconds_in_time() + rhs._cumulative_seconds_in_time();
477
* The resulting seconds might be more than a day. If do,
478
* adjust our resulting days up 1.
480
if (second_diff >= DRIZZLE_SECONDS_IN_DAY)
483
second_diff%= DRIZZLE_SECONDS_IN_DAY;
486
result._hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
487
second_diff%= DRIZZLE_SECONDS_IN_HOUR;
488
result._minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
489
second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
490
result._seconds= (uint32_t) second_diff;
492
/* Handle the microsecond precision */
493
int64_t microsecond_diff= _useconds - rhs._useconds;
494
if (microsecond_diff < 0)
496
microsecond_diff= (-1 * microsecond_diff);
499
result._useconds= (uint32_t) microsecond_diff;
505
* Variation of + and - operator which returns a reference to the left-hand
506
* side DateTime object and adds the right-hand side Time to itself.
508
Date& Date::operator+=(const Time& rhs)
510
int64_t second_diff= _cumulative_seconds_in_time() + rhs._cumulative_seconds_in_time();
512
* The resulting seconds might be more than a day. If do,
513
* adjust our resulting days up 1.
515
if (second_diff >= DRIZZLE_SECONDS_IN_DAY)
518
second_diff%= DRIZZLE_SECONDS_IN_DAY;
521
_hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
522
second_diff%= DRIZZLE_SECONDS_IN_HOUR;
523
_minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
524
second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
525
_seconds= (uint32_t) second_diff;
527
/* Handle the microsecond precision */
528
int64_t microsecond_diff= _useconds - rhs._useconds;
529
if (microsecond_diff < 0)
531
microsecond_diff= (-1 * microsecond_diff);
534
_useconds= (uint32_t) microsecond_diff;
536
* @TODO Once exceptions are supported, we should raise an error here if
537
* the result Time is not valid?
541
Date& Date::operator-=(const Time& rhs)
543
int64_t second_diff= _cumulative_seconds_in_time() - rhs._cumulative_seconds_in_time();
546
* The resulting diff might be negative. If it is, that means that
547
* we have subtracting a larger time piece from the datetime, like so:
549
* x = DateTime("2007-06-09 09:30:00");
550
* x-= Time("16:30:00");
552
* In these cases, we need to subtract a day from the resulting
558
_hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
559
second_diff%= DRIZZLE_SECONDS_IN_HOUR;
560
_minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
561
second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
562
_seconds= (uint32_t) second_diff;
564
/* Handle the microsecond precision */
565
int64_t microsecond_diff= _useconds - rhs._useconds;
566
if (microsecond_diff < 0)
568
microsecond_diff= (-1 * microsecond_diff);
571
_useconds= (uint32_t) microsecond_diff;
573
* @TODO Once exceptions are supported, we should raise an error here if
574
* the result Time is not valid?
580
* We can add/subtract two Dates to/from each other. The result
581
* is always another Date instance.
583
const Date Date::operator-(const Date &rhs)
585
/* Figure out the difference in days between the two dates */
586
int64_t day_left= julian_day_number_from_gregorian_date(_years, _months, _days);
587
int64_t day_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
588
int64_t day_diff= day_left - day_right;
591
/* Now re-compose the Date's structure from the resulting Julian Day Number */
592
gregorian_date_from_julian_day_number(day_diff, &result._years, &result._months, &result._days);
595
const Date Date::operator+(const Date &rhs)
598
* Figure out the new Julian Day Number by adding the JDNs of both
601
int64_t day_left= julian_day_number_from_gregorian_date(_years, _months, _days);
602
int64_t day_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
603
int64_t day_diff= day_left + day_right;
605
/** @TODO Need an exception check here for bounds of JDN... */
608
/* Now re-compose the Date's structure from the resulting Julian Day Number */
609
gregorian_date_from_julian_day_number(day_diff, &result._years, &result._months, &result._days);
612
/* Similar to the above, but we add/subtract the right side to this object itself */
613
Date& Date::operator-=(const Date &rhs)
615
int64_t day_left= julian_day_number_from_gregorian_date(_years, _months, _days);
616
int64_t day_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
617
int64_t day_diff= day_left - day_right;
619
/* Now re-compose the Date's structure from the resulting Julian Day Number */
620
gregorian_date_from_julian_day_number(day_diff, &_years, &_months, &_days);
623
Date& Date::operator+=(const Date &rhs)
626
* Figure out the new Julian Day Number by adding the JDNs of both
629
int64_t day_left= julian_day_number_from_gregorian_date(_years, _months, _days);
630
int64_t day_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
631
int64_t day_diff= day_left + day_right;
633
/** @TODO Need an exception check here for bounds of JDN... */
635
/* Now re-compose the Date's structure from the resulting Julian Day Number */
636
gregorian_date_from_julian_day_number(day_diff, &_years, &_months, &_days);
640
Date& Date::operator=(const DateTime &rhs)
642
/* Only copy the Date components of the assigned DateTime... */
644
_months= rhs._months;
646
/* Zero-out everything else.. */
647
_hours= _minutes= _seconds= _useconds= _nseconds= 0;
652
* We can add/subtract two DateTimes to/from each other. The result
653
* is always another DateTime instance.
655
const Date Date::operator-(const DateTime &rhs)
657
/* Figure out the difference in days between the two dates. */
658
int64_t day_left= julian_day_number_from_gregorian_date(_years, _months, _days);
659
int64_t day_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
660
int64_t day_diff= day_left - day_right;
663
/* Now re-compose the Date's structure from the resulting Julian Day Number */
664
gregorian_date_from_julian_day_number(day_diff, &result._years, &result._months, &result._days);
666
/* And now handle the time components */
667
int64_t second_diff= _cumulative_seconds_in_time() - rhs._cumulative_seconds_in_time();
670
* The resulting diff might be negative. If it is, that means that
671
* we have subtracting a larger time piece from the datetime, like so:
673
* x = DateTime("2007-06-09 09:30:00");
674
* x-= Time("16:30:00");
676
* In these cases, we need to subtract a day from the resulting
682
result._hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
683
second_diff%= DRIZZLE_SECONDS_IN_HOUR;
684
result._minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
685
second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
686
result._seconds= (uint32_t) second_diff;
688
/* Handle the microsecond precision */
689
int64_t microsecond_diff= _useconds - rhs._useconds;
690
if (microsecond_diff < 0)
692
microsecond_diff= (-1 * microsecond_diff);
695
result._useconds= (uint32_t) microsecond_diff;
699
const Date Date::operator+(const DateTime &rhs)
702
* Figure out the new Julian Day Number by adding the JDNs of both
705
int64_t day_left= julian_day_number_from_gregorian_date(_years, _months, _days);
706
int64_t day_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
707
int64_t day_diff= day_left + day_right;
709
/** @TODO Need an exception check here for bounds of JDN... */
712
/* Now re-compose the Date's structure from the resulting Julian Day Number */
713
gregorian_date_from_julian_day_number(day_diff, &result._years, &result._months, &result._days);
715
/* And now handle the time components */
716
int64_t second_diff= _cumulative_seconds_in_time() + rhs._cumulative_seconds_in_time();
719
* The resulting seconds might be more than a day. If do,
720
* adjust our resulting days up 1.
722
if (second_diff >= DRIZZLE_SECONDS_IN_DAY)
725
second_diff%= DRIZZLE_SECONDS_IN_DAY;
728
result._hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
729
second_diff%= DRIZZLE_SECONDS_IN_HOUR;
730
result._minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
731
second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
732
result._seconds= (uint32_t) second_diff;
734
/* Handle the microsecond precision */
735
int64_t microsecond_diff= _useconds - rhs._useconds;
736
if (microsecond_diff < 0)
738
microsecond_diff= (-1 * microsecond_diff);
741
result._useconds= (uint32_t) microsecond_diff;
745
/* Similar to the above, but we add/subtract the right side to this object itself */
746
Date& Date::operator-=(const DateTime &rhs)
748
/* Figure out the difference in days between the two dates. */
749
int64_t day_left= julian_day_number_from_gregorian_date(_years, _months, _days);
750
int64_t day_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
751
int64_t day_diff= day_left - day_right;
753
/* Now re-compose the Date's structure from the ng Julian Day Number */
754
gregorian_date_from_julian_day_number(day_diff, &_years, &_months, &_days);
756
/* And now handle the time components */
757
int64_t second_diff= _cumulative_seconds_in_time() - rhs._cumulative_seconds_in_time();
760
* The resulting diff might be negative. If it is, that means that
761
* we have subtracting a larger time piece from the datetime, like so:
763
* x = DateTime("2007-06-09 09:30:00");
764
* x-= Time("16:30:00");
766
* In these cases, we need to subtract a day from the ng
772
_hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
773
second_diff%= DRIZZLE_SECONDS_IN_HOUR;
774
_minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
775
second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
776
_seconds= (uint32_t) second_diff;
778
/* Handle the microsecond precision */
779
int64_t microsecond_diff= _useconds - rhs._useconds;
780
if (microsecond_diff < 0)
782
microsecond_diff= (-1 * microsecond_diff);
785
_useconds= (uint32_t) microsecond_diff;
789
Date& Date::operator+=(const DateTime &rhs)
792
* Figure out the new Julian Day Number by adding the JDNs of both
795
int64_t day_left= julian_day_number_from_gregorian_date(_years, _months, _days);
796
int64_t day_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
797
int64_t day_diff= day_left + day_right;
799
/** @TODO Need an exception check here for bounds of JDN... */
801
/* Now re-compose the Date's structure from the ng Julian Day Number */
802
gregorian_date_from_julian_day_number(day_diff, &_years, &_months, &_days);
804
/* And now handle the time components */
805
int64_t second_diff= _cumulative_seconds_in_time() + rhs._cumulative_seconds_in_time();
808
* The resulting seconds might be more than a day. If do,
809
* adjust our ng days up 1.
811
if (second_diff >= DRIZZLE_SECONDS_IN_DAY)
814
second_diff%= DRIZZLE_SECONDS_IN_DAY;
817
_hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
818
second_diff%= DRIZZLE_SECONDS_IN_HOUR;
819
_minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
820
second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
821
_seconds= (uint32_t) second_diff;
823
/* Handle the microsecond precision */
824
int64_t microsecond_diff= _useconds - rhs._useconds;
825
if (microsecond_diff < 0)
827
microsecond_diff= (-1 * microsecond_diff);
830
_useconds= (uint32_t) microsecond_diff;
836
* Comparison operators between a Date and a Timestamp
838
bool Date::operator==(const Timestamp& rhs)
840
return (_years == rhs._years && _months == rhs._months && _days == rhs._days);
842
bool Date::operator!=(const Timestamp& rhs)
844
return ! (*this == rhs);
846
bool Date::operator<(const Timestamp& rhs)
848
if (_years < rhs._years)
850
if (_years > rhs._years)
853
if (_months < rhs._months)
855
if (_months > rhs._months)
858
return _days < rhs._days;
860
bool Date::operator<=(const Timestamp& rhs)
862
return (*this < rhs || *this == rhs);
864
bool Date::operator>(const Timestamp& rhs)
866
return ! (*this <= rhs);
868
bool Date::operator>=(const Timestamp& rhs)
870
return ! (*this < rhs);
873
* Comparison operators between a Timestamp and a Date
875
bool Timestamp::operator==(const Date& rhs)
877
return (_years == rhs._years && _months == rhs._months && _days == rhs._days);
879
bool Timestamp::operator!=(const Date& rhs)
881
return ! (*this == rhs);
883
bool Timestamp::operator<(const Date& rhs)
885
if (_years < rhs._years)
887
if (_years > rhs._years)
890
if (_months < rhs._months)
892
if (_months > rhs._months)
895
return _days < rhs._days;
897
bool Timestamp::operator<=(const Date& rhs)
899
return (*this < rhs || *this == rhs);
901
bool Timestamp::operator>(const Date& rhs)
903
return ! (*this <= rhs);
905
bool Timestamp::operator>=(const Date& rhs)
907
return ! (*this < rhs);
910
* Comparison operators between a Timestamp and a DateTime
912
bool Timestamp::operator==(const DateTime& rhs)
914
return (_years == rhs._years && _months == rhs._months && _days == rhs._days
915
&& _hours == rhs._hours && _minutes == rhs._minutes && _seconds == rhs._seconds);
917
bool Timestamp::operator!=(const DateTime& rhs)
919
return ! (*this == rhs);
921
bool Timestamp::operator<(const DateTime& rhs)
923
if (_years < rhs._years)
925
if (_years > rhs._years)
928
if (_months < rhs._months)
930
if (_months > rhs._months)
933
if (_days < rhs._days)
935
if (_days > rhs._days)
938
if (_hours < rhs._hours)
940
if (_hours > rhs._hours)
943
if (_minutes < rhs._minutes)
945
if (_minutes > rhs._minutes)
948
return _seconds < rhs._seconds;
950
bool Timestamp::operator<=(const DateTime& rhs)
952
return (*this < rhs || *this == rhs);
954
bool Timestamp::operator>(const DateTime& rhs)
956
return ! (*this <= rhs);
958
bool Timestamp::operator>=(const DateTime& rhs)
960
return ! (*this < rhs);
963
* Comparison operators between two Timestamps
965
bool Timestamp::operator==(const Timestamp& rhs)
967
return (_epoch_seconds == rhs._epoch_seconds);
969
bool Timestamp::operator!=(const Timestamp& rhs)
971
return ! (*this == rhs);
973
bool Timestamp::operator<(const Timestamp& rhs)
975
return (_epoch_seconds < rhs._epoch_seconds);
977
bool Timestamp::operator<=(const Timestamp& rhs)
979
return (_epoch_seconds <= rhs._epoch_seconds);
981
bool Timestamp::operator>(const Timestamp& rhs)
983
return ! (*this <= rhs);
985
bool Timestamp::operator>=(const Timestamp& rhs)
987
return ! (*this < rhs);
991
* Push the contents of the timestamp into the output stream
992
* as a formatted Timestamp value.
994
* @TODO This unfortunately fails in a weird way...even with std::noskipws,
995
* the output stream only reads up to the space in the string... :(
997
std::ostream& operator<<(std::ostream& os, const Timestamp& subject)
999
return os << subject.years() << '-'
1000
<< std::setw(2) << std::setfill('0') << subject.months() << '-'
1001
<< std::setw(2) << std::setfill('0') << subject.days() << ' '
1002
<< std::setw(2) << std::setfill('0') << subject.hours() << ':'
1003
<< std::setw(2) << std::setfill('0') << subject.minutes() << ':'
1004
<< std::setw(2) << std::setfill('0') << subject.seconds();
1007
bool Time::from_string(const char *from, size_t from_len)
1010
* Loop through the known time formats and see if
1013
bool matched= false;
1014
TemporalFormat *current_format;
1015
std::vector<TemporalFormat *>::iterator current= known_time_formats.begin();
1017
while (current != known_time_formats.end())
1019
current_format= *current;
1020
if (current_format->matches(from, from_len, this))
1034
int Time::to_string(char *to, size_t to_len) const
1036
return snprintf(to, to_len,
1037
"%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32,
1038
_hours, _minutes, _seconds);
1041
int Date::to_string(char *to, size_t to_len) const
1043
return snprintf(to, to_len,
1044
"%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32,
1045
_years, _months, _days);
1048
int DateTime::to_string(char *to, size_t to_len) const
1050
/* If the temporal has a microsecond component, use a slightly different output */
1053
return snprintf(to, to_len,
1054
"%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32
1055
" %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32,
1056
_years, _months, _days,
1057
_hours, _minutes, _seconds);
1061
return snprintf(to, to_len,
1062
"%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32
1063
" %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ".%06" PRIu32,
1064
_years, _months, _days,
1065
_hours, _minutes, _seconds, _useconds);
1069
int MicroTimestamp::to_string(char *to, size_t to_len) const
1071
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);
1078
void Time::to_decimal(my_decimal *to) const
1080
int64_t time_portion= (((_hours * 100L) + _minutes) * 100L) + _seconds;
1081
(void) int2my_decimal(E_DEC_FATAL_ERROR, time_portion, false, to);
1084
to->buf[(to->intg-1) / 9 + 1]= _useconds * 1000;
1089
void Date::to_decimal(my_decimal *to) const
1091
int64_t date_portion= (((_years * 100L) + _months) * 100L) + _days;
1092
(void) int2my_decimal(E_DEC_FATAL_ERROR, date_portion, false, to);
1095
void DateTime::to_decimal(my_decimal *to) const
1097
int64_t date_portion= (((_years * 100L) + _months) * 100L) + _days;
1098
int64_t time_portion= (((((date_portion * 100L) + _hours) * 100L) + _minutes) * 100L) + _seconds;
1099
(void) int2my_decimal(E_DEC_FATAL_ERROR, time_portion, false, to);
1102
to->buf[(to->intg-1) / 9 + 1]= _useconds * 1000;
1107
void Date::to_int64_t(int64_t *to) const
1109
*to= (_years * INT32_C(10000))
1110
+ (_months * INT32_C(100))
1114
void Date::to_int32_t(int32_t *to) const
1116
*to= (_years * INT32_C(10000))
1117
+ (_months * INT32_C(100))
1121
void Time::to_int32_t(int32_t *to) const
1123
*to= (_hours * INT32_C(10000))
1124
+ (_minutes * INT32_C(100))
1128
void DateTime::to_int64_t(int64_t *to) const
1131
(_years * INT64_C(10000))
1132
+ (_months * INT64_C(100))
1134
) * INT64_C(1000000))
1136
(_hours * INT64_C(10000))
1137
+ (_minutes * INT64_C(100) )
1142
void Date::to_tm(struct tm *to) const
1147
to->tm_mday= _days; /* Drizzle format uses ordinal, standard tm does too! */
1148
to->tm_mon= _months - 1; /* Drizzle format uses ordinal, standard tm does NOT! */
1149
to->tm_year= _years - 1900;
1152
void DateTime::to_tm(struct tm *to) const
1154
to->tm_sec= _seconds;
1155
to->tm_min= _minutes;
1156
to->tm_hour= _hours;
1157
to->tm_mday= _days; /* Drizzle format uses ordinal, standard tm does too! */
1158
to->tm_mon= _months - 1; /* Drizzle format uses ordinal, standard tm does NOT! */
1159
to->tm_year= _years - 1900;
1162
bool Date::from_julian_day_number(const int64_t from)
1164
gregorian_date_from_julian_day_number(from, &_years, &_months, &_days);
1168
void Date::to_julian_day_number(int64_t *to) const
1170
*to= julian_day_number_from_gregorian_date(_years, _months, _days);
1174
* Ignore overflow and pass-through to DateTime::from_int64_t()
1176
bool Date::from_int32_t(const int32_t from)
1178
return ((DateTime *) this)->from_int64_t((int64_t) from);
1182
* Attempt to interpret the supplied 4-byte integer as
1183
* a TIME value in the format HHmmSS
1185
bool Time::from_int32_t(const int32_t from)
1187
uint32_t copy_from= (uint32_t) from;
1188
_hours= copy_from / INT32_C(10000);
1189
_minutes= (copy_from % INT32_C(10000)) / INT32_C(100);
1190
_seconds= copy_from % INT32_C(100); /* Masks off all but last 2 digits */
1195
* We try to intepret the incoming number as a datetime "string".
1196
* This is pretty much a hack for usability, but keeps us compatible
1199
bool DateTime::from_int64_t(const int64_t from, bool convert)
1201
int64_t copy_from= from;
1205
if (copy_from == 0LL)
1208
if (convert && copy_from < 10000101000000LL)
1210
if (copy_from < 101)
1212
else if (copy_from <= (DRIZZLE_YY_PART_YEAR-1)*10000L+1231L)
1213
copy_from= (copy_from+20000000L)*1000000L; /* YYMMDD, year: 2000-2069 */
1214
else if (copy_from < (DRIZZLE_YY_PART_YEAR)*10000L+101L)
1216
else if (copy_from <= 991231L)
1217
copy_from= (copy_from+19000000L)*1000000L; /* YYMMDD, year: 1970-1999 */
1218
else if (copy_from < 10000101L)
1220
else if (copy_from <= 99991231L)
1221
copy_from= copy_from*1000000L;
1222
else if (copy_from < 101000000L)
1224
else if (copy_from <= (DRIZZLE_YY_PART_YEAR-1) * 10000000000LL + 1231235959LL)
1225
copy_from= copy_from + 20000000000000LL; /* YYMMDDHHMMSS, 2000-2069 */
1226
else if (copy_from < DRIZZLE_YY_PART_YEAR * 10000000000LL + 101000000LL)
1228
else if (copy_from <= 991231235959LL)
1229
copy_from= copy_from + 19000000000000LL; /* YYMMDDHHMMSS, 1970-1999 */
1232
part1= (int64_t) (copy_from / 1000000LL);
1233
part2= (int64_t) (copy_from - (int64_t) part1 * 1000000LL);
1234
_years= (uint32_t) (part1/10000L);
1237
_months= (uint32_t) part1 / 100;
1238
_days= (uint32_t) part1 % 100;
1239
_hours= (uint32_t) (part2/10000L);
1242
_minutes= (uint32_t) part2 / 100;
1243
_seconds= (uint32_t) part2 % 100;
1245
set_epoch_seconds();
1249
bool Date::in_unix_epoch() const
1251
return in_unix_epoch_range(_years, _months, _days, 0, 0, 0);
1254
bool DateTime::in_unix_epoch() const
1256
return in_unix_epoch_range(_years, _months, _days, _hours, _minutes, _seconds);
1259
bool Date::from_tm(const struct tm *from)
1261
_years= 1900 + from->tm_year;
1262
_months= 1 + from->tm_mon; /* Month is NOT ordinal for struct tm! */
1263
_days= from->tm_mday; /* Day IS ordinal for struct tm */
1264
_hours= from->tm_hour;
1265
_minutes= from->tm_min;
1266
_seconds= from->tm_sec;
1267
/* Set hires precision to zero */
1271
set_epoch_seconds();
1276
* We convert as if it's a Datetime, then simply
1277
* drop the date portions...
1279
bool Time::from_time_t(const time_t from)
1281
struct tm broken_time;
1284
result= gmtime_r(&from, &broken_time);
1290
_hours= broken_time.tm_hour;
1291
_minutes= broken_time.tm_min;
1292
_seconds= broken_time.tm_sec;
1293
_epoch_seconds= 0; /* Don't store the time_t, since we only use part of it */
1294
/* Set hires precision to zero */
1297
return true; /* Always true... */
1303
bool Date::from_time_t(const time_t from)
1305
struct tm broken_time;
1308
result= gmtime_r(&from, &broken_time);
1311
_years= 1900 + broken_time.tm_year;
1312
_months= 1 + broken_time.tm_mon; /* Month is NOT ordinal for struct tm! */
1313
_days= broken_time.tm_mday; /* Day IS ordinal for struct tm */
1317
_epoch_seconds= 0; /* Don't store the time_t, since we only use part of it */
1318
/* Set hires precision to zero */
1327
bool DateTime::from_time_t(const time_t from)
1329
struct tm broken_time;
1332
result= gmtime_r(&from, &broken_time);
1335
_years= 1900 + broken_time.tm_year;
1336
_months= 1 + broken_time.tm_mon; /* Month is NOT ordinal for struct tm! */
1337
_days= broken_time.tm_mday; /* Day IS ordinal for struct tm */
1338
_hours= broken_time.tm_hour;
1339
_minutes= broken_time.tm_min;
1340
_seconds= broken_time.tm_sec;
1341
_epoch_seconds= from;
1342
/* Set hires precision to zero */
1351
void Date::to_time_t(time_t *to) const
1353
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;
1372
void NanoTimestamp::to_timespec(struct timespec *to) const
1374
to->tv_sec= _epoch_seconds;
1375
to->tv_nsec= _nseconds;
1378
bool Date::is_valid() const
1380
return (_years >= DRIZZLE_MIN_YEARS_SQL && _years <= DRIZZLE_MAX_YEARS_SQL)
1381
&& (_months >= 1 && _months <= 12)
1382
&& (_days >= 1 && _days <= days_in_gregorian_year_month(_years, _months));
1385
bool Time::is_valid() const
1387
return (_years == 0)
1392
&& (_seconds <= 59); /* No Leap second... TIME is for elapsed time... */
1395
bool DateTime::is_valid() const
1397
return (_years >= DRIZZLE_MIN_YEARS_SQL && _years <= DRIZZLE_MAX_YEARS_SQL)
1398
&& (_months >= 1 && _months <= 12)
1399
&& (_days >= 1 && _days <= days_in_gregorian_year_month(_years, _months))
1402
&& (_seconds <= 61); /* Leap second... */
1405
bool Timestamp::is_valid() const
1407
return DateTime::is_valid()
1408
&& in_unix_epoch_range(_years, _months, _days, _hours, _minutes, _seconds)
1409
&& (_seconds <= 59);
1412
bool MicroTimestamp::is_valid() const
1414
return Timestamp::is_valid()
1415
&& (_useconds <= UINT32_C(999999));
1418
bool NanoTimestamp::is_valid() const
1420
return Timestamp::is_valid()
1421
&& (_useconds <= UINT32_C(999999))
1422
&& (_nseconds <= UINT32_C(999999999));
1425
} /* namespace drizzled */