Functions
datetime_compare
datetime_compare(t1, t2) compares t1-t2 and returns the diff in seconds
Input
datetime.datetime, datetime.datetime
Output
Integer, timedelta in seconds.
Example
actions:
- name: trigger-slack
condition:
- type: threshold
value: keep.datetime_compare(keep.utcnow(), keep.to_utc("{{ steps.this.results[0][0] }}"))
compare_to: 3600 # seconds (1 hour)
compare_type: gt # greater than
from datetime import datetime
def datetime_compare(t1: datetime, t2: datetime) -> int:
"""
Compares two datetime objects and returns the time difference in seconds.
:param t1: First datetime object
:param t2: Second datetime object
:return: Time difference in seconds
"""
return int((t1 - t2).total_seconds())
# Example usage:
# t1 = datetime.utcnow()
# t2 = datetime.utcnow() - timedelta(hours=2)
# print(datetime_compare(t1, t2)) # Should return 7200 (2 hours * 3600 seconds)