OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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

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

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

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

Definition at line 397 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().

399  {
400  switch (datepart) {
401  case dtNANOSECOND:
402  return (enddate - startdate) * kNanoSecsPerSec;
403  case dtMICROSECOND:
404  return (enddate - startdate) * kMicroSecsPerSec;
405  case dtMILLISECOND:
406  return (enddate - startdate) * kMilliSecsPerSec;
407  case dtSECOND:
408  return enddate - startdate;
409  case dtMINUTE:
410  return (enddate - startdate) / kSecsPerMin;
411  case dtHOUR:
412  return (enddate - startdate) / kSecsPerHour;
413  case dtQUARTERDAY:
414  return (enddate - startdate) / (kSecsPerDay / 4);
415  case dtDAY:
416  return (enddate - startdate) / kSecsPerDay;
417  case dtWEEK:
418  case dtWEEK_SUNDAY:
419  case dtWEEK_SATURDAY:
420  return (enddate - startdate) / (7 * kSecsPerDay);
421  default:
422  return (EraTime::make(enddate) - EraTime::make(startdate)).count(datepart);
423  }
424 }
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:

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 
)

Definition at line 427 of file DateTruncate.cpp.

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

Referenced by DateDiffHighPrecisionNullable().

431  {
432  // Return pow(10,i). Only valid for i = 0, 3, 6, 9.
433  constexpr int pow10[10]{1, 0, 0, 1000, 0, 0, 1000 * 1000, 0, 0, 1000 * 1000 * 1000};
434  switch (datepart) {
435  case dtNANOSECOND:
436  case dtMICROSECOND:
437  case dtMILLISECOND: {
438  static_assert(dtMILLISECOND + 1 == dtMICROSECOND, "Please keep these consecutive.");
439  static_assert(dtMICROSECOND + 1 == dtNANOSECOND, "Please keep these consecutive.");
440  int const target_dim = (datepart - (dtMILLISECOND - 1)) * 3; // 3, 6, or 9.
441  int const delta_dim = end_dim - start_dim; // in [-9,9] multiple of 3
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];
446  }
447  default:
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;
453  // sub-second values must be accounted for when calling DateDiff. Examples:
454  // 2000-02-15 12:00:00.006 to 2000-03-15 12:00:00.005 is 0 months.
455  // 2000-02-15 12:00:00.006 to 2000-03-15 12:00:00.006 is 1 month.
456  int const adj_sec = 0 < delta_s && delta_ns < 0 ? -1
457  : delta_s < 0 && 0 < delta_ns ? 1
458  : 0;
459  return DateDiff(datepart, start_seconds, end_seconds + adj_sec);
460  }
461 }
RUNTIME_EXPORT DEVICE int64_t DateDiff(const DatetruncField datepart, const int64_t startdate, const int64_t enddate)
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:

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 
)

Definition at line 474 of file DateTruncate.cpp.

References DateDiffHighPrecision().

479  {
480  if (startdate == null_val || enddate == null_val) {
481  return null_val;
482  }
483  return DateDiffHighPrecision(datepart, startdate, enddate, start_dim, end_dim);
484 }
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)

+ Here is the call graph for this function:

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

Definition at line 463 of file DateTruncate.cpp.

References DateDiff().

466  {
467  if (startdate == null_val || enddate == null_val) {
468  return null_val;
469  }
470  return DateDiff(datepart, startdate, enddate);
471 }
RUNTIME_EXPORT DEVICE int64_t DateDiff(const DatetruncField datepart, const int64_t startdate, const int64_t enddate)

+ Here is the call graph for this function:

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_century ( int64_t  timeval)

Definition at line 215 of file DateTruncate.cpp.

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

Referenced by DateTruncate().

215  {
216  int64_t const day = floor_div(timeval, kSecsPerDay);
217  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
218  // Day-of-century = Days since last 010101 (Jan 1 1901, 2001, 2101, etc.)
219  unsigned const doc = doe < MARJAN ? doe + (36525 - MARJAN) : (doe - MARJAN) % 36524;
220  return (day - doc) * kSecsPerDay;
221 }
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:

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_day ( int64_t  timeval)

