Source code for gigl.src.common.utils.time
import datetime
import uuid
from gigl.src.common.constants.time import (
DEFAULT_DATETIME_FORMAT,
NODASH_DATE_UNDERSCORE_TIME_FORMAT,
)
[docs]
def new_test_run_id(prefix: str) -> str:
"""Returns an ID for naming the resources of one test run.
Each call returns a new ID, so call it once per test run and reuse the result.
The ID is ``<prefix>_<UTC YYYYmmdd_HHMMSS>_<6 random hex chars>``. The random
suffix keeps runs that start in the same second from sharing GCS paths,
BigQuery tables, or Dataflow job names. When ``prefix`` is lowercase
``[a-z0-9_]``, so is the ID, which makes it valid as:
- a KFP job name
- a BigQuery table name
- a GCS path component
- a Dataflow job name, after Dataflow's own sanitizing
Args:
prefix: Leading text that identifies the test.
Returns:
str: The unique ID.
"""
timestamp = current_formatted_datetime(fmt=NODASH_DATE_UNDERSCORE_TIME_FORMAT)
return f"{prefix}_{timestamp}_{uuid.uuid4().hex[:6]}"
[docs]
def current_datetime() -> datetime.datetime:
"""
Returns:
str: current timezone aware utc datetime.
"""
return datetime.datetime.now(datetime.timezone.utc)
[docs]
def convert_days_to_ms(days: int) -> int:
return days * 24 * 60 * 60 * 1000