14 """This contains common helpers for working with dates and time."""
17 from typing
import Optional, Pattern
19 RE_ZERO_OFFSET: Pattern[str] = re.compile(
r'[+\-]00:?00$')
23 """Construct a datetime from current time in UTC timezone."""
24 return datetime.datetime.now(datetime.timezone.utc)
28 """Replace ±00:00 timezone designator with Z (zero offset AKA Zulu time)."""
29 return RE_ZERO_OFFSET.sub(
'Z', utc_datetime_str)
33 """Return datetime relative to current in ISO-8601 format, UTC tz."""
34 time: datetime.datetime =
utc_now()
41 """Return current UTC date, and time in a format useful for resource naming.
44 - 20210626-1859 (seconds=False)
45 - 20210626-185942 (seconds=True)
46 Use in resources names incompatible with ISO 8601, e.g. some GCP resources
47 that only allow lowercase alphanumeric chars and dashes.
49 Hours and minutes are joined together for better readability, so time is
50 visually distinct from dash-separated date.
52 return utc_now().strftime(
'%Y%m%d-%H%M' + (
'%S' if seconds
else ''))