Skip to content

Commit

Permalink
fix(time): update deprecated 'utcfromtimestamp'
Browse files Browse the repository at this point in the history
  • Loading branch information
ljgray committed Sep 26, 2024
1 parent fcd4307 commit aa65a49
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions caput/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"""

import warnings
from datetime import datetime
from datetime import datetime, timezone

import numpy as np
from scipy.optimize import brentq
Expand Down Expand Up @@ -982,7 +982,7 @@ def era_to_unix(era, time0):
def unix_to_datetime(unix_time):
"""Converts unix time to a :class:`~datetime.datetime` object.
Equivalent to :meth:`datetime.datetime.utcfromtimestamp`.
Equivalent to timezone-aware :meth:`datetime.datetime.fromtimestamp`.
Parameters
----------
Expand All @@ -998,7 +998,7 @@ def unix_to_datetime(unix_time):
--------
:func:`datetime_to_unix`
"""
dt = datetime.utcfromtimestamp(unix_time)
dt = datetime.fromtimestamp(unix_time, timezone.utc)

return naive_datetime_to_utc(dt)

Expand Down Expand Up @@ -1026,7 +1026,7 @@ def datetime_to_unix(dt):
"""
# Noting that this operation is ignorant of leap seconds.
dt = naive_datetime_to_utc(dt)
epoch_start = naive_datetime_to_utc(datetime.utcfromtimestamp(0))
epoch_start = naive_datetime_to_utc(datetime.fromtimestamp(0, timezone.utc))
since_epoch = dt - epoch_start
return since_epoch.total_seconds()

Expand Down Expand Up @@ -1195,7 +1195,7 @@ def time_of_day(time):
time : float
Time since start of UTC day in seconds.
"""
dt = datetime.utcfromtimestamp(ensure_unix(time))
dt = datetime.fromtimestamp(ensure_unix(time), timezone.utc)
d = dt.replace(hour=0, minute=0, second=0, microsecond=0)
return (dt - d).total_seconds()

Expand Down
6 changes: 3 additions & 3 deletions tests/test_time.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import random
import time
from datetime import datetime
from datetime import datetime, timezone

import numpy as np
import pytest
Expand Down Expand Up @@ -254,7 +254,7 @@ def test_from_unix_time():
"""

unix_time = random.random() * 2e6
dt = datetime.utcfromtimestamp(unix_time)
dt = datetime.fromtimestamp(unix_time, timezone.utc)
st = ctime.unix_to_skyfield_time(unix_time)
new_dt = st.utc_datetime()
assert dt.year == new_dt.year
Expand Down Expand Up @@ -287,7 +287,7 @@ def test_time_precision():

def test_datetime_to_unix():
unix_time = time.time()
dt = datetime.utcfromtimestamp(unix_time)
dt = datetime.fromtimestamp(unix_time, timezone.utc)
new_unix_time = ctime.datetime_to_unix(dt)
assert new_unix_time == approx(unix_time, abs=1e-5)

Expand Down

0 comments on commit aa65a49

Please sign in to comment.