def get_ebs_age()

in netbench-cdk/netbench-monitor/handler.py [0:0]


def get_ebs_age(date_obj: datetime|str) -> int:
    """
    Convert a date object or str to age in seconds
    """

    # String to Date object conversion is done automajically with boto3
    # but not with a standard json.load() from tests
    if type(date_obj) == str:
      date_format = '%Y-%m-%dT%H:%M:%S%z'
      try:
        date_obj = datetime.strptime(date_obj, date_format)
      except ValueError as e:
        print(f"The date format was unexpected: {e} ")
        raise
    
    now = datetime.now(tz=timezone.utc)
    delta = now - date_obj
    if delta.total_seconds() < 1:
        raise ValueError(f"Date is in the future:{delta}")
    else:
      return int(delta.total_seconds())