OmniSciDB  2c44a3935d
ExtractFromTime.h File Reference
#include <cstdint>
#include <ctime>
#include "../Shared/funcannotations.h"
+ Include dependency graph for ExtractFromTime.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  ExtractField {
  kYEAR, kQUARTER, kMONTH, kDAY,
  kHOUR, kMINUTE, kSECOND, kMILLISECOND,
  kMICROSECOND, kNANOSECOND, kDOW, kISODOW,
  kDOY, kEPOCH, kQUARTERDAY, kWEEK,
  kDATEEPOCH
}
 

Functions

DEVICE NEVER_INLINE int64_t extract_dow (const int64_t lcltime)
 
DEVICE tm gmtime_r_newlib (const int64_t lcltime, tm &res)
 
DEVICE int64_t ExtractFromTime (ExtractField field, const int64_t timeval)
 

Variables

static constexpr int64_t kNanoSecsPerSec = 1000000000
 
static constexpr int64_t kMicroSecsPerSec = 1000000
 
static constexpr int64_t kMilliSecsPerSec = 1000
 
static constexpr int64_t kSecsPerMin = 60
 
static constexpr int64_t kMinsPerHour = 60
 
static constexpr int64_t kHoursPerDay = 24
 
static constexpr int64_t kSecPerHour = 3600
 
static constexpr int64_t kSecsPerDay = 86400
 
static constexpr int64_t kSecsPerQuarterDay = 21600
 
static constexpr int32_t kDaysPerWeek = 7
 
static constexpr int32_t kMonsPerYear = 12
 
static constexpr int64_t kSecsPerHalfDay = 43200
 
static constexpr int64_t kMinsPerMonth = 43200
 
static constexpr int32_t kYearBase = 1900
 
static constexpr int32_t kEpochAdjustedDays = 11017
 
static constexpr int32_t kEpochAdjustedYears = 2000
 
static constexpr int32_t kEpochAdjustedWDay = 3
 
static constexpr int64_t kDaysPer400Years = 146097
 
static constexpr int64_t kDaysPer100Years = 36524
 
static constexpr int32_t kDaysPer4Years = 3 * 365 + 366
 
static constexpr int32_t kDaysPerYear = 365
 
static constexpr int32_t kDaysInJanuary = 31
 
static constexpr int32_t kDaysInFebruary = 28
 
static constexpr uint32_t kSecondsPerNonLeapYear = 31536000
 
static constexpr uint32_t kSecondsPer4YearCycle = 126230400
 
static constexpr uint32_t kUSecsPerDay = 86400
 
static constexpr uint32_t kEpochOffsetYear1900 = 2208988800
 
static constexpr uint32_t kSecsJanToMar1900 = 5097600
 

Enumeration Type Documentation

◆ ExtractField

Enumerator
kYEAR 
kQUARTER 
kMONTH 
kDAY 
kHOUR 
kMINUTE 
kSECOND 
kMILLISECOND 
kMICROSECOND 
kNANOSECOND 
kDOW 
kISODOW 
kDOY 
kEPOCH 
kQUARTERDAY 
kWEEK 
kDATEEPOCH 

Definition at line 69 of file ExtractFromTime.h.

Function Documentation

◆ extract_dow()

DEVICE NEVER_INLINE int64_t extract_dow ( const int64_t  lcltime)

Definition at line 75 of file ExtractFromTime.cpp.

References anonymous_namespace{ExtractFromTime.cpp}::floor_div(), kDaysPerWeek, kSecsPerDay, and anonymous_namespace{ExtractFromTime.cpp}::unsigned_mod().

Referenced by DateTruncate(), and ExtractFromTime().

75  {
76  int64_t const days_past_epoch = floor_div(lcltime, kSecsPerDay);
77  return unsigned_mod(days_past_epoch + 4, kDaysPerWeek);
78 }
static constexpr int64_t kSecsPerDay
DEVICE int64_t unsigned_mod(int64_t const dividend, int64_t const divisor)
DEVICE int64_t floor_div(int64_t const dividend, int64_t const divisor)
static constexpr int32_t kDaysPerWeek
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ExtractFromTime()

DEVICE int64_t ExtractFromTime ( ExtractField  field,
const int64_t  timeval 
)

Definition at line 320 of file ExtractFromTime.cpp.

