From 8bdba2a1537d3d0cecefa629febaa6ecf2d30041 Mon Sep 17 00:00:00 2001 From: "P. L. Lim" <2090236+pllim@users.noreply.github.com> Date: Thu, 21 Sep 2023 20:23:13 -0400 Subject: [PATCH] Backport PR #15298: Address ruff's DTZ003 rule violations --- astropy/time/core.py | 10 +++++----- astropy/time/tests/test_basic.py | 7 +++++-- astropy/utils/iers/iers.py | 6 ++++-- docs/conf.py | 4 ++-- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/astropy/time/core.py b/astropy/time/core.py index 896940f3105..f6d2ae787c1 100644 --- a/astropy/time/core.py +++ b/astropy/time/core.py @@ -11,7 +11,7 @@ import operator import os import threading -from datetime import date, datetime, timedelta +from datetime import date, datetime, timedelta, timezone from time import strftime from warnings import warn @@ -1930,10 +1930,10 @@ def now(cls): method is called. .. note:: - "Now" is determined using the `~datetime.datetime.utcnow` + "Now" is determined using the `~datetime.datetime.now` function, so its accuracy and precision is determined by that function. Generally that means it is set by the accuracy of - your system clock. + your system clock. The timezone is set to UTC. Returns ------- @@ -1941,8 +1941,8 @@ def now(cls): A new `Time` object (or a subclass of `Time` if this is called from such a subclass) at the current time. """ - # call `utcnow` immediately to be sure it's ASAP - dtnow = datetime.utcnow() + # call `now` immediately to be sure it's ASAP + dtnow = datetime.now(tz=timezone.utc) return cls(val=dtnow, format="datetime", scale="utc") info = TimeInfo() diff --git a/astropy/time/tests/test_basic.py b/astropy/time/tests/test_basic.py index a06aead92ec..356598b229a 100644 --- a/astropy/time/tests/test_basic.py +++ b/astropy/time/tests/test_basic.py @@ -1475,7 +1475,10 @@ def test_now(): Tests creating a Time object with the `now` class method. """ - now = datetime.datetime.utcnow() + # `Time.datetime` is not timezone aware, meaning `.replace` is necessary for + # `now` also not be timezone aware. + now = datetime.datetime.now(tz=datetime.timezone.utc).replace(tzinfo=None) + t = Time.now() assert t.format == "datetime" @@ -1483,7 +1486,7 @@ def test_now(): dt = t.datetime - now # a datetime.timedelta object - # this gives a .1 second margin between the `utcnow` call and the `Time` + # this gives a .1 second margin between the `now` call and the `Time` # initializer, which is really way more generous than necessary - typical # times are more like microseconds. But it seems safer in case some # platforms have slow clock calls or something. diff --git a/astropy/utils/iers/iers.py b/astropy/utils/iers/iers.py index 1ea3903456a..c4c1d9df138 100644 --- a/astropy/utils/iers/iers.py +++ b/astropy/utils/iers/iers.py @@ -10,7 +10,7 @@ """ import re -from datetime import datetime +from datetime import datetime, timezone from urllib.parse import urlparse from warnings import warn @@ -1077,7 +1077,9 @@ def open(cls, file=None, cache=False): def _today(): # Get current day in scale='tai' without going through a scale change # (so we do not need leap seconds). - s = "{0.year:04d}-{0.month:02d}-{0.day:02d}".format(datetime.utcnow()) + s = "{0.year:04d}-{0.month:02d}-{0.day:02d}".format( + datetime.now(tz=timezone.utc) + ) return Time(s, scale="tai", format="iso", out_subfmt="date") @classmethod diff --git a/docs/conf.py b/docs/conf.py index 7992e23358c..49a65fdecd2 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -28,7 +28,7 @@ import doctest import os import sys -from datetime import datetime +from datetime import datetime, timezone from importlib import metadata from packaging.requirements import Requirement @@ -208,7 +208,7 @@ project = "Astropy" author = "The Astropy Developers" -copyright = f"2011–{datetime.utcnow().year}, " + author +copyright = f"2011–{datetime.now(tz=timezone.utc).year}, " + author # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the