From 3affdf7d46a8c82a0e0f34be2f6af9d8a10f7eac Mon Sep 17 00:00:00 2001 From: Jelloeater Date: Sun, 8 Sep 2024 20:26:27 -0400 Subject: [PATCH] Added NTP for time pull --- README.md | 3 ++- go.mod | 3 +++ go.sum | 14 ++++++++++++++ main.go | 19 +++++++++++++++++-- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9e2d8fa..a42e4d2 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/go.mod b/go.mod index 62d9e8a..aa51c08 100644 --- a/go.mod +++ b/go.mod @@ -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 ) @@ -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 ) diff --git a/go.sum b/go.sum index 578a3a1..6d52675 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 459ecf1..dd2fe14 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "github.com/atotto/clipboard" + "github.com/beevik/ntp" "github.com/urfave/cli/v2" "log" "log/slog" @@ -15,7 +16,7 @@ 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") @@ -23,6 +24,9 @@ func writeDate(format string, timezone string) { 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 { @@ -30,6 +34,11 @@ func writeDate(format string, timezone string) { } 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") @@ -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, @@ -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 },