58 template <
unsigned OFFSET>
66 return datetrunc_week<dtMONDAY>(timeval);
71 return datetrunc_week<dtSUNDAY>(timeval);
76 return datetrunc_week<dtSATURDAY>(timeval);
96 uint32_t four_year_period_seconds =
98 uint32_t year_seconds_past_4year_period =
100 if (seconds_past_4year_period >=
104 uint32_t seconds_past_march =
105 seconds_past_4year_period - year_seconds_past_4year_period;
109 month = month <= 11 ? month : 11;
110 if (cumulative_month_epoch_starts[month] > seconds_past_march) {
113 return (static_cast<int64_t>(four_year_period_seconds) +
114 year_seconds_past_4year_period + cumulative_month_epoch_starts[month] -
119 unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
120 unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
121 unsigned const moy = (5 * doy + 2) / 153;
122 unsigned const dom = doy - (153 * moy + 2) / 5;
130 STATIC_QUAL const uint32_t cumulative_quarter_epoch_starts[4] = {
131 0, 7776000, 15638400, 23587200};
132 STATIC_QUAL const uint32_t cumulative_quarter_epoch_starts_leap_year[4] = {
133 0, 7862400, 15724800, 23673600};
138 uint32_t base_year_leap_years = (year - 1) / 4;
139 uint32_t base_year_seconds =
141 const bool is_leap_year = year % 4 == 0 && year != 0;
142 const uint32_t* quarter_offsets = is_leap_year
143 ? cumulative_quarter_epoch_starts_leap_year
144 : cumulative_quarter_epoch_starts;
145 uint32_t partial_year_seconds = seconds_1900 % base_year_seconds;
146 uint32_t quarter = partial_year_seconds / (90 *
kUSecsPerDay);
147 quarter = quarter <= 3 ? quarter : 3;
148 if (quarter_offsets[quarter] > partial_year_seconds) {
151 return (static_cast<int64_t>(base_year_seconds) + quarter_offsets[quarter] -
156 unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
157 unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
158 constexpr
unsigned apr1 = 31;
161 bool const leap = yoe % 4 == 0 && (yoe % 100 != 0 || yoe == 0);
162 doq =
JANMAR + leap + doy;
164 unsigned const q = (3 * (doy - apr1) + 2) / 275;
165 doq = doy - (apr1 + q * 92 - (q != 0));
177 uint32_t base_year_leap_years = (year - 1) / 4;
183 unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
184 unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
187 bool const leap = yoe % 4 == 0 && (yoe == 0 || yoe % 100 != 0);
188 docy =
JANMAR + leap + doy;
198 constexpr
unsigned decmarjan =
MARJAN + 9 * 365 + 2;
201 unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
202 unsigned const decoe = yoe - yoe % 10;
204 unsigned days_after_decade = doe - (365 * decoe + decoe / 4 - decoe / 100);
205 if (days_after_decade < decmarjan) {
206 bool const leap = decoe % 4 == 0 && (decoe == 0 || decoe % 100 != 0);
207 days_after_decade +=
JANMAR + leap;
209 days_after_decade -= decmarjan;
225 constexpr
unsigned millennium2001 = 365242;
230 dom += millennium2001 + 1 -
MARJAN;
231 }
else if (dom <
MARJAN + millennium2001) {
234 dom -=
MARJAN + millennium2001;
277 return std::numeric_limits<int64_t>::min();
293 const int64_t null_val) {
294 if (timeval == null_val) {
314 int const yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
315 int const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
316 int const moy = (5 * doy + 2) / 153;
317 int const dom = doy - (153 * moy + 2) / 5;
318 return {era, yoe, moy, dom, sod};
324 return {era - rhs.
era, yoe - rhs.
yoe, moy - rhs.
moy, dom - rhs.
dom, sod - rhs.
sod};
333 return era < 0 ? -1 : 1;
337 return yoe < 0 ? -1 : 1;
341 return moy < 0 ? -1 : 1;
345 return dom < 0 ? -1 : 1;
349 return sod < 0 ? -1 : 1;
357 int const sgn = sign(ERA);
358 EraTime const ut = sgn == -1 ? -*
this : *
this;
361 return sgn * (12 * (400 * ut.
era + ut.
yoe) + ut.
moy - (ut.
sign(DOM) == -1));
363 int const quarters = ut.
moy / 3;
364 int const rem = ut.
moy % 3;
365 return sgn * (4 * (400 * ut.
era + ut.
yoe) + quarters -
366 (rem < 0 || (rem == 0 && ut.
sign(DOM) == -1)));
369 return sgn * (400 * ut.
era + ut.
yoe - (ut.
sign(MOY) == -1));
371 uint64_t
const decades = (400 * ut.
era + ut.
yoe) / 10;
372 unsigned const rem = (400 * ut.
era + ut.
yoe) % 10;
373 return sgn * (decades - (rem == 0 && ut.
sign(MOY) == -1));
376 uint64_t
const centuries = (400 * ut.
era + ut.
yoe) / 100;
377 unsigned const rem = (400 * ut.
era + ut.
yoe) % 100;
378 return sgn * (centuries - (rem == 0 && ut.
sign(MOY) == -1));
381 uint64_t
const millennia = (400 * ut.
era + ut.
yoe) / 1000;
382 unsigned const rem = (400 * ut.
era + ut.
yoe) % 1000;
383 return sgn * (millennia - (rem == 0 && ut.
sign(MOY) == -1));
387 return std::numeric_limits<int64_t>::min();
398 const int64_t startdate,
399 const int64_t enddate) {
408 return enddate - startdate;
422 return (EraTime::make(enddate) - EraTime::make(startdate)).count(datepart);
428 const int64_t startdate,
429 const int64_t enddate,
430 const int32_t start_dim,
431 const int32_t end_dim) {
433 constexpr
int pow10[10]{1, 0, 0, 1000, 0, 0, 1000 * 1000, 0, 0, 1000 * 1000 * 1000};
441 int const delta_dim = end_dim - start_dim;
442 int const adj_dim = target_dim - (delta_dim < 0 ? start_dim : end_dim);
443 int64_t
const numerator = delta_dim < 0 ? enddate * pow10[-delta_dim] - startdate
444 : enddate - startdate * pow10[delta_dim];
445 return adj_dim < 0 ? numerator / pow10[-adj_dim] : numerator * pow10[adj_dim];
448 int64_t
const end_seconds =
floor_div(enddate, pow10[end_dim]);
449 int delta_ns = (enddate - end_seconds * pow10[end_dim]) * pow10[9 - end_dim];
450 int64_t
const start_seconds =
floor_div(startdate, pow10[start_dim]);
451 delta_ns -= (startdate - start_seconds * pow10[start_dim]) * pow10[9 - start_dim];
452 int64_t
const delta_s = end_seconds - start_seconds;
456 int const adj_sec = 0 < delta_s && delta_ns < 0 ? -1
457 : delta_s < 0 && 0 < delta_ns ? 1
459 return DateDiff(datepart, start_seconds, end_seconds + adj_sec);
464 const int64_t startdate,
465 const int64_t enddate,
466 const int64_t null_val) {
467 if (startdate == null_val || enddate == null_val) {
470 return DateDiff(datepart, startdate, enddate);
475 const int64_t startdate,
476 const int64_t enddate,
477 const int32_t start_dim,
478 const int32_t end_dim,
479 const int64_t null_val) {
480 if (startdate == null_val || enddate == null_val) {
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_millennium(int64_t timeval)
DEVICE int sign(Field const field) const
RUNTIME_EXPORT DEVICE int64_t DateDiffHighPrecisionNullable(const DatetruncField datepart, const int64_t startdate, const int64_t enddate, const int32_t start_dim, const int32_t end_dim, const int64_t null_val)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_quarter(int64_t timeval)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_quarterday(int64_t timeval)
DEVICE int64_t DateTruncate(DatetruncField field, const int64_t timeval)
static DEVICE EraTime make(int64_t const time)
RUNTIME_EXPORT DEVICE int64_t DateDiff(const DatetruncField datepart, const int64_t startdate, const int64_t enddate)
RUNTIME_EXPORT DEVICE int64_t DateDiffHighPrecision(const DatetruncField datepart, const int64_t startdate, const int64_t enddate, const int32_t start_dim, const int32_t end_dim)
DEVICE EraTime operator-() const
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_day(int64_t timeval)
constexpr unsigned dtSUNDAY
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t DateTruncateHighPrecisionToDateNullable(const int64_t timeval, const int64_t scale, const int64_t null_val)
DEVICE EraTime operator-(EraTime const &rhs)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_century(int64_t timeval)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_decade(int64_t timeval)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_week_saturday(int64_t timeval)
ALWAYS_INLINE DEVICE int64_t datetrunc_week(int64_t timeval)
constexpr unsigned dtSATURDAY
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_hour(int64_t timeval)
DEVICE int64_t count(DatetruncField const field) const
constexpr unsigned dtMONDAY
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t DateTruncateHighPrecisionToDate(const int64_t timeval, const int64_t scale)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_month(int64_t timeval)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_week_sunday(int64_t timeval)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_minute(int64_t timeval)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_week_monday(int64_t timeval)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_year(int64_t timeval)
RUNTIME_EXPORT DEVICE int64_t DateDiffNullable(const DatetruncField datepart, const int64_t startdate, const int64_t enddate, const int64_t null_val)