Skip to content

Instantly share code, notes, and snippets.

@johncalvinroberts
Last active November 1, 2021 05:27
Show Gist options
  • Save johncalvinroberts/55ba56a710990e6820409a2d637f90f3 to your computer and use it in GitHub Desktop.
Save johncalvinroberts/55ba56a710990e6820409a2d637f90f3 to your computer and use it in GitHub Desktop.
XBar plugin to show local time in various timezones and difference with local time
// <xbar.title>Locations</xbar.title>
// <xbar.version>v1.0</xbar.version>
// <xbar.author>John Roberts</xbar.author>
// <xbar.author.github>johncalvinroberts</xbar.author.github>
// <xbar.desc>The script displays the time in different timezones</xbar.desc>
// <xbar.image>https://raw.githubusercontent.com/christophschlosser/bitbar-plugins/checkhosts/Network/checkhosts.png</xbar.image>
// <xbar.dependencies>go</xbar.dependencies>
package main
import (
"fmt"
"time"
)
// locations, refer to IANA timezone database
// https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
// https://www.iana.org/time-zones
var locations = [][]string{
{"PVG", "Asia/Shanghai"},
{"HND", "Asia/Tokyo"},
{"PDX", "America/Los_Angeles"},
{"SLC", "America/Boise"},
{"MSP", "America/Chicago"},
{"NY", "America/New_York"},
{"CPH", "Europe/Copenhagen"},
}
const layout = "3:04pm, Jan 2"
func main() {
var toprint string
nowHere := time.Now()
for _, tuple := range locations {
key, value := tuple[0], tuple[1]
// get local time
// print key + local time + difference to current time
loc, err := time.LoadLocation(value)
var nextline string
if err != nil {
nextline = fmt.Sprintf("%s: %s", key, "error")
} else {
nowThere := time.Now().In(loc)
nowThereButHere := time.Date(
nowThere.Year(),
nowThere.Month(),
nowThere.Day(),
nowThere.Hour(),
nowThere.Minute(),
nowThere.Second(),
nowThere.Nanosecond(),
nowHere.Location())
offset := nowHere.Sub(nowThereButHere)
nextline = fmt.Sprintf("%s: %s (%.0f hrs diff)", key, nowThere.Format(layout), offset.Hours())
}
toprint = fmt.Sprintf("%s%s\n", toprint, nextline)
}
fmt.Println(":earth_asia:")
fmt.Println("---")
fmt.Println(toprint)
fmt.Println("Refresh | refresh=true")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment