use interface for Duration and Time printing, fix diff ts parsing

This commit is contained in:
2025-09-24 08:31:22 +02:00
parent c4b1bf6487
commit 84792667e7
2 changed files with 16 additions and 83 deletions

View File

@@ -16,7 +16,7 @@
# #
# no need to modify anything below # no need to modify anything below
tool = gfn tool = ts
VERSION = $(shell grep VERSION config.go | head -1 | cut -d '"' -f2) VERSION = $(shell grep VERSION config.go | head -1 | cut -d '"' -f2)
archs = darwin freebsd linux windows archs = darwin freebsd linux windows
PREFIX = /usr/local PREFIX = /usr/local
@@ -72,8 +72,8 @@ release:
gh release create v$(VERSION) --generate-notes gh release create v$(VERSION) --generate-notes
show-versions: buildlocal show-versions: buildlocal
@echo "### gfn version:" @echo "### ts version:"
@./gfn -V @./ts -V
@echo @echo
@echo "### go module versions:" @echo "### go module versions:"

View File

@@ -7,7 +7,7 @@ import (
"github.com/ijt/go-anytime" "github.com/ijt/go-anytime"
"github.com/itlightning/dateparse" "github.com/itlightning/dateparse"
"github.com/jinzhu/now" modnow "github.com/jinzhu/now"
) )
type TimestampProccessor struct { type TimestampProccessor struct {
@@ -23,7 +23,7 @@ func NewTP(conf *Config) *TimestampProccessor {
"Mo. 02 Jan. 2006 15:04:05 MST", // freebsd date (fails, see golang/go/issues/75576) "Mo. 02 Jan. 2006 15:04:05 MST", // freebsd date (fails, see golang/go/issues/75576)
} }
now.TimeFormats = append(now.TimeFormats, formats...) modnow.TimeFormats = append(modnow.TimeFormats, formats...)
return &TimestampProccessor{Config: *conf} return &TimestampProccessor{Config: *conf}
} }
@@ -58,24 +58,23 @@ func (tp *TimestampProccessor) Parse(timestamp string) (time.Time, error) {
return ts, nil return ts, nil
} }
// anytime failed, try module now // anytime failed, try module modnow
ts, err = now.Parse(timestamp) ts, err = modnow.Parse(timestamp)
if err == nil { if err == nil {
return ts, nil return ts, nil
} }
// now failed, try module dateparse // modnow failed, try module dateparse
return dateparse.ParseAny(timestamp) return dateparse.ParseAny(timestamp)
} }
func (tp *TimestampProccessor) Calc(timestampA, timestampB string) error { func (tp *TimestampProccessor) Calc(timestampA, timestampB string) error {
now := time.Now() tsA, err := tp.Parse(timestampA)
tsA, err := anytime.Parse(timestampA, now)
if err != nil { if err != nil {
return err return err
} }
tsB, err := anytime.Parse(timestampB, now) tsB, err := tp.Parse(timestampB)
if err != nil { if err != nil {
return err return err
} }
@@ -88,86 +87,20 @@ func (tp *TimestampProccessor) Calc(timestampA, timestampB string) error {
} else { } else {
diff = tsB.Sub(tsA) diff = tsB.Sub(tsA)
} }
tp.Print(diff) tp.Print(TPduration{TimestampProccessor: *tp, Data: diff})
case ModeAdd: case ModeAdd:
seconds := (tsB.Hour() * 3600) + (tsB.Minute() * 60) + tsB.Second() seconds := (tsB.Hour() * 3600) + (tsB.Minute() * 60) + tsB.Second()
tp.Print(tsA.Add(time.Duration(seconds) * time.Second)) sum := tsA.Add(time.Duration(seconds) * time.Second)
tp.Print(TPdatetime{TimestampProccessor: *tp, Data: sum})
} }
return nil return nil
} }
func (tp *TimestampProccessor) Print(msg any) { func (tp *TimestampProccessor) Print(ts TimestampWriter) {
var repr string _, err := fmt.Fprintln(tp.Output, ts.String())
switch msg := msg.(type) {
case string:
repr = msg
case time.Time:
repr = tp.StringTime(msg)
case time.Duration:
repr = tp.StringDuration(msg)
}
_, err := fmt.Fprintln(tp.Output, repr)
if err != nil { if err != nil {
log.Fatalf("failed to print to given output handle: %s", err) log.Fatalf("failed to print to given output handle: %s", err)
} }
} }
func (tp *TimestampProccessor) StringDuration(msg time.Duration) string {
var unit string
if tp.Unit {
switch tp.Format {
case "d", "day", "days":
unit = " days"
case "h", "hour", "hours":
unit = " hours"
case "m", "min", "mins", "minutes":
unit = " minutes"
case "s", "sec", "secs", "seconds":
unit = " seconds"
case "ms", "msec", "msecs", "milliseconds":
unit = " milliseconds"
}
}
// duration, days, hour, min, sec, msec
switch tp.Format {
case "d", "day", "days":
return fmt.Sprintf("%.02f%s", msg.Hours()/24+(msg.Minutes()/60), unit)
case "h", "hour", "hours":
return fmt.Sprintf("%.02f%s", msg.Hours(), unit)
case "m", "min", "mins", "minutes":
return fmt.Sprintf("%.02f%s", msg.Minutes(), unit)
case "s", "sec", "secs", "seconds":
return fmt.Sprintf("%.02f%s", msg.Seconds(), unit)
case "ms", "msec", "msecs", "milliseconds":
return fmt.Sprintf("%d%s", msg.Milliseconds(), unit)
case "dur", "duration":
fallthrough
default:
return msg.String()
}
}
func (tp *TimestampProccessor) StringTime(msg time.Time) string {
// datetime(default), date, time, unix, string
switch tp.Format {
case "rfc3339":
return msg.Format(time.RFC3339)
case "date":
return msg.Format("2006-01-02")
case "time":
return msg.Format("03:04:05")
case "unix":
return fmt.Sprintf("%d", msg.Unix())
case "datetime":
fallthrough
case "":
return msg.String()
default:
return msg.Format(tp.Format)
}
}