Time Equal Handles Zones And Monotonic

Compare times with Equal and Compare: the eq operator fails across locations and after Round strips the monotonic clock reading.

GoData Integrity

Code

gosnippet
25 lines
package main

import (
	"fmt"
	"time"
)

func main() {
	utc := time.Date(2026, 11, 21, 9, 0, 0, 0, time.UTC)
	est := time.FixedZone("EST", -5*3600) // 固定时区 zone,无需时区数据库
	sameInstant := utc.In(est)            // 同一时间点,不同时区表示

	// == 逐字段比较:位置不同即判不等
	fmt.Println("eq op:", utc == sameInstant)
	// Equal 只比较时间点,忽略位置与单调时钟读数
	fmt.Println("Equal:", utc.Equal(sameInstant))

	now := time.Now()
	stripped := now.Round(0) // Round(0) 剥离单调时钟读数 monotonic clock reading
	fmt.Println("now == stripped:", now == stripped)
	fmt.Println("now Equal stripped:", now.Equal(stripped))

	// Compare 返回三态顺序,同样只看时间点本身
	fmt.Println("Compare:", now.Compare(stripped))
}

Inside the code

Under the hood: the function main.

Points of care

  • The API surface is all built-in — no supply chain to review.

Using it

The main signature reads func main()

The output comes labeled — eq op:, Equal:, now == stripped: — when the file runs.

Key statements

stripped := now.Round(0) // Round(0) 剥离单调时钟读数 monotonic clock reading

Interactive Tools for This Topic

FAQ

What does Time Equal Handles Zones And Monotonic print when run?
Running the file directly is enough — expect labeled output lines such as `eq op:`, `Equal:`, `now == stripped:`.