34
34
* their single parameter.
37
#include "drizzled/global.h"
39
#include "drizzled/charset_info.h"
40
#include "drizzled/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"
53
51
#include <string.h>
53
/* time.h may already have been included in global.h, but we
54
need to pick up the extra defs as well, after the global time.h */
55
#ifndef HAVE_DECL_TIMEGM
56
# include <gnulib/time.h>
59
extern std::vector<drizzled::TemporalFormat *> known_datetime_formats;
60
extern std::vector<drizzled::TemporalFormat *> known_date_formats;
61
extern std::vector<drizzled::TemporalFormat *> known_time_formats;
58
extern std::vector<TemporalFormat *> known_datetime_formats;
59
extern std::vector<TemporalFormat *> known_date_formats;
60
extern std::vector<TemporalFormat *> known_time_formats;
62
66
Temporal::Temporal()
64
68
_calendar(GREGORIAN)
800
#ifdef NOTYETIMPLEMENTED
801
Date& Date::operator+=(const TemporalIntervalYear &rhs)
803
/* Simple one...add the years and adjust for any leaps */
804
int64_t new_years= _years;
805
new_years+= rhs._years;
806
if (new_years > DRIZZLE_MAX_YEARS_SQL)
809
* Set everything to zero. We got an overflow.
810
* @TODO Exceptions would be great here...
816
_years= (uint32_t) new_years;
817
if (_months == 2 && _days == 29 && days_in_gregorian_year_month(_years, _months) != 366)
822
Date& Date::operator-=(const TemporalIntervalYear &rhs)
824
/* Simple one...subtract the years and adjust for any leaps */
825
int64_t new_years= _years;
826
new_years-= rhs._years;
830
* Set everything to zero. We got an overflow.
831
* @TODO Exceptions would be great here...
837
_years= (uint32_t) new_years;
838
if (_months == 2 && _days == 29 && days_in_gregorian_year_month(_years, _months) != 366)
843
Date& Date::operator+=(const TemporalIntervalDayOrWeek &rhs)
845
/* Simple one...add the days */
846
int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, _days) + rhs._days;
847
gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
851
Date& Date::operator-=(const TemporalIntervalDayOrWeek &rhs)
853
/* Simple one...subtract the days */
854
int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, _days) - rhs._days;
855
gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
859
Date& Date::operator+=(const TemporalIntervalYearMonth &rhs)
861
/* Simple one...add the months in the period adjust */
862
int64_t period= (_years * 12) + (rhs._years * 12) + (_months - 1) + rhs._months;
863
int64_t new_years= (period / 12);
864
if (new_years > DRIZZLE_MAX_YEARS_SQL)
867
* Set everything to zero. We got an overflow.
868
* @TODO Exceptions would be great here...
874
_years= (uint32_t) new_years;
875
_months= (uint32_t) (period % 12) + 1;
877
/* Adjust day if the new month doesn't have enough days */
878
uint32_t days_in_new_month= days_in_gregorian_year_month(_years, _months);
879
if (_days > days_in_new_month)
880
_days= days_in_new_month;
884
Date& Date::operator-=(const TemporalIntervalYearMonth &rhs)
886
/* Simple one...subtract the months in the period and adjust */
887
int64_t period= (_years * 12) - (rhs._years * 12) + (_months - 1) - rhs._months;
888
int64_t new_years= (period / 12);
892
* Set everything to zero. We got an overflow.
893
* @TODO Exceptions would be great here...
899
_years= (uint32_t) (period / 12);
900
_months= (uint32_t) (period % 12) + 1;
902
/* Adjust day if the new month doesn't have enough days */
903
uint32_t days_in_new_month= days_in_gregorian_year_month(_years, _months);
904
if (_days > days_in_new_month)
905
_days= days_in_new_month;
909
Date& Date::operator+=(const TemporalIntervalDayOrLess &rhs)
912
* Convert the temporal and the interval into a number of
913
* microseconds, then add them together and convert the
914
* resulting microseconds back into a broken-down temporal
918
int64_t new_microseconds;
921
new_microseconds= _useconds + rhs._useconds;
922
extra_sec= new_microseconds / INT64_C(1000000);
923
new_microseconds= new_microseconds % INT64_C(1000000);
925
new_seconds= ((_days - 1) * 3600 * 24) + (_hours * 3600) + (_minutes * 60) + _seconds;
926
new_seconds+= (rhs._days * 3600 * 24) + (rhs._hours * 3600) + (rhs._minutes * 60) + rhs._seconds;
927
new_seconds+= extra_sec;
929
if (new_microseconds < 0)
931
new_microseconds+= INT64_C(1000000);
935
new_days= new_seconds / (3600 * 24L);
936
new_seconds-= new_days * 3600 * 24L;
940
new_seconds+= 3600 * 24L;
942
_useconds= (uint32_t) new_microseconds;
943
_seconds= (uint32_t) (new_seconds % 60);
944
_minutes= (uint32_t) ((new_seconds / 60) % 60);
945
_hours= (uint32_t) (new_seconds / 3600);
946
int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, 1) + new_days;
947
gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
951
Date& Date::operator-=(const TemporalIntervalDayOrLess &rhs)
954
* Convert the temporal and the interval into a number of
955
* microseconds, then subtract them from each other and convert
956
* the resulting microseconds back into a broken-down temporal
960
int64_t new_microseconds;
963
new_microseconds= _useconds - rhs._useconds;
964
extra_sec= new_microseconds / INT64_C(1000000);
965
new_microseconds= new_microseconds % INT64_C(1000000);
967
new_seconds= ((_days - 1) * 3600 * 24) + (_hours * 3600) + (_minutes * 60) + _seconds;
968
new_seconds-= (rhs._days * 3600 * 24) + (rhs._hours * 3600) + (rhs._minutes * 60) + rhs._seconds;
969
new_seconds+= extra_sec;
971
if (new_microseconds < 0)
973
new_microseconds+= INT64_C(1000000);
977
new_days= new_seconds / (3600 * 24L);
978
new_seconds-= new_days * 3600 * 24L;
982
new_seconds+= 3600 * 24L;
984
_useconds= (uint32_t) new_microseconds;
985
_seconds= (uint32_t) (new_seconds % 60);
986
_minutes= (uint32_t) ((new_seconds / 60) % 60);
987
_hours= (uint32_t) (new_seconds / 3600);
988
int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, 1) + new_days;
989
gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
992
#endif /* NOTYETIMPLEMENTED */
836
994
* Comparison operators between a Date and a Timestamp
1031
1189
return is_valid();
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
1192
void Time::to_string(char *to, size_t *to_len) const
1195
, "%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32
1201
void Date::to_string(char *to, size_t *to_len) const
1204
, "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32
1210
void DateTime::to_string(char *to, size_t *to_len) const
1050
1212
/* If the temporal has a microsecond component, use a slightly different output */
1051
1213
if (_useconds == 0)
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);
1216
, "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32 " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32
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);
1227
, "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32 " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ".%06" PRIu32
1069
int MicroTimestamp::to_string(char *to, size_t to_len) const
1238
void 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);
1241
, "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32 " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ".%06" PRIu32
1078
1251
void Time::to_decimal(my_decimal *to) const