@@ -2437,7 +2437,8 @@ def test_utcfromtimestamp(self):
2437
2437
2438
2438
ts = time .time ()
2439
2439
expected = time .gmtime (ts )
2440
- got = self .theclass .utcfromtimestamp (ts )
2440
+ with self .assertWarns (DeprecationWarning ):
2441
+ got = self .theclass .utcfromtimestamp (ts )
2441
2442
self .verify_field_equality (expected , got )
2442
2443
2443
2444
# Run with US-style DST rules: DST begins 2 a.m. on second Sunday in
@@ -2483,8 +2484,12 @@ def test_timestamp_aware(self):
2483
2484
2484
2485
@support .run_with_tz ('MSK-03' ) # Something east of Greenwich
2485
2486
def test_microsecond_rounding (self ):
2487
+ def utcfromtimestamp (* args , ** kwargs ):
2488
+ with self .assertWarns (DeprecationWarning ):
2489
+ return self .theclass .utcfromtimestamp (* args , ** kwargs )
2490
+
2486
2491
for fts in [self .theclass .fromtimestamp ,
2487
- self . theclass . utcfromtimestamp ]:
2492
+ utcfromtimestamp ]:
2488
2493
zero = fts (0 )
2489
2494
self .assertEqual (zero .second , 0 )
2490
2495
self .assertEqual (zero .microsecond , 0 )
@@ -2581,10 +2586,11 @@ def test_fromtimestamp_limits(self):
2581
2586
self .theclass .fromtimestamp (ts )
2582
2587
2583
2588
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" )
2588
2594
2589
2595
min_dt = self .theclass .min .replace (tzinfo = timezone .utc )
2590
2596
min_ts = min_dt .timestamp ()
@@ -2597,10 +2603,11 @@ def test_utcfromtimestamp_limits(self):
2597
2603
("maximum" , max_ts , max_dt .replace (tzinfo = None )),
2598
2604
]:
2599
2605
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 ))
2604
2611
2605
2612
self .assertEqual (actual , expected )
2606
2613
@@ -2645,7 +2652,8 @@ def test_negative_float_fromtimestamp(self):
2645
2652
2646
2653
@unittest .skipIf (sys .platform == "win32" , "Windows doesn't accept negative timestamps" )
2647
2654
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 )
2649
2657
self .assertEqual (d , self .theclass (1969 , 12 , 31 , 23 , 59 , 58 , 950000 ))
2650
2658
2651
2659
def test_utcnow (self ):
@@ -2655,8 +2663,11 @@ def test_utcnow(self):
2655
2663
# a second of each other.
2656
2664
tolerance = timedelta (seconds = 1 )
2657
2665
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 ())
2660
2671
if abs (from_timestamp - from_now ) <= tolerance :
2661
2672
break
2662
2673
# Else try again a few times.
@@ -2956,7 +2967,11 @@ def __new__(cls, *args, **kwargs):
2956
2967
constr_name = constr_name ):
2957
2968
constructor = getattr (base_obj , constr_name )
2958
2969
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 )
2960
2975
2961
2976
# Test that it creates the right subclass
2962
2977
self .assertIsInstance (dt , DateTimeSubclass )
@@ -2986,7 +3001,11 @@ def __new__(cls, *args, **kwargs):
2986
3001
for name , meth_name , kwargs in test_cases :
2987
3002
with self .subTest (name ):
2988
3003
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 )
2990
3009
2991
3010
self .assertIsInstance (dt , DateTimeSubclass )
2992
3011
self .assertEqual (dt .extra , 7 )
@@ -4642,7 +4661,8 @@ def test_tzinfo_now(self):
4642
4661
for dummy in range (3 ):
4643
4662
now = datetime .now (weirdtz )
4644
4663
self .assertIs (now .tzinfo , weirdtz )
4645
- utcnow = datetime .utcnow ().replace (tzinfo = utc )
4664
+ with self .assertWarns (DeprecationWarning ):
4665
+ utcnow = datetime .utcnow ().replace (tzinfo = utc )
4646
4666
now2 = utcnow .astimezone (weirdtz )
4647
4667
if abs (now - now2 ) < timedelta (seconds = 30 ):
4648
4668
break
@@ -4676,7 +4696,8 @@ def test_tzinfo_fromtimestamp(self):
4676
4696
4677
4697
# Try to make sure tz= actually does some conversion.
4678
4698
timestamp = 1000000000
4679
- utcdatetime = datetime .utcfromtimestamp (timestamp )
4699
+ with self .assertWarns (DeprecationWarning ):
4700
+ utcdatetime = datetime .utcfromtimestamp (timestamp )
4680
4701
# In POSIX (epoch 1970), that's 2001-09-09 01:46:40 UTC, give or take.
4681
4702
# But on some flavor of Mac, it's nowhere near that. So we can't have
4682
4703
# any idea here what time that actually is, we can only test that
@@ -4690,7 +4711,8 @@ def test_tzinfo_fromtimestamp(self):
4690
4711
def test_tzinfo_utcnow (self ):
4691
4712
meth = self .theclass .utcnow
4692
4713
# 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 ()
4694
4716
# Try with and without naming the keyword; for whatever reason,
4695
4717
# utcnow() doesn't accept a tzinfo argument.
4696
4718
off42 = FixedOffset (42 , "42" )
@@ -4702,7 +4724,8 @@ def test_tzinfo_utcfromtimestamp(self):
4702
4724
meth = self .theclass .utcfromtimestamp
4703
4725
ts = time .time ()
4704
4726
# 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 )
4706
4729
# Try with and without naming the keyword; for whatever reason,
4707
4730
# utcfromtimestamp() doesn't accept a tzinfo argument.
4708
4731
off42 = FixedOffset (42 , "42" )
@@ -5309,7 +5332,7 @@ def dst(self, dt):
5309
5332
5310
5333
def test_fromutc (self ):
5311
5334
self .assertRaises (TypeError , Eastern .fromutc ) # not enough args
5312
- now = datetime .utcnow (). replace ( tzinfo = utc_real )
5335
+ now = datetime .now ( tz = utc_real )
5313
5336
self .assertRaises (ValueError , Eastern .fromutc , now ) # wrong tzinfo
5314
5337
now = now .replace (tzinfo = Eastern ) # insert correct tzinfo
5315
5338
enow = Eastern .fromutc (now ) # doesn't blow up
@@ -5411,9 +5434,11 @@ def test_bug_1028306(self):
5411
5434
self .assertEqual (datetime_sc , as_datetime )
5412
5435
5413
5436
def test_extra_attributes (self ):
5437
+ with self .assertWarns (DeprecationWarning ):
5438
+ utcnow = datetime .utcnow ()
5414
5439
for x in [date .today (),
5415
5440
time (),
5416
- datetime . utcnow () ,
5441
+ utcnow ,
5417
5442
timedelta (),
5418
5443
tzinfo (),
5419
5444
timezone (timedelta ())]:
@@ -6073,6 +6098,7 @@ def stats(cls, start_year=1):
6073
6098
def transitions (self ):
6074
6099
for (_ , prev_ti ), (t , ti ) in pairs (zip (self .ut , self .ti )):
6075
6100
shift = ti [0 ] - prev_ti [0 ]
6101
+ # TODO: Remove this use of utcfromtimestamp
6076
6102
yield datetime .utcfromtimestamp (t ), shift
6077
6103
6078
6104
def nondst_folds (self ):
0 commit comments