1
1
# See https://github.com/python/cpython/blob/3.9/Lib/test/datetimetester.py
2
2
import unittest
3
- from datetime import \
4
- timedelta as td ,\
5
- timezone as tz ,\
6
- datetime as dt ,\
7
- fromisoformat as fif
3
+ from datetime import timedelta as td , timezone as tz , datetime as dt , fromisoformat as fif
8
4
9
5
10
6
### timedelta ################################################################
17
13
t2 = td (2 , 3 , 4 )
18
14
t3 = td (2 , 3 , 5 )
19
15
20
- class TestTimeDelta (unittest .TestCase ):
21
16
17
+ class TestTimeDelta (unittest .TestCase ):
22
18
def test_constructor01 (self ):
23
19
self .assertEqual (td (), td (weeks = 0 , days = 0 , hours = 0 , minutes = 0 , seconds = 0 ))
24
20
@@ -65,16 +61,22 @@ def test_constructor15(self):
65
61
self .assertEqual (td (microseconds = 0.001 ), td (nanoseconds = 1 ))
66
62
67
63
def test_constant01 (self ):
68
- self .assertTrue (td (0 , 0 , 0 , 365 * td .MINYEAR ).total_seconds () >= - 2 ** 63 / 10 ** 9 )
64
+ self .assertTrue (td (0 , 0 , 0 , 365 * td .MINYEAR ).total_seconds () >= - ( 2 ** 63 ) / 10 ** 9 )
69
65
70
66
def test_constant02 (self ):
71
- self .assertFalse (td (0 , 0 , 0 , 365 * (td .MINYEAR - 1 )).total_seconds () >= - 2 ** 63 / 10 ** 9 )
67
+ self .assertFalse (
68
+ td (0 , 0 , 0 , 365 * (td .MINYEAR - 1 )).total_seconds () >= - (2 ** 63 ) / 10 ** 9
69
+ )
72
70
73
71
def test_constant03 (self ):
74
- self .assertTrue (td (23 , 59 , 59 , 365 * td .MAXYEAR ).total_seconds () <= (2 ** 63 - 1 ) / 10 ** 9 )
72
+ self .assertTrue (
73
+ td (23 , 59 , 59 , 365 * td .MAXYEAR ).total_seconds () <= (2 ** 63 - 1 ) / 10 ** 9
74
+ )
75
75
76
76
def test_constant04 (self ):
77
- self .assertFalse (td (23 , 59 , 59 , 365 * (td .MAXYEAR + 1 )).total_seconds () <= (2 ** 63 - 1 ) / 10 ** 9 )
77
+ self .assertFalse (
78
+ td (23 , 59 , 59 , 365 * (td .MAXYEAR + 1 )).total_seconds () <= (2 ** 63 - 1 ) / 10 ** 9
79
+ )
78
80
79
81
def test_computation01 (self ):
80
82
self .assertEqual (a + b + c , td (7 , 6 , 10 ))
@@ -270,40 +272,40 @@ def test_compare06(self):
270
272
self .assertTrue (not t1 > t2 )
271
273
272
274
def test_compare07 (self ):
273
- self .assertTrue (t1 < t3 )
275
+ self .assertTrue (t1 < t3 )
274
276
275
277
def test_compare08 (self ):
276
- self .assertTrue (t3 > t1 )
278
+ self .assertTrue (t3 > t1 )
277
279
278
280
def test_compare09 (self ):
279
- self .assertTrue (t1 <= t3 )
281
+ self .assertTrue (t1 <= t3 )
280
282
281
283
def test_compare10 (self ):
282
- self .assertTrue (t3 >= t1 )
284
+ self .assertTrue (t3 >= t1 )
283
285
284
286
def test_compare11 (self ):
285
- self .assertTrue (t1 != t3 )
287
+ self .assertTrue (t1 != t3 )
286
288
287
289
def test_compare12 (self ):
288
- self .assertTrue (t3 != t1 )
290
+ self .assertTrue (t3 != t1 )
289
291
290
292
def test_compare13 (self ):
291
- self .assertTrue (not t1 == t3 )
293
+ self .assertTrue (not t1 == t3 )
292
294
293
295
def test_compare14 (self ):
294
- self .assertTrue (not t3 == t1 )
296
+ self .assertTrue (not t3 == t1 )
295
297
296
298
def test_compare15 (self ):
297
- self .assertTrue (not t1 > t3 )
299
+ self .assertTrue (not t1 > t3 )
298
300
299
301
def test_compare16 (self ):
300
- self .assertTrue (not t3 < t1 )
302
+ self .assertTrue (not t3 < t1 )
301
303
302
304
def test_compare17 (self ):
303
- self .assertTrue (not t1 >= t3 )
305
+ self .assertTrue (not t1 >= t3 )
304
306
305
307
def test_compare18 (self ):
306
- self .assertTrue (not t3 <= t1 )
308
+ self .assertTrue (not t3 <= t1 )
307
309
308
310
def test_str01 (self ):
309
311
self .assertEqual (str (td (days = 1 )), "1d 00:00:00" )
@@ -324,13 +326,18 @@ def test_str06(self):
324
326
self .assertEqual (str (td (2 , 3 , 4 )), "02:03:04" )
325
327
326
328
def test_repr01 (self ):
327
- self .assertEqual (repr (td (1 )), "datetime.timedelta(seconds={})" .format (1 * 3600.0 ))
329
+ self .assertEqual (repr (td (1 )), "datetime.timedelta(seconds={})" .format (1 * 3600.0 ))
328
330
329
331
def test_repr02 (self ):
330
- self .assertEqual (repr (td (10 , 2 )), "datetime.timedelta(seconds={})" .format (10 * 3600 + 2 * 60.0 ))
332
+ self .assertEqual (
333
+ repr (td (10 , 2 )), "datetime.timedelta(seconds={})" .format (10 * 3600 + 2 * 60.0 )
334
+ )
331
335
332
336
def test_repr03 (self ):
333
- self .assertEqual (repr (td (- 10 , 2 , 40 )), "datetime.timedelta(seconds={})" .format (- 10 * 3600 + 2 * 60 + 40.0 ))
337
+ self .assertEqual (
338
+ repr (td (- 10 , 2 , 40 )),
339
+ "datetime.timedelta(seconds={})" .format (- 10 * 3600 + 2 * 60 + 40.0 ),
340
+ )
334
341
335
342
def test_bool01 (self ):
336
343
self .assertTrue (td (1 ))
@@ -381,6 +388,7 @@ def test_divmod02(self):
381
388
382
389
### timezone #################################################################
383
390
391
+
384
392
class Cet (tz ):
385
393
# Central European Time (see https://en.wikipedia.org/wiki/Summer_time_in_Europe)
386
394
@@ -391,7 +399,7 @@ def dst(self, dt):
391
399
return td (hours = 1 ) if self .isdst (dt ) else td (0 )
392
400
393
401
def tzname (self , dt ):
394
- return ' CEST' if self .isdst (dt ) else ' CET'
402
+ return " CEST" if self .isdst (dt ) else " CET"
395
403
396
404
def isdst (self , dt ):
397
405
if dt is None :
@@ -402,54 +410,59 @@ def isdst(self, dt):
402
410
if 3 < month < 10 :
403
411
return True
404
412
if month == 3 :
405
- beg = 31 - (5 * year // 4 + 4 ) % 7 # last Sunday of March
406
- if day < beg : return False
407
- if day > beg : return True
413
+ beg = 31 - (5 * year // 4 + 4 ) % 7 # last Sunday of March
414
+ if day < beg :
415
+ return False
416
+ if day > beg :
417
+ return True
408
418
return hour >= 3
409
419
if month == 10 :
410
- end = 31 - (5 * year // 4 + 1 ) % 7 # last Sunday of October
411
- if day < end : return True
412
- if day > end : return False
420
+ end = 31 - (5 * year // 4 + 1 ) % 7 # last Sunday of October
421
+ if day < end :
422
+ return True
423
+ if day > end :
424
+ return False
413
425
return hour < 3
414
426
return False
415
427
428
+
416
429
tz1 = tz (td (hours = - 1 ))
417
430
tz2 = Cet ()
418
431
419
- class TestTimeZone (unittest .TestCase ):
420
432
433
+ class TestTimeZone (unittest .TestCase ):
421
434
def test_constructor01 (self ):
422
- self .assertEqual (str (tz1 ), ' UTC-01:00' )
435
+ self .assertEqual (str (tz1 ), " UTC-01:00" )
423
436
424
437
def test_constructor02 (self ):
425
- self .assertEqual (str (tz2 ), ' CET' )
438
+ self .assertEqual (str (tz2 ), " CET" )
426
439
427
440
def test_utcoffset01 (self ):
428
- self .assertEqual (str (tz2 .utcoffset (None )), ' 01:00:00' )
441
+ self .assertEqual (str (tz2 .utcoffset (None )), " 01:00:00" )
429
442
430
443
def test_utcoffset02 (self ):
431
- self .assertEqual (str (tz2 .utcoffset (dt (2010 , 3 , 27 , 12 ))), ' 01:00:00' )
444
+ self .assertEqual (str (tz2 .utcoffset (dt (2010 , 3 , 27 , 12 ))), " 01:00:00" )
432
445
433
446
def test_utcoffset03 (self ):
434
- self .assertEqual (str (tz2 .utcoffset (dt (2010 , 3 , 28 , 12 ))), ' 02:00:00' )
447
+ self .assertEqual (str (tz2 .utcoffset (dt (2010 , 3 , 28 , 12 ))), " 02:00:00" )
435
448
436
449
def test_utcoffset04 (self ):
437
- self .assertEqual (str (tz2 .utcoffset (dt (2010 , 10 , 30 , 12 ))), ' 02:00:00' )
450
+ self .assertEqual (str (tz2 .utcoffset (dt (2010 , 10 , 30 , 12 ))), " 02:00:00" )
438
451
439
452
def test_utcoffset05 (self ):
440
- self .assertEqual (str (tz2 .utcoffset (dt (2010 , 10 , 31 , 12 ))), ' 01:00:00' )
453
+ self .assertEqual (str (tz2 .utcoffset (dt (2010 , 10 , 31 , 12 ))), " 01:00:00" )
441
454
442
455
def test_isoformat01 (self ):
443
- self .assertEqual (tz2 .isoformat (dt (2011 , 1 , 1 )), ' UTC+01:00' )
456
+ self .assertEqual (tz2 .isoformat (dt (2011 , 1 , 1 )), " UTC+01:00" )
444
457
445
458
def test_isoformat02 (self ):
446
- self .assertEqual (tz2 .isoformat (dt (2011 , 8 , 1 )), ' UTC+02:00' )
459
+ self .assertEqual (tz2 .isoformat (dt (2011 , 8 , 1 )), " UTC+02:00" )
447
460
448
461
def test_tzname01 (self ):
449
- self .assertEqual (tz2 .tzname (dt (2011 , 1 , 1 )), ' CET' )
462
+ self .assertEqual (tz2 .tzname (dt (2011 , 1 , 1 )), " CET" )
450
463
451
464
def test_tzname02 (self ):
452
- self .assertEqual (tz2 .tzname (dt (2011 , 8 , 1 )), ' CEST' )
465
+ self .assertEqual (tz2 .tzname (dt (2011 , 8 , 1 )), " CEST" )
453
466
454
467
455
468
### datetime #################################################################
@@ -464,8 +477,8 @@ def test_tzname02(self):
464
477
day = td (days = 1 )
465
478
week = td (weeks = 1 )
466
479
467
- class TestTimeZone (unittest .TestCase ):
468
480
481
+ class TestTimeZone (unittest .TestCase ):
469
482
def test_constructor01 (self ):
470
483
d = dt (2002 , 3 , 1 , 12 , 0 )
471
484
year , month , day , hour , minute , second , nanosecond , tz = d .tuple ()
@@ -551,8 +564,7 @@ def test_constructor22(self):
551
564
552
565
def test_computation01 (self ):
553
566
d = d1 - d2
554
- self .assertEqual (d .total_seconds (),
555
- (46 * 365 + len (range (1956 , 2002 , 4 )))* 24 * 60 * 60 )
567
+ self .assertEqual (d .total_seconds (), (46 * 365 + len (range (1956 , 2002 , 4 ))) * 24 * 60 * 60 )
556
568
557
569
def test_computation02 (self ):
558
570
self .assertEqual (d4 + hour , dt (2002 , 3 , 2 , 18 , 6 ))
@@ -561,7 +573,7 @@ def test_computation02(self):
561
573
self .assertEqual (hour + d4 , dt (2002 , 3 , 2 , 18 , 6 ))
562
574
563
575
def test_computation03 (self ):
564
- self .assertEqual (d4 + 10 * hour , dt (2002 , 3 , 3 , 3 , 6 ))
576
+ self .assertEqual (d4 + 10 * hour , dt (2002 , 3 , 3 , 3 , 6 ))
565
577
566
578
def test_computation04 (self ):
567
579
self .assertEqual (d4 - hour , dt (2002 , 3 , 2 , 16 , 6 ))
@@ -573,7 +585,7 @@ def test_computation06(self):
573
585
self .assertEqual (d4 - hour , d4 + - hour )
574
586
575
587
def test_computation07 (self ):
576
- self .assertEqual (d4 - 20 * hour , dt (2002 , 3 , 1 , 21 , 6 ))
588
+ self .assertEqual (d4 - 20 * hour , dt (2002 , 3 , 1 , 21 , 6 ))
577
589
578
590
def test_computation08 (self ):
579
591
self .assertEqual (d4 + day , dt (2002 , 3 , 3 , 17 , 6 ))
@@ -588,10 +600,10 @@ def test_computation11(self):
588
600
self .assertEqual (d4 - week , dt (2002 , 2 , 23 , 17 , 6 ))
589
601
590
602
def test_computation12 (self ):
591
- self .assertEqual (d4 + 52 * week , dt (2003 , 3 , 1 , 17 , 6 ))
603
+ self .assertEqual (d4 + 52 * week , dt (2003 , 3 , 1 , 17 , 6 ))
592
604
593
605
def test_computation13 (self ):
594
- self .assertEqual (d4 - 52 * week , dt (2001 , 3 , 3 , 17 , 6 ))
606
+ self .assertEqual (d4 - 52 * week , dt (2001 , 3 , 3 , 17 , 6 ))
595
607
596
608
def test_computation14 (self ):
597
609
self .assertEqual ((d4 + week ) - d4 , week )
@@ -738,5 +750,5 @@ def test_fromisoformat05(self):
738
750
self .assertEqual (str (fif ("1975-08-10 23:30:12+01:00" )), "1975-08-10 23:30:12+01:00" )
739
751
740
752
741
- if __name__ == ' __main__' :
753
+ if __name__ == " __main__" :
742
754
unittest .main ()
0 commit comments