Skip to content

Commit

Permalink
fix: ValueError: time data does not match format '%Y-%m-%dT%H:%M:%S.%fZ'
Browse files Browse the repository at this point in the history
  • Loading branch information
orenlab committed Dec 12, 2024
1 parent 45e8bcc commit a2255c0
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions pytmbot/parsers/filters.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
from datetime import datetime
from typing import Union

from dateutil import parser

def format_timestamp(value: str or int) -> str:

def format_timestamp(value: Union[str, int]) -> str:
"""
Format a timestamp (either string or integer) into a human-readable string.
Format a timestamp (either ISO 8601 string or integer in milliseconds)
into a human-readable string.
Args:
value (str or int): Timestamp in ISO 8601 string format or in seconds (integer).
value (Union[str, int]): Timestamp in ISO 8601 format or integer (milliseconds).
Returns:
str: Formatted timestamp as a string in the format '%d-%m-%Y %H:%M:%S'.
str: Formatted timestamp as '%d-%m-%Y %H:%M:%S'.
"""
if isinstance(value, str):
try:
dt = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%fZ")
dt = parser.isoparse(value)
except ValueError:
dt = datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ")
raise ValueError("Invalid string format for timestamp.")
elif isinstance(value, int):
dt = datetime.fromtimestamp(value / 1000)
else:
Expand Down

0 comments on commit a2255c0

Please sign in to comment.