10 #include "common/datatypes/Date.h"
19 static int64_t dateTimeDiffSeconds(
const DateTime &dateTime0,
const DateTime &dateTime1);
22 static int64_t dateTimeToUnixSeconds(
const DateTime &dateTime) {
23 return dateTimeDiffSeconds(dateTime, kEpoch);
26 static DateTime unixSecondsToDateTime(int64_t seconds);
29 static DateTime dateTimeShift(
const DateTime &dateTime, int64_t offsetSeconds) {
30 if (offsetSeconds == 0) {
33 auto dt = unixSecondsToDateTime(dateTimeToUnixSeconds(dateTime) + offsetSeconds);
34 dt.microsec = dateTime.microsec;
39 static int64_t dateToUnixSeconds(
const Date &date) {
40 return dateTimeDiffSeconds(
DateTime(date), kEpoch);
43 static Date unixSecondsToDate(int64_t seconds) {
44 auto dateTime = unixSecondsToDateTime(seconds);
45 return Date(dateTime.year, dateTime.month, dateTime.day);
49 static Date dateShift(
const Date &date, int64_t offsetSeconds) {
50 if (offsetSeconds == 0) {
53 return unixSecondsToDate(dateToUnixSeconds(date) + offsetSeconds);
57 static int64_t timeToSeconds(
const Time &time) {
58 int64_t seconds = time.sec;
59 seconds += (time.minute * kSecondsOfMinute);
60 seconds += (time.hour * kSecondsOfHour);
64 static Time unixSecondsToTime(int64_t seconds) {
66 auto dt = unixSecondsToDateTime(seconds);
74 static Time timeShift(
const Time &time, int64_t offsetSeconds) {
75 if (offsetSeconds == 0) {
78 auto t = unixSecondsToTime(timeToSeconds(time) + offsetSeconds);
79 t.microsec = time.microsec;
84 static bool isLeapYear(int16_t year) {
87 }
else if (year % 100 != 0) {
89 }
else if (year % 400 != 0) {
98 static constexpr
int kDayOfLeapYear = 366;
99 static constexpr
int kDayOfCommonYear = 365;
101 static constexpr int64_t kSecondsOfMinute = 60;
102 static constexpr int64_t kSecondsOfHour = 60 * kSecondsOfMinute;
103 static constexpr int64_t kSecondsOfDay = 24 * kSecondsOfHour;
112 static int64_t shr(int64_t a,
int b) {
114 return (-one >> 1 == -1 ? a >> b : (a + (a < 0)) / (one << b) - (a < 0));