Skip to content

Commit

Permalink
Fix #892: [Bug] UnixTimeS & UnixTimeMS.now() ignore time zone
Browse files Browse the repository at this point in the history
  • Loading branch information
trentmc committed Apr 15, 2024
1 parent 9367b33 commit 885376e
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion pdr_backend/util/test_noganache/test_time_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
from enforce_typing import enforce_types

from pdr_backend.util.time_types import UnixTimeMs
from pdr_backend.util.time_types import UnixTimeMs, UnixTimeS


@enforce_types
Expand Down Expand Up @@ -113,3 +113,26 @@ def test_dt_to_ut_and_back():

dt2 = ut.to_dt()
assert dt2 == dt


@enforce_types
def test_timezones():
# set targets
dt = datetime.datetime.now()
dt = dt.replace(tzinfo=timezone.utc) # tack on timezone
ut_s_target = dt.timestamp()
ut_ms_target = ut_s_target * 1000

# set tolerances
tol_s = 1
tol_ms = tol_s * 1000

# test UnixTimeS
# - if it's off by 7200, that's 3600 * 2 --> 2 timezones from UTC
ut_s = UnixTimeS.now()
assert ut_s == pytest.approx(ut_s_target, abs=tol_s)

# test UnixTimeMs
# - if it's off by 7200*1000, that's 3600 * 2 * 1000 --> 2 timezones fr UTC
ut_ms = UnixTimeMs().now()
assert ut_ms == pytest.approx(ut_ms_target, abs=tol_ms)

0 comments on commit 885376e

Please sign in to comment.