Skip to content

Fix PCAN timestamp determination across platforms - #2153

Open
swaldhoer wants to merge 4 commits into
hardbyte:mainfrom
swaldhoer:no-uptime-requirement
Open

swaldhoer wants to merge 4 commits into
hardbyte:mainfrom
swaldhoer:no-uptime-requirement

Conversation

@swaldhoer

Copy link
Copy Markdown

Summary of Changes

Use native epoch timestamps on Linux and Windows Vista and newer, with 'uptime' as a fallback for legacy Windows and other platforms.
uptime is no longer maintained; the last update was 11 years ago (https://github.com/Cairnarvon/uptime); the package might still work, but there is a simple path, at least on Windows, to omit this dependency while keeping the meaningful timestamps.

I fixed a test in test/test_scripts.py on the way (maybe I saw this, because I am currently on Windows).

Related Issues / Pull Requests

  • Closes #
  • Related to #

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactoring
  • Other (please describe): omit obsolete dependency

Checklist

  • I have followed the contribution guide.
  • I have added or updated tests as appropriate.
  • I have added or updated documentation as appropriate.
  • I have added a news fragment for towncrier.
  • All checks and tests pass (tox).

Additional Notes

Stefan Waldhör added 2 commits November 24, 2026 23:04
Use native epoch timestamps on Linux and Windows Vista and newer, with
'uptime' as a fallback for legacy Windows and other platforms.
@zariiii9003

Copy link
Copy Markdown
Collaborator

AI Review: Fix PCAN timestamp determination across platforms

The Windows path replaces uptime.boottime() with a direct GetTickCount64 call. This is numerically identical to what uptime does on Windows (time.time() - GetTickCount64() / 1000), so behaviour is unchanged when uptime is installed. Windows users without uptime now get epoch timestamps instead of boot-relative ones. This is an improvement over main, but the diff is much larger than the change requires.

Code

  • can/interfaces/pcan/pcan.py: python-can requires Python ≥ 3.11, which runs only on Windows 8.1+, where GetTickCount64 always exists and cannot fail. The two try/except blocks, the Windows fallback to uptime, the if not boottimeEpoch: re-checks and the if IS_LINUX: pass nesting are therefore unreachable. Fix: keep main's structure and add a single branch:

    elif IS_WINDOWS:
        kernel32 = ctypes.windll.kernel32
        kernel32.GetTickCount64.restype = ctypes.c_uint64
        boottimeEpoch = time.time() - kernel32.GetTickCount64() / 1000
  • Same file: the 15-line comment block repeats doc/interfaces/pcan.rst, and the local import time repeats the import at the top of the module. Fix: remove both.

Tests

  • test/test_pcan.py: the five new test_timestamp_* tests assert the module global boottimeEpoch rather than observable behaviour. The Linux one duplicates the linux_* cases of test_timestamp_clock. The native-Windows test cannot fail: the mocked uptime returns the same boot time (1 700 000 000), so removing the native calculation leaves every test green. Fix: drop the five tests and the new helper flags, and make the existing windows_classic case of test_timestamp_clock patch ctypes.windll with uptime absent from sys.modules.
  • test/test_scripts.py: this change is unrelated to PCAN timestamps. In addition, Path(sys.executable).parent / name fails for a non-venv Windows install (python.exe in C:\PythonXY\, scripts in C:\PythonXY\Scripts\). Fix: move it to a separate PR and use Path(sysconfig.get_path("scripts")) / name there.

Documentation

  • doc/interfaces/pcan.rst, doc/installation.rst: the "Windows XP or older" case cannot occur (Python ≥ 3.11; PCAN-Basic supports Windows 10/11 only). Fix: change one sentence per file to state that Windows uses the native API and needs no uptime, and keep the rest of the existing text.

News fragments

  • doc/changelog.d/xxxx.changed.rst: xxxx is not a valid name under the contribution guide, and Linux epoch timestamps are already on main. Fix: rename to 2153.changed.rst and limit the text to Windows, e.g. "PCAN on Windows now derives the boot time from GetTickCount64, so the uptime package is no longer needed there."
  • doc/changelog.d/xxxx.fixed.rst: describes the unrelated test-only change. Fix: remove it.

Optional

  • pyproject.toml: the pcan extra still installs uptime on Windows and Linux, where it is no longer used. Consider adding the marker uptime~=3.0.1; platform_system != 'Windows' and platform_system != 'Linux'.

Verdict: request changes

- Simplify overall implementation (possible due to the packages platform
  constraints)
- Simplify tests
- Fix documentation
- Remove unrelated test-only fix (follow up PR will be created)
@swaldhoer

Copy link
Copy Markdown
Author
  • I should have addressed the points.
  • I'll make a follow up PR for the test/test_scripts.py fix.
  • I find the tests pretty confusing tbh. I think they can be simplified in a way that Linux and Windows tests run independent of the host platform. And it would be easier to understand where happens what. If you are open for this, I'll have a look.

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.

2 participants