Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ ignore = [

# flake8-datetimez (DTZ)
"DTZ001", # call-datetime-without-tzinfo
"DTZ003", # call-datetime-utcnow
"DTZ005", # call-datetime-now-without-tzinfo
"DTZ007", # call-datetime-strptime-without-zone

Expand Down
10 changes: 5 additions & 5 deletions astropy/time/core.py
Original file line number Diff line number Diff line change
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 @@ -1928,19 +1928,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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a new (optional) argument should be added to now that allows the user to specify the timezone.
The default value should be gotten from time.tzname, corrected for daylight savings with 'time.localtime().tm_isdst (see https://stackoverflow.com/questions/1111056/get-time-zone-information-of-the-system-in-python and https://bugs.python.org/issue22798).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think time.localtime().tm_gmtoff will be necessary to get the offset from utc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

daylight savings corrections might use tzinfo.dst instead of tm_isdst. I haven't investigated details

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of an optional argument, but it would be a big API change to make Time.now() return non-UTC times, so I think the default has to be timezone.utc. Obviously, this could also be done as follow-up, since here the idea is just to avoid the utcnow() method. Indeed, given the need to get this in for #14784, perhaps it is best to make minimal changes here.

return cls(val=dtnow, format="datetime", scale="utc")

info = TimeInfo()
Expand Down
7 changes: 5 additions & 2 deletions astropy/time/tests/test_basic.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

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

Expand Down Expand Up @@ -1091,7 +1091,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
Original file line number Diff line number Diff line change
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