Skip to content

Commit

Permalink
Added NTP for time pull
Browse files Browse the repository at this point in the history
  • Loading branch information
Jelloeater committed Sep 9, 2024
1 parent 4623f04 commit 3affdf7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ go install github.com/Jelloeater/stampy@latest
### Settings
You can use ENV_VARs to override settings
```shell
export STAMPY_TZ=EST
export STAMPY_TZ='EST'
export STAMPY_FORMAT='01/02/2006 15:04'
export STAMPY_NTP='pool.ntp.org'
```

### Help
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22

require (
github.com/atotto/clipboard v0.1.4
github.com/beevik/ntp v1.4.3
github.com/urfave/cli/v2 v2.27.4

)
Expand All @@ -12,4 +13,6 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/beevik/ntp v1.4.3 h1:PlbTvE5NNy4QHmA4Mg57n7mcFTmr1W1j3gcK7L1lqho=
github.com/beevik/ntp v1.4.3/go.mod h1:Unr8Zg+2dRn7d8bHFuehIMSvvUYssHMxW3Q5Nx4RW5Q=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/urfave/cli/v2 v2.27.4 h1:o1owoI+02Eb+K107p27wEX9Bb8eqIoZCfLXloLUSWJ8=
github.com/urfave/cli/v2 v2.27.4/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
19 changes: 17 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"github.com/atotto/clipboard"
"github.com/beevik/ntp"
"github.com/urfave/cli/v2"
"log"
"log/slog"
Expand All @@ -15,21 +16,29 @@ var ( // Create by GoRelease at compile time
//date = "unknown"
)

func writeDate(format string, timezone string) {
func writeDate(format string, timezone string, ntpServer string) {

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")
}

loc, e := time.LoadLocation(timezone)
if e != nil {
log.Fatal(e)
}
now := time.Now().In(loc)

if ntpServer != "" { // Override local time with NTP server
ntpTime, _ := ntp.Time(ntpServer)
now = ntpTime
println("NTP from " + ntpServer)
}
timestamp := now.Format(format)
clip := timestamp
println(clip + " copied to clipboard")
Expand Down Expand Up @@ -60,6 +69,11 @@ func mainCliApp() error {
Value: "UTC",
Usage: "Timezone",
},
&cli.StringFlag{
Name: "ntp_server",
Value: "",
Usage: "NTP Server (ex pool.ntp.org)",
},
},
EnableBashCompletion: true,
HideHelp: false,
Expand All @@ -68,7 +82,8 @@ func mainCliApp() error {
Action: func(c *cli.Context) error {
format := c.String("format")
timezone := c.String("timezone")
writeDate(format, timezone)
ntpServer := c.String("ntp_server")
writeDate(format, timezone, ntpServer)

return nil
},
Expand Down

0 comments on commit 3affdf7

Please sign in to comment.