34
34
* their single parameter.
37
#include "drizzled/global.h"
39
#include "drizzled/charset_info.h"
40
#include "drizzled/type/decimal.h"
39
#include "mystrings/m_ctype.h"
40
#include "drizzled/my_decimal.h"
41
41
#include "drizzled/calendar.h"
42
42
#include "drizzled/temporal.h"
43
#ifdef NOTYETIMPLEMENTED
44
#include "drizzled/temporal_interval.h"
43
46
#include "drizzled/temporal_format.h"
44
#include "drizzled/time_functions.h"
47
#include <drizzled/util/gmtime.h>
55
52
#include <string.h>
54
extern std::vector<drizzled::TemporalFormat *> known_datetime_formats;
55
extern std::vector<drizzled::TemporalFormat *> known_date_formats;
56
extern std::vector<drizzled::TemporalFormat *> known_time_formats;
60
extern std::vector<TemporalFormat *> known_datetime_formats;
61
extern std::vector<TemporalFormat *> known_date_formats;
62
extern std::vector<TemporalFormat *> known_time_formats;
64
Temporal::Temporal() :
78
76
uint64_t Temporal::_cumulative_seconds_in_time() const
795
#ifdef NOTYETIMPLEMENTED
796
Date& Date::operator+=(const TemporalIntervalYear &rhs)
798
/* Simple one...add the years and adjust for any leaps */
799
int64_t new_years= _years;
800
new_years+= rhs._years;
801
if (new_years > DRIZZLE_MAX_YEARS_SQL)
804
* Set everything to zero. We got an overflow.
805
* @TODO Exceptions would be great here...
811
_years= (uint32_t) new_years;
812
if (_months == 2 && _days == 29 && days_in_gregorian_year_month(_years, _months) != 366)
817
Date& Date::operator-=(const TemporalIntervalYear &rhs)
819
/* Simple one...subtract the years and adjust for any leaps */
820
int64_t new_years= _years;
821
new_years-= rhs._years;
825
* Set everything to zero. We got an overflow.
826
* @TODO Exceptions would be great here...
832
_years= (uint32_t) new_years;
833
if (_months == 2 && _days == 29 && days_in_gregorian_year_month(_years, _months) != 366)
838
Date& Date::operator+=(const TemporalIntervalDayOrWeek &rhs)
840
/* Simple one...add the days */
841
int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, _days) + rhs._days;
842
gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
846
Date& Date::operator-=(const TemporalIntervalDayOrWeek &rhs)
848
/* Simple one...subtract the days */
849
int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, _days) - rhs._days;
850
gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
854
Date& Date::operator+=(const TemporalIntervalYearMonth &rhs)
856
/* Simple one...add the months in the period adjust */
857
int64_t period= (_years * 12) + (rhs._years * 12) + (_months - 1) + rhs._months;
858
int64_t new_years= (period / 12);
859
if (new_years > DRIZZLE_MAX_YEARS_SQL)
862
* Set everything to zero. We got an overflow.
863
* @TODO Exceptions would be great here...
869
_years= (uint32_t) new_years;
870
_months= (uint32_t) (period % 12) + 1;
872
/* Adjust day if the new month doesn't have enough days */
873
uint32_t days_in_new_month= days_in_gregorian_year_month(_years, _months);
874
if (_days > days_in_new_month)
875
_days= days_in_new_month;
879
Date& Date::operator-=(const TemporalIntervalYearMonth &rhs)
881
/* Simple one...subtract the months in the period and adjust */
882
int64_t period= (_years * 12) - (rhs._years * 12) + (_months - 1) - rhs._months;
883
int64_t new_years= (period / 12);
887
* Set everything to zero. We got an overflow.
888
* @TODO Exceptions would be great here...
894
_years= (uint32_t) (period / 12);
895
_months= (uint32_t) (period % 12) + 1;
897
/* Adjust day if the new month doesn't have enough days */
898
uint32_t days_in_new_month= days_in_gregorian_year_month(_years, _months);
899
if (_days > days_in_new_month)
900
_days= days_in_new_month;
904
Date& Date::operator+=(const TemporalIntervalDayOrLess &rhs)
907
* Convert the temporal and the interval into a number of
908
* microseconds, then add them together and convert the
909
* resulting microseconds back into a broken-down temporal
913
int64_t new_microseconds;
916
new_microseconds= _useconds + rhs._useconds;
917
extra_sec= new_microseconds / INT64_C(1000000);
918
new_microseconds= new_microseconds % INT64_C(1000000);
920
new_seconds= ((_days - 1) * 3600 * 24) + (_hours * 3600) + (_minutes * 60) + _seconds;
921
new_seconds+= (rhs._days * 3600 * 24) + (rhs._hours * 3600) + (rhs._minutes * 60) + rhs._seconds;
922
new_seconds+= extra_sec;
924
if (new_microseconds < 0)
926
new_microseconds+= INT64_C(1000000);
930
new_days= new_seconds / (3600 * 24L);
931
new_seconds-= new_days * 3600 * 24L;
935
new_seconds+= 3600 * 24L;
937
_useconds= (uint32_t) new_microseconds;
938
_seconds= (uint32_t) (new_seconds % 60);
939
_minutes= (uint32_t) ((new_seconds / 60) % 60);
940
_hours= (uint32_t) (new_seconds / 3600);
941
int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, 1) + new_days;
942
gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
946
Date& Date::operator-=(const TemporalIntervalDayOrLess &rhs)
949
* Convert the temporal and the interval into a number of
950
* microseconds, then subtract them from each other and convert
951
* the resulting microseconds back into a broken-down temporal
955
int64_t new_microseconds;
958
new_microseconds= _useconds - rhs._useconds;
959
extra_sec= new_microseconds / INT64_C(1000000);
960
new_microseconds= new_microseconds % INT64_C(1000000);
962
new_seconds= ((_days - 1) * 3600 * 24) + (_hours * 3600) + (_minutes * 60) + _seconds;
963
new_seconds-= (rhs._days * 3600 * 24) + (rhs._hours * 3600) + (rhs._minutes * 60) + rhs._seconds;
964
new_seconds+= extra_sec;
966
if (new_microseconds < 0)
968
new_microseconds+= INT64_C(1000000);
972
new_days= new_seconds / (3600 * 24L);
973
new_seconds-= new_days * 3600 * 24L;
977
new_seconds+= 3600 * 24L;
979
_useconds= (uint32_t) new_microseconds;
980
_seconds= (uint32_t) (new_seconds % 60);
981
_minutes= (uint32_t) ((new_seconds / 60) % 60);
982
_hours= (uint32_t) (new_seconds / 3600);
983
int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, 1) + new_days;
984
gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
987
#endif /* NOTYETIMPLEMENTED */
838
989
* Comparison operators between a Date and a Timestamp
1071
1222
int MicroTimestamp::to_string(char *to, size_t to_len) const
1073
1224
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);
1225
"%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32
1226
" %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ".%06" PRIu32,
1227
_years, _months, _days,
1228
_hours, _minutes, _seconds, _useconds);
1080
void Time::to_decimal(type::Decimal *to) const
1231
void Time::to_decimal(my_decimal *to) const
1082
1233
int64_t time_portion= (((_hours * 100L) + _minutes) * 100L) + _seconds;
1083
(void) int2_class_decimal(E_DEC_FATAL_ERROR, time_portion, false, to);
1234
(void) int2my_decimal(E_DEC_FATAL_ERROR, time_portion, false, to);
1084
1235
if (_useconds > 0)
1086
1237
to->buf[(to->intg-1) / 9 + 1]= _useconds * 1000;
1091
void Date::to_decimal(type::Decimal *to) const
1242
void Date::to_decimal(my_decimal *to) const
1093
1244
int64_t date_portion= (((_years * 100L) + _months) * 100L) + _days;
1094
(void) int2_class_decimal(E_DEC_FATAL_ERROR, date_portion, false, to);
1245
(void) int2my_decimal(E_DEC_FATAL_ERROR, date_portion, false, to);
1097
void DateTime::to_decimal(type::Decimal *to) const
1248
void DateTime::to_decimal(my_decimal *to) const
1099
1250
int64_t date_portion= (((_years * 100L) + _months) * 100L) + _days;
1100
1251
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);
1252
(void) int2my_decimal(E_DEC_FATAL_ERROR, time_portion, false, to);
1102
1253
if (_useconds > 0)
1104
1255
to->buf[(to->intg-1) / 9 + 1]= _useconds * 1000;