OmniSciDB  04ee39c94c
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
}
 

Functions

DEVICE int32_t extract_dow (const int64_t lcltime)
 
DEVICE tm gmtime_r_newlib (const int64_t lcltime, tm &res)
 
DEVICE NEVER_INLINE 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 

Definition at line 69 of file ExtractFromTime.h.

Function Documentation

◆ extract_dow()

DEVICE int32_t extract_dow ( const int64_t  lcltime)

Definition at line 63 of file ExtractFromTime.cpp.

References kDaysPerWeek, kEpochAdjustedDays, kEpochAdjustedWDay, and kSecsPerDay.

Referenced by DateTruncate(), and ExtractFromTime().

63  {
64  int64_t days, rem;
65  int32_t weekday;
66  days = lcltime / kSecsPerDay - kEpochAdjustedDays;
67  rem = lcltime % kSecsPerDay;
68  if (rem < 0) {
69  rem += kSecsPerDay;
70  --days;
71  }
72 
73  if ((weekday = ((kEpochAdjustedWDay + days) % kDaysPerWeek)) < 0) {
74  weekday += kDaysPerWeek;
75  }
76  return weekday;
77 }
static constexpr int64_t kSecsPerDay
static constexpr int32_t kEpochAdjustedDays
static constexpr int32_t kDaysPerWeek
static constexpr int32_t kEpochAdjustedWDay
+ Here is the caller graph for this function:

◆ ExtractFromTime()

DEVICE NEVER_INLINE int64_t ExtractFromTime ( ExtractField  field,
const int64_t  timeval 
)

Definition at line 247 of file ExtractFromTime.cpp.

References extract_dow(), extract_hour(), extract_microsecond(), extract_millisecond(), extract_minute(), extract_month_fast(), extract_nanosecond(), extract_quarter_fast(), extract_quarterday(), extract_second(), extract_year_fast(), gmtime_r_newlib(), kDAY, kDOW, kDOY, kEPOCH, kEpochOffsetYear1900, kHOUR, kISODOW, kMICROSECOND, kMILLISECOND, kMINUTE, kMONTH, kNANOSECOND, kQUARTER, kQUARTERDAY, kSECOND, kWEEK, and kYEAR.

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

248  {
249  // We have fast paths for the 5 fields below - do not need to do full gmtime
250  switch (field) {
251  case kEPOCH:
252  return timeval;
253  case kQUARTERDAY:
254  return extract_quarterday(timeval);
255  case kHOUR:
256  return extract_hour(timeval);
257  case kMINUTE:
258  return extract_minute(timeval);
259  case kSECOND:
260  return extract_second(timeval);
261  case kMILLISECOND:
262  return extract_millisecond(timeval);
263  case kMICROSECOND:
264  return extract_microsecond(timeval);
265  case kNANOSECOND:
266  return extract_nanosecond(timeval);
267  case kDOW:
268  return extract_dow(timeval);
269  case kISODOW: {
270  int64_t dow = extract_dow(timeval);
271  return (dow == 0 ? 7 : dow);
272  }
273  case kMONTH: {
274  if (timeval >= 0L && timeval <= UINT32_MAX - kEpochOffsetYear1900) {
275  return extract_month_fast(timeval);
276  }
277  break;
278  }
279  case kQUARTER: {
280  if (timeval >= 0L && timeval <= UINT32_MAX - kEpochOffsetYear1900) {
281  return extract_quarter_fast(timeval);
282  }
283  break;
284  }
285  case kYEAR: {
286  if (timeval >= 0L && timeval <= UINT32_MAX - kEpochOffsetYear1900) {
287  return extract_year_fast(timeval);
288  }
289  break;
290  }
291  default:
292  break;
293  }
294 
295  tm tm_struct;
296  gmtime_r_newlib(timeval, tm_struct);
297  switch (field) {
298  case kYEAR:
299  return 1900 + tm_struct.tm_year;
300  case kQUARTER:
301  return (tm_struct.tm_mon) / 3 + 1;
302  case kMONTH:
303  return tm_struct.tm_mon + 1;
304  case kDAY:
305  return tm_struct.tm_mday;
306  case kDOY:
307  return tm_struct.tm_yday + 1;
308  case kWEEK: {
309  int32_t doy = tm_struct.tm_yday; // numbered from 0
310  int32_t dow = extract_dow(timeval) + 1; // use Sunday 1 - Saturday 7
311  int32_t week = (doy / 7) + 1;
312  // now adjust for offset at start of year
313  // S M T W T F S
314  // doy 0 1 2 3 4
315  // doy 5 6
316  // mod 5 6 0 1 2 3 4
317  // dow 1 2 3 4 5 6 7
318  // week 2 2 1 1 1 1 1
319  if (dow > (doy % 7)) {
320  return week;
321  }
322  return week + 1;
323  }
324  default:
325 #ifdef __CUDACC__
326  return -1;
327 #else
328  abort();
329 #endif
330  }
331 }
NEVER_INLINE DEVICE int32_t extract_hour(const int64_t lcltime)
DEVICE int32_t extract_quarter_fast(const int64_t lcltime)
DEVICE int64_t extract_nanosecond(const int64_t lcltime)
static constexpr uint32_t kEpochOffsetYear1900
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
DEVICE int32_t extract_dow(const int64_t lcltime)
DEVICE int32_t extract_second(const int64_t lcltime)
DEVICE int64_t extract_millisecond(const int64_t lcltime)
DEVICE tm gmtime_r_newlib(const int64_t lcltime, tm &res)
DEVICE int32_t extract_quarterday(const int64_t lcltime)
DEVICE int32_t extract_minute(const int64_t lcltime)
DEVICE int32_t extract_month_fast(const int64_t lcltime)
DEVICE int64_t extract_microsecond(const int64_t lcltime)
DEVICE int32_t extract_year_fast(const int64_t lcltime)
+ 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 150 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(), and ExtractFromTime().

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

◆ kEpochAdjustedWDay

constexpr int32_t kEpochAdjustedWDay = 3
static

Definition at line 49 of file ExtractFromTime.h.

Referenced by extract_dow(), and 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().