Skip to content

Commit 4c3e39f

Browse files
authored
Datetime test coverage (#7544)
* Added a test case for strftime("%z"). The added test checks a case with UTC offest expressed in an integer number of seconds. * Added a test comparing naive and aware datetimes. Check that a greater than comparison of a naive datetime instance with an aware one raises a TypeError. * Test datetime in fold or in gap comparison both ways.
1 parent 12f482e commit 4c3e39f

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Lib/test/datetimetester.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,9 +2345,10 @@ def test_more_strftime(self):
23452345
t = self.theclass(2004, 12, 31, 6, 22, 33, 47)
23462346
self.assertEqual(t.strftime("%m %d %y %f %S %M %H %j"),
23472347
"12 31 04 000047 33 22 06 366")
2348-
tz = timezone(-timedelta(hours=2, seconds=33, microseconds=123))
2349-
t = t.replace(tzinfo=tz)
2350-
self.assertEqual(t.strftime("%z"), "-020033.000123")
2348+
for (s, us), z in [((33, 123), "33.000123"), ((33, 0), "33"),]:
2349+
tz = timezone(-timedelta(hours=2, seconds=s, microseconds=us))
2350+
t = t.replace(tzinfo=tz)
2351+
self.assertEqual(t.strftime("%z"), "-0200" + z)
23512352

23522353
def test_extract(self):
23532354
dt = self.theclass(2002, 3, 4, 18, 45, 3, 1234)
@@ -3647,6 +3648,9 @@ def test_even_more_compare(self):
36473648
t2 = self.theclass.min
36483649
self.assertNotEqual(t1, t2)
36493650
self.assertEqual(t2, t2)
3651+
# and > comparison should fail
3652+
with self.assertRaises(TypeError):
3653+
t1 > t2
36503654

36513655
# It's also naive if it has tzinfo but tzinfo.utcoffset() is None.
36523656
class Naive(tzinfo):
@@ -5073,11 +5077,13 @@ def test_mixed_compare_fold(self):
50735077
t_fold = datetime(2002, 10, 27, 1, 45, tzinfo=Eastern2)
50745078
t_fold_utc = t_fold.astimezone(timezone.utc)
50755079
self.assertNotEqual(t_fold, t_fold_utc)
5080+
self.assertNotEqual(t_fold_utc, t_fold)
50765081

50775082
def test_mixed_compare_gap(self):
50785083
t_gap = datetime(2002, 4, 7, 2, 45, tzinfo=Eastern2)
50795084
t_gap_utc = t_gap.astimezone(timezone.utc)
50805085
self.assertNotEqual(t_gap, t_gap_utc)
5086+
self.assertNotEqual(t_gap_utc, t_gap)
50815087

50825088
def test_hash_aware(self):
50835089
t = datetime(2000, 1, 1, tzinfo=Eastern2)

0 commit comments

Comments
 (0)