Skip to content

time: always return utc() timezone for Time.unix/0#25233

Merged
spytheman merged 3 commits into
vlang:masterfrom
Le0Developer:fix-17784
Sep 4, 2025
Merged

time: always return utc() timezone for Time.unix/0#25233
spytheman merged 3 commits into
vlang:masterfrom
Le0Developer:fix-17784

Conversation

@Le0Developer

Copy link
Copy Markdown
Member

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.Time objects 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:

  • Added new method local_unix() to time.Time to return the local UNIX timestamp, and updated the existing unix() 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))
  • Updated the equality (==) and less-than (<) operators for time.Time to use both the is_local flag 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:

  • Changed time addition logic in 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))
  • Updated formatting and relative time calculation functions to use the new 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:

  • Modified JSON encoding logic for time.Time in the C backend to use the correct time__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))
  • Enhanced the debug() output for time.Time to include the is_local flag, 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:

  • Removed obsolete or incorrect tests that depended on old time behavior, and updated existing tests to reflect the new time handling logic, including correct usage of local and UTC UNIX timestamps and formatting. ([[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.

@huly-for-github

Copy link
Copy Markdown

Connected to Huly®: V_0.6-24728

@Le0Developer Le0Developer requested a review from Copilot September 4, 2025 09:31

Copilot AI 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.

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 making unix() 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.

Comment thread vlib/time/time.v Outdated
Comment thread vlib/time/time.v
Comment thread vlib/time/operator.v
Comment thread vlib/time/time.v
@spytheman spytheman merged commit 2b4253c into vlang:master Sep 4, 2025
83 checks passed
@Le0Developer Le0Developer deleted the fix-17784 branch September 5, 2025 06:52
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.

epoch generation is based on timezone

3 participants