@@ -357,9 +357,13 @@ def test_drange():
357
357
# dates from an half open interval [start, end)
358
358
assert len (mdates .drange (start , end , delta )) == 24
359
359
360
+ # Same if interval ends slightly earlier
361
+ end = end - datetime .timedelta (microseconds = 1 )
362
+ assert len (mdates .drange (start , end , delta )) == 24
363
+
360
364
# if end is a little bit later, we expect the range to contain one element
361
365
# more
362
- end = end + datetime .timedelta (microseconds = 1 )
366
+ end = end + datetime .timedelta (microseconds = 2 )
363
367
assert len (mdates .drange (start , end , delta )) == 25
364
368
365
369
# reset end
@@ -1012,6 +1016,20 @@ def attach_tz(dt, zi):
1012
1016
1013
1017
_test_rrulewrapper (attach_tz , dateutil .tz .gettz )
1014
1018
1019
+ SYD = dateutil .tz .gettz ('Australia/Sydney' )
1020
+ dtstart = datetime .datetime (2017 , 4 , 1 , 0 )
1021
+ dtend = datetime .datetime (2017 , 4 , 4 , 0 )
1022
+ rule = mdates .rrulewrapper (freq = dateutil .rrule .DAILY , dtstart = dtstart ,
1023
+ tzinfo = SYD , until = dtend )
1024
+ assert rule .after (dtstart ) == datetime .datetime (2017 , 4 , 2 , 0 , 0 ,
1025
+ tzinfo = SYD )
1026
+ assert rule .before (dtend ) == datetime .datetime (2017 , 4 , 3 , 0 , 0 ,
1027
+ tzinfo = SYD )
1028
+
1029
+ # Test parts of __getattr__
1030
+ assert rule ._base_tzinfo == SYD
1031
+ assert rule ._interval == 1
1032
+
1015
1033
1016
1034
@pytest .mark .pytz
1017
1035
def test_rrulewrapper_pytz ():
@@ -1046,6 +1064,15 @@ def test_yearlocator_pytz():
1046
1064
'2014-01-01 00:00:00-05:00' , '2015-01-01 00:00:00-05:00' ]
1047
1065
st = list (map (str , mdates .num2date (locator (), tz = tz )))
1048
1066
assert st == expected
1067
+ assert np .allclose (locator .tick_values (x [0 ], x [1 ]), np .array (
1068
+ [14610.20833333 , 14610.33333333 , 14610.45833333 , 14610.58333333 ,
1069
+ 14610.70833333 , 14610.83333333 , 14610.95833333 , 14611.08333333 ,
1070
+ 14611.20833333 ]))
1071
+ assert np .allclose (locator .get_locator (x [1 ], x [0 ]).tick_values (x [0 ], x [1 ]),
1072
+ np .array (
1073
+ [14610.20833333 , 14610.33333333 , 14610.45833333 , 14610.58333333 ,
1074
+ 14610.70833333 , 14610.83333333 , 14610.95833333 , 14611.08333333 ,
1075
+ 14611.20833333 ]))
1049
1076
1050
1077
1051
1078
def test_YearLocator ():
@@ -1290,18 +1317,14 @@ def test_datestr2num():
1290
1317
month = 1 , day = 10 )).size == 0
1291
1318
1292
1319
1293
- def test_concise_formatter_exceptions ():
1320
+ @pytest .mark .parametrize ('kwarg' ,
1321
+ ('formats' , 'zero_formats' , 'offset_formats' ))
1322
+ def test_concise_formatter_exceptions (kwarg ):
1294
1323
locator = mdates .AutoDateLocator ()
1295
- with pytest .raises (ValueError , match = "formats argument must be a list" ):
1296
- mdates .ConciseDateFormatter (locator , formats = ['' , '%Y' ])
1297
-
1298
- with pytest .raises (ValueError ,
1299
- match = "zero_formats argument must be a list" ):
1300
- mdates .ConciseDateFormatter (locator , zero_formats = ['' , '%Y' ])
1301
-
1302
- with pytest .raises (ValueError ,
1303
- match = "offset_formats argument must be a list" ):
1304
- mdates .ConciseDateFormatter (locator , offset_formats = ['' , '%Y' ])
1324
+ kwargs = {kwarg : ['' , '%Y' ]}
1325
+ match = f"{ kwarg } argument must be a list"
1326
+ with pytest .raises (ValueError , match = match ):
1327
+ mdates .ConciseDateFormatter (locator , ** kwargs )
1305
1328
1306
1329
1307
1330
def test_concise_formatter_call ():
@@ -1340,3 +1363,29 @@ def test_datetime_masked():
1340
1363
fig , ax = plt .subplots ()
1341
1364
ax .plot (x , m )
1342
1365
assert ax .get_xlim () == (0 , 1 )
1366
+
1367
+
1368
+ @pytest .mark .parametrize ('val' , (- 1000000 , 10000000 ))
1369
+ def test_num2date_error (val ):
1370
+ with pytest .raises (ValueError , match = f"Date ordinal { val } converts" ):
1371
+ mdates .num2date (val )
1372
+
1373
+
1374
+ def test_num2date_roundoff ():
1375
+ assert mdates .num2date (100000.0000578702 ) == datetime .datetime (
1376
+ 2243 , 10 , 17 , 0 , 0 , 4 , 999980 , tzinfo = datetime .timezone .utc )
1377
+ # Slightly larger, steps of 20 microseconds
1378
+ assert mdates .num2date (100000.0000578703 ) == datetime .datetime (
1379
+ 2243 , 10 , 17 , 0 , 0 , 5 , tzinfo = datetime .timezone .utc )
1380
+
1381
+
1382
+ def test_DateFormatter_settz ():
1383
+ time = mdates .date2num (datetime .datetime (2011 , 1 , 1 , 0 , 0 ,
1384
+ tzinfo = mdates .UTC ))
1385
+ formatter = mdates .DateFormatter ('%Y-%b-%d %H:%M' )
1386
+ # Default UTC
1387
+ assert formatter (time ) == '2011-Jan-01 00:00'
1388
+
1389
+ # Set tzinfo
1390
+ formatter .set_tzinfo ('Pacific/Kiritimati' )
1391
+ assert formatter (time ) == '2011-Jan-01 14:00'
0 commit comments