-
Notifications
You must be signed in to change notification settings - Fork 783
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: update datetime-compare.mdx (#1279)
Signed-off-by: ydv129 <[email protected]> Co-authored-by: Tal <[email protected]>
- Loading branch information
Showing
1 changed file
with
20 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |