ssb_timeseries.dates¶
Helper module for date and time utility functions.
Notable examples include converting between UTC and local time, standardised string formats for file names, and (planned for future use) intervals.
- date_cet(some_date, **kwargs)¶
Convert date to time_zone CET (preserve instant).
Note that this is NOT the same as Europe/Oslo. Europe/Oslo shifts between CET and CEST. For most cases, one should use date_eur_no or `date_tz(…, <TZ>.
The output can be rounded to the precision specified by kwarg ‘rounding’.
- Return type:
datetime- Parameters:
some_date (datetime | str)
- date_eur_no(some_date, **kwargs)¶
Convert date to time_zone Europe/Oslo /preserve instant).
Note that Europe/Oslo provides automatic shifts between CET and CEST. The output can be rounded to the precision specified by kwarg ‘rounding’.
- Return type:
datetime- Parameters:
some_date (datetime | str)
- date_local(some_date, **kwargs)¶
Convert date to default timezone (preserve instant).
If not configured otherwise the default is Europe/Oslo which provides automatic shifts between CET and CEST. The output can be rounded to the precision specified by kwarg ‘rounding’.
- Return type:
datetime- Parameters:
some_date (datetime | str)
- date_round(d, **kwargs)¶
Round date to specified by kwarg ‘rounding’ or default precision MAX_TIME_PRECISION.
Rounding can take the values ‘none’, ‘day’, ‘d’, ‘hour’, ‘h’, ‘minute’, ‘min’, ‘m’, ‘second’, ‘sec’, or ‘s’.
Default precision ‘seconds’ is used if none is provided.
- Return type:
datetime- Parameters:
d (datetime)
- date_tz(some_date, tz, **kwargs)¶
Convert datetime or date string to specified TZ.
The output will be rounded to the precision specified by kwarg ‘rounding’.
- Return type:
datetime- Parameters:
some_date (datetime | str)
tz (ZoneInfo | str | None)
- date_utc(some_date, **kwargs)¶
Convert datetime or date string to UTC (preserve instant).
If date has no timezone information, the data is assumed to be in default timezone. The output will be rounded to the precision specified by kwarg ‘rounding’.
- Return type:
datetime- Parameters:
some_date (datetime | str | None)
- ensure_datetime(some_date_representation, **kwargs)¶
Make sure that a date representation is a datetime object, convert if possible.
- Return type:
datetime- Parameters:
some_date_representation (Any)
- ensure_tz_aware(some_date, tz=zoneinfo.ZoneInfo(key='Europe/Oslo'))¶
Make sure that a datetime object is timezone aware.
Assume DEAFAULT_TZ if timezone information is missing.
- Return type:
datetime- Parameters:
some_date (datetime)
tz (ZoneInfo | str)
- is_tz_aware(d)¶
- Return type:
bool- Parameters:
d (datetime)
- is_tz_naive(d)¶
- Return type:
bool- Parameters:
d (datetime)
- local_timezone()¶
Return the local timezone of the computer.
- Return type:
ZoneInfo
- now_cet(**kwargs)¶
Return now in CET.
- Return type:
datetime
- now_eur_no(**kwargs)¶
Return now in Europe/Oslo.
- Return type:
datetime
- now_utc(**kwargs)¶
Return now in UTC.
- Return type:
datetime
- utc_iso(d, timespec='seconds')¶
Convert date to UTC and return as an ISO formatted string.
- Return type:
str- Parameters:
d (Any)
timespec (str)
- utc_iso_no_colon(d, timespec='seconds')¶
Convert date to UTC and return as an ISO formatted string without the colons.
- Return type:
str- Parameters:
d (datetime)
timespec (str)
- validate_timezone(tz='')¶
Return a valid time zone as string or ‘DEFAULT_TZ’ for the empty string or ‘None’.
- Return type:
str- Parameters:
tz (ZoneInfo | str | None)