Skip to content

Commit c2e5c4a

Browse files
Fix #1012: stop using deprecated utcnow()
1 parent 767b1cb commit c2e5c4a

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

CHANGELOG.rst

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Unreleased future version
2222
reference frame was off by nearly 0.02 arcseconds because it applied
2323
precession but forgot to apply the frame tie between ICRS and J2000.
2424

25+
* To avoid deprecation warnings on recent Pythons, Skyfield now avoids
26+
Python’s old ``datetime.utcnow()`` method.
27+
2528
v1.51 — 2025 February 23
2629
------------------------
2730

skyfield/tests/fixes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def setup(utc=(2015, 10, 11, 10)):
2929

3030
def teardown():
3131
skyfield.api.load = skyfield.iokit.Loader('.')
32-
skyfield.timelib.Timescale._utcnow = dt.datetime.utcnow
32+
skyfield.timelib.Timescale._utcnow = skyfield.timelib._utcnow
3333
dt.datetime = _real_datetime_class
3434

3535
class low_precision_ERA(object):

skyfield/tests/test_timelib.py

+4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ def test_time_creation_methods(ts, continuous_timescale, time_value):
110110
string = strftime('%S.%f')
111111
assert string == '37.500000'
112112

113+
def test_now(ts):
114+
t = ts.now()
115+
assert isinstance(t, Time)
116+
113117
def test_months_overflow_correctly(ts):
114118
assert ts.tt(2020, -1).tt_strftime('%Y-%m') == '2019-11'
115119
assert ts.tt(2020, 15).tt_strftime('%Y-%m') == '2021-03'

skyfield/timelib.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ def second(self): return self[5]
4949

5050
if hasattr(dt_module, 'timezone'):
5151
utc = dt_module.timezone.utc
52+
@staticmethod
53+
def _utcnow():
54+
return datetime.now(dt_module.UTC)
5255
else:
5356
class UTC(dt_module.tzinfo):
5457
'UTC'
@@ -61,6 +64,7 @@ def dst(self, dt):
6164
return self.zero
6265

6366
utc = UTC()
67+
_utcnow = datetime.utcnow
6468

6569
# Much of the following code is adapted from the USNO's "novas.c".
6670

@@ -89,7 +93,7 @@ class Timescale(object):
8993
timescale.
9094
9195
"""
92-
_utcnow = datetime.utcnow
96+
_utcnow = _utcnow
9397
polar_motion_table = None
9498

9599
def __init__(self, delta_t_recent, leap_dates, leap_offsets):
@@ -123,14 +127,7 @@ def __init__(self, delta_t_recent, leap_dates, leap_offsets):
123127
self._leap_tai = self._leap_utc + self._leap_offsets
124128

125129
def now(self):
126-
"""Return the current date and time as a `Time` object.
127-
128-
For the return value to be correct, your operating system time
129-
and timezone settings must be set so that the Python Standard
130-
Library constructor ``datetime.datetime.utcnow()`` returns a
131-
correct UTC date and time.
132-
133-
"""
130+
"""Return the current date and time as a `Time` object."""
134131
return self.from_datetime(self._utcnow().replace(tzinfo=utc))
135132

136133
def from_datetime(self, datetime):

0 commit comments

Comments
 (0)