OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExtractFromTime.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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 #ifndef QUERYENGINE_EXTRACTFROMTIME_H
18 #define QUERYENGINE_EXTRACTFROMTIME_H
19 
20 #include <cstdint>
21 #include <ctime>
22 /* `../` is required for UDFCompiler */
23 #include "../Shared/funcannotations.h"
24 
25 static constexpr int64_t kNanoSecsPerSec = 1000000000;
26 static constexpr int64_t kMicroSecsPerSec = 1000000;
27 static constexpr int64_t kMilliSecsPerSec = 1000;
28 static constexpr int64_t kMilliSecsPerMin = 60000;
29 static constexpr int64_t kMilliSecsPerHour = 3600000;
30 static constexpr int64_t kMilliSecsPerDay = 86400000;
31 static constexpr int64_t kSecsPerMin = 60;
32 static constexpr int64_t kMinsPerHour = 60;
33 static constexpr int64_t kHoursPerDay = 24;
34 static constexpr int64_t kSecsPerHour = 3600;
35 static constexpr int64_t kSecsPerDay = 86400;
36 static constexpr int64_t kSecsPerQuarterDay = 21600;
37 static constexpr int32_t kDaysPerWeek = 7;
38 static constexpr int32_t kMonsPerYear = 12;
39 static constexpr int64_t kSecsPerHalfDay = 43200;
40 static constexpr int64_t kMinsPerMonth = 43200; // Month of 30 days
41 
42 static constexpr int32_t kYearBase = 1900;
43 
44 /* move epoch from 01.01.1970 to 01.03.2000 - this is the first day of new
45  * 400-year long cycle, right after additional day of leap year. This adjustment
46  * is required only for date calculation, so instead of modifying time value
47  * (which would require 64-bit operations to work correctly) it's enough to
48  * adjust the calculated number of days since epoch. */
49 static constexpr int32_t kEpochAdjustedDays = 11017;
50 /* year to which the adjustment was made */
51 static constexpr int32_t kEpochAdjustedYears = 2000;
52 /* 1st March of 2000 is Wednesday */
53 static constexpr int32_t kEpochAdjustedWDay = 3;
54 /* there are 97 leap years in 400-year periods. ((400 - 97) * 365 + 97 * 366) */
55 static constexpr int64_t kDaysPer400Years = 146097;
56 /* there are 24 leap years in 100-year periods. ((100 - 24) * 365 + 24 * 366) */
57 static constexpr int64_t kDaysPer100Years = 36524;
58 /* there is one leap year every 4 years */
59 static constexpr int32_t kDaysPer4Years = 3 * 365 + 366;
60 /* number of days in a non-leap year */
61 static constexpr int32_t kDaysPerYear = 365;
62 /* number of days in January */
63 static constexpr int32_t kDaysInJanuary = 31;
64 /* number of days in non-leap February */
65 static constexpr int32_t kDaysInFebruary = 28;
66 
67 static constexpr uint32_t kSecondsPerNonLeapYear = 31536000;
68 static constexpr uint32_t kSecondsPer4YearCycle = 126230400;
69 static constexpr uint32_t kUSecsPerDay = 86400;
70 static constexpr uint32_t kEpochOffsetYear1900 = 2208988800;
71 static constexpr uint32_t kSecsJanToMar1900 = 5097600;
72 
73 // Number of days from March 1 to Jan 1.
74 constexpr unsigned MARJAN = 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31;
75 constexpr unsigned JANMAR = 31 + 28; // leap day handled separately
76 
98 };
99 
100 DEVICE int64_t ExtractFromTime(ExtractField field, const int64_t timeval);
101 
102 // Return floor(dividend / divisor).
103 // Assumes 0 < divisor.
104 DEVICE inline int64_t floor_div(int64_t const dividend, int64_t const divisor) {
105  return (dividend < 0 ? dividend - (divisor - 1) : dividend) / divisor;
106 }
107 
108 // Return remainer r of dividend / divisor, where 0 <= r < divisor.
109 // Assumes 0 < divisor.
110 // The uint64_t casts are potential optimizations, since unsigned integer division is
111 // faster on some architectures than signed division. However in the case when
112 // dividend == std::numeric_limits<int64_t>::min() then it is uniquely required.
113 // Additionally this avoids architecture-dependent behavior of % when numerator < 0.
114 DEVICE inline int64_t unsigned_mod(int64_t const dividend, int64_t const divisor) {
115  if (dividend < 0) {
116  int64_t const mod = static_cast<int64_t>(uint64_t(-dividend) % uint64_t(divisor));
117  return mod ? divisor - mod : int64_t(0);
118  }
119  return static_cast<int64_t>(uint64_t(dividend) % uint64_t(divisor));
120 }
121 
122 #endif // QUERYENGINE_EXTRACTFROMTIME_H
static constexpr int64_t kSecsPerDay
static constexpr uint32_t kSecsJanToMar1900
static constexpr uint32_t kUSecsPerDay
static constexpr int64_t kSecsPerHour
static constexpr int32_t kDaysPerYear
static constexpr int64_t kSecsPerMin
static constexpr int64_t kNanoSecsPerSec
static constexpr int64_t kMilliSecsPerDay
constexpr unsigned JANMAR
static constexpr int64_t kSecsPerQuarterDay
static constexpr int64_t kMinsPerHour
DEVICE int64_t floor_div(int64_t const dividend, int64_t const divisor)
static constexpr int32_t kDaysInJanuary
static constexpr uint32_t kEpochOffsetYear1900
static constexpr int32_t kYearBase
static constexpr int64_t kSecsPerHalfDay
#define DEVICE
static constexpr int64_t kMilliSecsPerMin
static constexpr int64_t kMinsPerMonth
static constexpr int64_t kMilliSecsPerSec
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:33
static constexpr int32_t kEpochAdjustedDays
static constexpr int32_t kDaysPerWeek
static constexpr int32_t kDaysInFebruary
static constexpr int32_t kEpochAdjustedYears
static constexpr uint32_t kSecondsPer4YearCycle
static constexpr int64_t kHoursPerDay
static constexpr int64_t kDaysPer400Years
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
static constexpr int32_t kDaysPer4Years
static constexpr int64_t kMilliSecsPerHour
static constexpr uint32_t kSecondsPerNonLeapYear
static constexpr int64_t kMicroSecsPerSec
static constexpr int32_t kEpochAdjustedWDay
static constexpr int32_t kMonsPerYear
static constexpr int64_t kDaysPer100Years