OmniSciDB  bf83d84833
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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:
+ This graph shows which files directly or indirectly include this file:

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)
 
template<unsigned OFFSET>
ALWAYS_INLINE DEVICE int64_t datetrunc_week (int64_t timeval)
 
ALWAYS_INLINE DEVICE int64_t datetrunc_week_monday (int64_t timeval)
 
ALWAYS_INLINE DEVICE int64_t datetrunc_week_sunday (int64_t timeval)
 
ALWAYS_INLINE DEVICE int64_t datetrunc_week_saturday (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)
 

Variables

constexpr unsigned anonymous_namespace{DateTruncate.cpp}::dtMONDAY = 3
 
constexpr unsigned anonymous_namespace{DateTruncate.cpp}::dtSUNDAY = 4
 
constexpr unsigned anonymous_namespace{DateTruncate.cpp}::dtSATURDAY = 5
 

Function Documentation

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

Definition at line 389 of file DateTruncate.cpp.

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

Referenced by DateDiffHighPrecision(), and DateDiffNullable().

391  {
392  switch (datepart) {
393  case dtNANOSECOND:
394  return (enddate - startdate) * kNanoSecsPerSec;
395  case dtMICROSECOND:
396  return (enddate - startdate) * kMicroSecsPerSec;
397  case dtMILLISECOND:
398  return (enddate - startdate) * kMilliSecsPerSec;
399  case dtSECOND:
400  return enddate - startdate;
401  case dtMINUTE:
402  return (enddate - startdate) / kSecsPerMin;
403  case dtHOUR:
404  return (enddate - startdate) / kSecsPerHour;
405  case dtQUARTERDAY:
406  return (enddate - startdate) / (kSecsPerDay / 4);
407  case dtDAY:
408  return (enddate - startdate) / kSecsPerDay;
409  case dtWEEK:
410  case dtWEEK_SUNDAY:
411  case dtWEEK_SATURDAY:
412  return (enddate - startdate) / (7 * kSecsPerDay);
413  default:
414  return (EraTime::make(enddate) - EraTime::make(startdate)).count(datepart);
415  }
416 }
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:

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 418 of file DateTruncate.cpp.

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

Referenced by DateDiffHighPrecisionNullable().

422  {
423  // Return pow(10,i). Only valid for i = 0, 3, 6, 9.
424  constexpr int pow10[10]{1, 0, 0, 1000, 0, 0, 1000 * 1000, 0, 0, 1000 * 1000 * 1000};
425  switch (datepart) {
426  case dtNANOSECOND:
427  case dtMICROSECOND:
428  case dtMILLISECOND: {
429  static_assert(dtMILLISECOND + 1 == dtMICROSECOND, "Please keep these consecutive.");
430  static_assert(dtMICROSECOND + 1 == dtNANOSECOND, "Please keep these consecutive.");
431  int const target_dim = (datepart - (dtMILLISECOND - 1)) * 3; // 3, 6, or 9.
432  int const delta_dim = end_dim - start_dim; // in [-9,9] multiple of 3
433  int const adj_dim = target_dim - (delta_dim < 0 ? start_dim : end_dim);
434  int64_t const numerator = delta_dim < 0 ? enddate * pow10[-delta_dim] - startdate
435  : enddate - startdate * pow10[delta_dim];
436  return adj_dim < 0 ? numerator / pow10[-adj_dim] : numerator * pow10[adj_dim];
437  }
438  default:
439  int64_t const end_seconds = floor_div(enddate, pow10[end_dim]);
440  int delta_ns = (enddate - end_seconds * pow10[end_dim]) * pow10[9 - end_dim];
441  int64_t const start_seconds = floor_div(startdate, pow10[start_dim]);
442  delta_ns -= (startdate - start_seconds * pow10[start_dim]) * pow10[9 - start_dim];
443  int64_t const delta_s = end_seconds - start_seconds;
444  // sub-second values must be accounted for when calling DateDiff. Examples:
445  // 2000-02-15 12:00:00.006 to 2000-03-15 12:00:00.005 is 0 months.
446  // 2000-02-15 12:00:00.006 to 2000-03-15 12:00:00.006 is 1 month.
447  int const adj_sec =
448  0 < delta_s && delta_ns < 0 ? -1 : delta_s < 0 && 0 < delta_ns ? 1 : 0;
449  return DateDiff(datepart, start_seconds, end_seconds + adj_sec);
450  }
451 }
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:

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 463 of file DateTruncate.cpp.

References DateDiffHighPrecision().

468  {
469  if (startdate == null_val || enddate == null_val) {
470  return null_val;
471  }
472  return DateDiffHighPrecision(datepart, startdate, enddate, start_dim, end_dim);
473 }
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:

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

