gfw.common.datetime#
Utility functions for working with datetime objects and timezones.
Functions
Creates |
|
Converts a datetime string to a timezone-aware |
|
Extracts a zone-aware |
|
Converts a Unix timestamp to a timezone-aware |
- datetime_from_timestamp(ts, tz=datetime.timezone.utc)[source]#
Converts a Unix timestamp to a timezone-aware
datetime.By default, the timestamp is converted to UTC. If you need a different timezone, specify it using the
tzargument.
- datetime_from_isoformat(s, tz=datetime.timezone.utc)[source]#
Converts a datetime string to a timezone-aware
datetime.
- datetime_from_date(d, t=None, tz=datetime.timezone.utc)[source]#
Creates
datetimefrom adatetime.dateobject.
- datetime_from_string(s, date_format='%Y-%m-%d', time_format='%H_%M_%SZ', allow_no_time=True, tz=datetime.timezone.utc)[source]#
Extracts a zone-aware
datetimefrom a string.- Parameters:
s (str) – The string containing the datetime to extract.
date_format (str) – The strftime/strptime format of the date part. Defaults to
%Y-%m-%d.time_format (str) – The strftime/strptime format of the time part. Defaults to
%H_%M_%SZ.allow_no_time (bool) – If True, allows input strings with no time information.
tz (timezone) – The timezone to apply if the parsed datetime has no tzinfo. Defaults to UTC.
- Raises:
When date is not found in the input string. - When time is not found in the input string and
allow_no_timeis False.
- Returns:
A timezone-aware
datetimeobject.- Return type: