Parse fractional seconds exactly in timestamp() (fix off-by-one microseconds) - #178
Open
gaoflow wants to merge 1 commit into
Open
Parse fractional seconds exactly in timestamp() (fix off-by-one microseconds)#178gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
timestamp() computed microseconds as int(float(frac) * 1000000), but a binary float can't represent most decimal fractions exactly, so the product lands just under the true integer and int() truncates down: e.g. '.00397' s gave 3969 us instead of 3970, wrong for 1153 of every 100000 five-digit fractions. Parse the fractional digits as an integer instead (right-padded/truncated to microsecond resolution), which is exact and cannot overflow datetime's 0..999999 microsecond range.
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.
The bug
nmea_utils.timestamp()converts the fractional seconds of a sentence time through a binary float, which truncates and yields microseconds that are off by one for ~1.15% of inputs:timestamp()is the library's central time parser, so this reaches every sentence that carries a time (GGA/RMC/GLL/ZDA/…) viaparse():A sweep of all 100000 five-digit fractions finds 1153 systematically wrong, always exactly 1 µs low.
Why the expected value is unambiguous
microsecondis an integer field, and a fractional second with ≤6 digits is exactly representable at microsecond resolution —.00397 sis exactly3970 µs. The existingtest_timestampalready asserts exact values (.123456→123456,.1234→123400) andtest_norasserts.3341→334100; it simply never picked a float-unsafe fraction, so it missed this.Root cause
pynmea2/nmea_utils.py:Binary floats can't represent most decimal fractions exactly;
float('.00397') * 1000000is3969.9999999…, andint()truncates downward → off by one. (A naiveround()"fix" is also wrong:round(0.9999995 * 1e6) == 1000000, whichdatetime.time(...)rejects withmicrosecond must be in 0..999999.)The fix
Parse the fractional digits as an integer, right-padded/truncated to 6-digit microsecond resolution — exact, and inherently overflow-safe:
Tests
test_timestamp_fractional_seconds_exact: the known-bad values, an exhaustive sweep asserting every 5-digit fractionigivesi*10µs, the >6-digit truncation and the.9999999→999999no-overflow boundary, the no-fraction case, and the publicparse()entry point. The new test fails onmaster(assert 3969 == 3970) and passes with the fix; the full suite stays green (106 passed), and all pre-existingtimestampassertions (incl. the 7-digit truncation case andtest_nor) are preserved.