From 37fc7950372a5c12a83fb6594ab80dd4365be41e Mon Sep 17 00:00:00 2001 From: yangzhongjiao Date: Wed, 25 Mar 2026 06:45:20 +0000 Subject: [PATCH] feat(timeutil): add utility functions for UTC time conversion - Introduced ToUTC function to convert a time pointer to its UTC equivalent, returning nil if the input is nil. - Added NowUTC function to retrieve the current time in UTC. --- pkg/timeutil/timeutil.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 pkg/timeutil/timeutil.go diff --git a/pkg/timeutil/timeutil.go b/pkg/timeutil/timeutil.go new file mode 100644 index 00000000..abf1c99c --- /dev/null +++ b/pkg/timeutil/timeutil.go @@ -0,0 +1,22 @@ +package timeutil + +import "time" + +// ToUTC returns a pointer to the same instant expressed in UTC. +// Returns nil if t is nil. +// +// The go-sql-driver/mysql with loc=Local symmetrically converts on both +// write (In(loc)) and read (parsed as loc), so the time.Time already +// carries the correct instant; calling UTC() only normalises the zone. +func ToUTC(t *time.Time) *time.Time { + if t == nil { + return nil + } + utc := t.UTC() + return &utc +} + +// NowUTC returns the current time in UTC. +func NowUTC() time.Time { + return time.Now().UTC() +}