@@ -2437,7 +2437,8 @@ def test_utcfromtimestamp(self):
24372437
24382438 ts = time .time ()
24392439 expected = time .gmtime (ts )
2440- got = self .theclass .utcfromtimestamp (ts )
2440+ with self .assertWarns (DeprecationWarning ):
2441+ got = self .theclass .utcfromtimestamp (ts )
24412442 self .verify_field_equality (expected , got )
24422443
24432444 # Run with US-style DST rules: DST begins 2 a.m. on second Sunday in
@@ -2483,8 +2484,12 @@ def test_timestamp_aware(self):
24832484
24842485 @support .run_with_tz ('MSK-03' ) # Something east of Greenwich
24852486 def test_microsecond_rounding (self ):
2487+ def utcfromtimestamp (* args , ** kwargs ):
2488+ with self .assertWarns (DeprecationWarning ):
2489+ return self .theclass .utcfromtimestamp (* args , ** kwargs )
2490+
24862491 for fts in [self .theclass .fromtimestamp ,
2487- self . theclass . utcfromtimestamp ]:
2492+ utcfromtimestamp ]:
24882493 zero = fts (0 )
24892494 self .assertEqual (zero .second , 0 )
24902495 self .assertEqual (zero .microsecond , 0 )
@@ -2581,10 +2586,11 @@ def test_fromtimestamp_limits(self):
25812586 self .theclass .fromtimestamp (ts )
25822587
25832588 def test_utcfromtimestamp_limits (self ):
2584- try :
2585- self .theclass .utcfromtimestamp (- 2 ** 32 - 1 )
2586- except (OSError , OverflowError ):
2587- self .skipTest ("Test not valid on this platform" )
2589+ with self .assertWarns (DeprecationWarning ):
2590+ try :
2591+ self .theclass .utcfromtimestamp (- 2 ** 32 - 1 )
2592+ except (OSError , OverflowError ):
2593+ self .skipTest ("Test not valid on this platform" )
25882594
25892595 min_dt = self .theclass .min .replace (tzinfo = timezone .utc )
25902596 min_ts = min_dt .timestamp ()
@@ -2597,10 +2603,11 @@ def test_utcfromtimestamp_limits(self):
25972603 ("maximum" , max_ts , max_dt .replace (tzinfo = None )),
25982604 ]:
25992605 with self .subTest (test_name , ts = ts , expected = expected ):
2600- try :
2601- actual = self .theclass .utcfromtimestamp (ts )
2602- except (OSError , OverflowError ) as exc :
2603- self .skipTest (str (exc ))
2606+ with self .assertWarns (DeprecationWarning ):
2607+ try :
2608+ actual = self .theclass .utcfromtimestamp (ts )
2609+ except (OSError , OverflowError ) as exc :
2610+ self .skipTest (str (exc ))
26042611
26052612 self .assertEqual (actual , expected )
26062613
@@ -2645,7 +2652,8 @@ def test_negative_float_fromtimestamp(self):
26452652
26462653 @unittest .skipIf (sys .platform == "win32" , "Windows doesn't accept negative timestamps" )
26472654 def test_negative_float_utcfromtimestamp (self ):
2648- d = self .theclass .utcfromtimestamp (- 1.05 )
2655+ with self .assertWarns (DeprecationWarning ):
2656+ d = self .theclass .utcfromtimestamp (- 1.05 )
26492657 self .assertEqual (d , self .theclass (1969 , 12 , 31 , 23 , 59 , 58 , 950000 ))
26502658
26512659 def test_utcnow (self ):
@@ -2655,8 +2663,11 @@ def test_utcnow(self):
26552663 # a second of each other.
26562664 tolerance = timedelta (seconds = 1 )
26572665 for dummy in range (3 ):
2658- from_now = self .theclass .utcnow ()
2659- from_timestamp = self .theclass .utcfromtimestamp (time .time ())
2666+ with self .assertWarns (DeprecationWarning ):
2667+ from_now = self .theclass .utcnow ()
2668+
2669+ with self .assertWarns (DeprecationWarning ):
2670+ from_timestamp = self .theclass .utcfromtimestamp (time .time ())
26602671 if abs (from_timestamp - from_now ) <= tolerance :
26612672 break
26622673 # Else try again a few times.
@@ -2956,7 +2967,11 @@ def __new__(cls, *args, **kwargs):
29562967 constr_name = constr_name ):
29572968 constructor = getattr (base_obj , constr_name )
29582969
2959- dt = constructor (* constr_args )
2970+ if constr_name == "utcfromtimestamp" :
2971+ with self .assertWarns (DeprecationWarning ):
2972+ dt = constructor (* constr_args )
2973+ else :
2974+ dt = constructor (* constr_args )
29602975
29612976 # Test that it creates the right subclass
29622977 self .assertIsInstance (dt , DateTimeSubclass )
@@ -2986,7 +3001,11 @@ def __new__(cls, *args, **kwargs):
29863001 for name , meth_name , kwargs in test_cases :
29873002 with self .subTest (name ):
29883003 constr = getattr (DateTimeSubclass , meth_name )
2989- dt = constr (** kwargs )
3004+ if constr == "utcnow" :
3005+ with self .assertWarns (DeprecationWarning ):
3006+ dt = constr (** kwargs )
3007+ else :
3008+ dt = constr (** kwargs )
29903009
29913010 self .assertIsInstance (dt , DateTimeSubclass )
29923011 self .assertEqual (dt .extra , 7 )
@@ -4642,7 +4661,8 @@ def test_tzinfo_now(self):
46424661 for dummy in range (3 ):
46434662 now = datetime .now (weirdtz )
46444663 self .assertIs (now .tzinfo , weirdtz )
4645- utcnow = datetime .utcnow ().replace (tzinfo = utc )
4664+ with self .assertWarns (DeprecationWarning ):
4665+ utcnow = datetime .utcnow ().replace (tzinfo = utc )
46464666 now2 = utcnow .astimezone (weirdtz )
46474667 if abs (now - now2 ) < timedelta (seconds = 30 ):
46484668 break
@@ -4676,7 +4696,8 @@ def test_tzinfo_fromtimestamp(self):
46764696
46774697 # Try to make sure tz= actually does some conversion.
46784698 timestamp = 1000000000
4679- utcdatetime = datetime .utcfromtimestamp (timestamp )
4699+ with self .assertWarns (DeprecationWarning ):
4700+ utcdatetime = datetime .utcfromtimestamp (timestamp )
46804701 # In POSIX (epoch 1970), that's 2001-09-09 01:46:40 UTC, give or take.
46814702 # But on some flavor of Mac, it's nowhere near that. So we can't have
46824703 # any idea here what time that actually is, we can only test that
@@ -4690,7 +4711,8 @@ def test_tzinfo_fromtimestamp(self):
46904711 def test_tzinfo_utcnow (self ):
46914712 meth = self .theclass .utcnow
46924713 # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
4693- base = meth ()
4714+ with self .assertWarns (DeprecationWarning ):
4715+ base = meth ()
46944716 # Try with and without naming the keyword; for whatever reason,
46954717 # utcnow() doesn't accept a tzinfo argument.
46964718 off42 = FixedOffset (42 , "42" )
@@ -4702,7 +4724,8 @@ def test_tzinfo_utcfromtimestamp(self):
47024724 meth = self .theclass .utcfromtimestamp
47034725 ts = time .time ()
47044726 # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
4705- base = meth (ts )
4727+ with self .assertWarns (DeprecationWarning ):
4728+ base = meth (ts )
47064729 # Try with and without naming the keyword; for whatever reason,
47074730 # utcfromtimestamp() doesn't accept a tzinfo argument.
47084731 off42 = FixedOffset (42 , "42" )
@@ -5309,7 +5332,7 @@ def dst(self, dt):
53095332
53105333 def test_fromutc (self ):
53115334 self .assertRaises (TypeError , Eastern .fromutc ) # not enough args
5312- now = datetime .utcnow (). replace ( tzinfo = utc_real )
5335+ now = datetime .now ( tz = utc_real )
53135336 self .assertRaises (ValueError , Eastern .fromutc , now ) # wrong tzinfo
53145337 now = now .replace (tzinfo = Eastern ) # insert correct tzinfo
53155338 enow = Eastern .fromutc (now ) # doesn't blow up
@@ -5411,9 +5434,11 @@ def test_bug_1028306(self):
54115434 self .assertEqual (datetime_sc , as_datetime )
54125435
54135436 def test_extra_attributes (self ):
5437+ with self .assertWarns (DeprecationWarning ):
5438+ utcnow = datetime .utcnow ()
54145439 for x in [date .today (),
54155440 time (),
5416- datetime . utcnow () ,
5441+ utcnow ,
54175442 timedelta (),
54185443 tzinfo (),
54195444 timezone (timedelta ())]:
@@ -6073,6 +6098,7 @@ def stats(cls, start_year=1):
60736098 def transitions (self ):
60746099 for (_ , prev_ti ), (t , ti ) in pairs (zip (self .ut , self .ti )):
60756100 shift = ti [0 ] - prev_ti [0 ]
6101+ # TODO: Remove this use of utcfromtimestamp
60766102 yield datetime .utcfromtimestamp (t ), shift
60776103
60786104 def nondst_folds (self ):
0 commit comments