Source code for pysurfex.datetime_utils
#!/usr/bin/env python3
"""Implement helper routines to deal with dates and times."""
from datetime import date, datetime, timedelta, timezone
# TODO use ISO times
[docs]def as_datetime(dtg):
"""Convert string to datetime."""
if len(dtg) == 10:
fmt = "%Y%m%d%H"
elif len(dtg) == 12:
fmt = "%Y%m%d%H%M"
elif len(dtg) == 14:
fmt = "%Y%m%d%H%M%S"
else:
raise RuntimeError(f"dtg={dtg} len(dtg) is {len(dtg)}")
return datetime.strptime(dtg, fmt).replace(tzinfo=timezone.utc)
[docs]def as_datetime_string(dtg):
"""Convert string to datetime."""
fmt = "%Y%m%d%H%M%S"
return dtg.strftime(fmt)
[docs]def offsetaware(dtg):
"""Make offset aware."""
return dtg.replace(tzinfo=timezone.utc)
[docs]def as_timedelta(seconds=0):
"""Convert seconds to timedelta."""
return timedelta(seconds=seconds)
[docs]def fromtimestamp(validtime):
"""Convert timestamp to validtime."""
return datetime.fromtimestamp(validtime)
[docs]def utcfromtimestamp(epochtime):
"""Convert timestamp to validtime."""
return datetime.utcfromtimestamp(epochtime).replace(tzinfo=timezone.utc)
[docs]def isdatetime(obj):
"""Check if is a datetime objects."""
return isinstance(obj, date)
[docs]def as_datetime_args(year=None, month=None, day=None, hour=0, minute=0, second=0):
"""Set datetime object from args."""
return datetime(
year=year, month=month, day=day, hour=hour, minute=minute, second=second
).replace(tzinfo=timezone.utc)