Repository navigation
Conversation
datetime.fromtimestamp raises OSError for negative values on Windows, so to_tzaware_datetime(-86400) failed there. Build the datetime from the epoch plus a timedelta instead.
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
vectorbt.utils.datetime_.to_tzaware_datetimeraisesOSErrorfor negative Unix timestamps (dates before 1970) on Windows, becausedatetime.fromtimestamprejects negative values there.(bash, from the repo root; Windows 11, Python 3.13.1,
origin/masterat 9ac1b69.)Fix
Add a small helper that builds the UTC datetime as
1970-01-01 UTC + timedelta(seconds=ts)and use it in the float and int branches.For timestamps where
datetime.fromtimestampsucceeded, the result is identical: old and new computations were compared on the same set of about 3.7 million int/float/ms/us/ns values, including half-microsecond ties, on Windows (Python 3.13) and on Linux (Python 3.12, under WSL), with no difference. Two side effects: on Windows, timestamps between about year 3001 and year 9999 used to raiseOSErrorand now return a datetime (for example253402300799.0), and out-of-range values now raiseOverflowErrorinstead ofValueError/OSError. No caller in the repository catches these.Not addressed
len(str(dt_like))counts the minus sign, so negative integers of 10 or more digits (for example ms/us/ns values) are still misread:to_tzaware_datetime(-1500000000)gives 1965 instead of 1922. On Windows this used to raise and now returns that wrong date, as it already does on Linux. That is a separate issue and is not changed here.Tests
Added negative int and float cases to
test_to_tzaware_datetime. Windows 11, Python 3.13.1,PYTHONUTF8=0:pytest tests/test_utils.py -k test_to_tzaware_datetimegives1 failed, 60 passed(the filter also selects the 60 parametrizedtest_to_tzaware_datetime_numpy*cases; onlytest_to_tzaware_datetimefails, withOSError).61 passed.pytest tests/test_utils.pygives168 passed, 1 skipped.Not verified: the full test suite, and the tests themselves on Linux and macOS (only the old/new comparison above was run on Linux).