Added clocks tool
This commit is contained in:
parent
8dc2621f87
commit
7073a2d3f2
23 changed files with 1061 additions and 3 deletions
13
cmds/clocks/elems.go
Normal file
13
cmds/clocks/elems.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package main
|
||||
|
||||
import "syscall/js"
|
||||
|
||||
func findClockElements(id string) htmlElements {
|
||||
doc := js.Global().Get("document")
|
||||
rootClockElem := doc.Call("querySelector", "#"+id)
|
||||
return htmlElements{
|
||||
locationDiv: rootClockElem.Call("querySelector", ".location"),
|
||||
timeDiv: rootClockElem.Call("querySelector", ".time"),
|
||||
dateDiv: rootClockElem.Call("querySelector", ".date"),
|
||||
}
|
||||
}
|
||||
87
cmds/clocks/main.go
Normal file
87
cmds/clocks/main.go
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
//go:build js
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
_ "time/tzdata"
|
||||
)
|
||||
|
||||
func main() {
|
||||
melTime, err := time.LoadLocation("Australia/Melbourne")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
sg, err := time.LoadLocation("Asia/Singapore")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
nyc, err := time.LoadLocation("America/New_York")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
la, err := time.LoadLocation("America/Los_Angeles")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
utc := time.UTC
|
||||
|
||||
clocks := []clock{
|
||||
{
|
||||
name: "UTC",
|
||||
loc: utc,
|
||||
elements: findClockElements("utc"),
|
||||
},
|
||||
{
|
||||
name: "Australia/Melbourne",
|
||||
loc: melTime,
|
||||
elements: findClockElements("mel"),
|
||||
},
|
||||
{
|
||||
name: "Asia/Singapore",
|
||||
loc: sg,
|
||||
elements: findClockElements("sg"),
|
||||
},
|
||||
{
|
||||
name: "America/New_York",
|
||||
loc: nyc,
|
||||
elements: findClockElements("nyc"),
|
||||
},
|
||||
{
|
||||
name: "America/Los_Angeles",
|
||||
loc: la,
|
||||
elements: findClockElements("la"),
|
||||
},
|
||||
}
|
||||
|
||||
updateClocks(clocks)
|
||||
go startClockUpdater(clocks)
|
||||
|
||||
<-make(chan struct{})
|
||||
}
|
||||
|
||||
func startClockUpdater(clocks []clock) {
|
||||
for range time.Tick(1 * time.Second) {
|
||||
updateClocks(clocks)
|
||||
}
|
||||
}
|
||||
|
||||
func updateClocks(clocks []clock) {
|
||||
n := time.Now()
|
||||
for _, c := range clocks {
|
||||
updateClock(n, c)
|
||||
}
|
||||
}
|
||||
|
||||
func updateClock(t time.Time, clk clock) {
|
||||
lt := t.In(clk.loc)
|
||||
clk.elements.locationDiv.Set("innerText", clk.name)
|
||||
clk.elements.dateDiv.Set("innerText", lt.Format("02 Jan 2006"))
|
||||
clk.elements.timeDiv.Set("innerText", lt.Format("15:04:05"))
|
||||
}
|
||||
18
cmds/clocks/models.go
Normal file
18
cmds/clocks/models.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"syscall/js"
|
||||
"time"
|
||||
)
|
||||
|
||||
type clock struct {
|
||||
name string
|
||||
loc *time.Location
|
||||
elements htmlElements
|
||||
}
|
||||
|
||||
type htmlElements struct {
|
||||
locationDiv js.Value
|
||||
timeDiv js.Value
|
||||
dateDiv js.Value
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue