OmniSciDB  2e3a973ef4
DateTruncate.cpp File Reference
#include "DateTruncate.h"
#include "ExtractFromTime.h"
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <iostream>
#include <limits>
+ Include dependency graph for DateTruncate.cpp:

Go to the source code of this file.

Classes

struct  anonymous_namespace{DateTruncate.cpp}::EraTime
 

Namespaces

 anonymous_namespace{DateTruncate.cpp}
 

Functions

ALWAYS_INLINE DEVICE int64_t datetrunc_minute (int64_t timeval)
 
ALWAYS_INLINE DEVICE int64_t datetrunc_hour (int64_t timeval)
 
ALWAYS_INLINE DEVICE int64_t datetrunc_quarterday (int64_t timeval)
 
ALWAYS_INLINE DEVICE int64_t datetrunc_day (int64_t timeval)
 
ALWAYS_INLINE DEVICE int64_t datetrunc_week (int64_t timeval)
 
ALWAYS_INLINE DEVICE int64_t datetrunc_month (int64_t timeval)
 
ALWAYS_INLINE DEVICE int64_t datetrunc_quarter (int64_t timeval)
 
ALWAYS_INLINE DEVICE int64_t datetrunc_year (int64_t timeval)
 
ALWAYS_INLINE DEVICE int64_t datetrunc_decade (int64_t timeval)
 
ALWAYS_INLINE DEVICE int64_t datetrunc_century (int64_t timeval)
 
ALWAYS_INLINE DEVICE int64_t datetrunc_millennium (int64_t timeval)
 
int64_t DateTruncate (DatetruncField field, const int64_t timeval)
 
ALWAYS_INLINE DEVICE int64_t DateTruncateHighPrecisionToDate (const int64_t timeval, const int64_t scale)
 
ALWAYS_INLINE DEVICE int64_t DateTruncateHighPrecisionToDateNullable (const int64_t timeval, const int64_t scale, const int64_t null_val)
 
DEVICE int64_t DateDiff (const DatetruncField datepart, const int64_t startdate, const int64_t enddate)
 
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 int64_t DateDiffNullable (const DatetruncField datepart, const int64_t startdate, const int64_t enddate, const int64_t null_val)
 
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)
 

Function Documentation

◆ DateDiff()

DEVICE int64_t DateDiff ( const DatetruncField  datepart,
const int64_t  startdate,
const int64_t  enddate 
)

Definition at line 365 of file DateTruncate.cpp.

References dtDAY, dtHOUR, dtMICROSECOND, dtMILLISECOND, dtMINUTE, dtNANOSECOND, dtQUARTERDAY, dtSECOND, dtWEEK, kMicroSecsPerSec, kMilliSecsPerSec, kNanoSecsPerSec, kSecsPerDay, kSecsPerHour, and kSecsPerMin.

Referenced by DateDiffHighPrecision(), and DateDiffNullable().

367  {
368  switch (datepart) {
369  case dtNANOSECOND:
370  return (enddate - startdate) * kNanoSecsPerSec;
371  case dtMICROSECOND:
372  return (enddate - startdate) * kMicroSecsPerSec;
373  case dtMILLISECOND:
374  return (enddate - startdate) * kMilliSecsPerSec;
375  case dtSECOND:
376  return enddate - startdate;
377  case dtMINUTE:
378  return (enddate - startdate) / kSecsPerMin;
379  case dtHOUR:
380  return (enddate - startdate) / kSecsPerHour;
381  case dtQUARTERDAY:
382  return (enddate - startdate) / (kSecsPerDay / 4);
383  case dtDAY:
384  return (enddate - startdate) / kSecsPerDay;
385  case dtWEEK:
386  return (enddate - startdate) / (7 * kSecsPerDay);
387  default:
388  return (EraTime::make(enddate) - EraTime::make(startdate)).count(datepart);
389  }
390 }
static constexpr int64_t kSecsPerDay
static constexpr int64_t kSecsPerHour
static constexpr int64_t kSecsPerMin
static constexpr int64_t kNanoSecsPerSec
static constexpr int64_t kMilliSecsPerSec
static constexpr int64_t kMicroSecsPerSec
+ Here is the caller graph for this function:

◆ DateDiffHighPrecision()

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 
)

Definition at line 392 of file DateTruncate.cpp.

References DateDiff(), dtMICROSECOND, dtMILLISECOND, dtNANOSECOND, and floor_div().

Referenced by DateDiffHighPrecisionNullable().

396  {
397  // Return pow(10,i). Only valid for i = 0, 3, 6, 9.
398  constexpr int pow10[10]{1, 0, 0, 1000, 0, 0, 1000 * 1000, 0, 0, 1000 * 1000 * 1000};
399  switch (datepart) {
400  case dtNANOSECOND:
401  case dtMICROSECOND:
402  case dtMILLISECOND: {
403  static_assert(dtMILLISECOND + 1 == dtMICROSECOND, "Please keep these consecutive.");
404  static_assert(dtMICROSECOND + 1 == dtNANOSECOND, "Please keep these consecutive.");
405  int const target_dim = (datepart - (dtMILLISECOND - 1)) * 3; // 3, 6, or 9.
406  int const delta_dim = end_dim - start_dim; // in [-9,9] multiple of 3
407  int const adj_dim = target_dim - (delta_dim < 0 ? start_dim : end_dim);
408  int64_t const numerator = delta_dim < 0 ? enddate * pow10[-delta_dim] - startdate
409  : enddate - startdate * pow10[delta_dim];
410  return adj_dim < 0 ? numerator / pow10[-adj_dim] : numerator * pow10[adj_dim];
411  }
412  default:
413  int64_t const end_seconds = floor_div(enddate, pow10[end_dim]);
414  int delta_ns = (enddate - end_seconds * pow10[end_dim]) * pow10[9 - end_dim];
415  int64_t const start_seconds = floor_div(startdate, pow10[start_dim]);
416  delta_ns -= (startdate - start_seconds * pow10[start_dim]) * pow10[9 - start_dim];
417  int64_t const delta_s = end_seconds - start_seconds;
418  // sub-second values must be accounted for when calling DateDiff. Examples:
419  // 2000-02-15 12:00:00.006 to 2000-03-15 12:00:00.005 is 0 months.
420  // 2000-02-15 12:00:00.006 to 2000-03-15 12:00:00.006 is 1 month.
421  int const adj_sec =
422  0 < delta_s && delta_ns < 0 ? -1 : delta_s < 0 && 0 < delta_ns ? 1 : 0;
423  return DateDiff(datepart, start_seconds, end_seconds + adj_sec);
424  }
425 }
DEVICE int64_t floor_div(int64_t const dividend, int64_t const divisor)
DEVICE int64_t DateDiff(const DatetruncField datepart, const int64_t startdate, const int64_t enddate)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DateDiffHighPrecisionNullable()

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 
)

Definition at line 437 of file DateTruncate.cpp.

References DateDiffHighPrecision().

442  {
443  if (startdate == null_val || enddate == null_val) {
444  return null_val;
445  }
446  return DateDiffHighPrecision(datepart, startdate, enddate, start_dim, end_dim);
447 }
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)
+ Here is the call graph for this function:

◆ DateDiffNullable()

DEVICE int64_t DateDiffNullable ( const DatetruncField  datepart,
const int64_t  startdate,
const int64_t  enddate,
const int64_t  null_val 
)

Definition at line 427 of file DateTruncate.cpp.

References DateDiff().

430  {
431  if (startdate == null_val || enddate == null_val) {
432  return null_val;
433  }
434  return DateDiff(datepart, startdate, enddate);
435 }
DEVICE int64_t DateDiff(const DatetruncField datepart, const int64_t startdate, const int64_t enddate)
+ Here is the call graph for this function:

◆ datetrunc_century()

ALWAYS_INLINE DEVICE int64_t datetrunc_century ( int64_t  timeval)

Definition at line 188 of file DateTruncate.cpp.

References floor_div(), kDaysPer400Years, kEpochAdjustedDays, kSecsPerDay, MARJAN, and unsigned_mod().

Referenced by DateTruncate().

