add unit tests and gpl

This commit is contained in:
2025-09-24 11:29:13 +02:00
parent 84792667e7
commit 0038ba2094
7 changed files with 101 additions and 10 deletions

View File

@@ -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 ./...

View File

@@ -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 (

View File

@@ -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 (

View File

@@ -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 (
@@ -12,20 +29,27 @@ import (
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)
} }
modnow.TimeFormats = append(modnow.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,10 +74,9 @@ 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
} }
@@ -82,16 +105,20 @@ 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(TPduration{TimestampProccessor: *tp, Data: 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()
sum := tsA.Add(time.Duration(seconds) * time.Second) sum := tsA.Add(time.Duration(seconds) * time.Second)
tp.Print(TPdatetime{TimestampProccessor: *tp, Data: sum}) tp.Print(TPdatetime{TimestampProccessor: *tp, Data: sum})
} }

4
go.mod
View File

@@ -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
View File

@@ -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
View File

@@ -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 (