-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
48 lines (43 loc) · 1.03 KB
/
worker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"github.com/atotto/clipboard"
"github.com/beevik/ntp"
"log"
"os"
"time"
)
func writeDate(format string, timezone string, ntpServer string, diaryFormat bool) {
if os.Getenv("STAMPY_TZ") != "" {
timezone = os.Getenv("STAMPY_TZ")
}
if os.Getenv("STAMPY_FORMAT") != "" {
format = os.Getenv("STAMPY_FORMAT")
}
if os.Getenv("STAMPY_NTP") != "" {
ntpServer = os.Getenv("STAMPY_NTP")
}
if diaryFormat {
format = "Monday January 2 2006 3:04PM"
}
now := time.Time{} // Declare now outside the if-else block
if timezone != "" {
loc, err := time.LoadLocation(timezone)
if err != nil {
log.Panic(err)
}
now = time.Now().In(loc)
} else {
now = time.Now()
}
if ntpServer != "" { // Override local time with NTP server
ntpTime, err := ntp.Time(ntpServer)
if err == nil { // Check for errors when getting NTP time
now = ntpTime
println("NTP from " + ntpServer)
}
}
timestamp := now.Format(format)
clip := timestamp
println(clip + " copied to clipboard")
_ = clipboard.WriteAll(clip)
}