Definition at line 453 of file DateTruncate.cpp.

References DateDiff().

456  {
457  if (startdate == null_val || enddate == null_val) {
458  return null_val;
459  }
460  return DateDiff(datepart, startdate, enddate);
461 }
DEVICE int64_t DateDiff(const DatetruncField datepart, const int64_t startdate, const int64_t enddate)

+ Here is the call graph for this function:

ALWAYS_INLINE DEVICE int64_t datetrunc_century ( int64_t  timeval)

Definition at line 208 of file DateTruncate.cpp.

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

Referenced by DateTruncate().

208  {
209  int64_t const day = floor_div(timeval, kSecsPerDay);
210  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
211  // Day-of-century = Days since last 010101 (Jan 1 1901, 2001, 2101, etc.)
212  unsigned const doc = doe < MARJAN ? doe + (36525 - MARJAN) : (doe - MARJAN) % 36524;
213  return (day - doc) * kSecsPerDay;
214 }
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:

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:

ALWAYS_INLINE DEVICE int64_t datetrunc_decade ( int64_t  timeval)

Definition at line 190 of file DateTruncate.cpp.

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

Referenced by DateTruncate().

190  {
191  // Number of days from x00301 to (x+1)00101. Always includes exactly two leap days.
192  constexpr unsigned decmarjan = MARJAN + 9 * 365 + 2;
193  int64_t const day = floor_div(timeval, kSecsPerDay);
194  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
195  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
196  unsigned const decoe = yoe - yoe % 10; // Decade-of-era
197  // Initialize to days after mar1 of decade, then adjust to after jan1 below.
198  unsigned days_after_decade = doe - (365 * decoe + decoe / 4 - decoe / 100);
199  if (days_after_decade < decmarjan) {
200  bool const leap = decoe % 4 == 0 && (decoe == 0 || decoe % 100 != 0);
201  days_after_decade += JANMAR + leap;
202  } else {
203  days_after_decade -= decmarjan;
204  }
205  return (day - days_after_decade) * kSecsPerDay;
206 }
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:

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:

ALWAYS_INLINE DEVICE int64_t datetrunc_millennium ( int64_t  timeval)

Definition at line 216 of file DateTruncate.cpp.

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

Referenced by DateTruncate().

216  {
217  constexpr unsigned millennium2001 = 365242; // Days from Jan 1 2001 to 3001.
218  int64_t const day = floor_div(timeval, kSecsPerDay);
219  // lcm(400, 1000) = 2000 so use 5*400-year eras at a time.
220  unsigned dom = unsigned_mod(day - kEpochAdjustedDays, 5 * kDaysPer400Years);
221  if (dom < MARJAN) {
222  dom += millennium2001 + 1 - MARJAN;
223  } else if (dom < MARJAN + millennium2001) {
224  dom -= MARJAN;
225  } else {
226  dom -= MARJAN + millennium2001;
227  }
228  return (day - dom) * kSecsPerDay;
229 }
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:

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:

ALWAYS_INLINE DEVICE int64_t datetrunc_month ( int64_t  timeval)

Definition at line 74 of file DateTruncate.cpp.

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

Referenced by DateTruncate().

