Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jelloeater committed Sep 19, 2024
2 parents 2724187 + 59577d5 commit 0f9a728
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.go]
indent_style = tab

[*.md]
trim_trailing_whitespace = false

Expand Down
1 change: 0 additions & 1 deletion Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ tasks:
- rm_deb
- snapshot_build
cmds:
- goreleaser --snapshot --clean
- sudo dpkg -i $(find dist/*amd64.deb)

install-hooks:
Expand Down
50 changes: 36 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package main

import (
"github.com/atotto/clipboard"
"github.com/beevik/ntp"
"github.com/urfave/cli/v2"
"log"
"log/slog"
"os"
"time"

"github.com/atotto/clipboard"
"github.com/beevik/ntp"
"github.com/urfave/cli/v2"
)

var ( // Create by GoRelease at compile time
Expand All @@ -16,7 +17,7 @@ var ( // Create by GoRelease at compile time
//date = "unknown"
)

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

if os.Getenv("STAMPY_TZ") != "" {
timezone = os.Getenv("STAMPY_TZ")
Expand All @@ -27,23 +28,33 @@ func writeDate(format string, timezone string, ntpServer string) {
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

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

if ntpServer != "" { // Override local time with NTP server
ntpTime, _ := ntp.Time(ntpServer)
now = ntpTime
println("NTP from " + ntpServer)
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)

}

func mainCliApp() error {
Expand All @@ -57,13 +68,18 @@ func mainCliApp() error {
Name: "stampy",
Usage: "Copy formatted timestamp to system clipboard",
Args: true,
Version: "v" + version + " build " + commit,
Version: "v" + version + "+" + commit,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Value: "2006-01-02T15:04:05Z07:00",
Usage: "Timestamp format",
},
&cli.BoolFlag{
Name: "diary",
Value: false,
Usage: "Diary format",
},
&cli.StringFlag{
Name: "timezone",
Value: "UTC",
Expand All @@ -83,7 +99,13 @@ func mainCliApp() error {
format := c.String("format")
timezone := c.String("timezone")
ntpServer := c.String("ntp_server")
writeDate(format, timezone, ntpServer)
diaryFormat := c.Bool("diary")
writeDate(
format,
timezone,
ntpServer,
diaryFormat,
)

return nil
},
Expand Down

0 comments on commit 0f9a728

Please sign in to comment.