-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathhelpers.py
More file actions
309 lines (235 loc) · 7.08 KB
/
helpers.py
File metadata and controls
309 lines (235 loc) · 7.08 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# -*- coding: utf-8 -*-
import pendulum
from math import copysign
from datetime import datetime, timedelta
try:
from ._extensions._helpers import local_time, parse_iso8601 as _parse_iso8601
def parse_iso8601(text, day_first=False):
return _parse_iso8601(text, day_first)
except ImportError:
from ._extensions.helpers import local_time
parse_iso8601 = None
from .constants import (
DAYS_PER_MONTHS, DAY_OF_WEEK_TABLE, DAYS_PER_L_YEAR, DAYS_PER_N_YEAR
)
def is_leap(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
def add_duration(dt, years=0, months=0, weeks=0, days=0,
hours=0, minutes=0, seconds=0, microseconds=0):
"""
Adds a duration to a datetime instance.
:param dt: The datetime instance
:type dt: datetime.datetime
:param years: The number of years
:type years: int
:param months: The number of months
:type months: int
:param weeks: The number of weeks
:type weeks: int
:param days: The number of days
:type days: int
:param hours: The number of hours
:type hours: int
:param minutes: The number of minutes
:type minutes: int
:param seconds: The number of seconds
:type seconds: int
:param microseconds: The number of microseconds
:type microseconds: int
:rtype: datetime.datetime
"""
days += weeks * 7
# Normalizing
if abs(microseconds) > 999999:
s = _sign(microseconds)
div, mod = divmod(microseconds * s, 1000000)
microseconds = mod * s
seconds += div * s
if abs(seconds) > 59:
s = _sign(seconds)
div, mod = divmod(seconds * s, 60)
seconds = mod * s
minutes += div * s
if abs(minutes) > 59:
s = _sign(minutes)
div, mod = divmod(minutes * s, 60)
minutes = mod * s
hours += div * s
if abs(hours) > 23:
s = _sign(hours)
div, mod = divmod(hours * s, 24)
hours = mod * s
days += div * s
if abs(months) > 11:
s = _sign(months)
div, mod = divmod(months * s, 12)
months = mod * s
years += div * s
year = dt.year + years
month = dt.month
if months:
month += months
if month > 12:
year += 1
month -= 12
elif month < 1:
year -= 1
month += 12
day = min(DAYS_PER_MONTHS[int(is_leap(year))][month], dt.day)
dt = dt.replace(year=year, month=month, day=day)
return dt + timedelta(
days=days,
hours=hours,
minutes=minutes,
seconds=seconds,
microseconds=microseconds
)
def precise_diff(d1, d2):
"""
Calculate a precise difference between two datetimes.
:param d1: The first datetime
:type d1: pendulum.Pendulum or pendulum.Date
:param d2: The second datetime
:type d2: pendulum.Pendulum or pendulum.Date
:rtype: dict
"""
diff = {
'years': 0,
'months': 0,
'days': 0,
'hours': 0,
'minutes': 0,
'seconds': 0,
'microseconds': 0,
'total': {
'days': 0
}
}
sign = 1
if d1 == d2:
return diff
tzinfo1 = d1.tzinfo if isinstance(d1, datetime) else None
tzinfo2 = d2.tzinfo if isinstance(d2, datetime) else None
if (tzinfo1 is None and tzinfo2 is not None
or tzinfo2 is None and tzinfo1 is not None):
raise ValueError(
'Comparison between naive and aware datetimes is not supported'
)
if d1 > d2:
d1, d2 = d2, d1
sign = -1
y_diff = d2.year - d1.year
m_diff = d2.month - d1.month
d_diff = d2.day - d1.day
hour_diff = 0
min_diff = 0
sec_diff = 0
mic_diff = 0
total_days = (
_day_number(d2.year, d2.month, d2.day)
- _day_number(d1.year, d1.month, d1.day)
)
in_same_tz = False
tz1 = None
tz2 = None
# Trying to figure out the timezone names
# If we can't find them, we assume different timezones
if tzinfo1 and tzinfo2:
if hasattr(tzinfo1, 'name'):
# Pendulum timezone
tz1 = tzinfo1.name
elif hasattr(tzinfo1, 'zone'):
# pytz timezone
tz1 = tzinfo1.zone
if hasattr(tzinfo2, 'name'):
tz2 = tzinfo2.name
elif hasattr(tzinfo2, 'zone'):
tz2 = tzinfo2.zone
in_same_tz = tz1 == tz2 and tz1 is not None
if hasattr(d2, 'hour'):
# If we are not in the same timezone
# we need to adjust
if not in_same_tz:
offset1 = d1.utcoffset()
offset2 = d2.utcoffset()
if offset1:
d1 = d1 - offset1
if offset2:
d2 = d2 - offset2
hour_diff = d2.hour - d1.hour
min_diff = d2.minute - d1.minute
sec_diff = d2.second - d1.second
mic_diff = d2.microsecond - d1.microsecond
if mic_diff < 0:
mic_diff += 1000000
sec_diff -= 1
if sec_diff < 0:
sec_diff += 60
min_diff -= 1
if min_diff < 0:
min_diff += 60
hour_diff -= 1
if hour_diff < 0:
hour_diff += 24
d_diff -= 1
if d_diff < 0:
year = d2.year
month = d2.month
if month == 1:
month = 12
year -= 1
else:
month -= 1
leap = int(is_leap(year))
days_in_last_month = DAYS_PER_MONTHS[leap][month]
days_in_month = DAYS_PER_MONTHS[int(is_leap(d2.year))][d2.month]
if d_diff < days_in_month - days_in_last_month:
# We don't have a full month, we calculate days
if days_in_last_month < d1.day:
d_diff += d1.day
else:
d_diff += days_in_last_month
elif d_diff == days_in_month - days_in_last_month:
# We have exactly a full month
# We remove the days difference
# and add one to the months difference
d_diff = 0
m_diff += 1
else:
# We have a full month
d_diff += days_in_last_month
m_diff -= 1
if m_diff < 0:
m_diff += 12
y_diff -= 1
diff['microseconds'] = sign * mic_diff
diff['seconds'] = sign * sec_diff
diff['minutes'] = sign * min_diff
diff['hours'] = sign * hour_diff
diff['days'] = sign * d_diff
diff['months'] = sign * m_diff
diff['years'] = sign * y_diff
diff['total']['days'] = sign * total_days
return diff
def week_day(year, month, day):
if month < 3:
year -= 1
w = (year + year//4 - year//100 + year//400 + DAY_OF_WEEK_TABLE[month - 1] + day) % 7
if not w:
w = 7
return w
def days_in_year(year):
if is_leap(year):
return DAYS_PER_L_YEAR
return DAYS_PER_N_YEAR
def _sign(x):
return int(copysign(1, x))
def _day_number(year, month, day):
month = (month + 9) % 12
year = year - month // 10
return (
365 * year
+ year // 4 - year // 100 + year // 400
+ (month * 306 + 5) // 10
+ (day - 1 )
)