Skip to content

fix(proto): handle the sign of negative Time and Time64 values - #1252

Open
pcbeingused333 wants to merge 1 commit into
ClickHouse:mainfrom
pcbeingused333:fix-negative-time
Open

pcbeingused333 wants to merge 1 commit into
ClickHouse:mainfrom
pcbeingused333:fix-negative-time

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 25, 2026 •

Copy link
Copy Markdown

Fixes #1249

Summary

ParseTime32 and ParseTime64 return a wrong value for a negative Time text, and String() prints a minus sign on each part.

Cause:

  • The parsers add the signed hours to the positive minutes and seconds. For -00 hours, strconv.Atoi returns 0 and the sign is lost.
  • String() formats each part of a negative value with its own sign.

Fix:

  • The parsers read a leading - as the sign of the whole value.
  • String() prints the sign one time, before the hours.
  • The output of positive values does not change. ParseTime32 still reads the old -1:-2:-3 form.

Tests: TestTime_Negative in proto/time_test.go fails on main and passes with this change. go test ./proto/ and golangci-lint run ./proto/... pass. No test against a ClickHouse server was run.

Checklist

  • Unit and integration tests covering the common scenarios were added (unit test)
  • A human-readable description of the changes was provided to include in CHANGELOG: Fix the sign of negative Time and Time64 values in ParseTime32, ParseTime64 and String().

This change was prepared with the help of an AI assistant.


Note

Low Risk
Localized proto time parsing/formatting fix with positive paths unchanged; only negative sign semantics change, which corrects a bug rather than altering production-critical paths.

Overview
Fixes incorrect handling of negative Time32/Time64 values in ParseTime32, ParseTime64, and String().

String: Negative durations now emit a single leading - (e.g. -01:02:03) instead of a minus on each field.

Parse: A leading - on the hour field applies to the entire duration (including -00:00:01), with components taken in absolute value before negating the total; Time64 applies the same rule to fractional nanoseconds.

Compat: Positive formatting/parsing is unchanged; the legacy -1:-2:-3 form still parses to the same negative value.

Adds TestTime_Negative round-trip coverage for parse/String() on both types.

Reviewed by Cursor Bugbot for commit 41b6f21. Bugbot is set up for automated code reviews on this repo. Configure here.

@AmanBoronbaev AmanBoronbaev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I checked the negative-duration change at 41b6f21 against ClickHouse 26.10.13.11. Parse followed by String matched the server for both Time and Time64(9) on five inputs: the negative and positive 999-hour endpoints, -01:02:03.456, and one nanosecond on either side of zero. For the subsecond cases I supplied only the integral part to Time. In particular, the server and the client both rendered -1 ns as -00:00:00.000000001, while the seconds-only value rendered as 00:00:00.

The new TestTime_Negative also passed locally.

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.

proto: ParseTime32 and ParseTime64 return a wrong value for a negative time, and String() prints "-1:-2:-3"

2 participants