From 1f280ff6d43aedfbc2c0b3aa085647414acdd304 Mon Sep 17 00:00:00 2001 From: Chao Pang Date: Fri, 25 Oct 2024 14:24:32 -0400 Subject: [PATCH] generalized convert_date_to_posix_time by allowing either datetime or date objects --- .../hf_data_generator/hf_dataset_mapping.py | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/cehrbert/data_generators/hf_data_generator/hf_dataset_mapping.py b/src/cehrbert/data_generators/hf_data_generator/hf_dataset_mapping.py index 4094e5e..9d6f492 100644 --- a/src/cehrbert/data_generators/hf_data_generator/hf_dataset_mapping.py +++ b/src/cehrbert/data_generators/hf_data_generator/hf_dataset_mapping.py @@ -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: