time: always return utc() timezone for Time.unix/0#25233
Merged
Conversation
|
Connected to Huly®: V_0.6-24728 |
There was a problem hiding this comment.
Pull Request Overview
This PR fixes time handling by ensuring that Time.unix() always returns the UTC timezone timestamp, addressing issue #17784. The changes improve consistency between local and UTC time representations and update related serialization logic.
- Added
local_unix()method to return local UNIX timestamp while makingunix()always return UTC timestamp - Updated time comparison operators to properly handle local vs UTC time comparisons
- Fixed JSON serialization to use the correct time method calls
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| vlib/v/gen/c/json.v | Updated JSON encoding to use time__Time_unix() function calls instead of direct field access |
| vlib/time/time.v | Added local_unix() method, modified unix() to always return UTC, updated time arithmetic and debug output |
| vlib/time/operator.v | Updated equality and less-than operators to properly compare local vs UTC times |
| vlib/time/time_test.v | Removed obsolete tests that depended on old time behavior |
| vlib/time/time_test.c.v | Updated test to use local_unix() for local time comparison |
| vlib/time/time_format_test.v | Fixed test to convert UTC time to local for proper formatting comparison |
| vlib/time/private_test.c.v | Removed entire test file that tested deprecated behavior |
| vlib/orm/orm_test.v | Updated test to compare UTC timestamps instead of formatted strings |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
spytheman
reviewed
Sep 4, 2025
spytheman
reviewed
Sep 4, 2025
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.
Fixes #17784
We definitely need to clean time up, supporting only two timezones is quite bad and we diverged quite a bit from Go.
It's possible that I forgot to dereference one of the calls in the cgen for json. Not all the branches are covered by tests.
Copilot summary
This pull request introduces significant improvements to time handling in the V standard library, focusing on more accurate and explicit management of local and UTC time representations. The changes ensure that comparisons, arithmetic, and formatting involving
time.Timeobjects are correct and consistent, especially with respect to time zones and internal representation. Additionally, the pull request updates serialization logic and cleans up obsolete or incorrect tests.Time Representation and Comparison Improvements:
local_unix()totime.Timeto return the local UNIX timestamp, and updated the existingunix()method to always return the UTC UNIX timestamp for clarity and correctness. ([vlib/time/time.vR109-R114](https://github.com/vlang/v/pull/25233/files#diff-31c344f2cfe6fecdafedef24cc42ca595e29556e07d4efc67c5ab8115e199c88R109-R114))==) and less-than (<) operators fortime.Timeto use both theis_localflag and the appropriate local UNIX timestamp, ensuring comparisons are accurate for both local and UTC times. ([vlib/time/operator.vL6-R15](https://github.com/vlang/v/pull/25233/files#diff-a3d381c0223e3b7cdaa9f0d8991a5ca77e3560884795429fa8403504ba4027acL6-R15))Time Arithmetic and Formatting Adjustments:
add()to use local UNIX time and handle recalculation of the UNIX timestamp for local times, preventing infinite recursion and ensuring correct results. ([vlib/time/time.vL138-R163](https://github.com/vlang/v/pull/25233/files#diff-31c344f2cfe6fecdafedef24cc42ca595e29556e07d4efc67c5ab8115e199c88L138-R163))unix()method, guaranteeing consistent UTC-based time calculations. ([[1]](https://github.com/vlang/v/pull/25233/files#diff-31c344f2cfe6fecdafedef24cc42ca595e29556e07d4efc67c5ab8115e199c88L180-R198),[[2]](https://github.com/vlang/v/pull/25233/files#diff-31c344f2cfe6fecdafedef24cc42ca595e29556e07d4efc67c5ab8115e199c88L242-R260),[[3]](https://github.com/vlang/v/pull/25233/files#diff-bfb6f36bf9173246fa5841608044c7e7830996f0bb08297b8c2d333907efaad4L15-R15))Serialization and Debugging Updates:
time.Timein the C backend to use the correcttime__Time_unix()function, ensuring that the serialized value is always the UTC UNIX timestamp. ([[1]](https://github.com/vlang/v/pull/25233/files#diff-1067da5849e2cc623b0efeb705a1dfb35a5beb58d9e8972cde1dd2945bdc5d48L420-R420),[[2]](https://github.com/vlang/v/pull/25233/files#diff-1067da5849e2cc623b0efeb705a1dfb35a5beb58d9e8972cde1dd2945bdc5d48L445-R445),[[3]](https://github.com/vlang/v/pull/25233/files#diff-1067da5849e2cc623b0efeb705a1dfb35a5beb58d9e8972cde1dd2945bdc5d48L957-R959))debug()output fortime.Timeto include theis_localflag, making it easier to distinguish between local and UTC time objects during debugging. ([vlib/time/time.vL367-R387](https://github.com/vlang/v/pull/25233/files#diff-31c344f2cfe6fecdafedef24cc42ca595e29556e07d4efc67c5ab8115e199c88L367-R387))Test Suite Cleanup and Corrections:
[[1]](https://github.com/vlang/v/pull/25233/files#diff-6c972a3c6164cafde5e7324da5b2d8f065262bf1ebb61b8008b846401a59c51dL1-L14),[[2]](https://github.com/vlang/v/pull/25233/files#diff-2fd7b5446296ac4dcb322e862c1a68b67fa3afb7c5febb4570925d9d6dc963f3L18-R18),[[3]](https://github.com/vlang/v/pull/25233/files#diff-b56c81f693db672f625845374a4bd67074d4d60ba2180b658f6d5b63fc0106b7L93-L103),[[4]](https://github.com/vlang/v/pull/25233/files#diff-b56c81f693db672f625845374a4bd67074d4d60ba2180b658f6d5b63fc0106b7L112-L128),[[5]](https://github.com/vlang/v/pull/25233/files#diff-0a2aaf850dfca20c675e71c8062ef7823f57fcd9e2a4d248c1ed06e951eb7ed6L396-R398))These changes collectively improve the reliability and clarity of time operations in V, especially when dealing with local versus UTC time, and ensure that serialization and comparisons behave as expected across different platforms and time zones.