forked from openai/chatgpt-retrieval-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.py
23 lines (18 loc) · 793 Bytes
/
date.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import arrow
def to_unix_timestamp(date_str: str) -> int:
"""
Convert a date string to a unix timestamp (seconds since epoch).
Args:
date_str: The date string to convert.
Returns:
The unix timestamp corresponding to the date string.
If the date string cannot be parsed as a valid date format, returns the current unix timestamp and prints a warning.
"""
# Try to parse the date string using arrow, which supports many common date formats
try:
date_obj = arrow.get(date_str)
return int(date_obj.timestamp())
except arrow.parser.ParserError:
# If the parsing fails, return the current unix timestamp and print a warning
print(f"Invalid date format: {date_str}")
return int(arrow.now().timestamp())