Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee7931c

Browse files
committed
Make from_ordinalf use mostly functions from datetime for time conversion.
Signed-off-by: Paul G <[email protected]>
1 parent 5af753c commit ee7931c

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

lib/matplotlib/dates.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,13 @@ def _get_rc_timezone():
191191
DAYS_PER_YEAR = 365.0
192192

193193
MINUTES_PER_DAY = MIN_PER_HOUR * HOURS_PER_DAY
194-
SECONDS_PER_DAY = SEC_PER_MIN * MINUTES_PER_DAY
195-
MUSECONDS_PER_DAY = 1e6 * SECONDS_PER_DAY
196194

197195
SEC_PER_HOUR = SEC_PER_MIN * MIN_PER_HOUR
198196
SEC_PER_DAY = SEC_PER_HOUR * HOURS_PER_DAY
199-
SEC_PER_WEEK = SEC_PER_DAY * DAYS_PER_WEEK # Days per week
197+
SEC_PER_WEEK = SEC_PER_DAY * DAYS_PER_WEEK
198+
199+
MUSECONDS_PER_DAY = 1e6 * SEC_PER_DAY
200+
200201
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY = (
201202
MO, TU, WE, TH, FR, SA, SU)
202203
WEEKDAYS = (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY)
@@ -219,7 +220,7 @@ def _to_ordinalf(dt):
219220

220221
base = float(base)
221222
if td_remainder > 0:
222-
base ++ td_remainder / SECONDS_PER_DAY
223+
base ++ td_remainder / SEC_PER_DAY
223224

224225
return base
225226

@@ -238,18 +239,8 @@ def _from_ordinalf(x, tz=None):
238239
ix = int(x)
239240
dt = datetime.datetime.fromordinal(ix)
240241
remainder = float(x) - ix
241-
hour, remainder = divmod(HOURS_PER_DAY * remainder, 1)
242-
minute, remainder = divmod(MIN_PER_HOUR * remainder, 1)
243-
second, remainder = divmod(SEC_PER_MIN * remainder, 1)
244-
microsecond = int(1e6 * remainder)
245-
if microsecond < 10:
246-
microsecond = 0 # compensate for rounding errors
247-
dt = datetime.datetime(
248-
dt.year, dt.month, dt.day, int(hour), int(minute), int(second),
249-
microsecond, tzinfo=UTC).astimezone(tz)
250-
251-
if microsecond > 999990: # compensate for rounding errors
252-
dt += datetime.timedelta(microseconds=1e6 - microsecond)
242+
243+
dt += datetime.timedelta(seconds = remainder*SEC_PER_DAY)
253244

254245
return dt
255246

@@ -374,7 +365,7 @@ def drange(dstart, dend, delta):
374365
*dend* are :class:`datetime` instances. *delta* is a
375366
:class:`datetime.timedelta` instance.
376367
"""
377-
step = (delta.days + delta.seconds / SECONDS_PER_DAY +
368+
step = (delta.days + delta.seconds / SEC_PER_DAY +
378369
delta.microseconds / MUSECONDS_PER_DAY)
379370
f1 = _to_ordinalf(dstart)
380371
f2 = _to_ordinalf(dend)
@@ -767,7 +758,7 @@ def get_unit_generic(freq):
767758
elif (freq == MINUTELY):
768759
return (1.0 / MINUTES_PER_DAY)
769760
elif (freq == SECONDLY):
770-
return (1.0 / SECONDS_PER_DAY)
761+
return (1.0 / SEC_PER_DAY)
771762
else:
772763
# error
773764
return -1 # or should this just return '1'?

0 commit comments

Comments
 (0)