-
Notifications
You must be signed in to change notification settings - Fork 12
Add filetime to datetime conversion #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
625000c
Add filetime to datetime conversion utility functions to Datetime.py
MattRoyle 3b84801
Added None return type to Datetime tests
MattRoyle 464e7d7
added tzdata to the test requirements
MattRoyle 8aefef1
removed tzdata from the test requirements, changed doc string to the …
MattRoyle bc6ce02
added rounding to the filetime conversion using divmod
MattRoyle fb4cbf0
renamed the functions to specify it is windows filetime
MattRoyle d747a74
fix the docstrings
MattRoyle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import unittest | ||
| import datetime | ||
| from nion.utils import DateTime | ||
|
|
||
| class Test(unittest.TestCase): | ||
MattRoyle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def test_datetime_to_filetime(self) -> None: | ||
| test_datetimes = [ | ||
| (datetime.datetime(2025, 1, 1), 133801632000000000), | ||
| (datetime.datetime(2025, 1, 1, 12, 30, 45), 133802082450000000), | ||
| (datetime.datetime(2025, 12, 31, 23, 59, 59, 999999), 134116991999999990), # max microsecond | ||
| (datetime.datetime(1970, 1, 1, 0, 0, 0), 116444736000000000), # Unix epoch | ||
| (datetime.datetime(9999, 12, 31, 23, 59, 59, 999999), 2650467743999999990), # Python max datetime.datetime | ||
| (datetime.datetime(2024, 2, 29, 15, 0), 133536924000000000), # leap day | ||
| (datetime.datetime(2000, 2, 29, 23, 59, 59), 125963423990000000), # leap year divisible by 400 | ||
| (datetime.datetime(2025, 6, 1, 12, 0, tzinfo=datetime.timezone.utc), 133932528000000000),# UTC aware | ||
| (datetime.datetime(2025, 6, 1, 12, 0, tzinfo=datetime.timezone(datetime.timedelta(hours=5, minutes=30))),133932330000000000), # India Standard Time | ||
| (datetime.datetime(1950, 5, 17, 8, 20, 0, tzinfo=datetime.timezone.utc), 110251020000000000), # negative time stamp | ||
| (datetime.datetime(1066, 1, 1, 0, 0, 0), -168829920000000000), # before windows filetime start | ||
| (datetime.datetime(2025, 7, 15, 10, 5, 30, 123456), 133970475301234560), # microseconds | ||
| (datetime.datetime(2025, 7, 15, 10, 5, 30, 0), 133970475300000000), # zero microseconds | ||
| (datetime.datetime(2035, 8, 1, 9, 0, tzinfo=datetime.timezone.utc), 137140452000000000), # 10 years ahead | ||
| (datetime.datetime(2100, 1, 1, 0, 0, 0),157469184000000000), # non‑leap century year | ||
| (datetime.datetime(2025, 3, 30, 0, 30, tzinfo=datetime.timezone(datetime.timedelta(hours=-1))), 133877718000000000), # before daylight savings | ||
| (datetime.datetime(2025, 3, 30, 1, 30, tzinfo=datetime.timezone(datetime.timedelta(hours=0))), 133877718000000000), # ambiguous transition | ||
| (datetime.datetime(2025, 10, 26, 1, 30, tzinfo=datetime.timezone(datetime.timedelta(hours=0))), 134059158000000000), # repeated hour | ||
| (datetime.datetime(2025, 6, 1, 12, 0, tzinfo=datetime.timezone(datetime.timedelta(hours=4))), 133932384000000000), # US Eastern | ||
| (datetime.datetime(2025, 6, 1, 12, 0, tzinfo=datetime.timezone(datetime.timedelta(hours=9))), 133932204000000000), # Japan time | ||
| ] | ||
| for datetime_in, expected_filetime in test_datetimes: | ||
| with self.subTest(f"Datetime to filetime {datetime_in.isoformat()} expects {expected_filetime}"): | ||
| filetime = DateTime.get_windows_filetime_from_datetime(datetime_in) | ||
| print(filetime) | ||
| self.assertEqual(filetime, expected_filetime) | ||
| datetime_out = DateTime.get_datetime_from_windows_filetime(filetime) | ||
| if datetime_in.tzinfo is None: | ||
|
|
||
| time_in_utc = datetime_in.replace(tzinfo=datetime.timezone.utc) | ||
| else: | ||
| time_in_utc = datetime_in.astimezone(tz=datetime.timezone.utc) | ||
| self.assertEqual(time_in_utc, datetime_out) | ||
|
|
||
| with self.subTest(f"Filetime to datetime {datetime_in} expects {expected_filetime}"): | ||
| datetime_out = DateTime.get_datetime_from_windows_filetime(expected_filetime) | ||
| if datetime_in.tzinfo is None: | ||
|
|
||
| time_in_utc = datetime_in.replace(tzinfo=datetime.timezone.utc) | ||
| else: | ||
| time_in_utc = datetime_in.astimezone(tz=datetime.timezone.utc) | ||
| self.assertEqual(time_in_utc, datetime_out) | ||
| filetime_out = DateTime.get_windows_filetime_from_datetime(datetime_out) | ||
| self.assertEqual(filetime_out, expected_filetime) | ||
|
|
||
| def test_invalid_times(self) -> None: | ||
| test_filetimes = [ | ||
| (datetime.datetime.max, 2650467744999999990), # Above the max datetime | ||
| (datetime.datetime.max, 9223372036854775807), # Int max, | ||
| (datetime.datetime.min, -9223372036854775808), # int min | ||
| ] | ||
| for i, (expected_datetime, filetime_in) in enumerate(test_filetimes): | ||
| with self.subTest(f"Invalid filetime test: {expected_datetime} expects {filetime_in}"): | ||
| datetime_out = DateTime.get_datetime_from_windows_filetime(filetime_in) | ||
| if expected_datetime.tzinfo is None: | ||
|
|
||
| time_in_utc = expected_datetime.replace(tzinfo=datetime.timezone.utc) | ||
| else: | ||
| time_in_utc = expected_datetime.astimezone(tz=datetime.timezone.utc) | ||
| self.assertEqual(datetime_out, time_in_utc) | ||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.