Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An easy way to print out two timezones side by side.
package main
import (
"bytes"
"flag"
"fmt"
"text/tabwriter"
"time"
)
var flagZone = flag.String("z", "UTC", "remote time zone")
func init() {
flag.Parse()
}
func main() {
tz, e := time.LoadLocation(*flagZone)
if e != nil {
fmt.Println(e)
return
}
now := time.Now()
tt := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
buf := bytes.Buffer{}
w := new(tabwriter.Writer)
w.Init(&buf, 10, 0, 1, ' ', 0)
fmt.Fprintf(w, "%s\t%s\n", tt.Location(), tz)
fmt.Fprintln(w, "-------\t-------")
for i := 0; i < 24; i++ {
l := tt.Add(time.Duration(i) * time.Hour).Format("3:04pm")
r := tt.In(tz).Add(time.Duration(i) * time.Hour).Format("3:04pm")
fmt.Fprintf(w, "% 7s\t% 7s\n", l, r)
}
w.Flush()
fmt.Println(buf.String())
}
@JenniferMack

This comment has been minimized.

Copy link
Owner Author

JenniferMack commented Jul 6, 2019

Sample output:

Local     UTC
-------   -------
12:00am    7:00am
 1:00am    8:00am
 2:00am    9:00am
 3:00am   10:00am
 4:00am   11:00am
 5:00am   12:00pm
 6:00am    1:00pm
 7:00am    2:00pm
 8:00am    3:00pm
 9:00am    4:00pm
10:00am    5:00pm
11:00am    6:00pm
12:00pm    7:00pm
 1:00pm    8:00pm
 2:00pm    9:00pm
 3:00pm   10:00pm
 4:00pm   11:00pm
 5:00pm   12:00am
 6:00pm    1:00am
 7:00pm    2:00am
 8:00pm    3:00am
 9:00pm    4:00am
10:00pm    5:00am
11:00pm    6:00am
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.