diff --git a/cbutil/util.py b/cbutil/util.py index 8b8312b03d87345dff6676b4060cf41db6abd09a..9625540102a90a31010a6ad0f17b6b585921695c 100644 --- a/cbutil/util.py +++ b/cbutil/util.py @@ -1,6 +1,8 @@ import logging import operator import os +import time + from datetime import datetime from functools import reduce from pathlib import Path @@ -71,5 +73,17 @@ def time_conversion(time_stamp, *, pattern="%Y-%m-%d %H:%M:%S"): return int(dt.timestamp()) +def get_time_stamp_from_env(key='CI_PIPELINE_CREATED_AT'): + """ + Try to get the time stamp from the environment and + convert it to an int. If that fails use the current time + """ + try: + return time_conversion(os.environ[key]) + except KeyError: + logger.debug(f'Could not find timestamp with ${key}') + return int(time.time()) + + def get_from_nested_dict(nested_dict, keys): return reduce(operator.getitem, keys, nested_dict)