FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pendulum/pendulum/interval.py at develop · python-pendulum/pendulum · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
python-pendulum
/
pendulum
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
445
Star
6.7k
Code
Issues
206
Pull requests
60
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pendulum
/
pendulum
/
interval.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
309 lines (224 loc) · 7.79 KB
Breadcrumbs
pendulum
/
pendulum
/
interval.py
Copy path
File metadata and controls
309 lines (224 loc) · 7.79 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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 -*-
from
datetime
import
timedelta
from
.
mixins
.
interval
import
(
WordableIntervalMixin
)
from
.
constants
import
(
SECONDS_PER_DAY
,
SECONDS_PER_HOUR
,
SECONDS_PER_MINUTE
)
def
_divide_and_round
(
a
,
b
):
"""divide a by b and round result to the nearest integer
When the ratio is exactly half-way between two integers,
the even integer is returned.
"""
# Based on the reference implementation for divmod_near
# in Objects/longobject.c.
q
,
r
=
divmod
(
a
,
b
)
# round up if either r / b > 0.5, or r / b == 0.5 and q is odd.
# The expression r / b > 0.5 is equivalent to 2 * r > b if b is
# positive, 2 * r < b if b negative.
r
*=
2
greater_than_half
=
r
>
b
if
b
>
0
else
r
<
b
if
greater_than_half
or
r
==
b
and
q
%
2
==
1
:
q
+=
1
return
q
class
BaseInterval
(
timedelta
):
"""
Base class for all inherited interval classes.
"""
_y
=
None
_m
=
None
_w
=
None
_d
=
None
_h
=
None
_i
=
None
_s
=
None
_invert
=
None
def
__new__
(
cls
,
days
=
0
,
seconds
=
0
,
microseconds
=
0
,
milliseconds
=
0
,
minutes
=
0
,
hours
=
0
,
weeks
=
0
):
self
=
timedelta
.
__new__
(
cls
,
days
,
seconds
,
microseconds
,
milliseconds
,
minutes
,
hours
,
weeks
)
# Intuitive normalization
total
=
self
.
total_seconds
()
m
=
1
if
total
<
0
:
m
=
-
1
self
.
_microseconds
=
round
(
total
%
m
*
1e6
)
self
.
_seconds
=
abs
(
int
(
total
))
%
SECONDS_PER_DAY
*
m
self
.
_days
=
abs
(
int
(
total
))
//
SECONDS_PER_DAY
*
m
return
self
def
total_minutes
(
self
):
return
self
.
total_seconds
()
/
SECONDS_PER_MINUTE
def
total_hours
(
self
):
return
self
.
total_seconds
()
/
SECONDS_PER_HOUR
def
total_days
(
self
):
return
self
.
total_seconds
()
/
SECONDS_PER_DAY
def
total_weeks
(
self
):
return
self
.
total_days
()
/
7
@
property
def
weeks
(
self
):
return
abs
(
self
.
days
)
//
7
*
self
.
_sign
(
self
.
_days
)
@
property
def
days
(
self
):
return
self
.
_days
@
property
def
remaining_days
(
self
):
return
abs
(
self
.
_days
)
%
7
*
self
.
_sign
(
self
.
_days
)
@
property
def
hours
(
self
):
if
self
.
_h
is
None
:
seconds
=
self
.
_seconds
self
.
_h
=
0
if
abs
(
seconds
)
>=
3600
:
self
.
_h
=
(
abs
(
seconds
)
//
3600
%
24
)
*
self
.
_sign
(
seconds
)
return
self
.
_h
@
property
def
minutes
(
self
):
if
self
.
_i
is
None
:
seconds
=
self
.
_seconds
self
.
_i
=
0
if
abs
(
seconds
)
>=
60
:
self
.
_i
=
(
abs
(
seconds
)
//
60
%
60
)
*
self
.
_sign
(
seconds
)
return
self
.
_i
@
property
def
seconds
(
self
):
return
self
.
_seconds
@
property
def
remaining_seconds
(
self
):
if
self
.
_s
is
None
:
self
.
_s
=
self
.
_seconds
self
.
_s
=
abs
(
self
.
_s
)
%
60
*
self
.
_sign
(
self
.
_s
)
return
self
.
_s
@
property
def
microseconds
(
self
):
return
self
.
_microseconds
@
property
def
invert
(
self
):
if
self
.
_invert
is
None
:
self
.
_invert
=
self
.
total_seconds
()
<
0
return
self
.
_invert
def
in_weeks
(
self
):
return
int
(
self
.
total_weeks
())
def
in_days
(
self
):
return
int
(
self
.
total_days
())
def
in_hours
(
self
):
return
int
(
self
.
total_hours
())
def
in_minutes
(
self
):
return
int
(
self
.
total_minutes
())
def
in_seconds
(
self
):
return
int
(
self
.
total_seconds
())
def
_sign
(
self
,
value
):
if
value
<
0
:
return
-
1
return
1
def
as_timedelta
(
self
):
"""
Return the interval as a native timedelta.
:rtype: timedelta
"""
return
timedelta
(
seconds
=
self
.
total_seconds
())
class
Interval
(
WordableIntervalMixin
,
BaseInterval
):
"""
Replacement for the standard timedelta class.
Provides several improvements over the base class.
"""
@
classmethod
def
instance
(
cls
,
delta
):
"""
Creates a Interval from a timedelta
:type delta: timedelta
:rtype: Interval
"""
return
cls
(
days
=
delta
.
days
,
seconds
=
delta
.
seconds
,
microseconds
=
delta
.
microseconds
)
def
__add__
(
self
,
other
):
if
isinstance
(
other
,
timedelta
):
return
self
.
__class__
(
seconds
=
self
.
total_seconds
()
+
other
.
total_seconds
())
return
NotImplemented
__radd__
=
__add__
def
__sub__
(
self
,
other
):
if
isinstance
(
other
,
timedelta
):
return
self
.
__class__
(
seconds
=
self
.
total_seconds
()
-
other
.
total_seconds
())
return
NotImplemented
def
__neg__
(
self
):
return
self
.
__class__
(
seconds
=
-
self
.
total_seconds
())
def
_to_microseconds
(
self
):
return
((
self
.
_days
*
(
24
*
3600
)
+
self
.
_seconds
)
*
1000000
+
self
.
_microseconds
)
def
__mul__
(
self
,
other
):
if
isinstance
(
other
,
int
):
return
self
.
__class__
(
seconds
=
self
.
total_seconds
()
*
other
)
if
isinstance
(
other
,
float
):
usec
=
self
.
_to_microseconds
()
a
,
b
=
other
.
as_integer_ratio
()
return
self
.
__class__
(
0
,
0
,
_divide_and_round
(
usec
*
a
,
b
))
return
NotImplemented
__rmul__
=
__mul__
def
__floordiv__
(
self
,
other
):
if
not
isinstance
(
other
, (
int
,
timedelta
)):
return
NotImplemented
usec
=
self
.
_to_microseconds
()
if
isinstance
(
other
,
timedelta
):
return
usec
//
other
.
_to_microseconds
()
if
isinstance
(
other
,
int
):
return
self
.
__class__
(
0
,
0
,
usec
//
other
)
def
__truediv__
(
self
,
other
):
if
not
isinstance
(
other
, (
int
,
float
,
timedelta
)):
return
NotImplemented
usec
=
self
.
_to_microseconds
()
if
isinstance
(
other
,
timedelta
):
return
usec
/
other
.
_to_microseconds
()
if
isinstance
(
other
,
int
):
return
self
.
__class__
(
0
,
0
,
_divide_and_round
(
usec
,
other
))
if
isinstance
(
other
,
float
):
a
,
b
=
other
.
as_integer_ratio
()
return
self
.
__class__
(
0
,
0
,
_divide_and_round
(
b
*
usec
,
a
))
__div__
=
__floordiv__
def
__mod__
(
self
,
other
):
if
isinstance
(
other
,
timedelta
):
r
=
self
.
_to_microseconds
()
%
other
.
_to_microseconds
()
return
self
.
__class__
(
0
,
0
,
r
)
return
NotImplemented
def
__divmod__
(
self
,
other
):
if
isinstance
(
other
,
timedelta
):
q
,
r
=
divmod
(
self
.
_to_microseconds
(),
other
.
_to_microseconds
())
return
q
,
self
.
__class__
(
0
,
0
,
r
)
return
NotImplemented
Interval
.
min
=
Interval
(
-
999999999
)
Interval
.
max
=
Interval
(
days
=
999999999
,
hours
=
23
,
minutes
=
59
,
seconds
=
59
,
microseconds
=
999999
)
Interval
.
resolution
=
Interval
(
microseconds
=
1
)
class
AbsoluteInterval
(
Interval
):
"""
Interval that expresses a time difference in absolute values.
"""
def
__new__
(
cls
,
days
=
0
,
seconds
=
0
,
microseconds
=
0
,
milliseconds
=
0
,
minutes
=
0
,
hours
=
0
,
weeks
=
0
):
self
=
timedelta
.
__new__
(
cls
,
days
,
seconds
,
microseconds
,
milliseconds
,
minutes
,
hours
,
weeks
)
# We need to compute the total_seconds() value
# on a native timedelta object
delta
=
timedelta
(
days
,
seconds
,
microseconds
,
milliseconds
,
minutes
,
hours
,
weeks
)
# Intuitive normalization
self
.
_total
=
delta
.
total_seconds
()
total
=
abs
(
self
.
_total
)
self
.
_microseconds
=
round
(
total
%
1
*
1e6
)
self
.
_seconds
=
int
(
total
)
%
SECONDS_PER_DAY
self
.
_days
=
int
(
total
)
//
SECONDS_PER_DAY
return
self
def
total_seconds
(
self
):
return
abs
(
self
.
_total
)
@
property
def
invert
(
self
):
if
self
.
_invert
is
None
:
self
.
_invert
=
self
.
_total
<
0
return
self
.
_invert
Back
|
FazBrowse Home
|
New Git URL