Skip to content

Instantly share code, notes, and snippets.

@senorprogrammer
Created March 1, 2020 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save senorprogrammer/ccdc51118b586b1841d154369a4c13bc to your computer and use it in GitHub Desktop.
Save senorprogrammer/ccdc51118b586b1841d154369a4c13bc to your computer and use it in GitHub Desktop.
Golang Time fun
package main
import (
"fmt"
"time"
)
func TimeIn(t time.Time, name string) (time.Time, error) {
loc, err := time.LoadLocation(name)
if err == nil {
t = t.In(loc)
}
return t, err
}
func main() {
t := time.Now()
a, _ := TimeIn(t, "America/New_York")
b, _ := TimeIn(t, "Asia/Shanghai")
fmt.Println(fmt.Sprintf("A:\t%v", a))
fmt.Println(fmt.Sprintf("B:\t%v", b))
fmt.Println(fmt.Sprintf("Same time? %v", a == b))
fmt.Println(fmt.Sprintf("Equal time? %v", a.Equal(b)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment