Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle UNIX timestamps in handle_datetime #374

Merged
merged 9 commits into from
Feb 16, 2024
3 changes: 3 additions & 0 deletions becquerel/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import datetime
from dateutil.parser import parse as dateutil_parse
from dateutil.parser import ParserError
from numbers import Number
from uncertainties import UFloat, unumpy
import warnings
import numpy as np
Expand Down Expand Up @@ -101,6 +102,8 @@ def handle_datetime(input_time, error_name="datetime arg", allow_none=False):
"datetime.date passed in with no time; defaulting to 0:00 on date"
)
return datetime.datetime(input_time.year, input_time.month, input_time.day)
elif isinstance(input_time, Number):
return datetime.datetime.utcfromtimestamp(input_time)
elif isinstance(input_time, str):
try:
return dateutil_parse(input_time)
Expand Down
1 change: 0 additions & 1 deletion tests/isotope_qty_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ def test_handle_datetime(date_in):
"iso, date, kwargs, error",
[
(["Cs-137"], None, {"atoms": 1e24}, TypeError),
("Cs-137", 123, {"bq": 456}, TypeError),
("Cs-137", datetime.datetime.now(), {"asdf": 3}, IsotopeQuantityError),
("Cs-137", None, {"bq": -13.3}, ValueError),
],
Expand Down
6 changes: 2 additions & 4 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def test_sqrt_bins():
datetime.datetime(year=2023, month=6, day=14, hour=0, minute=0, second=0),
"2023_06_14_00_00_00",
"2023-06-14T00:00:00.000Z-0000", # ISO 8601, with timezone
1686700800.0, # UNIX timestamp
],
)
def test_handle_datetime(timestamp):
Expand All @@ -51,10 +52,7 @@ def test_handle_datetime_None():

@pytest.mark.parametrize(
"arg,error_type",
[
("2023_06_14-08_01_02", ValueError),
(2022, TypeError),
],
[("2023_06_14-08_01_02", ValueError)],
)
def test_handle_datetime_err(arg, error_type):
with pytest.raises(error_type):
Expand Down
Loading