References extract_dateepoch(), extract_day(), extract_day_of_week(), extract_dow(), extract_epoch(), extract_hour(), extract_isodow(), extract_microsecond(), extract_millisecond(), extract_minute(), extract_month(), extract_nanosecond(), extract_quarter(), extract_quarterday(), extract_second(), extract_week(), extract_year(), kDATEEPOCH, kDAY, kDOW, kDOY, kEPOCH, kHOUR, kISODOW, kMICROSECOND, kMILLISECOND, kMINUTE, kMONTH, kNANOSECOND, kQUARTER, kQUARTERDAY, kSECOND, kWEEK, and kYEAR.

Referenced by getExpressionRange(), and DateTimeTranslator::getExtractFromTimeConstantValue().

320  {
321  switch (field) {
322  case kEPOCH:
323  return extract_epoch(timeval);
324  case kDATEEPOCH:
325  return extract_dateepoch(timeval);
326  case kQUARTERDAY:
327  return extract_quarterday(timeval);
328  case kHOUR:
329  return extract_hour(timeval);
330  case kMINUTE:
331  return extract_minute(timeval);
332  case kSECOND:
333  return extract_second(timeval);
334  case kMILLISECOND:
335  return extract_millisecond(timeval);
336  case kMICROSECOND:
337  return extract_microsecond(timeval);
338  case kNANOSECOND:
339  return extract_nanosecond(timeval);
340  case kDOW:
341  return extract_dow(timeval);
342  case kISODOW:
343  return extract_isodow(timeval);
344  case kDAY:
345  return extract_day(timeval);
346  case kWEEK:
347  return extract_week(timeval);
348  case kDOY:
349  return extract_day_of_week(timeval);
350  case kMONTH:
351  return extract_month(timeval);
352  case kQUARTER:
353  return extract_quarter(timeval);
354  case kYEAR:
355  return extract_year(timeval);
356  }
357 
358 #ifdef __CUDACC__
359  return -1;
360 #else
361  abort();
362 #endif
363 }
NEVER_INLINE DEVICE int64_t extract_dow(const int64_t lcltime)
NEVER_INLINE DEVICE int64_t extract_week(const int64_t timeval)
NEVER_INLINE DEVICE int64_t extract_dateepoch(const int64_t timeval)
NEVER_INLINE DEVICE int64_t extract_quarterday(const int64_t lcltime)
NEVER_INLINE DEVICE int64_t extract_microsecond(const int64_t lcltime)
NEVER_INLINE DEVICE int64_t extract_minute(const int64_t lcltime)
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
NEVER_INLINE DEVICE int64_t extract_epoch(const int64_t timeval)
NEVER_INLINE DEVICE int64_t extract_day_of_week(const int64_t timeval)
NEVER_INLINE DEVICE int64_t extract_nanosecond(const int64_t lcltime)
NEVER_INLINE DEVICE int64_t extract_hour(const int64_t lcltime)
NEVER_INLINE DEVICE int64_t extract_millisecond(const int64_t lcltime)
NEVER_INLINE DEVICE int64_t extract_day(const int64_t timeval)
NEVER_INLINE DEVICE int64_t extract_second(const int64_t lcltime)
NEVER_INLINE DEVICE int64_t extract_year(const int64_t timeval)
NEVER_INLINE DEVICE int64_t extract_quarter(const int64_t timeval)
NEVER_INLINE DEVICE int64_t extract_month(const int64_t timeval)
NEVER_INLINE DEVICE int64_t extract_isodow(const int64_t timeval)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ gmtime_r_newlib()

DEVICE tm gmtime_r_newlib ( const int64_t  lcltime,
tm &  res 
)

Definition at line 149 of file ExtractFromTime.cpp.

References kDaysInFebruary, kDaysInJanuary, kDaysPer100Years, kDaysPer400Years, kDaysPer4Years, kDaysPerWeek, kDaysPerYear, kEpochAdjustedDays, kEpochAdjustedWDay, kEpochAdjustedYears, kMonsPerYear, kSecPerHour, kSecsPerDay, kSecsPerMin, kYearBase, and run_benchmark_import::res.

Referenced by DateTruncate(), extract_day(), extract_day_of_week(), extract_month(), extract_quarter(), and extract_year().

149  {
150  const int32_t month_lengths[2][kMonsPerYear] = {
151  {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
152  {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
153  int64_t days, rem;
154  int32_t year, month, yearday, weekday;
155  int32_t years400, years100, years4, remainingyears;
156  int32_t yearleap;
157  const int32_t* ip;
158 
159  days = lcltime / kSecsPerDay - kEpochAdjustedDays;
160  rem = lcltime % kSecsPerDay;
161  if (rem < 0) {
162  rem += kSecsPerDay;
163  --days;
164  }
165 
166  /* compute hour, min, and sec */
167  res.tm_hour = static_cast<int32_t>(rem / kSecPerHour);
168  rem %= kSecPerHour;
169  res.tm_min = static_cast<int32_t>(rem / kSecsPerMin);
170  res.tm_sec = static_cast<int32_t>(rem % kSecsPerMin);
171 
172  /* compute day of week */
173  if ((weekday = ((kEpochAdjustedWDay + days) % kDaysPerWeek)) < 0) {
174  weekday += kDaysPerWeek;
175  }
176  res.tm_wday = weekday;
177 
178  /* compute year & day of year */
179  years400 = static_cast<int32_t>(days / kDaysPer400Years);
180  days -= years400 * kDaysPer400Years;
181  /* simplify by making the values positive */
182  if (days < 0) {
183  days += kDaysPer400Years;
184  --years400;
185  }
186 
187  years100 = static_cast<int32_t>(days / kDaysPer100Years);
188  if (years100 == 4) { /* required for proper day of year calculation */
189  --years100;
190  }
191  days -= years100 * kDaysPer100Years;
192  years4 = days / kDaysPer4Years;
193  days -= years4 * kDaysPer4Years;
194  remainingyears = days / kDaysPerYear;
195  if (remainingyears == 4) { /* required for proper day of year calculation */
196  --remainingyears;
197  }
198  days -= remainingyears * kDaysPerYear;
199 
200  year =
201  kEpochAdjustedYears + years400 * 400 + years100 * 100 + years4 * 4 + remainingyears;
202 
203  /* If remainingyears is zero, it means that the years were completely
204  * "consumed" by modulo calculations by 400, 100 and 4, so the year is:
205  * 1. a multiple of 4, but not a multiple of 100 or 400 - it's a leap year,
206  * 2. a multiple of 4 and 100, but not a multiple of 400 - it's not a leap
207  * year,
208  * 3. a multiple of 4, 100 and 400 - it's a leap year.
209  * If years4 is non-zero, it means that the year is not a multiple of 100 or
210  * 400 (case 1), so it's a leap year. If years100 is zero (and years4 is zero
211  * - due to short-circuiting), it means that the year is a multiple of 400
212  * (case 3), so it's also a leap year. */
213  yearleap = remainingyears == 0 && (years4 != 0 || years100 == 0);
214 
215  /* adjust back to 1st January */
216  yearday = days + kDaysInJanuary + kDaysInFebruary + yearleap;
217  if (yearday >= kDaysPerYear + yearleap) {
218  yearday -= kDaysPerYear + yearleap;
219  ++year;
220  }
221  res.tm_yday = yearday;
222  res.tm_year = year - kYearBase;
223 
224  /* Because "days" is the number of days since 1st March, the additional leap
225  * day (29th of February) is the last possible day, so it doesn't matter much
226  * whether the year is actually leap or not. */
227  ip = month_lengths[1];
228  month = 2;
229  while (days >= ip[month]) {
230  days -= ip[month];
231  if (++month >= kMonsPerYear) {
232  month = 0;
233  }
234  }
235  res.tm_mon = month;
236  res.tm_mday = days + 1;
237 
238  res.tm_isdst = 0;
239 
240  return res;
241 }
static constexpr int64_t kSecsPerDay
static constexpr int32_t kDaysPerYear
static constexpr int64_t kSecsPerMin
static constexpr int32_t kDaysInJanuary
static constexpr int32_t kYearBase
static constexpr int32_t kEpochAdjustedDays
static constexpr int32_t kDaysPerWeek
static constexpr int32_t kDaysInFebruary
static constexpr int32_t kEpochAdjustedYears
static constexpr int64_t kDaysPer400Years
static constexpr int64_t kSecPerHour
static constexpr int32_t kDaysPer4Years
static constexpr int32_t kEpochAdjustedWDay
static constexpr int32_t kMonsPerYear
static constexpr int64_t kDaysPer100Years
+ Here is the caller graph for this function:

Variable Documentation

◆ kDaysInFebruary

constexpr int32_t kDaysInFebruary = 28
static

Definition at line 61 of file ExtractFromTime.h.

Referenced by create_epoch(), and gmtime_r_newlib().

◆ kDaysInJanuary

constexpr int32_t kDaysInJanuary = 31
static

Definition at line 59 of file ExtractFromTime.h.

Referenced by create_epoch(), and gmtime_r_newlib().

◆ kDaysPer100Years

constexpr int64_t kDaysPer100Years = 36524
static

Definition at line 53 of file ExtractFromTime.h.

Referenced by create_epoch(), and gmtime_r_newlib().

◆ kDaysPer400Years

constexpr int64_t kDaysPer400Years = 146097
static

Definition at line 51 of file ExtractFromTime.h.

Referenced by create_epoch(), extract_week(), and gmtime_r_newlib().

◆ kDaysPer4Years

constexpr int32_t kDaysPer4Years = 3 * 365 + 366
static

Definition at line 55 of file ExtractFromTime.h.

Referenced by create_epoch(), and gmtime_r_newlib().

◆ kDaysPerWeek

constexpr int32_t kDaysPerWeek = 7
static

Definition at line 33 of file ExtractFromTime.h.

Referenced by DateDiff(), extract_dow(), extract_isodow(), and gmtime_r_newlib().

◆ kDaysPerYear

constexpr int32_t kDaysPerYear = 365
static

Definition at line 57 of file ExtractFromTime.h.

Referenced by create_epoch(), and gmtime_r_newlib().

◆ kEpochAdjustedDays

constexpr int32_t kEpochAdjustedDays = 11017
static

Definition at line 45 of file ExtractFromTime.h.

Referenced by create_epoch(), extract_week(), and gmtime_r_newlib().

◆ kEpochAdjustedWDay

constexpr int32_t kEpochAdjustedWDay = 3
static

Definition at line 49 of file ExtractFromTime.h.

Referenced by gmtime_r_newlib().

◆ kEpochAdjustedYears

constexpr int32_t kEpochAdjustedYears = 2000
static

Definition at line 47 of file ExtractFromTime.h.

Referenced by create_epoch(), and gmtime_r_newlib().

◆ kEpochOffsetYear1900

constexpr uint32_t kEpochOffsetYear1900 = 2208988800
static

◆ kHoursPerDay

constexpr int64_t kHoursPerDay = 24
static

Definition at line 29 of file ExtractFromTime.h.

◆ kMicroSecsPerSec

◆ kMilliSecsPerSec

◆ kMinsPerHour

constexpr int64_t kMinsPerHour = 60
static

Definition at line 28 of file ExtractFromTime.h.

◆ kMinsPerMonth

constexpr int64_t kMinsPerMonth = 43200
static

Definition at line 36 of file ExtractFromTime.h.

◆ kMonsPerYear

constexpr int32_t kMonsPerYear = 12
static

Definition at line 34 of file ExtractFromTime.h.

Referenced by DateTruncate(), extract_month_fast(), and gmtime_r_newlib().

◆ kNanoSecsPerSec

◆ kSecondsPer4YearCycle

constexpr uint32_t kSecondsPer4YearCycle = 126230400
static

◆ kSecondsPerNonLeapYear

constexpr uint32_t kSecondsPerNonLeapYear = 31536000
static

◆ kSecPerHour

constexpr int64_t kSecPerHour = 3600
static

◆ kSecsJanToMar1900

constexpr uint32_t kSecsJanToMar1900 = 5097600
static

◆ kSecsPerDay

◆ kSecsPerHalfDay

constexpr int64_t kSecsPerHalfDay = 43200
static

Definition at line 35 of file ExtractFromTime.h.

Referenced by TimeGM::parse_meridians().

◆ kSecsPerMin

constexpr int64_t kSecsPerMin = 60
static

◆ kSecsPerQuarterDay

constexpr int64_t kSecsPerQuarterDay = 21600
static

Definition at line 32 of file ExtractFromTime.h.

Referenced by DateDiff(), DateTruncate(), and extract_quarterday().

◆ kUSecsPerDay

constexpr uint32_t kUSecsPerDay = 86400
static

◆ kYearBase

constexpr int32_t kYearBase = 1900
static

Definition at line 38 of file ExtractFromTime.h.

Referenced by DateTruncate(), and gmtime_r_newlib().