Skip to content

Commit

Permalink
docs: update datetime-compare.mdx (#1279)
Browse files Browse the repository at this point in the history
Signed-off-by: ydv129 <[email protected]>
Co-authored-by: Tal <[email protected]>
  • Loading branch information
ydv129 and talboren authored Jun 26, 2024
1 parent 3820b6e commit 616cd3d
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions docs/workflows/functions/datetime-compare.mdx
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)

0 comments on commit 616cd3d

Please sign in to comment.