Created
December 4, 2020 20:46
-
-
Save kylebrandt/00b45c307d35216e14ca643b8c6f57c0 to your computer and use it in GitHub Desktop.
random time zone go illustrations
This file contains hidden or 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 main() { | |
utc6 := time.FixedZone("UTC+6", 6*3600) | |
utc7 := time.FixedZone("UTC+7", 7*3600) | |
aTime := time.Unix(3600, 0) | |
// Call string with default format, play.golang.org is in UTC. | |
fmt.Println(aTime) | |
// Same time will be displayed in different timezones. "7am and 8am" | |
fmt.Println(aTime.In(utc6), "||", aTime.In(utc7)) | |
// .Unix() will not change depending on time zone. | |
fmt.Println(aTime.In(utc6).Unix(), aTime.In(utc7).Unix()) | |
// time.Time's Equal method is true across different timezones. | |
fmt.Println(aTime.In(utc6).Equal(aTime.In(utc7))) | |
// but they are different according to the equal operator | |
fmt.Println(aTime.In(utc6) == aTime.In(utc7)) | |
} | |
// Only first line of output differs depending on host timezone | |
// Play output (UTC): | |
// 1970-01-01 01:00:00 +0000 UTC | |
// 1970-01-01 07:00:00 +0600 UTC+6 || 1970-01-01 08:00:00 +0700 UTC+7 | |
// 3600 3600 | |
// true | |
// false | |
// Output on a machine in EST: | |
// 1969-12-31 20:00:00 -0500 EST | |
// 1970-01-01 07:00:00 +0600 UTC+6 || 1970-01-01 08:00:00 +0700 UTC+7 | |
// 3600 3600 | |
// true | |
// false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment