OmniSciDB  bf83d84833
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ExtractFromTime.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * Thank you Howard Hinnant for public domain date algorithms
19  * http://howardhinnant.github.io/date_algorithms.html
20  */
21 
22 #include "ExtractFromTime.h"
23 
24 #ifndef __CUDACC__
25 #include <cstdlib> // abort()
26 #endif
27 
28 namespace {
29 
30 // Number of days until Wednesday (because 2000-03-01 is a Wednesday.)
31 constexpr unsigned MONDAY = 2;
32 constexpr unsigned SUNDAY = 3;
33 constexpr unsigned SATURDAY = 4;
34 
35 // If OFFSET=MONDAY,
36 // then return day-of-era of the Monday of ISO 8601 week 1 in the given year-of-era.
37 // Similarly for SUNDAY and SATURDAY. In all cases, week 1 always contains Jan 4.
38 template <unsigned OFFSET>
39 DEVICE unsigned week_start_from_yoe(unsigned const yoe) {
40  unsigned const march1 = yoe * 365 + yoe / 4 - yoe / 100;
41  unsigned const jan4 = march1 + (MARJAN + 3);
42  unsigned const jan4dow = (jan4 + OFFSET) % 7;
43  return jan4 - jan4dow;
44 }
45 
46 } // namespace
47 
48 extern "C" ALWAYS_INLINE DEVICE int64_t extract_hour(const int64_t lcltime) {
49  return unsigned_mod(lcltime, kSecsPerDay) / kSecsPerHour;
50 }
51 
52 extern "C" ALWAYS_INLINE DEVICE int64_t extract_minute(const int64_t lcltime) {
53  return unsigned_mod(lcltime, kSecsPerHour) / kSecsPerMin;
54 }
55 
56 extern "C" ALWAYS_INLINE DEVICE int64_t extract_second(const int64_t lcltime) {
57  return unsigned_mod(lcltime, kSecsPerMin);
58 }
59 
60 extern "C" ALWAYS_INLINE DEVICE int64_t extract_millisecond(const int64_t lcltime) {
61  return unsigned_mod(lcltime, kSecsPerMin * kMilliSecsPerSec);
62 }
63 
64 extern "C" ALWAYS_INLINE DEVICE int64_t extract_microsecond(const int64_t lcltime) {
65  return unsigned_mod(lcltime, kSecsPerMin * kMicroSecsPerSec);
66 }
67 
68 extern "C" ALWAYS_INLINE DEVICE int64_t extract_nanosecond(const int64_t lcltime) {
69  return unsigned_mod(lcltime, kSecsPerMin * kNanoSecsPerSec);
70 }
71 
72 // First day of epoch is Thursday, so + 4 to have Sunday=0.
73 extern "C" ALWAYS_INLINE DEVICE int64_t extract_dow(const int64_t lcltime) {
74  int64_t const days_past_epoch = floor_div(lcltime, kSecsPerDay);
75  return unsigned_mod(days_past_epoch + 4, kDaysPerWeek);
76 }
77 
78 extern "C" ALWAYS_INLINE DEVICE int64_t extract_quarterday(const int64_t lcltime) {
79  return unsigned_mod(lcltime, kSecsPerDay) / kSecsPerQuarterDay + 1;
80 }
81 
82 DEVICE int32_t extract_month_fast(const int64_t lcltime) {
83  STATIC_QUAL const uint32_t cumulative_month_epoch_starts[kMonsPerYear] = {0,
84  2678400,
85  5270400,
86  7948800,
87  10540800,
88  13219200,
89  15897600,
90  18489600,
91  21168000,
92  23760000,
93  26438400,
94  29116800};
95  uint32_t seconds_march_1900 = lcltime + kEpochOffsetYear1900 - kSecsJanToMar1900;
96  uint32_t seconds_past_4year_period = seconds_march_1900 % 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 be
107  // 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 (month + 2) % 12 + 1;
113 }
114 
115 DEVICE int32_t extract_quarter_fast(const int64_t lcltime) {
116  STATIC_QUAL const uint32_t cumulative_quarter_epoch_starts[4] = {
117  0, 7776000, 15638400, 23587200};
118  STATIC_QUAL const uint32_t cumulative_quarter_epoch_starts_leap_year[4] = {
119  0, 7862400, 15724800, 23673600};
120  uint32_t seconds_1900 = lcltime + kEpochOffsetYear1900;
121  uint32_t leap_years = (seconds_1900 - kSecsJanToMar1900) / kSecondsPer4YearCycle;
122  uint32_t year = (seconds_1900 - leap_years * kSecsPerDay) / kSecondsPerNonLeapYear;
123  uint32_t base_year_leap_years = (year - 1) / 4;
124  uint32_t base_year_seconds =
125  year * kSecondsPerNonLeapYear + base_year_leap_years * kUSecsPerDay;
126  bool is_leap_year = year % 4 == 0 && year != 0;
127  const uint32_t* quarter_offsets = is_leap_year
128  ? cumulative_quarter_epoch_starts_leap_year
129  : cumulative_quarter_epoch_starts;
130  uint32_t partial_year_seconds = seconds_1900 % base_year_seconds;
131  uint32_t quarter = partial_year_seconds / (90 * kUSecsPerDay);
132  quarter = quarter <= 3 ? quarter : 3;
133  if (quarter_offsets[quarter] > partial_year_seconds) {
134  quarter--;
135  }
136  return quarter + 1;
137 }
138 
139 DEVICE int32_t extract_year_fast(const int64_t lcltime) {
140  const uint32_t seconds_1900 = lcltime + kEpochOffsetYear1900;
141  const uint32_t leap_years = (seconds_1900 - kSecsJanToMar1900) / kSecondsPer4YearCycle;
142  const uint32_t year =
143  (seconds_1900 - leap_years * kUSecsPerDay) / kSecondsPerNonLeapYear + 1900;
144  return year;
145 }
146 
147 extern "C" ALWAYS_INLINE DEVICE int64_t extract_epoch(const int64_t timeval) {
148  return timeval;
149 }
150 
151 extern "C" ALWAYS_INLINE DEVICE int64_t extract_dateepoch(const int64_t timeval) {
152  return timeval - unsigned_mod(timeval, kSecsPerDay);
153 }
154 
155 // First day of epoch is Thursday, so + 3 to have Monday=0, then + 1 at the end.
156 extern "C" ALWAYS_INLINE DEVICE int64_t extract_isodow(const int64_t timeval) {
157  int64_t const days_past_epoch = floor_div(timeval, kSecsPerDay);
158  return unsigned_mod(days_past_epoch + 3, kDaysPerWeek) + 1;
159 }
160 
161 extern "C" ALWAYS_INLINE DEVICE int64_t extract_day(const int64_t timeval) {
162  int64_t const day = floor_div(timeval, kSecsPerDay);
163  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
164  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
165  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
166  unsigned const moy = (5 * doy + 2) / 153;
167  return doy - (153 * moy + 2) / 5 + 1;
168 }
169 
170 extern "C" ALWAYS_INLINE DEVICE int64_t extract_day_of_year(const int64_t timeval) {
171  int64_t const day = floor_div(timeval, kSecsPerDay);
172  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
173  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
174  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
175  return doy + (doy < MARJAN ? 1 + JANMAR + (yoe % 4 == 0 && (yoe % 100 != 0 || yoe == 0))
176  : 1 - MARJAN);
177 }
178 
179 template <unsigned OFFSET>
180 ALWAYS_INLINE DEVICE int64_t extract_week(const int64_t timeval) {
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 week_start = week_start_from_yoe<OFFSET>(yoe);
185  if (doe < week_start) {
186  if (yoe == 0) {
187  // 2000-03-01 is OFFSET days from start of week, week + 9.
188  return (doe + OFFSET) / 7 + 9;
189  } else {
190  week_start = week_start_from_yoe<OFFSET>(yoe - 1);
191  }
192  }
193  return (doe - week_start) / 7 + 1;
194 }
195 
196 extern "C" ALWAYS_INLINE DEVICE int64_t extract_week_monday(const int64_t timeval) {
197  return extract_week<MONDAY>(timeval);
198 }
199 
200 extern "C" ALWAYS_INLINE DEVICE int64_t extract_week_sunday(const int64_t timeval) {
201  return extract_week<SUNDAY>(timeval);
202 }
203 
204 extern "C" ALWAYS_INLINE DEVICE int64_t extract_week_saturday(const int64_t timeval) {
205  return extract_week<SATURDAY>(timeval);
206 }
207 
208 extern "C" ALWAYS_INLINE DEVICE int64_t extract_month(const int64_t timeval) {
209  if (timeval >= 0L && timeval <= UINT32_MAX - kEpochOffsetYear1900) {
210  return extract_month_fast(timeval);
211  }
212  int64_t const day = floor_div(timeval, kSecsPerDay);
213  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
214  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
215  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
216  unsigned const moy = (5 * doy + 2) / 153;
217  return moy + (moy < 10 ? 3 : -9);
218 }
219 
220 extern "C" ALWAYS_INLINE DEVICE int64_t extract_quarter(const int64_t timeval) {
221  if (timeval >= 0L && timeval <= UINT32_MAX - kEpochOffsetYear1900) {
222  return extract_quarter_fast(timeval);
223  }
224  constexpr int64_t quarter[12]{1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 1, 1};
225  int64_t const day = floor_div(timeval, kSecsPerDay);
226  unsigned const doe = unsigned_mod(day - kEpochAdjustedDays, kDaysPer400Years);
227  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
228  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
229  unsigned const moy = (5 * doy + 2) / 153;
230  return quarter[moy];
231 }
232 
233 extern "C" ALWAYS_INLINE DEVICE int64_t extract_year(const int64_t timeval) {
234  if (timeval >= 0L && timeval <= UINT32_MAX - kEpochOffsetYear1900) {
235  return extract_year_fast(timeval);
236  }
237  int64_t const day = floor_div(timeval, kSecsPerDay);
238  int64_t const era = floor_div(day - kEpochAdjustedDays, kDaysPer400Years);
239  unsigned const doe = day - kEpochAdjustedDays - era * kDaysPer400Years;
240  unsigned const yoe = (doe - doe / 1460 + doe / 36524 - (doe == 146096)) / 365;
241  unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
242  return 2000 + era * 400 + yoe + (MARJAN <= doy);
243 }
244 
245 /*
246  * @brief support the SQL EXTRACT function
247  */
248 DEVICE int64_t ExtractFromTime(ExtractField field, const int64_t timeval) {
249  switch (field) {
250  case kEPOCH:
251  return extract_epoch(timeval);
252  case kDATEEPOCH:
253  return extract_dateepoch(timeval);
254  case kQUARTERDAY:
255  return extract_quarterday(timeval);
256  case kHOUR:
257  return extract_hour(timeval);
258  case kMINUTE:
259  return extract_minute(timeval);
260  case kSECOND:
261  return extract_second(timeval);
262  case kMILLISECOND:
263  return extract_millisecond(timeval);
264  case kMICROSECOND:
265  return extract_microsecond(timeval);
266  case kNANOSECOND:
267  return extract_nanosecond(timeval);
268  case kDOW:
269  return extract_dow(timeval);
270  case kISODOW:
271  return extract_isodow(timeval);
272  case kDAY:
273  return extract_day(timeval);
274  case kWEEK:
275  return extract_week_monday(timeval);
276  case kWEEK_SUNDAY:
277  return extract_week_sunday(timeval);
278  case kWEEK_SATURDAY:
279  return extract_week_saturday(timeval);
280  case kDOY:
281  return extract_day_of_year(timeval);
282  case kMONTH:
283  return extract_month(timeval);
284  case kQUARTER:
285  return extract_quarter(timeval);
286  case kYEAR:
287  return extract_year(timeval);
288  }
289 
290 #ifdef __CUDACC__
291  return -1;
292 #else
293  abort();
294 #endif
295 }
static constexpr int64_t kSecsPerDay
static constexpr uint32_t kSecsJanToMar1900
static constexpr uint32_t kUSecsPerDay
ALWAYS_INLINE DEVICE int64_t extract_second(const int64_t lcltime)
ALWAYS_INLINE DEVICE int64_t extract_nanosecond(const int64_t lcltime)
DEVICE int32_t extract_quarter_fast(const int64_t lcltime)
static constexpr int64_t kSecsPerHour
static constexpr int64_t kSecsPerMin
static constexpr int64_t kNanoSecsPerSec
ALWAYS_INLINE DEVICE int64_t extract_month(const int64_t timeval)
constexpr unsigned JANMAR
ALWAYS_INLINE DEVICE int64_t extract_week(const int64_t timeval)
ALWAYS_INLINE DEVICE int64_t extract_isodow(const int64_t timeval)
static constexpr int64_t kSecsPerQuarterDay
ALWAYS_INLINE DEVICE int64_t extract_day_of_year(const int64_t timeval)
#define STATIC_QUAL
ALWAYS_INLINE DEVICE int64_t extract_minute(const int64_t lcltime)
DEVICE int64_t floor_div(int64_t const dividend, int64_t const divisor)
ALWAYS_INLINE DEVICE int64_t extract_quarterday(const int64_t lcltime)
ALWAYS_INLINE DEVICE int64_t extract_week_sunday(const int64_t timeval)
ALWAYS_INLINE DEVICE int64_t extract_quarter(const int64_t timeval)
ALWAYS_INLINE DEVICE int64_t extract_dow(const int64_t lcltime)
static constexpr uint32_t kEpochOffsetYear1900
ALWAYS_INLINE DEVICE int64_t extract_week_monday(const int64_t timeval)
#define DEVICE
static constexpr int64_t kMilliSecsPerSec
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
ALWAYS_INLINE DEVICE int64_t extract_dateepoch(const int64_t timeval)
ALWAYS_INLINE DEVICE int64_t extract_year(const int64_t timeval)
DEVICE unsigned week_start_from_yoe(unsigned const yoe)
ALWAYS_INLINE DEVICE int64_t extract_week_saturday(const int64_t timeval)
static constexpr int32_t kEpochAdjustedDays
ALWAYS_INLINE DEVICE int64_t extract_microsecond(const int64_t lcltime)
ALWAYS_INLINE DEVICE int64_t extract_hour(const int64_t lcltime)
static constexpr int32_t kDaysPerWeek
static constexpr uint32_t kSecondsPer4YearCycle
ALWAYS_INLINE DEVICE int64_t extract_epoch(const int64_t timeval)
static constexpr int64_t kDaysPer400Years
ALWAYS_INLINE DEVICE int64_t extract_millisecond(const int64_t lcltime)
constexpr unsigned MARJAN
DEVICE int64_t unsigned_mod(int64_t const dividend, int64_t const divisor)
DEVICE int64_t ExtractFromTime(ExtractField field, const int64_t timeval)
ExtractField
DEVICE int32_t extract_month_fast(const int64_t lcltime)
ALWAYS_INLINE DEVICE int64_t extract_day(const int64_t timeval)
static constexpr uint32_t kSecondsPerNonLeapYear
#define ALWAYS_INLINE
static constexpr int64_t kMicroSecsPerSec
static constexpr int32_t kMonsPerYear
DEVICE int32_t extract_year_fast(const int64_t lcltime)