Skip to content

Changes to use math.floor instead of type cast to int - #322

Merged
joachimmetz merged 2 commits into
log2timeline:mainfrom
bacescuandrei:fix/negative-fraction-floor
Oct 26, 2026
Merged

joachimmetz merged 2 commits into
log2timeline:mainfrom
bacescuandrei:fix/negative-fraction-floor

Conversation

@bacescuandrei

Copy link
Copy Markdown
Contributor

_GetTimeValues uses int(), which truncates towards zero. For a negative timestamp with a fraction of a second, GetDate()/GetTimeOfDay()/GetDateWithTimeOfDay() are therefore one second late. For example:

  • PosixTimeInMilliseconds(timestamp=-500).GetDateWithTimeOfDay() returns (1970, 1, 1, 0, 0, 0) instead of (1969, 12, 31, 23, 59, 59).
  • Filetime(timestamp=1) (1601-01-01 00:00:00.0000001) is reported as 00:00:01.

CocoaTime, DelphiDateTime and OLEAutomationDate also call int() on the number of seconds in CopyToDateTimeString, but take the fraction of second with % 1, which floors:

  • CocoaTime(timestamp=-0.25) gives 2001-01-01 00:00:00.750000 instead of 2000-12-31 23:59:59.750000.
  • DelphiDateTime(timestamp=-0.000001) and OLEAutomationDate(timestamp=-0.000001) give 1899-12-30 00:00:00.913600 instead of 1899-12-29 23:59:59.913600.

The number of seconds is now rounded down with math.floor in all four places.

Tests

  • New assertions in tests/interface.py, tests/posix_time.py, tests/cocoa_time.py, tests/delphi_date_time.py and tests/ole_automation_date.py. On main, 5 tests fail (cocoa_time, delphi_date_time, interface, ole_automation_date, posix_time); with this change they pass.
  • python run_tests.py: 328 tests OK.
  • black is clean, and pylint is clean on the changed files.

Follow-up to #320: the expected Delphi/OLE dates depend on it.

_GetTimeValues truncated the number of seconds towards zero, so for a
negative normalized timestamp with a fraction of second, such as
PosixTimeInMilliseconds(-500), GetDateWithTimeOfDay returned
1970-01-01 00:00:00 instead of 1969-12-31 23:59:59. The same applied to
CocoaTime.CopyToDateTimeString, which combined the truncated seconds
with the floored fraction of second (timestamp % 1), so -0.25 was
formatted as 2001-01-01 00:00:00.750000 instead of
2000-12-31 23:59:59.750000.

The number of seconds is now rounded down.
DelphiDateTime and OLEAutomationDate also truncated the number of
seconds towards zero before determining the date and time values, while
the fraction of second is floored with % 1, so DelphiDateTime(-0.000001)
was formatted as 1899-12-30 00:00:00.913600 instead of
1899-12-29 23:59:59.913600. The number of seconds is now rounded down.
@codecov

codecov Bot commented Oct 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.90%. Comparing base (997870d) to head (eb180d6).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #322   +/-   ##
=======================================
  Coverage   98.89%   98.90%           
=======================================
  Files          24       24           
  Lines        2451     2455    +4     
=======================================
+ Hits         2424     2428    +4     
  Misses         27       27           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@joachimmetz joachimmetz self-assigned this Oct 26, 2026
@joachimmetz joachimmetz changed the title Fixed time of day of negative timestamps with a fraction of second Changes to use math.floor instead of truncation via cast to int Oct 26, 2026
@joachimmetz joachimmetz changed the title Changes to use math.floor instead of truncation via cast to int Changes to use math.floor instead of type cast to int Oct 26, 2026

@joachimmetz joachimmetz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@joachimmetz
joachimmetz merged commit 090bf3f into log2timeline:main Oct 26, 2026
19 checks passed
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