Definition at line 47 of file DateTruncate.cpp.

References kSecsPerDay, and unsigned_mod().

Referenced by DateTruncate().

47  {
48  return timeval - unsigned_mod(timeval, kSecsPerDay);
49 }
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:

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_decade ( int64_t  timeval)

Definition at line 196 of file DateTruncate.cpp.

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

Referenced by DateTruncate().

196  {
197  // Number of days from x00301 to (x+1)00101. Always includes exactly two leap days.
198  constexpr unsigned decmarjan = MARJAN + 9 * 365 + 2;
199  int64_t const day = floor_div(timeval, kSecsPerDay);
200  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
201  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
202  unsigned const decoe = yoe - yoe % 10; // Decade-of-era
203  // Initialize to days after mar1 of decade, then adjust to after jan1 below.
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;
208  } else {
209  days_after_decade -= decmarjan;
210  }
211  return (day - days_after_decade) * kSecsPerDay;
212 }
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:

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_hour ( int64_t  timeval)

Definition at line 38 of file DateTruncate.cpp.

References kSecsPerHour, and unsigned_mod().

Referenced by DateTruncate().

38  {
39  return timeval - unsigned_mod(timeval, kSecsPerHour);
40 }
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:

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_millennium ( int64_t  timeval)

Definition at line 224 of file DateTruncate.cpp.

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

Referenced by DateTruncate().

224  {
225  constexpr unsigned millennium2001 = 365242; // Days from Jan 1 2001 to 3001.
226  int64_t const day = floor_div(timeval, kSecsPerDay);
227  // lcm(400, 1000) = 2000 so use 5*400-year eras at a time.
228  unsigned dom = unsigned_mod(day - kEpochAdjustedDays, 5 * kDaysPer400Years);
229  if (dom < MARJAN) {
230  dom += millennium2001 + 1 - MARJAN;
231  } else if (dom < MARJAN + millennium2001) {
232  dom -= MARJAN;
233  } else {
234  dom -= MARJAN + millennium2001;
235  }
236  return (day - dom) * kSecsPerDay;
237 }
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:

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_minute ( int64_t  timeval)

Definition at line 34 of file DateTruncate.cpp.

References kSecsPerMin, and unsigned_mod().

Referenced by DateTruncate().

34  {
35  return timeval - unsigned_mod(timeval, kSecsPerMin);
36 }
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:

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_month ( int64_t  timeval)

Definition at line 79 of file DateTruncate.cpp.

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

Referenced by DateTruncate().

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

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_quarter ( int64_t  timeval)

Definition at line 128 of file DateTruncate.cpp.

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

Referenced by DateTruncate().

128  {
129  if (timeval >= 0LL && timeval <= UINT32_MAX - kEpochOffsetYear1900) {
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};
134  // Handles times from Thu 01 Jan 1970 00:00:00 - Thu 07 Feb 2036 06:28:15.
135  uint32_t seconds_1900 = timeval + kEpochOffsetYear1900;
136  uint32_t leap_years = (seconds_1900 - kSecsJanToMar1900) / kSecondsPer4YearCycle;
137  uint32_t year = (seconds_1900 - leap_years * kUSecsPerDay) / kSecondsPerNonLeapYear;
138  uint32_t base_year_leap_years = (year - 1) / 4;
139  uint32_t base_year_seconds =
140  year * kSecondsPerNonLeapYear + base_year_leap_years * kUSecsPerDay;
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) {
149  quarter--;
150  }
151  return (static_cast<int64_t>(base_year_seconds) + quarter_offsets[quarter] -
153  } else {
154  int64_t const day = floor_div(timeval, kSecsPerDay);
155  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
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; // Days in march
159  unsigned doq; // Day-of-quarter = Days since last Apr1, Jul1, Oct1, Jan1.
160  if (doy < apr1) {
161  bool const leap = yoe % 4 == 0 && (yoe % 100 != 0 || yoe == 0);
162  doq = JANMAR + leap + doy; // Q1
163  } else {
164  unsigned const q = (3 * (doy - apr1) + 2) / 275; // quarter = 0, 1, 2
165  doq = doy - (apr1 + q * 92 - (q != 0)); // Q2, Q3, Q4
166  }
167  return (day - doq) * kSecsPerDay;
168  }
169 }
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:

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_quarterday ( int64_t  timeval)

Definition at line 43 of file DateTruncate.cpp.

References kSecsPerQuarterDay, and unsigned_mod().

Referenced by DateTruncate().

43  {
44  return timeval - unsigned_mod(timeval, kSecsPerQuarterDay);
45 }
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 59 of file DateTruncate.cpp.

References kSecsPerDay, and unsigned_mod().

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

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_week_monday ( int64_t  timeval)

Definition at line 65 of file DateTruncate.cpp.

Referenced by DateTruncate().

65  {
66  return datetrunc_week<dtMONDAY>(timeval);
67 }

+ Here is the caller graph for this function:

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_week_saturday ( int64_t  timeval)

Definition at line 75 of file DateTruncate.cpp.

Referenced by DateTruncate().

75  {
76  return datetrunc_week<dtSATURDAY>(timeval);
77 }

+ Here is the caller graph for this function:

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_week_sunday ( int64_t  timeval)

Definition at line 70 of file DateTruncate.cpp.

Referenced by DateTruncate().

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

+ Here is the caller graph for this function:

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_year ( int64_t  timeval)

Definition at line 171 of file DateTruncate.cpp.

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

Referenced by DateTruncate().

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

DEVICE int64_t DateTruncate ( DatetruncField  field,
const int64_t  timeval 
)

Definition at line 242 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(), DateTimeTranslator::getDateTruncConstantValue(), Timestamp::truncateToDay(), Timestamp::truncateToHours(), Timestamp::truncateToMinutes(), Timestamp::truncateToMonth(), and Timestamp::truncateToYear().

242  {
243  switch (field) {
244  case dtNANOSECOND:
245  case dtMICROSECOND:
246  case dtMILLISECOND:
247  case dtSECOND:
248  return timeval;
249  case dtMINUTE:
250  return datetrunc_minute(timeval);
251  case dtHOUR:
252  return datetrunc_hour(timeval);
253  case dtQUARTERDAY:
254  return datetrunc_quarterday(timeval);
255  case dtDAY:
256  return datetrunc_day(timeval);
257  case dtWEEK:
258  return datetrunc_week_monday(timeval);
259  case dtWEEK_SUNDAY:
260  return datetrunc_week_sunday(timeval);
261  case dtWEEK_SATURDAY:
262  return datetrunc_week_saturday(timeval);
263  case dtMONTH:
264  return datetrunc_month(timeval);
265  case dtQUARTER:
266  return datetrunc_quarter(timeval);
267  case dtYEAR:
268  return datetrunc_year(timeval);
269  case dtDECADE:
270  return datetrunc_decade(timeval);
271  case dtCENTURY:
272  return datetrunc_century(timeval);
273  case dtMILLENNIUM:
274  return datetrunc_millennium(timeval);
275  default:
276 #ifdef __CUDACC__
277  return std::numeric_limits<int64_t>::min();
278 #else
279  abort();
280 #endif
281  }
282 }
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_millennium(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)
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:33
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_day(int64_t timeval)
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)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_hour(int64_t timeval)
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)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 286 of file DateTruncate.cpp.

References floor_div(), and kSecsPerDay.

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

286  {
287  return floor_div(timeval, scale * kSecsPerDay) * kSecsPerDay;
288 }
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:

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

Definition at line 291 of file DateTruncate.cpp.

References DateTruncateHighPrecisionToDate().

293  {
294  if (timeval == null_val) {
295  return null_val;
296  }
297  return DateTruncateHighPrecisionToDate(timeval, scale);
298 }
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t DateTruncateHighPrecisionToDate(const int64_t timeval, const int64_t scale)

+ Here is the call graph for this function: