ts = time.time()
expected = time.gmtime(ts)
- got = self.theclass.utcfromtimestamp(ts)
+ with self.assertWarns(DeprecationWarning):
+ got = self.theclass.utcfromtimestamp(ts)
self.verify_field_equality(expected, got)
# Run with US-style DST rules: DST begins 2 a.m. on second Sunday in
@support.run_with_tz('MSK-03') # Something east of Greenwich
def test_microsecond_rounding(self):
+ def utcfromtimestamp(*args, **kwargs):
+ with self.assertWarns(DeprecationWarning):
+ return self.theclass.utcfromtimestamp(*args, **kwargs)
+
for fts in [self.theclass.fromtimestamp,
- self.theclass.utcfromtimestamp]:
+ utcfromtimestamp]:
zero = fts(0)
self.assertEqual(zero.second, 0)
self.assertEqual(zero.microsecond, 0)
self.theclass.fromtimestamp(ts)
def test_utcfromtimestamp_limits(self):
- try:
- self.theclass.utcfromtimestamp(-2**32 - 1)
- except (OSError, OverflowError):
- self.skipTest("Test not valid on this platform")
+ with self.assertWarns(DeprecationWarning):
+ try:
+ self.theclass.utcfromtimestamp(-2**32 - 1)
+ except (OSError, OverflowError):
+ self.skipTest("Test not valid on this platform")
min_dt = self.theclass.min.replace(tzinfo=timezone.utc)
min_ts = min_dt.timestamp()
("maximum", max_ts, max_dt.replace(tzinfo=None)),
]:
with self.subTest(test_name, ts=ts, expected=expected):
- try:
- actual = self.theclass.utcfromtimestamp(ts)
- except (OSError, OverflowError) as exc:
- self.skipTest(str(exc))
+ with self.assertWarns(DeprecationWarning):
+ try:
+ actual = self.theclass.utcfromtimestamp(ts)
+ except (OSError, OverflowError) as exc:
+ self.skipTest(str(exc))
self.assertEqual(actual, expected)
@unittest.skipIf(sys.platform == "win32", "Windows doesn't accept negative timestamps")
def test_negative_float_utcfromtimestamp(self):
- d = self.theclass.utcfromtimestamp(-1.05)
+ with self.assertWarns(DeprecationWarning):
+ d = self.theclass.utcfromtimestamp(-1.05)
self.assertEqual(d, self.theclass(1969, 12, 31, 23, 59, 58, 950000))
def test_utcnow(self):
# a second of each other.
tolerance = timedelta(seconds=1)
for dummy in range(3):
- from_now = self.theclass.utcnow()
- from_timestamp = self.theclass.utcfromtimestamp(time.time())
+ with self.assertWarns(DeprecationWarning):
+ from_now = self.theclass.utcnow()
+
+ with self.assertWarns(DeprecationWarning):
+ from_timestamp = self.theclass.utcfromtimestamp(time.time())
if abs(from_timestamp - from_now) <= tolerance:
break
# Else try again a few times.
constr_name=constr_name):
constructor = getattr(base_obj, constr_name)
- dt = constructor(*constr_args)
+ if constr_name == "utcfromtimestamp":
+ with self.assertWarns(DeprecationWarning):
+ dt = constructor(*constr_args)
+ else:
+ dt = constructor(*constr_args)
# Test that it creates the right subclass
self.assertIsInstance(dt, DateTimeSubclass)
for name, meth_name, kwargs in test_cases:
with self.subTest(name):
constr = getattr(DateTimeSubclass, meth_name)
- dt = constr(**kwargs)
+ if constr == "utcnow":
+ with self.assertWarns(DeprecationWarning):
+ dt = constr(**kwargs)
+ else:
+ dt = constr(**kwargs)
self.assertIsInstance(dt, DateTimeSubclass)
self.assertEqual(dt.extra, 7)
for dummy in range(3):
now = datetime.now(weirdtz)
self.assertIs(now.tzinfo, weirdtz)
- utcnow = datetime.utcnow().replace(tzinfo=utc)
+ with self.assertWarns(DeprecationWarning):
+ utcnow = datetime.utcnow().replace(tzinfo=utc)
now2 = utcnow.astimezone(weirdtz)
if abs(now - now2) < timedelta(seconds=30):
break
# Try to make sure tz= actually does some conversion.
timestamp = 1000000000
- utcdatetime = datetime.utcfromtimestamp(timestamp)
+ with self.assertWarns(DeprecationWarning):
+ utcdatetime = datetime.utcfromtimestamp(timestamp)
# In POSIX (epoch 1970), that's 2001-09-09 01:46:40 UTC, give or take.
# But on some flavor of Mac, it's nowhere near that. So we can't have
# any idea here what time that actually is, we can only test that
def test_tzinfo_utcnow(self):
meth = self.theclass.utcnow
# Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
- base = meth()
+ with self.assertWarns(DeprecationWarning):
+ base = meth()
# Try with and without naming the keyword; for whatever reason,
# utcnow() doesn't accept a tzinfo argument.
off42 = FixedOffset(42, "42")
meth = self.theclass.utcfromtimestamp
ts = time.time()
# Ensure it doesn't require tzinfo (i.e., that this doesn't blow up).
- base = meth(ts)
+ with self.assertWarns(DeprecationWarning):
+ base = meth(ts)
# Try with and without naming the keyword; for whatever reason,
# utcfromtimestamp() doesn't accept a tzinfo argument.
off42 = FixedOffset(42, "42")
def test_fromutc(self):
self.assertRaises(TypeError, Eastern.fromutc) # not enough args
- now = datetime.utcnow().replace(tzinfo=utc_real)
+ now = datetime.now(tz=utc_real)
self.assertRaises(ValueError, Eastern.fromutc, now) # wrong tzinfo
now = now.replace(tzinfo=Eastern) # insert correct tzinfo
enow = Eastern.fromutc(now) # doesn't blow up
self.assertEqual(datetime_sc, as_datetime)
def test_extra_attributes(self):
+ with self.assertWarns(DeprecationWarning):
+ utcnow = datetime.utcnow()
for x in [date.today(),
time(),
- datetime.utcnow(),
+ utcnow,
timedelta(),
tzinfo(),
timezone(timedelta())]:
def transitions(self):
for (_, prev_ti), (t, ti) in pairs(zip(self.ut, self.ti)):
shift = ti[0] - prev_ti[0]
+ # TODO: Remove this use of utcfromtimestamp
yield datetime.utcfromtimestamp(t), shift
def nondst_folds(self):