188  {
189  int64_t const day = floor_div(timeval, kSecsPerDay);
190  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
191  // Day-of-century = Days since last 010101 (Jan 1 1901, 2001, 2101, etc.)
192  unsigned const doc = doe < MARJAN ? doe + (36525 - MARJAN) : (doe - MARJAN) % 36524;
193  return (day - doc) * kSecsPerDay;
194 }
static constexpr int64_t kSecsPerDay
DEVICE int64_t floor_div(int64_t const dividend, int64_t const divisor)
static constexpr int32_t kEpochAdjustedDays
static constexpr int64_t kDaysPer400Years
constexpr unsigned MARJAN
DEVICE int64_t unsigned_mod(int64_t const dividend, int64_t const divisor)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ datetrunc_day()

ALWAYS_INLINE DEVICE int64_t datetrunc_day ( int64_t  timeval)

Definition at line 45 of file DateTruncate.cpp.

References kSecsPerDay, and unsigned_mod().

Referenced by DateTruncate().

45  {
46  return timeval - unsigned_mod(timeval, kSecsPerDay);
47 }
static constexpr int64_t kSecsPerDay
DEVICE int64_t unsigned_mod(int64_t const dividend, int64_t const divisor)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ datetrunc_decade()

ALWAYS_INLINE DEVICE int64_t datetrunc_decade ( int64_t  timeval)

Definition at line 170 of file DateTruncate.cpp.

References floor_div(), JANMAR, kDaysPer400Years, kEpochAdjustedDays, kSecsPerDay, MARJAN, and unsigned_mod().

Referenced by DateTruncate().

170  {
171  // Number of days from x00301 to (x+1)00101. Always includes exactly two leap days.
172  constexpr unsigned decmarjan = MARJAN + 9 * 365 + 2;
173  int64_t const day = floor_div(timeval, kSecsPerDay);
174  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
175  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
176  unsigned const decoe = yoe - yoe % 10; // Decade-of-era
177  // Initialize to days after mar1 of decade, then adjust to after jan1 below.
178  unsigned days_after_decade = doe - (365 * decoe + decoe / 4 - decoe / 100);
179  if (days_after_decade < decmarjan) {
180  bool const leap = decoe % 4 == 0 && (decoe == 0 || decoe % 100 != 0);
181  days_after_decade += JANMAR + leap;
182  } else {
183  days_after_decade -= decmarjan;
184  }
185  return (day - days_after_decade) * kSecsPerDay;
186 }
static constexpr int64_t kSecsPerDay
constexpr unsigned JANMAR
DEVICE int64_t floor_div(int64_t const dividend, int64_t const divisor)
static constexpr int32_t kEpochAdjustedDays
static constexpr int64_t kDaysPer400Years
constexpr unsigned MARJAN
DEVICE int64_t unsigned_mod(int64_t const dividend, int64_t const divisor)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ datetrunc_hour()

ALWAYS_INLINE DEVICE int64_t datetrunc_hour ( int64_t  timeval)

Definition at line 37 of file DateTruncate.cpp.

References kSecsPerHour, and unsigned_mod().

Referenced by DateTruncate().

37  {
38  return timeval - unsigned_mod(timeval, kSecsPerHour);
39 }
static constexpr int64_t kSecsPerHour
DEVICE int64_t unsigned_mod(int64_t const dividend, int64_t const divisor)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ datetrunc_millennium()

ALWAYS_INLINE DEVICE int64_t datetrunc_millennium ( int64_t  timeval)

Definition at line 196 of file DateTruncate.cpp.

References floor_div(), kDaysPer400Years, kEpochAdjustedDays, kSecsPerDay, MARJAN, and unsigned_mod().

Referenced by DateTruncate().

196  {
197  constexpr unsigned millennium2001 = 365242; // Days from Jan 1 2001 to 3001.
198  int64_t const day = floor_div(timeval, kSecsPerDay);
199  // lcm(400, 1000) = 2000 so use 5*400-year eras at a time.
200  unsigned dom = unsigned_mod(day - kEpochAdjustedDays, 5 * kDaysPer400Years);
201  if (dom < MARJAN) {
202  dom += millennium2001 + 1 - MARJAN;
203  } else if (dom < MARJAN + millennium2001) {
204  dom -= MARJAN;
205  } else {
206  dom -= MARJAN + millennium2001;
207  }
208  return (day - dom) * kSecsPerDay;
209 }
static constexpr int64_t kSecsPerDay
DEVICE int64_t floor_div(int64_t const dividend, int64_t const divisor)
static constexpr int32_t kEpochAdjustedDays
static constexpr int64_t kDaysPer400Years
constexpr unsigned MARJAN
DEVICE int64_t unsigned_mod(int64_t const dividend, int64_t const divisor)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ datetrunc_minute()

ALWAYS_INLINE DEVICE int64_t datetrunc_minute ( int64_t  timeval)

Definition at line 33 of file DateTruncate.cpp.

References kSecsPerMin, and unsigned_mod().

Referenced by DateTruncate().

33  {
34  return timeval - unsigned_mod(timeval, kSecsPerMin);
35 }
static constexpr int64_t kSecsPerMin
DEVICE int64_t unsigned_mod(int64_t const dividend, int64_t const divisor)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ datetrunc_month()

ALWAYS_INLINE DEVICE int64_t datetrunc_month ( int64_t  timeval)

Definition at line 54 of file DateTruncate.cpp.

References floor_div(), kDaysPer400Years, kEpochAdjustedDays, kEpochOffsetYear1900, kMonsPerYear, kSecondsPer4YearCycle, kSecondsPerNonLeapYear, kSecsJanToMar1900, kSecsPerDay, kUSecsPerDay, STATIC_QUAL, and unsigned_mod().

Referenced by DateTruncate().

