Skip to content

fix(utils): do not raise OSError for negative timestamps on Windows - #917

Open
Lupin133 wants to merge 1 commit into
polakowo:masterfrom
Lupin133:fix/negative-timestamp-windows
Open

Lupin133 wants to merge 1 commit into
polakowo:masterfrom
Lupin133:fix/negative-timestamp-windows

Conversation

@Lupin133

Copy link
Copy Markdown

Problem

vectorbt.utils.datetime_.to_tzaware_datetime raises OSError for negative Unix timestamps (dates before 1970) on Windows, because datetime.fromtimestamp rejects negative values there.

$ python -c "
from vectorbt.utils.datetime_ import to_tzaware_datetime as f
for v in (-86400, -86400.5, 0):
    try: print(v, f(v))
    except Exception as e: print(v,'ERR',type(e).__name__,e)"
-86400 ERR OSError [Errno 22] Invalid argument
-86400.5 ERR OSError [Errno 22] Invalid argument
0 1970-01-01 00:00:00+00:00

(bash, from the repo root; Windows 11, Python 3.13.1, origin/master at 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.fromtimestamp succeeded, 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 raise OSError and now return a datetime (for example 253402300799.0), and out-of-range values now raise OverflowError instead of ValueError/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:

  • Before the fix: pytest tests/test_utils.py -k test_to_tzaware_datetime gives 1 failed, 60 passed (the filter also selects the 60 parametrized test_to_tzaware_datetime_numpy* cases; only test_to_tzaware_datetime fails, with OSError).
  • After the fix: same command gives 61 passed.
  • After the fix: pytest tests/test_utils.py gives 168 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).

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant