Junior — Middle
What is the best way to store timestamps considering time zones for universal processing?
sobes.tech AI
Answer from AI
The best way to store timestamps considering time zones is to use Coordinated Universal Time (UTC). This helps avoid issues with daylight saving time changes and time zone differences.
In Go, this is usually done using the time.Time type and the UTC() and Parse methods with a specified time zone.
Example:
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now().UTC() // current time in UTC
fmt.Println("UTC time:", now)
// Parsing time in UTC
t, err := time.Parse(time.RFC3339, "2024-06-01T15:04:05Z")
if err != nil {
panic(err)
}
fmt.Println("Parsed time:", t)
}
Storing time in UTC simplifies processing and comparison of timestamps across different time zones.