Last active
August 17, 2024 06:44
-
-
Save victorkristof/b9d794fe1ed12e708b9d to your computer and use it in GitHub Desktop.
Convert Matlab datenum into Python datetime
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
def datenum_to_datetime(datenum): | |
""" | |
Convert Matlab datenum into Python datetime. | |
:param datenum: Date in datenum format | |
:return: Datetime object corresponding to datenum. | |
""" | |
days = datenum % 1 | |
hours = days % 1 * 24 | |
minutes = hours % 1 * 60 | |
seconds = minutes % 1 * 60 | |
return datetime.fromordinal(int(datenum)) \ | |
+ timedelta(days=int(days)) \ | |
+ timedelta(hours=int(hours)) \ | |
+ timedelta(minutes=int(minutes)) \ | |
+ timedelta(seconds=round(seconds)) \ | |
- timedelta(days=366) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!