diff --git a/docs/workflows/functions/datetime-compare.mdx b/docs/workflows/functions/datetime-compare.mdx index f1e4380d5..d2c1db3c1 100644 --- a/docs/workflows/functions/datetime-compare.mdx +++ b/docs/workflows/functions/datetime-compare.mdx @@ -1,26 +1,28 @@ ---- -title: "datetime_compare" ---- - -### Input - -t1 - datetime.datetime, t2 - datetime.datetime - -### Output - -Integer - time difference, in seconds - -### Example - -```yaml actions: - name: trigger-slack condition: - type: threshold - # datetime_compare(t1, t2) compares t1-t2 and returns the diff in hours + # datetime_compare(t1, t2) compares t1-t2 and returns the diff in seconds # utcnow() returns the local machine datetime in UTC # to_utc() converts a datetime to UTC value: keep.datetime_compare(keep.utcnow(), keep.to_utc("{{ steps.this.results[0][0] }}")) - compare_to: 1 # hours + 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)