Last active
May 10, 2021 11:16
-
-
Save tomwojcik/45ec71a1e7659223db8860c4ab5545e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
DATE_SYSTEM_1900_EPOCH = datetime.datetime(1899, 12, 31) | |
DATE_SYSTEM_1904_EPOCH = DATE_SYSTEM_1900_EPOCH + datetime.timedelta(days=1462) | |
def _handle_excel_date_system( | |
ordinal: Union[str, float], | |
_epoch: datetime.datetime | |
) -> Optional[datetime.datetime]: | |
""" | |
https://stackoverflow.com/a/29387450/5833429 | |
""" | |
try: | |
ordinal = float(ordinal) | |
except ValueError: | |
return None | |
else: | |
# Excel leap year bug, 1900 is not a leap year! | |
if ordinal >= 60: | |
ordinal -= 1 | |
return (_epoch + datetime.timedelta(days=ordinal)).replace(microsecond=0) | |
def date_system_1900_to_datetime(ordinal, _epoch=DATE_SYSTEM_1900_EPOCH) -> datetime.datetime: | |
return _handle_excel_date_system(ordinal, _epoch=_epoch) | |
def date_system_1904_to_datetime(ordinal, _epoch=DATE_SYSTEM_1904_EPOCH) -> datetime.datetime: | |
return _handle_excel_date_system(ordinal, _epoch=_epoch) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment