Skip to content

Commit

Permalink
Merge pull request astropy#15371 from pllim/backport-of-pr-15298-on-v…
Browse files Browse the repository at this point in the history
…5.3.x

Backport PR astropy#15298 on branch v5.3.x (Address ruff's DTZ003 rule violations)
  • Loading branch information
pllim committed Sep 22, 2023
2 parents 5753610 + 8bdba2a commit 544714a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
10 changes: 5 additions & 5 deletions astropy/time/core.py
Expand Up @@ -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

Expand Down Expand Up @@ -1930,19 +1930,19 @@ 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
-------
nowtime : :class:`~astropy.time.Time`
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()
Expand Down
7 changes: 5 additions & 2 deletions astropy/time/tests/test_basic.py
Expand Up @@ -1475,15 +1475,18 @@ 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"
assert t.scale == "utc"

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.
Expand Down
6 changes: 4 additions & 2 deletions astropy/utils/iers/iers.py
Expand Up @@ -10,7 +10,7 @@
"""

import re
from datetime import datetime
from datetime import datetime, timezone
from urllib.parse import urlparse
from warnings import warn

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 544714a

Please sign in to comment.