Created
March 8, 2016 09:59
-
-
Save niels-s/f6b67c0f792c28be62b0 to your computer and use it in GitHub Desktop.
Time thing in golang
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" | |
) | |
var utc *time.Location | |
func main() { | |
utc, _ = time.LoadLocation("UTC") | |
nl, _ := time.LoadLocation("Europe/Amsterdam") | |
de, _ := time.LoadLocation("Europe/Berlin") | |
us, _ := time.LoadLocation("America/New_York") | |
// Morning newsletter | |
// NL | |
printStuff("2016-03-15T07:10:00Z", nl) | |
fmt.Println("-------------------------------------------------------") | |
// DE | |
printStuff("2016-03-15T07:10:00Z", de) | |
fmt.Println("-------------------------------------------------------") | |
// US | |
printStuff("2016-03-15T12:10:00Z", us) | |
fmt.Println("-------------------------------------------------------") | |
fmt.Println("") | |
fmt.Println("") | |
fmt.Println("Very long evening newsletter") | |
fmt.Println("") | |
// Evening newsletter (with very long delay) | |
// NL | |
printStuffAndCompare("2016-03-15T19:10:00Z", "2016-03-15T07:10:00Z", nl) | |
fmt.Println("-------------------------------------------------------") | |
// DE | |
printStuffAndCompare("2016-03-15T19:10:00Z", "2016-03-15T07:10:00Z", de) | |
fmt.Println("-------------------------------------------------------") | |
// US | |
printStuffAndCompare("2016-03-16T00:10:00Z", "2016-03-15T12:10:00Z", us) | |
} | |
func printStuffAndCompare(current_newsletter, last_sent_newsletter string, loc *time.Location) { | |
printStuff(current_newsletter, loc) | |
current_newsletter_time, _ := time.Parse(time.RFC3339, current_newsletter) | |
current_newsletter_without_time := withoutTime(current_newsletter_time.In(loc)) | |
last_sent_time, _ := time.Parse(time.RFC3339, last_sent_newsletter) | |
last_sent_without_time := withoutTime(last_sent_time.In(loc)) | |
if !current_newsletter_without_time.Equal(last_sent_without_time) { | |
fmt.Printf("Times didn't match up: %s - %s\n", current_newsletter_without_time, last_sent_without_time) | |
} | |
} | |
func printStuff(timeString string, loc *time.Location) { | |
parsed_time, _ := time.Parse(time.RFC3339, timeString) | |
fmt.Println(parsed_time) | |
fmt.Println(parsed_time.In(loc)) | |
fmt.Println(withoutTime(parsed_time.In(loc))) | |
} | |
func withoutTime(input time.Time) time.Time { | |
return time.Date(input.Year(), input.Month(), input.Day(), 0, 0, 0, 0, utc) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment