Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): fix hour parsing and formatting in the 1-12 and 1-24 conventions #4506

Merged
merged 16 commits into from
May 16, 2024

Conversation

jerrinot
Copy link
Contributor

@jerrinot jerrinot commented May 15, 2024

The hh pattern stands for 'Hour in am/pm (1-12)'. The original parser implementation was subtracting 1 and a formatter was adding 1.

As a result, it parsed e.g. 05:00 as if it was 04:00, which is wrong. But when converted back to a string (with the same pattern) it was formatted correctly - displaying 05:00 again. Thus the bug was not immediately visible. I assume it was an attempt to convert a 1-based indexing into a 0-based indexing. But clocks are not that simple. The 12-hour system with the 1-12 convention goes like this: 12am (midnight), 1am, 2am, 3am, ..... 11am, 12pm (noon), 1pm, 2pm, ... 11pm and then wraps over to 12am (midnight) of the new day.

The same problem was with the kk - Hour in day (1-24) pattern. It was also subtracting 1 during parsing and adding one when formatting.

Implementation notes:
There is little difference between 0-11 and 1-12 schemes. The only difference is that the 1-12 scheme uses 12 for noon and midnight whereas the 0-11 scheme uses 0 for noon and midnight. All other hour values are the same. We use this property to simplify the code parsing: We parse both 0-11 and 1-12 by using the same code. Later, when constructing a long timestamp, we treat 12 as if it was 0. Regardless of the 0-11 vs 1-12 scheme. One side effect is that the parser is somewhat more relaxed: Even when a parser is configured with the 0-11 it will still accept 12 as an hour value. I don't think it can cause issues in practice, except for possible compatibility issues if we ever decide to make it stricter again.

This change also adds tests comparing both hh and kk patterns with a reference SimpleDateFormat from JDK.

Fixes #4502

The 'hh' pattern stands 'Hour in am/pm (1-12)'
The original implementation was subtracting 1.

As a result it parsed e.g. 05:00 as if it was 04:00,
which is clearly wrong. It adds tests comparing 12-hour
clock parser/formatter with a refernce SimpleDateFormat from JDK.

Fixes #4502
@jerrinot
Copy link
Contributor Author

jerrinot commented May 15, 2024

there seems to be the same problem with the k pattern: Hour in day (1-24).
edit: I fixed that too.

@jerrinot jerrinot changed the title fix(core): fix hour parsing in the 12-clock convention fix(core): fix hour parsing and formatting in the 0-11 and 0-23 conventions May 15, 2024
@jerrinot jerrinot changed the title fix(core): fix hour parsing and formatting in the 0-11 and 0-23 conventions fix(core): fix hour parsing and formatting in the 1-12 and 1-24 conventions May 15, 2024
@jerrinot
Copy link
Contributor Author

unrelated test failure: #4511

@ideoma
Copy link
Collaborator

ideoma commented May 16, 2024

[PR Coverage check]

😍 pass : 57 / 57 (100.00%)

file detail

path covered line new line coverage
🔵 io/questdb/std/datetime/millitime/GenericDateFormat.java 4 4 100.00%
🔵 io/questdb/std/datetime/millitime/DateFormatCompiler.java 4 4 100.00%
🔵 io/questdb/std/datetime/microtime/GenericTimestampFormat.java 4 4 100.00%
🔵 io/questdb/std/datetime/microtime/TimestampFormatUtils.java 20 20 100.00%
🔵 io/questdb/std/datetime/microtime/TimestampFormatCompiler.java 4 4 100.00%
🔵 io/questdb/std/datetime/millitime/DateFormatUtils.java 21 21 100.00%

@bluestreak01 bluestreak01 merged commit 8e0d2d3 into master May 16, 2024
24 checks passed
@bluestreak01 bluestreak01 deleted the jh_12hour_clock_format branch May 16, 2024 11:32
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.

Error while parsing timestamps with times between midnight and 1am formatted in the 12-hour clock convention
3 participants