Skip to content

Commit

Permalink
generalized convert_date_to_posix_time by allowing either datetime or…
Browse files Browse the repository at this point in the history
… date objects
  • Loading branch information
ChaoPang committed Oct 25, 2024
1 parent 35dcd9e commit 1f280ff
Showing 1 changed file with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,39 @@
DATE_FORMAT = "%Y-%m-%d %H:%M:%S.%f"


def convert_date_to_posix_time(index_date: datetime.date) -> float:
return datetime.datetime.combine(index_date, datetime.datetime.min.time()).timestamp()
def convert_date_to_posix_time(index_date: Union[datetime.date, datetime.datetime]) -> float:
"""
Convert a date or datetime object to POSIX (Unix) time in seconds.
Parameters
----------
index_date : Union[datetime.date, datetime.datetime]
The date or datetime object to be converted to POSIX time.
Returns
-------
float
The POSIX time in seconds as a float.
Raises
------
ValueError
If `index_date` is not an instance of `datetime.date` or `datetime.datetime`.
Examples
--------
>>> convert_date_to_posix_time(datetime.date(2024, 10, 25))
1735104000.0
>>> convert_date_to_posix_time(datetime.datetime(2024, 10, 25, 12, 30))
1735144200.0
"""
if isinstance(index_date, datetime.datetime):
return index_date.timestamp()
elif isinstance(index_date, datetime.date):
return datetime.datetime.combine(index_date, datetime.datetime.min.time()).timestamp()
else:
raise ValueError("index_date must be datetime or datetime.datetime")


def replace_escape_chars(text: str) -> str:
Expand Down

0 comments on commit 1f280ff

Please sign in to comment.