OmniSciDB  fe05a0c208
 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

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

References count, 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().

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

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

Referenced by DateDiffHighPrecisionNullable().

430  {
431  // Return pow(10,i). Only valid for i = 0, 3, 6, 9.
432  constexpr int pow10[10]{1, 0, 0, 1000, 0, 0, 1000 * 1000, 0, 0, 1000 * 1000 * 1000};
433  switch (datepart) {
434  case dtNANOSECOND:
435  case dtMICROSECOND:
436  case dtMILLISECOND: {
437  static_assert(dtMILLISECOND + 1 == dtMICROSECOND, "Please keep these consecutive.");
438  static_assert(dtMICROSECOND + 1 == dtNANOSECOND, "Please keep these consecutive.");
439  int const target_dim = (datepart - (dtMILLISECOND - 1)) * 3; // 3, 6, or 9.
440  int const delta_dim = end_dim - start_dim; // in [-9,9] multiple of 3
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];
445  }
446  default:
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;
452  // sub-second values must be accounted for when calling DateDiff. Examples:
453  // 2000-02-15 12:00:00.006 to 2000-03-15 12:00:00.005 is 0 months.
454  // 2000-02-15 12:00:00.006 to 2000-03-15 12:00:00.006 is 1 month.
455  int const adj_sec =
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);
458  }
459 }
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 472 of file DateTruncate.cpp.

References DateDiffHighPrecision().

477  {
478  if (startdate == null_val || enddate == null_val) {
479  return null_val;
480  }
481  return DateDiffHighPrecision(datepart, startdate, enddate, start_dim, end_dim);
482 }
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 461 of file DateTruncate.cpp.

References DateDiff().

464  {
465  if (startdate == null_val || enddate == null_val) {
466  return null_val;
467  }
468  return DateDiff(datepart, startdate, enddate);
469 }
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 214 of file DateTruncate.cpp.

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

Referenced by DateTruncate().

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

References kSecsPerDay, and unsigned_mod().

Referenced by DateTruncate().

46  {
47  return timeval - unsigned_mod(timeval, kSecsPerDay);
48 }
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 195 of file DateTruncate.cpp.

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

Referenced by DateTruncate().

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

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_millennium ( int64_t  timeval)

Definition at line 223 of file DateTruncate.cpp.

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

Referenced by DateTruncate().

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

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_month ( int64_t  timeval)

Definition at line 78 of file DateTruncate.cpp.

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

Referenced by DateTruncate().

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

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

Referenced by DateTruncate().

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

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

References kSecsPerQuarterDay, and unsigned_mod().

Referenced by DateTruncate().

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

References kSecsPerDay, OFFSET, and unsigned_mod().

58  {
59  // Truncate to OFFSET.
60  return timeval - unsigned_mod(timeval + OFFSET * kSecsPerDay, 7 * kSecsPerDay);
61 }
static constexpr int64_t kSecsPerDay
#define OFFSET
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 64 of file DateTruncate.cpp.

Referenced by DateTruncate().

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

+ Here is the caller graph for this function:

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_week_saturday ( int64_t  timeval)

Definition at line 74 of file DateTruncate.cpp.

Referenced by DateTruncate().

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

+ Here is the caller graph for this function:

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_week_sunday ( int64_t  timeval)

Definition at line 69 of file DateTruncate.cpp.

Referenced by DateTruncate().

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

+ Here is the caller graph for this function:

RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t datetrunc_year ( int64_t  timeval)

Definition at line 170 of file DateTruncate.cpp.

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

Referenced by DateTruncate().

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

+ 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 241 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().

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

References floor_div(), and kSecsPerDay.

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

285  {
286  return floor_div(timeval, scale * kSecsPerDay) * kSecsPerDay;
287 }
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 290 of file DateTruncate.cpp.

References DateTruncateHighPrecisionToDate().

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

+ Here is the call graph for this function: