mirror of
https://codeberg.org/scip/ts.git
synced 2025-12-16 20:20:57 +01:00
Use interfaces, add unit tests, add duration diff support (#3)
* use interface for Duration and Time printing, fix diff ts parsing * add unit tests and gpl * added tests, add support for duration diff+add * bump version * catch setenv errors * fix ci job name
This commit is contained in:
2
.github/workflows/ci.yaml
vendored
2
.github/workflows/ci.yaml
vendored
@@ -1,4 +1,4 @@
|
|||||||
name: build-and-test-gfn
|
name: build-and-test
|
||||||
on: [push]
|
on: [push]
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
|||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -30,3 +30,5 @@ go.work.sum
|
|||||||
# Editor/IDE
|
# Editor/IDE
|
||||||
# .idea/
|
# .idea/
|
||||||
# .vscode/
|
# .vscode/
|
||||||
|
|
||||||
|
ts
|
||||||
|
|||||||
14
Makefile
14
Makefile
@@ -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
|
||||||
@@ -37,11 +37,10 @@ install: buildlocal
|
|||||||
install -o $(UID) -g $(GID) -m 444 $(tool).1 $(PREFIX)/man/man1/
|
install -o $(UID) -g $(GID) -m 444 $(tool).1 $(PREFIX)/man/man1/
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(tool) coverage.out testdata t/out
|
rm -rf $(tool) coverage.* testdata t/out
|
||||||
|
|
||||||
test: clean
|
test: clean
|
||||||
mkdir -p t/out
|
go test -cover ./... $(ARGS)
|
||||||
go test ./... $(ARGS)
|
|
||||||
|
|
||||||
testlint: test lint
|
testlint: test lint
|
||||||
|
|
||||||
@@ -60,7 +59,8 @@ singletest:
|
|||||||
|
|
||||||
cover-report:
|
cover-report:
|
||||||
go test ./... -cover -coverprofile=coverage.out
|
go test ./... -cover -coverprofile=coverage.out
|
||||||
go tool cover -html=coverage.out
|
go tool cover -html=coverage.out -o coverage.html
|
||||||
|
chromium coverage.html
|
||||||
|
|
||||||
goupdate:
|
goupdate:
|
||||||
go get -t -u=patch ./...
|
go get -t -u=patch ./...
|
||||||
@@ -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:"
|
||||||
|
|||||||
24
README.md
24
README.md
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
generic cli timestamp parser and calculator tool
|
generic cli timestamp parser and calculator tool
|
||||||
|
|
||||||
# Usage
|
## Usage
|
||||||
|
|
||||||
```default
|
```default
|
||||||
This is ts, a timestamp tool.
|
This is ts, a timestamp tool.
|
||||||
@@ -21,6 +21,28 @@ Usage: ts <time string> [<time string>]
|
|||||||
-e --examples Show examples or supported inputs.
|
-e --examples Show examples or supported inputs.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
```default
|
||||||
|
# diff between to day and yesterday 10 am
|
||||||
|
ts today "10am yesterday"
|
||||||
|
14h0m0s
|
||||||
|
|
||||||
|
# show timestamp from a couple days ago
|
||||||
|
ts "3 days ago"
|
||||||
|
2025-09-21 13:51:55.054754744 +0200 CEST
|
||||||
|
|
||||||
|
# show timestamp of one hour and 45 minutes before (-d is the defaul)
|
||||||
|
ts -d now 1h45m
|
||||||
|
2025-09-24 12:07:45.072300157 +0200 CEST m=-6299.999710536
|
||||||
|
|
||||||
|
# 10 hours from now
|
||||||
|
ts now 10h
|
||||||
|
2025-09-24 03:53:36.7095512 +0200 CEST m=-35999.999720767
|
||||||
|
```
|
||||||
|
|
||||||
|
To see a comprehensive list of supported inputs, call `ts -e`.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
The tool does not have any dependencies. Just download the binary for
|
The tool does not have any dependencies. Just download the binary for
|
||||||
|
|||||||
@@ -1,3 +1,20 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2025 Thomas von Dein
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -14,12 +31,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VERSIONstring = "0.0.1"
|
VERSIONstring = "0.0.2"
|
||||||
Usage string = `This is ts, a timestamp tool.
|
Usage string = `This is ts, a timestamp tool.
|
||||||
|
|
||||||
Usage: ts <time string> [<time string>]
|
Usage: ts <time string> [<time string>]
|
||||||
-d --diff Calculate difference between two timestamps (default)
|
-d --diff Calculate difference between two timestamps (default)
|
||||||
-a --add Add two timestamps (second parameter must be a time)
|
-a --add Add two timestamps (second parameter must be a duration)
|
||||||
-f --format For diffs: duration, hour, min, sec, msec
|
-f --format For diffs: duration, hour, min, sec, msec
|
||||||
For timestamps: datetime, rfc3339, date, time, unix, string
|
For timestamps: datetime, rfc3339, date, time, unix, string
|
||||||
string is a strftime(1) format string. datetime is
|
string is a strftime(1) format string. datetime is
|
||||||
@@ -54,7 +71,11 @@ noon Yesterday at 10:15am Mon, 02 Jan 2006 15:
|
|||||||
1:05pm Next dec 22nd at 3pm 3 days ago 1999AD
|
1:05pm Next dec 22nd at 3pm 3 days ago 1999AD
|
||||||
10:25:10am Next December 25th at 7:30am UTC-7 Three days ago 1999 AD
|
10:25:10am Next December 25th at 7:30am UTC-7 Three days ago 1999 AD
|
||||||
1:05:10pm Next December 23rd AT 5:25 PM 1 day from now 2008CE
|
1:05:10pm Next December 23rd AT 5:25 PM 1 day from now 2008CE
|
||||||
10:25 Last December 23rd AT 5:25 PM 1 week ago 2008 CE`
|
10:25 Last December 23rd AT 5:25 PM 1 week ago 2008 CE
|
||||||
|
|
||||||
|
Example durations for second parameter:
|
||||||
|
2d1h30m 2 days, one and a half hour
|
||||||
|
30m 30 minutes`
|
||||||
ModeDiff int = iota
|
ModeDiff int = iota
|
||||||
ModeAdd
|
ModeAdd
|
||||||
)
|
)
|
||||||
@@ -110,6 +131,15 @@ func InitConfig(output io.Writer) (*Config, error) {
|
|||||||
return nil, fmt.Errorf("error unmarshalling: %w", err)
|
return nil, fmt.Errorf("error unmarshalling: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// want examples?
|
||||||
|
if conf.Examples {
|
||||||
|
_, err := fmt.Fprintln(output, Examples)
|
||||||
|
if err != nil {
|
||||||
|
Die(err)
|
||||||
|
}
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
// args are timestamps
|
// args are timestamps
|
||||||
if len(flagset.Args()) == 0 {
|
if len(flagset.Args()) == 0 {
|
||||||
return nil, errors.New("no timestamp argument[s] specified.\n" + Usage)
|
return nil, errors.New("no timestamp argument[s] specified.\n" + Usage)
|
||||||
|
|||||||
27
cmd/root.go
27
cmd/root.go
@@ -1,10 +1,25 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2025 Thomas von Dein
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Die(err error) int {
|
func Die(err error) int {
|
||||||
@@ -19,14 +34,6 @@ func Main(output io.Writer) int {
|
|||||||
return Die(err)
|
return Die(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if conf.Examples {
|
|
||||||
_, err := fmt.Fprintln(output, Examples)
|
|
||||||
if err != nil {
|
|
||||||
Die(err)
|
|
||||||
}
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
tp := NewTP(conf)
|
tp := NewTP(conf)
|
||||||
|
|
||||||
if err := tp.ProcessTimestamps(); err != nil {
|
if err := tp.ProcessTimestamps(); err != nil {
|
||||||
|
|||||||
175
cmd/times.go
175
cmd/times.go
@@ -1,31 +1,58 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2025 Thomas von Dein
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"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 {
|
||||||
Config
|
Config
|
||||||
|
Reference time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTP(conf *Config) *TimestampProccessor {
|
func NewTP(conf *Config, ref ...time.Time) *TimestampProccessor {
|
||||||
|
// we add some pre-defined formats to modnow
|
||||||
formats := []string{
|
formats := []string{
|
||||||
time.UnixDate, time.RubyDate,
|
time.UnixDate, time.RubyDate,
|
||||||
time.RFC1123, time.RFC1123Z, time.RFC3339, time.RFC3339Nano,
|
time.RFC1123, time.RFC1123Z, time.RFC3339, time.RFC3339Nano,
|
||||||
time.RFC822, time.RFC822Z, time.RFC850,
|
time.RFC822, time.RFC822Z, time.RFC850,
|
||||||
"Mon Jan 02 15:04:05 PM MST 2006", // linux date
|
"Mon Jan 02 15:04:05 PM MST 2006", // linux date
|
||||||
"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}
|
tp := &TimestampProccessor{Config: *conf, Reference: time.Now()}
|
||||||
|
|
||||||
|
if len(ref) == 1 {
|
||||||
|
tp.Reference = ref[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
return tp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tp *TimestampProccessor) ProcessTimestamps() error {
|
func (tp *TimestampProccessor) ProcessTimestamps() error {
|
||||||
@@ -50,32 +77,37 @@ func (tp *TimestampProccessor) SingleTimestamp(timestamp string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse uses 3 different timestamp parser modules to provide the maximum flexibility
|
// Parse uses 3 different timestamp parser modules to provide maximum flexibility
|
||||||
func (tp *TimestampProccessor) Parse(timestamp string) (time.Time, error) {
|
func (tp *TimestampProccessor) Parse(timestamp string) (time.Time, error) {
|
||||||
reference := time.Now()
|
ts, err := anytime.Parse(timestamp, tp.Reference)
|
||||||
ts, err := anytime.Parse(timestamp, reference)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
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)
|
durB, err := duration2int(timestampB)
|
||||||
|
if err == nil {
|
||||||
|
// calculate with a duration
|
||||||
|
tp.CalcDuration(tsA, durB)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
tsB, err := tp.Parse(timestampB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -83,91 +115,84 @@ func (tp *TimestampProccessor) Calc(timestampA, timestampB string) error {
|
|||||||
switch tp.Mode {
|
switch tp.Mode {
|
||||||
case ModeDiff:
|
case ModeDiff:
|
||||||
var diff time.Duration
|
var diff time.Duration
|
||||||
|
|
||||||
|
// avoid negative results
|
||||||
if tsA.Unix() > tsB.Unix() {
|
if tsA.Unix() > tsB.Unix() {
|
||||||
diff = tsA.Sub(tsB)
|
diff = tsA.Sub(tsB)
|
||||||
} 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) CalcDuration(tsA time.Time, durB time.Duration) {
|
||||||
var repr string
|
var datetime time.Time
|
||||||
|
|
||||||
switch msg := msg.(type) {
|
switch tp.Mode {
|
||||||
case string:
|
case ModeDiff:
|
||||||
repr = msg
|
datetime = tsA.Add(-durB)
|
||||||
case time.Time:
|
case ModeAdd:
|
||||||
repr = tp.StringTime(msg)
|
datetime = tsA.Add(durB)
|
||||||
case time.Duration:
|
|
||||||
repr = tp.StringDuration(msg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := fmt.Fprintln(tp.Output, repr)
|
tp.Print(TPdatetime{TimestampProccessor: *tp, Data: datetime})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tp *TimestampProccessor) Print(ts TimestampWriter) {
|
||||||
|
_, err := fmt.Fprintln(tp.Output, ts.String())
|
||||||
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
|
We could use time.ParseDuration(), but this doesn't support days.
|
||||||
|
|
||||||
if tp.Unit {
|
We could also use github.com/xhit/go-str2duration/v2, which does
|
||||||
switch tp.Format {
|
the job, but it's just another dependency, just for this little
|
||||||
case "d", "day", "days":
|
gem. And we don't need a time.Time value.
|
||||||
unit = " days"
|
|
||||||
case "h", "hour", "hours":
|
Convert a duration into seconds (int).
|
||||||
unit = " hours"
|
|
||||||
case "m", "min", "mins", "minutes":
|
Valid time units are "s", "m", "h" and "d".
|
||||||
unit = " minutes"
|
|
||||||
case "s", "sec", "secs", "seconds":
|
Valid inputs: 2h5m (2 hours and 5 min), 10d12h (10 and a half days)
|
||||||
unit = " seconds"
|
*/
|
||||||
case "ms", "msec", "msecs", "milliseconds":
|
func duration2int(duration string) (time.Duration, error) {
|
||||||
unit = " milliseconds"
|
re := regexp.MustCompile(`(\d+)([dhms])`)
|
||||||
|
seconds := 0
|
||||||
|
found := false
|
||||||
|
|
||||||
|
for _, match := range re.FindAllStringSubmatch(duration, -1) {
|
||||||
|
if len(match) == 3 {
|
||||||
|
found = true
|
||||||
|
v, _ := strconv.Atoi(match[1])
|
||||||
|
switch match[2][0] {
|
||||||
|
case 'd':
|
||||||
|
seconds += v * 86400
|
||||||
|
case 'h':
|
||||||
|
seconds += v * 3600
|
||||||
|
case 'm':
|
||||||
|
seconds += v * 60
|
||||||
|
case 's':
|
||||||
|
seconds += v
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// duration, days, hour, min, sec, msec
|
if !found {
|
||||||
switch tp.Format {
|
return 0, errors.New("failed to parse duration")
|
||||||
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 {
|
return time.Duration(seconds) * time.Second, nil
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
152
cmd/times_test.go
Normal file
152
cmd/times_test.go
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2025 Thomas von Dein
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// reference time for our tests
|
||||||
|
var now = time.Date(2025, 9, 25, 12, 30, 00, 0, time.UTC)
|
||||||
|
|
||||||
|
// return fixed date at given params
|
||||||
|
func dateAtTime(dateFrom time.Time, hour int, min int, sec int) time.Time {
|
||||||
|
t := dateFrom
|
||||||
|
return time.Date(t.Year(), t.Month(), t.Day(), hour, min, sec, 0, t.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
func setUTC() {
|
||||||
|
// make sure to have a consistent environment
|
||||||
|
if err := os.Setenv("TZ", "UTC"); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
loc, _ := time.LoadLocation("UTC")
|
||||||
|
time.Local = loc
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseTimestamps(t *testing.T) {
|
||||||
|
setUTC()
|
||||||
|
|
||||||
|
var datetimes = []struct {
|
||||||
|
input string
|
||||||
|
want time.Time
|
||||||
|
}{
|
||||||
|
// some timestamps from ijt/go-anytime/anytime_test.go
|
||||||
|
{`a minute from now`, now.Add(time.Minute)},
|
||||||
|
{`5 minutes ago`, now.Add(-5 * time.Minute)},
|
||||||
|
{`an hour from now`, now.Add(time.Hour)},
|
||||||
|
{`Yesterday 10am`, dateAtTime(now.AddDate(0, 0, -1), 10, 0, 0)},
|
||||||
|
{`Mon Jan 2 15:04:05 2006`, time.Date(2006, 1, 2, 15, 4, 5, 0, now.Location())},
|
||||||
|
{`1 day from now`, now.Add(24 * time.Hour)},
|
||||||
|
{`One year ago`, now.AddDate(-1, 0, 0)},
|
||||||
|
{`03:15`, dateAtTime(now, 3, 15, 0)},
|
||||||
|
{`Wed Sep 25 12:30:00 PM UTC 2025`, now},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range datetimes {
|
||||||
|
testname := fmt.Sprintf("parsetimestamp-%s", strings.ReplaceAll(tt.input, " ", "-"))
|
||||||
|
t.Run(testname, func(t *testing.T) {
|
||||||
|
var writer bytes.Buffer
|
||||||
|
tp := NewTP(&Config{Args: []string{tt.input}, Output: &writer}, now)
|
||||||
|
|
||||||
|
// writer.String()
|
||||||
|
ts, err := tp.Parse(tt.input)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.want, ts)
|
||||||
|
|
||||||
|
err = tp.ProcessTimestamps()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, tt.want.String()+"\n", writer.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func preDiff(tsA, tsB time.Time) time.Duration {
|
||||||
|
var diff time.Duration
|
||||||
|
|
||||||
|
// avoid negative results
|
||||||
|
if tsA.Unix() > tsB.Unix() {
|
||||||
|
diff = tsA.Sub(tsB)
|
||||||
|
} else {
|
||||||
|
diff = tsB.Sub(tsA)
|
||||||
|
}
|
||||||
|
|
||||||
|
return diff
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDiffTimestamps(t *testing.T) {
|
||||||
|
setUTC()
|
||||||
|
|
||||||
|
var datetimes = []struct {
|
||||||
|
A string
|
||||||
|
B string
|
||||||
|
want time.Duration
|
||||||
|
}{
|
||||||
|
{`now`, `11:30`, preDiff(now, dateAtTime(now, 11, 30, 00))},
|
||||||
|
{`11:30`, `now`, preDiff(now, dateAtTime(now, 11, 30, 00))},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range datetimes {
|
||||||
|
testname := fmt.Sprintf("diff-%s-%s", strings.ReplaceAll(tt.A, " ", "-"), strings.ReplaceAll(tt.B, " ", "-"))
|
||||||
|
t.Run(testname, func(t *testing.T) {
|
||||||
|
var writer bytes.Buffer
|
||||||
|
tp := NewTP(&Config{Args: []string{tt.A, tt.B}, Output: &writer, Mode: ModeDiff}, now)
|
||||||
|
|
||||||
|
err := tp.ProcessTimestamps()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, tt.want.String()+"\n", writer.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddTimestamps(t *testing.T) {
|
||||||
|
setUTC()
|
||||||
|
|
||||||
|
var datetimes = []struct {
|
||||||
|
A string
|
||||||
|
B string
|
||||||
|
want time.Time
|
||||||
|
}{
|
||||||
|
{`now`, `01:30`, dateAtTime(now, 14, 00, 00)},
|
||||||
|
{`now`, `2h`, dateAtTime(now, 14, 30, 00)},
|
||||||
|
{`now`, `12d4h`, dateAtTime(now.Add(time.Hour*24*12), 16, 30, 00)},
|
||||||
|
{`now`, `45m`, dateAtTime(now, 13, 15, 00)},
|
||||||
|
{`now`, `1d10s`, dateAtTime(now.Add(time.Hour*24*1), 12, 30, 10)},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range datetimes {
|
||||||
|
testname := fmt.Sprintf("diff-%s-%s", strings.ReplaceAll(tt.A, " ", "-"), strings.ReplaceAll(tt.B, " ", "-"))
|
||||||
|
t.Run(testname, func(t *testing.T) {
|
||||||
|
var writer bytes.Buffer
|
||||||
|
tp := NewTP(&Config{Args: []string{tt.A, tt.B}, Output: &writer, Mode: ModeAdd}, now)
|
||||||
|
|
||||||
|
err := tp.ProcessTimestamps()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, tt.want.String()+"\n", writer.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
94
cmd/writer.go
Normal file
94
cmd/writer.go
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2025 Thomas von Dein
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TimestampWriter interface {
|
||||||
|
String() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type TPduration struct {
|
||||||
|
TimestampProccessor
|
||||||
|
Data time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
type TPdatetime struct {
|
||||||
|
TimestampProccessor
|
||||||
|
Data time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (duration TPduration) String() string {
|
||||||
|
var unit string
|
||||||
|
|
||||||
|
if duration.Unit {
|
||||||
|
switch duration.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 duration.Format {
|
||||||
|
case "d", "day", "days":
|
||||||
|
return fmt.Sprintf("%.02f%s", duration.Data.Hours()/24+(duration.Data.Minutes()/60), unit)
|
||||||
|
case "h", "hour", "hours":
|
||||||
|
return fmt.Sprintf("%.02f%s", duration.Data.Hours(), unit)
|
||||||
|
case "m", "min", "mins", "minutes":
|
||||||
|
return fmt.Sprintf("%.02f%s", duration.Data.Minutes(), unit)
|
||||||
|
case "s", "sec", "secs", "seconds":
|
||||||
|
return fmt.Sprintf("%.02f%s", duration.Data.Seconds(), unit)
|
||||||
|
case "ms", "msec", "msecs", "milliseconds":
|
||||||
|
return fmt.Sprintf("%d%s", duration.Data.Milliseconds(), unit)
|
||||||
|
case "dur", "duration":
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
return duration.Data.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (datetime TPdatetime) String() string {
|
||||||
|
// datetime(default), date, time, unix, string
|
||||||
|
switch datetime.Format {
|
||||||
|
case "rfc3339":
|
||||||
|
return datetime.Data.Format(time.RFC3339)
|
||||||
|
case "date":
|
||||||
|
return datetime.Data.Format("2006-01-02")
|
||||||
|
case "time":
|
||||||
|
return datetime.Data.Format("03:04:05")
|
||||||
|
case "unix":
|
||||||
|
return fmt.Sprintf("%d", datetime.Data.Unix())
|
||||||
|
case "datetime":
|
||||||
|
fallthrough
|
||||||
|
case "":
|
||||||
|
return datetime.Data.String()
|
||||||
|
default:
|
||||||
|
return datetime.Data.Format(datetime.Format)
|
||||||
|
}
|
||||||
|
}
|
||||||
4
go.mod
4
go.mod
@@ -5,6 +5,7 @@ go 1.23.0
|
|||||||
toolchain go1.23.5
|
toolchain go1.23.5
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
|
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
|
||||||
github.com/ijt/go-anytime v1.9.2 // indirect
|
github.com/ijt/go-anytime v1.9.2 // indirect
|
||||||
github.com/ijt/goparsify v0.0.0-20221203142333-3a5276334b8d // indirect
|
github.com/ijt/goparsify v0.0.0-20221203142333-3a5276334b8d // indirect
|
||||||
@@ -15,5 +16,8 @@ require (
|
|||||||
github.com/knadh/koanf/v2 v2.3.0 // indirect
|
github.com/knadh/koanf/v2 v2.3.0 // indirect
|
||||||
github.com/mitchellh/copystructure v1.2.0 // indirect
|
github.com/mitchellh/copystructure v1.2.0 // indirect
|
||||||
github.com/mitchellh/reflectwalk v1.0.2 // indirect
|
github.com/mitchellh/reflectwalk v1.0.2 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/spf13/pflag v1.0.10 // indirect
|
github.com/spf13/pflag v1.0.10 // indirect
|
||||||
|
github.com/stretchr/testify v1.11.1 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
9
go.sum
9
go.sum
@@ -1,3 +1,5 @@
|
|||||||
|
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/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
|
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
|
||||||
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||||
github.com/ijt/go-anytime v1.9.2 h1:DmYgVwUiFPNR+n6c1T5P070tlGATRZG4aYNJs6XDUfU=
|
github.com/ijt/go-anytime v1.9.2 h1:DmYgVwUiFPNR+n6c1T5P070tlGATRZG4aYNJs6XDUfU=
|
||||||
@@ -18,5 +20,12 @@ github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa1
|
|||||||
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
|
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
|
||||||
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
|
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
|
||||||
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
|
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
|
||||||
|
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/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
|
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
|
||||||
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
17
main.go
17
main.go
@@ -1,3 +1,20 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2025 Thomas von Dein
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
Reference in New Issue
Block a user