57 template <
unsigned OFFSET>
65 return datetrunc_week<dtMONDAY>(timeval);
70 return datetrunc_week<dtSUNDAY>(timeval);
75 return datetrunc_week<dtSATURDAY>(timeval);
95 uint32_t four_year_period_seconds =
97 uint32_t year_seconds_past_4year_period =
99 if (seconds_past_4year_period >=
103 uint32_t seconds_past_march =
104 seconds_past_4year_period - year_seconds_past_4year_period;
108 month = month <= 11 ? month : 11;
109 if (cumulative_month_epoch_starts[month] > seconds_past_march) {
112 return (static_cast<int64_t>(four_year_period_seconds) +
113 year_seconds_past_4year_period + cumulative_month_epoch_starts[month] -
118 unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
119 unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
120 unsigned const moy = (5 * doy + 2) / 153;
121 unsigned const dom = doy - (153 * moy + 2) / 5;
129 STATIC_QUAL const uint32_t cumulative_quarter_epoch_starts[4] = {
130 0, 7776000, 15638400, 23587200};
131 STATIC_QUAL const uint32_t cumulative_quarter_epoch_starts_leap_year[4] = {
132 0, 7862400, 15724800, 23673600};
137 uint32_t base_year_leap_years = (year - 1) / 4;
138 uint32_t base_year_seconds =
140 const bool is_leap_year = year % 4 == 0 && year != 0;
141 const uint32_t* quarter_offsets = is_leap_year
142 ? cumulative_quarter_epoch_starts_leap_year
143 : cumulative_quarter_epoch_starts;
144 uint32_t partial_year_seconds = seconds_1900 % base_year_seconds;
145 uint32_t quarter = partial_year_seconds / (90 *
kUSecsPerDay);
146 quarter = quarter <= 3 ? quarter : 3;
147 if (quarter_offsets[quarter] > partial_year_seconds) {
150 return (static_cast<int64_t>(base_year_seconds) + quarter_offsets[quarter] -
155 unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
156 unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
157 constexpr
unsigned apr1 = 31;
160 bool const leap = yoe % 4 == 0 && (yoe % 100 != 0 || yoe == 0);
161 doq =
JANMAR + leap + doy;
163 unsigned const q = (3 * (doy - apr1) + 2) / 275;
164 doq = doy - (apr1 + q * 92 - (q != 0));
176 uint32_t base_year_leap_years = (year - 1) / 4;
182 unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
183 unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
186 bool const leap = yoe % 4 == 0 && (yoe == 0 || yoe % 100 != 0);
187 docy =
JANMAR + leap + doy;
197 constexpr
unsigned decmarjan =
MARJAN + 9 * 365 + 2;
200 unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
201 unsigned const decoe = yoe - yoe % 10;
203 unsigned days_after_decade = doe - (365 * decoe + decoe / 4 - decoe / 100);
204 if (days_after_decade < decmarjan) {
205 bool const leap = decoe % 4 == 0 && (decoe == 0 || decoe % 100 != 0);
206 days_after_decade +=
JANMAR + leap;
208 days_after_decade -= decmarjan;
224 constexpr
unsigned millennium2001 = 365242;
229 dom += millennium2001 + 1 -
MARJAN;
230 }
else if (dom <
MARJAN + millennium2001) {
233 dom -=
MARJAN + millennium2001;
276 return std::numeric_limits<int64_t>::min();
292 const int64_t null_val) {
293 if (timeval == null_val) {
313 int const yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
314 int const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
315 int const moy = (5 * doy + 2) / 153;
316 int const dom = doy - (153 * moy + 2) / 5;
317 return {era, yoe, moy, dom, sod};
323 return {era - rhs.
era, yoe - rhs.
yoe, moy - rhs.
moy, dom - rhs.
dom, sod - rhs.
sod};
332 return era < 0 ? -1 : 1;
336 return yoe < 0 ? -1 : 1;
340 return moy < 0 ? -1 : 1;
344 return dom < 0 ? -1 : 1;
348 return sod < 0 ? -1 : 1;
356 int const sgn = sign(ERA);
357 EraTime const ut = sgn == -1 ? -*
this : *
this;
360 return sgn * (12 * (400 * ut.
era + ut.
yoe) + ut.
moy - (ut.
sign(DOM) == -1));
362 int const quarters = ut.
moy / 3;
363 int const rem = ut.
moy % 3;
364 return sgn * (4 * (400 * ut.
era + ut.
yoe) + quarters -
365 (rem < 0 || (rem == 0 && ut.
sign(DOM) == -1)));
368 return sgn * (400 * ut.
era + ut.
yoe - (ut.
sign(MOY) == -1));
370 uint64_t
const decades = (400 * ut.
era + ut.
yoe) / 10;
371 unsigned const rem = (400 * ut.
era + ut.
yoe) % 10;
372 return sgn * (decades - (rem == 0 && ut.
sign(MOY) == -1));
375 uint64_t
const centuries = (400 * ut.
era + ut.
yoe) / 100;
376 unsigned const rem = (400 * ut.
era + ut.
yoe) % 100;
377 return sgn * (centuries - (rem == 0 && ut.
sign(MOY) == -1));
380 uint64_t
const millennia = (400 * ut.
era + ut.
yoe) / 1000;
381 unsigned const rem = (400 * ut.
era + ut.
yoe) % 1000;
382 return sgn * (millennia - (rem == 0 && ut.
sign(MOY) == -1));
386 return std::numeric_limits<int64_t>::min();
397 const int64_t startdate,
398 const int64_t enddate) {
407 return enddate - startdate;
421 return (EraTime::make(enddate) - EraTime::make(startdate)).
count(datepart);
427 const int64_t startdate,
428 const int64_t enddate,
429 const int32_t start_dim,
430 const int32_t end_dim) {
432 constexpr
int pow10[10]{1, 0, 0, 1000, 0, 0, 1000 * 1000, 0, 0, 1000 * 1000 * 1000};
440 int const delta_dim = end_dim - start_dim;
441 int const adj_dim = target_dim - (delta_dim < 0 ? start_dim : end_dim);
442 int64_t
const numerator = delta_dim < 0 ? enddate * pow10[-delta_dim] - startdate
443 : enddate - startdate * pow10[delta_dim];
444 return adj_dim < 0 ? numerator / pow10[-adj_dim] : numerator * pow10[adj_dim];
447 int64_t
const end_seconds =
floor_div(enddate, pow10[end_dim]);
448 int delta_ns = (enddate - end_seconds * pow10[end_dim]) * pow10[9 - end_dim];
449 int64_t
const start_seconds =
floor_div(startdate, pow10[start_dim]);
450 delta_ns -= (startdate - start_seconds * pow10[start_dim]) * pow10[9 - start_dim];
451 int64_t
const delta_s = end_seconds - start_seconds;
456 0 < delta_s && delta_ns < 0 ? -1 : delta_s < 0 && 0 < delta_ns ? 1 : 0;
457 return DateDiff(datepart, start_seconds, end_seconds + adj_sec);
462 const int64_t startdate,
463 const int64_t enddate,
464 const int64_t null_val) {
465 if (startdate == null_val || enddate == null_val) {
468 return DateDiff(datepart, startdate, enddate);
473 const int64_t startdate,
474 const int64_t enddate,
475 const int32_t start_dim,
476 const int32_t end_dim,
477 const int64_t null_val) {
478 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)
int64_t DateTruncate(DatetruncField field, const int64_t timeval)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_quarter(int64_t timeval)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_quarterday(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)