Created
March 1, 2020 12:06
-
-
Save senorprogrammer/ccdc51118b586b1841d154369a4c13bc to your computer and use it in GitHub Desktop.
Golang Time fun
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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