forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime.cc
More file actions
274 lines (242 loc) · 8.72 KB
/
Copy pathdatetime.cc
File metadata and controls
274 lines (242 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include <algorithm>
#include <chrono>
#include <iostream>
#include "arrow/python/common.h"
#include "arrow/python/datetime.h"
#include "arrow/python/platform.h"
#include "arrow/status.h"
#include "arrow/type.h"
#include "arrow/util/logging.h"
namespace arrow {
namespace py {
namespace internal {
PyDateTime_CAPI* datetime_api = nullptr;
void InitDatetime() {
PyAcquireGIL lock;
datetime_api =
reinterpret_cast<PyDateTime_CAPI*>(PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0));
if (datetime_api == nullptr) {
Py_FatalError("Could not import datetime C API");
}
}
// The following code is adapted from
// https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/datetime.c
// Days per month, regular year and leap year
static int64_t _days_per_month_table[2][12] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
static bool is_leapyear(int64_t year) {
return (year & 0x3) == 0 && // year % 4 == 0
((year % 100) != 0 || (year % 400) == 0);
}
// Calculates the days offset from the 1970 epoch.
static int64_t get_days_from_date(int64_t date_year, int64_t date_month,
int64_t date_day) {
int64_t i, month;
int64_t year, days = 0;
int64_t* month_lengths;
year = date_year - 1970;
days = year * 365;
// Adjust for leap years
if (days >= 0) {
// 1968 is the closest leap year before 1970.
// Exclude the current year, so add 1.
year += 1;
// Add one day for each 4 years
days += year / 4;
// 1900 is the closest previous year divisible by 100
year += 68;
// Subtract one day for each 100 years
days -= year / 100;
// 1600 is the closest previous year divisible by 400
year += 300;
// Add one day for each 400 years
days += year / 400;
} else {
// 1972 is the closest later year after 1970.
// Include the current year, so subtract 2.
year -= 2;
// Subtract one day for each 4 years
days += year / 4;
// 2000 is the closest later year divisible by 100
year -= 28;
// Add one day for each 100 years
days -= year / 100;
// 2000 is also the closest later year divisible by 400
// Subtract one day for each 400 years
days += year / 400;
}
month_lengths = _days_per_month_table[is_leapyear(date_year)];
month = date_month - 1;
// Add the months
for (i = 0; i < month; ++i) {
days += month_lengths[i];
}
// Add the days
days += date_day - 1;
return days;
}
// Modifies '*days_' to be the day offset within the year,
// and returns the year.
static int64_t days_to_yearsdays(int64_t* days_) {
const int64_t days_per_400years = (400 * 365 + 100 - 4 + 1);
// Adjust so it's relative to the year 2000 (divisible by 400)
int64_t days = (*days_) - (365 * 30 + 7);
int64_t year;
// Break down the 400 year cycle to get the year and day within the year
if (days >= 0) {
year = 400 * (days / days_per_400years);
days = days % days_per_400years;
} else {
year = 400 * ((days - (days_per_400years - 1)) / days_per_400years);
days = days % days_per_400years;
if (days < 0) {
days += days_per_400years;
}
}
// Work out the year/day within the 400 year cycle
if (days >= 366) {
year += 100 * ((days - 1) / (100 * 365 + 25 - 1));
days = (days - 1) % (100 * 365 + 25 - 1);
if (days >= 365) {
year += 4 * ((days + 1) / (4 * 365 + 1));
days = (days + 1) % (4 * 365 + 1);
if (days >= 366) {
year += (days - 1) / 365;
days = (days - 1) % 365;
}
}
}
*days_ = days;
return year + 2000;
}
// Extracts the month and year and day number from a number of days
static void get_date_from_days(int64_t days, int64_t* date_year, int64_t* date_month,
int64_t* date_day) {
int64_t *month_lengths, i;
*date_year = days_to_yearsdays(&days);
month_lengths = _days_per_month_table[is_leapyear(*date_year)];
for (i = 0; i < 12; ++i) {
if (days < month_lengths[i]) {
*date_month = i + 1;
*date_day = days + 1;
return;
} else {
days -= month_lengths[i];
}
}
// Should never get here
return;
}
// Splitting time quantities, for example splitting total seconds into
// minutes and remaining seconds. After we run
// int64_t remaining = split_time(total, quotient, &next)
// we have
// total = next * quotient + remaining. Handles negative values by propagating
// them: If total is negative, next will be negative and remaining will
// always be non-negative.
static inline int64_t split_time(int64_t total, int64_t quotient, int64_t* next) {
int64_t r = total % quotient;
if (r < 0) {
*next = total / quotient - 1;
return r + quotient;
} else {
*next = total / quotient;
return r;
}
}
static inline Status PyTime_convert_int(int64_t val, const TimeUnit::type unit,
int64_t* hour, int64_t* minute, int64_t* second,
int64_t* microsecond) {
switch (unit) {
case TimeUnit::NANO:
if (val % 1000 != 0) {
return Status::Invalid("Value ", val, " has non-zero nanoseconds");
}
val /= 1000;
// fall through
case TimeUnit::MICRO:
*microsecond = split_time(val, 1000000LL, &val);
*second = split_time(val, 60, &val);
*minute = split_time(val, 60, hour);
break;
case TimeUnit::MILLI:
*microsecond = split_time(val, 1000, &val) * 1000;
// fall through
case TimeUnit::SECOND:
*second = split_time(val, 60, &val);
*minute = split_time(val, 60, hour);
break;
default:
break;
}
return Status::OK();
}
static inline Status PyDate_convert_int(int64_t val, const DateUnit unit, int64_t* year,
int64_t* month, int64_t* day) {
switch (unit) {
case DateUnit::MILLI:
val /= 86400000LL; // fall through
case DateUnit::DAY:
get_date_from_days(val, year, month, day);
default:
break;
}
return Status::OK();
}
Status PyTime_from_int(int64_t val, const TimeUnit::type unit, PyObject** out) {
int64_t hour = 0, minute = 0, second = 0, microsecond = 0;
RETURN_NOT_OK(PyTime_convert_int(val, unit, &hour, &minute, &second, µsecond));
*out = PyTime_FromTime(static_cast<int32_t>(hour), static_cast<int32_t>(minute),
static_cast<int32_t>(second), static_cast<int32_t>(microsecond));
return Status::OK();
}
Status PyDate_from_int(int64_t val, const DateUnit unit, PyObject** out) {
int64_t year = 0, month = 0, day = 0;
RETURN_NOT_OK(PyDate_convert_int(val, unit, &year, &month, &day));
*out = PyDate_FromDate(static_cast<int32_t>(year), static_cast<int32_t>(month),
static_cast<int32_t>(day));
return Status::OK();
}
Status PyDateTime_from_int(int64_t val, const TimeUnit::type unit, PyObject** out) {
int64_t hour = 0, minute = 0, second = 0, microsecond = 0;
RETURN_NOT_OK(PyTime_convert_int(val, unit, &hour, &minute, &second, µsecond));
int64_t total_days = 0;
hour = split_time(hour, 24, &total_days);
int64_t year = 0, month = 0, day = 0;
get_date_from_days(total_days, &year, &month, &day);
*out = PyDateTime_FromDateAndTime(
static_cast<int32_t>(year), static_cast<int32_t>(month), static_cast<int32_t>(day),
static_cast<int32_t>(hour), static_cast<int32_t>(minute),
static_cast<int32_t>(second), static_cast<int32_t>(microsecond));
return Status::OK();
}
Status PyDateTime_from_TimePoint(TimePoint val, PyObject** out) {
auto nanos = val.time_since_epoch();
auto micros = std::chrono::duration_cast<std::chrono::microseconds>(nanos);
RETURN_NOT_OK(PyDateTime_from_int(micros.count(), TimeUnit::MICRO, out));
return Status::OK();
}
int64_t PyDate_to_days(PyDateTime_Date* pydate) {
return get_days_from_date(PyDateTime_GET_YEAR(pydate), PyDateTime_GET_MONTH(pydate),
PyDateTime_GET_DAY(pydate));
}
} // namespace internal
} // namespace py
} // namespace arrow