54  {
55  if (timeval >= 0L && timeval <= UINT32_MAX - (kEpochOffsetYear1900)) {
56  STATIC_QUAL const uint32_t cumulative_month_epoch_starts[kMonsPerYear] = {0,
57  2678400,
58  5270400,
59  7948800,
60  10540800,
61  13219200,
62  15897600,
63  18489600,
64  21168000,
65  23760000,
66  26438400,
67  29116800};
68  // Handles times from Thu 01 Jan 1970 00:00:00 - Thu 07 Feb 2036 06:28:15.
69  uint32_t seconds_march_1900 = timeval + kEpochOffsetYear1900 - kSecsJanToMar1900;
70  uint32_t seconds_past_4year_period = seconds_march_1900 % kSecondsPer4YearCycle;
71  uint32_t four_year_period_seconds =
72  (seconds_march_1900 / kSecondsPer4YearCycle) * kSecondsPer4YearCycle;
73  uint32_t year_seconds_past_4year_period =
74  (seconds_past_4year_period / kSecondsPerNonLeapYear) * kSecondsPerNonLeapYear;
75  if (seconds_past_4year_period >=
76  kSecondsPer4YearCycle - kUSecsPerDay) { // if we are in Feb 29th
77  year_seconds_past_4year_period -= kSecondsPerNonLeapYear;
78  }
79  uint32_t seconds_past_march =
80  seconds_past_4year_period - year_seconds_past_4year_period;
81  uint32_t month =
82  seconds_past_march / (30 * kUSecsPerDay); // Will make the correct month either
83  // be the guessed month or month before
84  month = month <= 11 ? month : 11;
85  if (cumulative_month_epoch_starts[month] > seconds_past_march) {
86  month--;
87  }
88  return (static_cast<int64_t>(four_year_period_seconds) +
89  year_seconds_past_4year_period + cumulative_month_epoch_starts[month] -
91  } else {
92  int64_t const day = floor_div(timeval, kSecsPerDay);
93  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
94  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
95  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
96  unsigned const moy = (5 * doy + 2) / 153;
97  unsigned const dom = doy - (153 * moy + 2) / 5;
98  return (day - dom) * kSecsPerDay;
99  }
100 }
static constexpr int64_t kSecsPerDay
static constexpr uint32_t kSecsJanToMar1900
static constexpr uint32_t kUSecsPerDay
#define STATIC_QUAL
DEVICE int64_t floor_div(int64_t const dividend, int64_t const divisor)
static constexpr uint32_t kEpochOffsetYear1900
static constexpr int32_t kEpochAdjustedDays
static constexpr uint32_t kSecondsPer4YearCycle
static constexpr int64_t kDaysPer400Years
DEVICE int64_t unsigned_mod(int64_t const dividend, int64_t const divisor)
static constexpr uint32_t kSecondsPerNonLeapYear
static constexpr int32_t kMonsPerYear
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ datetrunc_quarter()

ALWAYS_INLINE DEVICE int64_t datetrunc_quarter ( int64_t  timeval)

Definition at line 102 of file DateTruncate.cpp.

References floor_div(), JANMAR, kDaysPer400Years, kEpochAdjustedDays, kEpochOffsetYear1900, kSecondsPer4YearCycle, kSecondsPerNonLeapYear, kSecsJanToMar1900, kSecsPerDay, kUSecsPerDay, STATIC_QUAL, and unsigned_mod().

Referenced by DateTruncate().

102  {
103  if (timeval >= 0L && timeval <= UINT32_MAX - kEpochOffsetYear1900) {
104  STATIC_QUAL const uint32_t cumulative_quarter_epoch_starts[4] = {
105  0, 7776000, 15638400, 23587200};
106  STATIC_QUAL const uint32_t cumulative_quarter_epoch_starts_leap_year[4] = {
107  0, 7862400, 15724800, 23673600};
108  // Handles times from Thu 01 Jan 1970 00:00:00 - Thu 07 Feb 2036 06:28:15.
109  uint32_t seconds_1900 = timeval + kEpochOffsetYear1900;
110  uint32_t leap_years = (seconds_1900 - kSecsJanToMar1900) / kSecondsPer4YearCycle;
111  uint32_t year = (seconds_1900 - leap_years * kUSecsPerDay) / kSecondsPerNonLeapYear;
112  uint32_t base_year_leap_years = (year - 1) / 4;
113  uint32_t base_year_seconds =
114  year * kSecondsPerNonLeapYear + base_year_leap_years * kUSecsPerDay;
115  const bool is_leap_year = year % 4 == 0 && year != 0;
116  const uint32_t* quarter_offsets = is_leap_year
117  ? cumulative_quarter_epoch_starts_leap_year
118  : cumulative_quarter_epoch_starts;
119  uint32_t partial_year_seconds = seconds_1900 % base_year_seconds;
120  uint32_t quarter = partial_year_seconds / (90 * kUSecsPerDay);
121  quarter = quarter <= 3 ? quarter : 3;
122  if (quarter_offsets[quarter] > partial_year_seconds) {
123  quarter--;
124  }
125  return (static_cast<int64_t>(base_year_seconds) + quarter_offsets[quarter] -
127  } else {
128  int64_t const day = floor_div(timeval, kSecsPerDay);
129  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
130  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
131  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
132  constexpr unsigned apr1 = 31; // Days in march
133  unsigned doq; // Day-of-quarter = Days since last Apr1, Jul1, Oct1, Jan1.
134  if (doy < apr1) {
135  bool const leap = yoe % 4 == 0 && (yoe % 100 != 0 || yoe == 0);
136  doq = JANMAR + leap + doy; // Q1
137  } else {
138  unsigned const q = (3 * (doy - apr1) + 2) / 275; // quarter = 0, 1, 2
139  doq = doy - (apr1 + q * 92 - (q != 0)); // Q2, Q3, Q4
140  }
141  return (day - doq) * kSecsPerDay;
142  }
143 }
static constexpr int64_t kSecsPerDay
static constexpr uint32_t kSecsJanToMar1900
static constexpr uint32_t kUSecsPerDay
constexpr unsigned JANMAR
#define STATIC_QUAL
DEVICE int64_t floor_div(int64_t const dividend, int64_t const divisor)
static constexpr uint32_t kEpochOffsetYear1900
static constexpr int32_t kEpochAdjustedDays
static constexpr uint32_t kSecondsPer4YearCycle
static constexpr int64_t kDaysPer400Years
DEVICE int64_t unsigned_mod(int64_t const dividend, int64_t const divisor)
static constexpr uint32_t kSecondsPerNonLeapYear
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ datetrunc_quarterday()

ALWAYS_INLINE DEVICE int64_t datetrunc_quarterday ( int64_t  timeval)

Definition at line 41 of file DateTruncate.cpp.

References kSecsPerQuarterDay, and unsigned_mod().

Referenced by DateTruncate().

41  {
42  return timeval - unsigned_mod(timeval, kSecsPerQuarterDay);
43 }
static constexpr int64_t kSecsPerQuarterDay
DEVICE int64_t unsigned_mod(int64_t const dividend, int64_t const divisor)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ datetrunc_week()

ALWAYS_INLINE DEVICE int64_t datetrunc_week ( int64_t  timeval)

Definition at line 49 of file DateTruncate.cpp.

References kSecsPerDay, and unsigned_mod().

Referenced by DateTruncate().

49  {
50  // Truncate to Monday. 1 Jan 1970 is a Thursday (+3*kSecsPerDay).
51  return timeval - unsigned_mod(timeval + 3 * kSecsPerDay, 7 * kSecsPerDay);
52 }
static constexpr int64_t kSecsPerDay
DEVICE int64_t unsigned_mod(int64_t const dividend, int64_t const divisor)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ datetrunc_year()

ALWAYS_INLINE DEVICE int64_t datetrunc_year ( int64_t  timeval)

Definition at line 145 of file DateTruncate.cpp.

References floor_div(), JANMAR, kDaysPer400Years, kEpochAdjustedDays, kEpochOffsetYear1900, kSecondsPer4YearCycle, kSecondsPerNonLeapYear, kSecsJanToMar1900, kSecsPerDay, kUSecsPerDay, MARJAN, and unsigned_mod().

Referenced by DateTruncate().

145  {
146  if (timeval >= 0L && timeval <= UINT32_MAX - kEpochOffsetYear1900) {
147  // Handles times from Thu 01 Jan 1970 00:00:00 - Thu 07 Feb 2036 06:28:15.
148  uint32_t seconds_1900 = static_cast<uint32_t>(timeval) + kEpochOffsetYear1900;
149  uint32_t leap_years = (seconds_1900 - kSecsJanToMar1900) / kSecondsPer4YearCycle;
150  uint32_t year = (seconds_1900 - leap_years * kUSecsPerDay) / kSecondsPerNonLeapYear;
151  uint32_t base_year_leap_years = (year - 1) / 4;
152  return (static_cast<int64_t>(year) * kSecondsPerNonLeapYear +
153  base_year_leap_years * kUSecsPerDay - kEpochOffsetYear1900);
154  } else {
155  int64_t const day = floor_div(timeval, kSecsPerDay);
156  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
157  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
158  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
159  unsigned docy; // Day-of-calendar-year = Days since last Jan1.
160  if (doy < MARJAN) {
161  bool const leap = yoe % 4 == 0 && (yoe == 0 || yoe % 100 != 0);
162  docy = JANMAR + leap + doy;
163  } else {
164  docy = doy - MARJAN;
165  }
166  return (day - docy) * kSecsPerDay;
167  }
168 }
static constexpr int64_t kSecsPerDay
static constexpr uint32_t kSecsJanToMar1900
static constexpr uint32_t kUSecsPerDay
constexpr unsigned JANMAR
DEVICE int64_t floor_div(int64_t const dividend, int64_t const divisor)
static constexpr uint32_t kEpochOffsetYear1900
static constexpr int32_t kEpochAdjustedDays
static constexpr uint32_t kSecondsPer4YearCycle
static constexpr int64_t kDaysPer400Years
constexpr unsigned MARJAN
DEVICE int64_t unsigned_mod(int64_t const dividend, int64_t const divisor)
static constexpr uint32_t kSecondsPerNonLeapYear
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DateTruncate()

int64_t DateTruncate ( DatetruncField  field,
const int64_t  timeval 
)

Definition at line 214 of file DateTruncate.cpp.

References ALWAYS_INLINE, datetrunc_century(), datetrunc_day(), datetrunc_decade(), datetrunc_hour(), datetrunc_millennium(), datetrunc_minute(), datetrunc_month(), datetrunc_quarter(), datetrunc_quarterday(), datetrunc_week(), datetrunc_year(), DEVICE, dtCENTURY, dtDAY, dtDECADE, dtHOUR, dtMICROSECOND, dtMILLENNIUM, dtMILLISECOND, dtMINUTE, dtMONTH, dtNANOSECOND, dtQUARTER, dtQUARTERDAY, dtSECOND, dtWEEK, and dtYEAR.

Referenced by Analyzer::Constant::do_cast(), anonymous_namespace{ExpressionRange.cpp}::getDateTimePrecisionCastRange(), and DateTimeTranslator::getDateTruncConstantValue().

214  {
215  switch (field) {
216  case dtNANOSECOND:
217  case dtMICROSECOND:
218  case dtMILLISECOND:
219  case dtSECOND:
220  return timeval;
221  case dtMINUTE:
222  return datetrunc_minute(timeval);
223  case dtHOUR:
224  return datetrunc_hour(timeval);
225  case dtQUARTERDAY:
226  return datetrunc_quarterday(timeval);
227  case dtDAY:
228  return datetrunc_day(timeval);
229  case dtWEEK:
230  return datetrunc_week(timeval);
231  case dtMONTH:
232  return datetrunc_month(timeval);
233  case dtQUARTER:
234  return datetrunc_quarter(timeval);
235  case dtYEAR:
236  return datetrunc_year(timeval);
237  case dtDECADE:
238  return datetrunc_decade(timeval);
239  case dtCENTURY:
240  return datetrunc_century(timeval);
241  case dtMILLENNIUM:
242  return datetrunc_millennium(timeval);
243  default:
244 #ifdef __CUDACC__
245  return std::numeric_limits<int64_t>::min();
246 #else
247  abort();
248 #endif
249  }
250 }
ALWAYS_INLINE DEVICE int64_t datetrunc_week(int64_t timeval)
ALWAYS_INLINE DEVICE int64_t datetrunc_quarter(int64_t timeval)
ALWAYS_INLINE DEVICE int64_t datetrunc_millennium(int64_t timeval)
ALWAYS_INLINE DEVICE int64_t datetrunc_quarterday(int64_t timeval)
ALWAYS_INLINE DEVICE int64_t datetrunc_decade(int64_t timeval)
ALWAYS_INLINE DEVICE int64_t datetrunc_hour(int64_t timeval)
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
ALWAYS_INLINE DEVICE int64_t datetrunc_year(int64_t timeval)
ALWAYS_INLINE DEVICE int64_t datetrunc_month(int64_t timeval)
ALWAYS_INLINE DEVICE int64_t datetrunc_century(int64_t timeval)
ALWAYS_INLINE DEVICE int64_t datetrunc_minute(int64_t timeval)
ALWAYS_INLINE DEVICE int64_t datetrunc_day(int64_t timeval)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DateTruncateHighPrecisionToDate()

ALWAYS_INLINE DEVICE int64_t DateTruncateHighPrecisionToDate ( const int64_t  timeval,
const int64_t  scale 
)

Definition at line 254 of file DateTruncate.cpp.

References ALWAYS_INLINE, DEVICE, floor_div(), and kSecsPerDay.

Referenced by DateTruncateHighPrecisionToDateNullable(), and Analyzer::Constant::do_cast().

254  {
255  return floor_div(timeval, scale * kSecsPerDay) * kSecsPerDay;
256 }
static constexpr int64_t kSecsPerDay
DEVICE int64_t floor_div(int64_t const dividend, int64_t const divisor)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DateTruncateHighPrecisionToDateNullable()

ALWAYS_INLINE DEVICE int64_t DateTruncateHighPrecisionToDateNullable ( const int64_t  timeval,
const int64_t  scale,
const int64_t  null_val 
)

Definition at line 259 of file DateTruncate.cpp.

References DateTruncateHighPrecisionToDate().

261  {
262  if (timeval == null_val) {
263  return null_val;
264  }
265  return DateTruncateHighPrecisionToDate(timeval, scale);
266 }
ALWAYS_INLINE DEVICE int64_t DateTruncateHighPrecisionToDate(const int64_t timeval, const int64_t scale)
+ Here is the call graph for this function: