diff --git a/Makefile b/Makefile index af81ecb..687f554 100644 --- a/Makefile +++ b/Makefile @@ -37,11 +37,10 @@ install: buildlocal install -o $(UID) -g $(GID) -m 444 $(tool).1 $(PREFIX)/man/man1/ clean: - rm -rf $(tool) coverage.out testdata t/out + rm -rf $(tool) coverage.* testdata t/out test: clean - mkdir -p t/out - go test ./... $(ARGS) + go test -cover ./... $(ARGS) testlint: test lint @@ -60,7 +59,8 @@ singletest: cover-report: 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: go get -t -u=patch ./... diff --git a/cmd/config.go b/cmd/config.go index aa489da..5afc452 100644 --- a/cmd/config.go +++ b/cmd/config.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 . +*/ + package cmd import ( diff --git a/cmd/root.go b/cmd/root.go index 05d1d0b..391eced 100644 --- a/cmd/root.go +++ b/cmd/root.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 . +*/ + package cmd import ( diff --git a/cmd/times.go b/cmd/times.go index 1dfc086..a827cc4 100644 --- a/cmd/times.go +++ b/cmd/times.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 . +*/ + package cmd import ( @@ -12,20 +29,27 @@ import ( type TimestampProccessor struct { 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{ time.UnixDate, time.RubyDate, time.RFC1123, time.RFC1123Z, time.RFC3339, time.RFC3339Nano, time.RFC822, time.RFC822Z, time.RFC850, "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) } 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 { @@ -50,10 +74,9 @@ func (tp *TimestampProccessor) SingleTimestamp(timestamp string) error { 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) { - reference := time.Now() - ts, err := anytime.Parse(timestamp, reference) + ts, err := anytime.Parse(timestamp, tp.Reference) if err == nil { return ts, nil } @@ -82,16 +105,20 @@ func (tp *TimestampProccessor) Calc(timestampA, timestampB string) error { switch tp.Mode { case ModeDiff: var diff time.Duration + + // avoid negative results if tsA.Unix() > tsB.Unix() { diff = tsA.Sub(tsB) } else { diff = tsB.Sub(tsA) } + tp.Print(TPduration{TimestampProccessor: *tp, Data: diff}) case ModeAdd: seconds := (tsB.Hour() * 3600) + (tsB.Minute() * 60) + tsB.Second() sum := tsA.Add(time.Duration(seconds) * time.Second) + tp.Print(TPdatetime{TimestampProccessor: *tp, Data: sum}) } diff --git a/go.mod b/go.mod index 0df2049..e86d343 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.23.0 toolchain go1.23.5 require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-viper/mapstructure/v2 v2.4.0 // indirect github.com/ijt/go-anytime v1.9.2 // 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/mitchellh/copystructure v1.2.0 // 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/stretchr/testify v1.11.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 1f433cb..142938b 100644 --- a/go.sum +++ b/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/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= 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/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= 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/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= diff --git a/main.go b/main.go index 5bd0622..0680f66 100644 --- a/main.go +++ b/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 . +*/ + package main import (