@@ -175,15 +175,28 @@ def _get_rc_timezone():
175
175
import pytz
176
176
return pytz .timezone (s )
177
177
178
+ """
179
+ Time-related constants.
180
+ """
181
+ EPOCH_OFFSET = 719163. # Days between 0001-01-01 and epoch, +1.
182
+ JULIAN_OFFSET = 1721424.5 # Julian date at 0001-01-01
178
183
MICROSECONDLY = SECONDLY + 1
179
184
HOURS_PER_DAY = 24.
180
- MINUTES_PER_DAY = 60. * HOURS_PER_DAY
181
- SECONDS_PER_DAY = 60. * MINUTES_PER_DAY
185
+ MIN_PER_HOUR = 60.
186
+ SEC_PER_MIN = 60.
187
+ MONTHS_PER_YEAR = 12.
188
+
189
+ DAYS_PER_WEEK = 7.
190
+ DAYS_PER_MONTH = 30.
191
+ DAYS_PER_YEAR = 365.0
192
+
193
+ MINUTES_PER_DAY = MIN_PER_HOUR * HOURS_PER_DAY
194
+ SECONDS_PER_DAY = SEC_PER_MIN * MINUTES_PER_DAY
182
195
MUSECONDS_PER_DAY = 1e6 * SECONDS_PER_DAY
183
- SEC_PER_MIN = 60
184
- SEC_PER_HOUR = 3600
185
- SEC_PER_DAY = SEC_PER_HOUR * 24
186
- SEC_PER_WEEK = SEC_PER_DAY * 7
196
+
197
+ SEC_PER_HOUR = SEC_PER_MIN * MIN_PER_HOUR
198
+ SEC_PER_DAY = SEC_PER_HOUR * HOURS_PER_DAY
199
+ SEC_PER_WEEK = SEC_PER_DAY * DAYS_PER_WEEK # Days per week
187
200
MONDAY , TUESDAY , WEDNESDAY , THURSDAY , FRIDAY , SATURDAY , SUNDAY = (
188
201
MO , TU , WE , TH , FR , SA , SU )
189
202
WEEKDAYS = (MONDAY , TUESDAY , WEDNESDAY , THURSDAY , FRIDAY , SATURDAY , SUNDAY )
@@ -224,9 +237,9 @@ def _from_ordinalf(x, tz=None):
224
237
ix = int (x )
225
238
dt = datetime .datetime .fromordinal (ix )
226
239
remainder = float (x ) - ix
227
- hour , remainder = divmod (24 * remainder , 1 )
228
- minute , remainder = divmod (60 * remainder , 1 )
229
- second , remainder = divmod (60 * remainder , 1 )
240
+ hour , remainder = divmod (HOURS_PER_DAY * remainder , 1 )
241
+ minute , remainder = divmod (MIN_PER_HOUR * remainder , 1 )
242
+ second , remainder = divmod (SEC_PER_MIN * remainder , 1 )
230
243
microsecond = int (1e6 * remainder )
231
244
if microsecond < 10 :
232
245
microsecond = 0 # compensate for rounding errors
@@ -316,7 +329,7 @@ def julian2num(j):
316
329
"""
317
330
if cbook .iterable (j ):
318
331
j = np .asarray (j )
319
- return j - 1721424.5
332
+ return j - JULIAN_OFFSET
320
333
321
334
322
335
def num2julian (n ):
@@ -325,7 +338,7 @@ def num2julian(n):
325
338
"""
326
339
if cbook .iterable (n ):
327
340
n = np .asarray (n )
328
- return n + 1721424.5
341
+ return n + JULIAN_OFFSET
329
342
330
343
331
344
def num2date (x , tz = None ):
@@ -436,8 +449,19 @@ def _findall(self, text, substr):
436
449
# calendar.
437
450
438
451
def strftime (self , dt , fmt ):
452
+ """
453
+ Prints `datetime.datetime` object `dt` using a C-like `strftime()`
454
+ format string.
455
+
456
+ Currently `datetime.datetime.strftime()` is not supported for
457
+ years <= 1900 in Python 2.x and years <= 1000 in Python 3.x. This
458
+ function extends this functionality to
459
+ """
439
460
fmt = self .illegal_s .sub (r"\1" , fmt )
440
461
fmt = fmt .replace ("%s" , "s" )
462
+
463
+ # strftime is not supported on datetime for years <= 1900 in Python 2.x
464
+ # or years <= 1000 in Python 3.x
441
465
if dt .year > 1900 :
442
466
return cbook .unicode_safe (dt .strftime (fmt ))
443
467
@@ -564,11 +588,11 @@ def __init__(self, locator, tz=None, defaultfmt='%Y-%m-%d'):
564
588
self ._tz = tz
565
589
self .defaultfmt = defaultfmt
566
590
self ._formatter = DateFormatter (self .defaultfmt , tz )
567
- self .scaled = {365.0 : '%Y' ,
568
- 30. : '%b %Y' ,
591
+ self .scaled = {DAYS_PER_YEAR : '%Y' ,
592
+ DAYS_PER_MONTH : '%b %Y' ,
569
593
1.0 : '%b %d %Y' ,
570
- 1. / 24. : '%H:%M:%S' ,
571
- 1. / (24. * 60. ): '%H:%M:%S.%f' }
594
+ 1. / HOURS_PER_DAY : '%H:%M:%S' ,
595
+ 1. / (MINUTES_PER_DAY ): '%H:%M:%S.%f' }
572
596
573
597
def __call__ (self , x , pos = None ):
574
598
locator_unit_scale = float (self ._locator ._get_unit ())
@@ -730,19 +754,19 @@ def _get_unit(self):
730
754
@staticmethod
731
755
def get_unit_generic (freq ):
732
756
if (freq == YEARLY ):
733
- return 365.0
757
+ return DAYS_PER_YEAR
734
758
elif (freq == MONTHLY ):
735
- return 30.0
759
+ return DAYS_PER_MONTH
736
760
elif (freq == WEEKLY ):
737
- return 7.0
761
+ return DAYS_PER_WEEK
738
762
elif (freq == DAILY ):
739
763
return 1.0
740
764
elif (freq == HOURLY ):
741
- return (1.0 / 24.0 )
765
+ return (1.0 / HOURS_PER_DAY )
742
766
elif (freq == MINUTELY ):
743
- return (1.0 / ( 24 * 60 ) )
767
+ return (1.0 / MINUTES_PER_DAY )
744
768
elif (freq == SECONDLY ):
745
- return (1.0 / ( 24 * 3600 ) )
769
+ return (1.0 / SECONDS_PER_DAY )
746
770
else :
747
771
# error
748
772
return - 1 # or should this just return '1'?
@@ -886,8 +910,8 @@ def nonsingular(self, vmin, vmax):
886
910
# whatever is thrown at us, we can scale the unit.
887
911
# But default nonsingular date plots at an ~4 year period.
888
912
if vmin == vmax :
889
- vmin = vmin - 365 * 2
890
- vmax = vmax + 365 * 2
913
+ vmin = vmin - DAYS_PER_YEAR * 2
914
+ vmax = vmax + DAYS_PER_YEAR * 2
891
915
return vmin , vmax
892
916
893
917
def set_axis (self , axis ):
@@ -920,11 +944,11 @@ def get_locator(self, dmin, dmax):
920
944
delta = - delta
921
945
922
946
numYears = (delta .years * 1.0 )
923
- numMonths = (numYears * 12.0 ) + delta .months
924
- numDays = (numMonths * 31.0 ) + delta .days
925
- numHours = (numDays * 24.0 ) + delta .hours
926
- numMinutes = (numHours * 60.0 ) + delta .minutes
927
- numSeconds = (numMinutes * 60.0 ) + delta .seconds
947
+ numMonths = (numYears * MONTHS_PER_YEAR ) + delta .months
948
+ numDays = (numMonths * DAYS_PER_MONTH ) + delta .days
949
+ numHours = (numDays * HOURS_PER_DAY ) + delta .hours
950
+ numMinutes = (numHours * MIN_PER_HOUR ) + delta .minutes
951
+ numSeconds = (numMinutes * SEC_PER_MIN ) + delta .seconds
928
952
numMicroseconds = (numSeconds * 1e6 ) + delta .microseconds
929
953
930
954
nums = [numYears , numMonths , numDays , numHours , numMinutes ,
@@ -1246,16 +1270,14 @@ def epoch2num(e):
1246
1270
Convert an epoch or sequence of epochs to the new date format,
1247
1271
that is days since 0001.
1248
1272
"""
1249
- spd = 24. * 3600.
1250
- return 719163 + np .asarray (e ) / spd
1273
+ return EPOCH_OFFSET + np .asarray (e ) / SEC_PER_DAY
1251
1274
1252
1275
1253
1276
def num2epoch (d ):
1254
1277
"""
1255
1278
Convert days since 0001 to epoch. *d* can be a number or sequence.
1256
1279
"""
1257
- spd = 24. * 3600.
1258
- return (np .asarray (d ) - 719163 ) * spd
1280
+ return (np .asarray (d ) - EPOCH_OFFSET ) * SEC_PER_DAY
1259
1281
1260
1282
1261
1283
def mx2num (mxdates ):
@@ -1281,14 +1303,14 @@ def date_ticker_factory(span, tz=None, numticks=5):
1281
1303
"""
1282
1304
1283
1305
if span == 0 :
1284
- span = 1 / 24.
1306
+ span = 1 / HOURS_PER_DAY
1285
1307
1286
- minutes = span * 24 * 60
1287
- hours = span * 24
1308
+ minutes = span * MINUTES_PER_DAY
1309
+ hours = span * HOURS_PER_DAY
1288
1310
days = span
1289
- weeks = span / 7.
1290
- months = span / 31. # approx
1291
- years = span / 365.
1311
+ weeks = span / DAYS_PER_WEEK
1312
+ months = span / DAYS_PER_MONTH # Approx
1313
+ years = span / DAYS_PER_YEAR # Approx
1292
1314
1293
1315
if years > numticks :
1294
1316
locator = YearLocator (int (years / numticks ), tz = tz ) # define
@@ -1335,14 +1357,14 @@ def hours(h):
1335
1357
"""
1336
1358
Return hours as days.
1337
1359
"""
1338
- return h / 24.
1360
+ return h / HOURS_PER_DAY
1339
1361
1340
1362
1341
1363
def weeks (w ):
1342
1364
"""
1343
1365
Return weeks as days.
1344
1366
"""
1345
- return w * 7.
1367
+ return w * DAYS_PER_WEEK
1346
1368
1347
1369
1348
1370
class DateConverter (units .ConversionInterface ):
0 commit comments