74  {
75  if (timeval >= 0L && timeval <= UINT32_MAX - (kEpochOffsetYear1900)) {
76  STATIC_QUAL const uint32_t cumulative_month_epoch_starts[kMonsPerYear] = {0,
77  2678400,
78  5270400,
79  7948800,
80  10540800,
81  13219200,
82  15897600,
83  18489600,
84  21168000,
85  23760000,
86  26438400,
87  29116800};
88  // Handles times from Thu 01 Jan 1970 00:00:00 - Thu 07 Feb 2036 06:28:15.
89  uint32_t seconds_march_1900 = timeval + kEpochOffsetYear1900 - kSecsJanToMar1900;
90  uint32_t seconds_past_4year_period = seconds_march_1900 % kSecondsPer4YearCycle;
91  uint32_t four_year_period_seconds =
92  (seconds_march_1900 / kSecondsPer4YearCycle) * kSecondsPer4YearCycle;
93  uint32_t year_seconds_past_4year_period =
94  (seconds_past_4year_period / kSecondsPerNonLeapYear) * kSecondsPerNonLeapYear;
95  if (seconds_past_4year_period >=
96  kSecondsPer4YearCycle - kUSecsPerDay) { // if we are in Feb 29th
97  year_seconds_past_4year_period -= kSecondsPerNonLeapYear;
98  }
99  uint32_t seconds_past_march =
100  seconds_past_4year_period - year_seconds_past_4year_period;
101  uint32_t month =
102  seconds_past_march / (30 * kUSecsPerDay); // Will make the correct month either
103  // be the guessed month or month before
104  month = month <= 11 ? month : 11;
105  if (cumulative_month_epoch_starts[month] > seconds_past_march) {
106  month--;
107  }
108  return (static_cast<int64_t>(four_year_period_seconds) +
109  year_seconds_past_4year_period + cumulative_month_epoch_starts[month] -
111  } else {
112  int64_t const day = floor_div(timeval, kSecsPerDay);
113  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
114  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
115  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
116  unsigned const moy = (5 * doy + 2) / 153;
117  unsigned const dom = doy - (153 * moy + 2) / 5;
118  return (day - dom) * kSecsPerDay;
119  }
120 }
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:

ALWAYS_INLINE DEVICE int64_t datetrunc_quarter ( int64_t  timeval)

Definition at line 122 of file DateTruncate.cpp.

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

Referenced by DateTruncate().

122  {
123  if (timeval >= 0L && timeval <= UINT32_MAX - kEpochOffsetYear1900) {
124  STATIC_QUAL const uint32_t cumulative_quarter_epoch_starts[4] = {
125  0, 7776000, 15638400, 23587200};
126  STATIC_QUAL const uint32_t cumulative_quarter_epoch_starts_leap_year[4] = {
127  0, 7862400, 15724800, 23673600};
128  // Handles times from Thu 01 Jan 1970 00:00:00 - Thu 07 Feb 2036 06:28:15.
129  uint32_t seconds_1900 = timeval + kEpochOffsetYear1900;
130  uint32_t leap_years = (seconds_1900 - kSecsJanToMar1900) / kSecondsPer4YearCycle;
131  uint32_t year = (seconds_1900 - leap_years * kUSecsPerDay) / kSecondsPerNonLeapYear;
132  uint32_t base_year_leap_years = (year - 1) / 4;
133  uint32_t base_year_seconds =
134  year * kSecondsPerNonLeapYear + base_year_leap_years * kUSecsPerDay;
135  const bool is_leap_year = year % 4 == 0 && year != 0;
136  const uint32_t* quarter_offsets = is_leap_year
137  ? cumulative_quarter_epoch_starts_leap_year
138  : cumulative_quarter_epoch_starts;
139  uint32_t partial_year_seconds = seconds_1900 % base_year_seconds;
140  uint32_t quarter = partial_year_seconds / (90 * kUSecsPerDay);
141  quarter = quarter <= 3 ? quarter : 3;
142  if (quarter_offsets[quarter] > partial_year_seconds) {
143  quarter--;
144  }
145  return (static_cast<int64_t>(base_year_seconds) + quarter_offsets[quarter] -
147  } else {
148  int64_t const day = floor_div(timeval, kSecsPerDay);
149  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
150  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
151  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
152  constexpr unsigned apr1 = 31; // Days in march
153  unsigned doq; // Day-of-quarter = Days since last Apr1, Jul1, Oct1, Jan1.
154  if (doy < apr1) {
155  bool const leap = yoe % 4 == 0 && (yoe % 100 != 0 || yoe == 0);
156  doq = JANMAR + leap + doy; // Q1
157  } else {
158  unsigned const q = (3 * (doy - apr1) + 2) / 275; // quarter = 0, 1, 2
159  doq = doy - (apr1 + q * 92 - (q != 0)); // Q2, Q3, Q4
160  }
161  return (day - doq) * kSecsPerDay;
162  }
163 }
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:

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:

template<unsigned OFFSET>
ALWAYS_INLINE DEVICE int64_t datetrunc_week ( int64_t  timeval)

Definition at line 57 of file DateTruncate.cpp.

References kSecsPerDay, and unsigned_mod().

57  {
58  // Truncate to OFFSET.
59  return timeval - unsigned_mod(timeval + OFFSET * kSecsPerDay, 7 * kSecsPerDay);
60 }
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:

ALWAYS_INLINE DEVICE int64_t datetrunc_week_monday ( int64_t  timeval)

Definition at line 62 of file DateTruncate.cpp.

Referenced by DateTruncate().

62  {
63  return datetrunc_week<dtMONDAY>(timeval);
64 }

+ Here is the caller graph for this function:

ALWAYS_INLINE DEVICE int64_t datetrunc_week_saturday ( int64_t  timeval)

Definition at line 70 of file DateTruncate.cpp.

Referenced by DateTruncate().

70  {
71  return datetrunc_week<dtSATURDAY>(timeval);
72 }

+ Here is the caller graph for this function:

ALWAYS_INLINE DEVICE int64_t datetrunc_week_sunday ( int64_t  timeval)

Definition at line 66 of file DateTruncate.cpp.

Referenced by DateTruncate().

66  {
67  return datetrunc_week<dtSUNDAY>(timeval);
68 }

+ Here is the caller graph for this function:

ALWAYS_INLINE DEVICE int64_t datetrunc_year ( int64_t  timeval)

Definition at line 165 of file DateTruncate.cpp.

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

Referenced by DateTruncate().

165  {
166  if (timeval >= 0L && timeval <= UINT32_MAX - kEpochOffsetYear1900) {
167  // Handles times from Thu 01 Jan 1970 00:00:00 - Thu 07 Feb 2036 06:28:15.
168  uint32_t seconds_1900 = static_cast<uint32_t>(timeval) + kEpochOffsetYear1900;
169  uint32_t leap_years = (seconds_1900 - kSecsJanToMar1900) / kSecondsPer4YearCycle;
170  uint32_t year = (seconds_1900 - leap_years * kUSecsPerDay) / kSecondsPerNonLeapYear;
171  uint32_t base_year_leap_years = (year - 1) / 4;
172  return (static_cast<int64_t>(year) * kSecondsPerNonLeapYear +
173  base_year_leap_years * kUSecsPerDay - kEpochOffsetYear1900);
174  } else {
175  int64_t const day = floor_div(timeval, kSecsPerDay);
176  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
177  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
178  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
179  unsigned docy; // Day-of-calendar-year = Days since last Jan1.
180  if (doy < MARJAN) {
181  bool const leap = yoe % 4 == 0 && (yoe == 0 || yoe % 100 != 0);
182  docy = JANMAR + leap + doy;
183  } else {
184  docy = doy - MARJAN;
185  }
186  return (day - docy) * kSecsPerDay;
187  }
188 }
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:

int64_t DateTruncate ( DatetruncField  field,
const int64_t  timeval 
)

Definition at line 234 of file DateTruncate.cpp.

References datetrunc_century(), datetrunc_day(), datetrunc_decade(), datetrunc_hour(), datetrunc_millennium(), datetrunc_minute(), datetrunc_month(), datetrunc_quarter(), datetrunc_quarterday(), datetrunc_week_monday(), datetrunc_week_saturday(), datetrunc_week_sunday(), datetrunc_year(), dtCENTURY, dtDAY, dtDECADE, dtHOUR, dtMICROSECOND, dtMILLENNIUM, dtMILLISECOND, dtMINUTE, dtMONTH, dtNANOSECOND, dtQUARTER, dtQUARTERDAY, dtSECOND, dtWEEK, dtWEEK_SATURDAY, dtWEEK_SUNDAY, and dtYEAR.

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

234  {
235  switch (field) {
236  case dtNANOSECOND:
237  case dtMICROSECOND:
238  case dtMILLISECOND:
239  case dtSECOND:
240  return timeval;
241  case dtMINUTE:
242  return datetrunc_minute(timeval);
243  case dtHOUR:
244  return datetrunc_hour(timeval);
245  case dtQUARTERDAY:
246  return datetrunc_quarterday(timeval);
247  case dtDAY:
248  return datetrunc_day(timeval);
249  case dtWEEK:
250  return datetrunc_week_monday(timeval);
251  case dtWEEK_SUNDAY:
252  return datetrunc_week_sunday(timeval);
253  case dtWEEK_SATURDAY:
254  return datetrunc_week_saturday(timeval);
255  case dtMONTH:
256  return datetrunc_month(timeval);
257  case dtQUARTER:
258  return datetrunc_quarter(timeval);
259  case dtYEAR:
260  return datetrunc_year(timeval);
261  case dtDECADE:
262  return datetrunc_decade(timeval);
263  case dtCENTURY:
264  return datetrunc_century(timeval);
265  case dtMILLENNIUM:
266  return datetrunc_millennium(timeval);
267  default:
268 #ifdef __CUDACC__
269  return std::numeric_limits<int64_t>::min();
270 #else
271  abort();
272 #endif
273  }
274 }
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_week_monday(int64_t timeval)
ALWAYS_INLINE DEVICE int64_t datetrunc_week_sunday(int64_t timeval)
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_week_saturday(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:

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

Definition at line 278 of file DateTruncate.cpp.

References floor_div(), and kSecsPerDay.

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

278  {
279  return floor_div(timeval, scale * kSecsPerDay) * kSecsPerDay;
280 }
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:

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

Definition at line 283 of file DateTruncate.cpp.

References DateTruncateHighPrecisionToDate().

285  {
286  if (timeval == null_val) {
287  return null_val;
288  }
289  return DateTruncateHighPrecisionToDate(timeval, scale);
290 }
ALWAYS_INLINE DEVICE int64_t DateTruncateHighPrecisionToDate(const int64_t timeval, const int64_t scale)

+ Here is the call graph for this function: