Added more unittests, fixed hour format output (#4)

* little refactoring
* added more tests, fixed hour format output
* bump version
This commit is contained in:
T.v.Dein
2025-09-24 19:43:50 +02:00
committed by GitHub
parent 71e36c36d3
commit ef4cc8b84b
10 changed files with 196 additions and 22 deletions

View File

@@ -31,7 +31,7 @@ import (
)
const (
VERSIONstring = "0.0.2"
VERSIONstring = "0.0.3"
Usage string = `This is ts, a timestamp tool.
Usage: ts <time string> [<time string>]
@@ -92,6 +92,7 @@ type Config struct {
Args []string
Output io.Writer
Mode int
TZ string // for unit tests
}
func InitConfig(output io.Writer) (*Config, error) {
@@ -135,8 +136,9 @@ func InitConfig(output io.Writer) (*Config, error) {
if conf.Examples {
_, err := fmt.Fprintln(output, Examples)
if err != nil {
Die(err)
Die("failed write to output file handle", err)
}
os.Exit(0)
}

View File

@@ -18,12 +18,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"fmt"
"io"
"log"
"os"
)
func Die(err error) int {
log.Fatal("Error: ", err.Error())
func Die(format string, err error) int {
fmt.Fprintf(os.Stderr, format+": %s\n", err)
return 1
}
@@ -31,13 +32,14 @@ func Die(err error) int {
func Main(output io.Writer) int {
conf, err := InitConfig(output)
if err != nil {
return Die(err)
fmt.Println(1)
return Die("failed to initialize", err)
}
tp := NewTP(conf)
if err := tp.ProcessTimestamps(); err != nil {
return Die(err)
return Die("failed to process timestamp[s]", err)
}
return 0

View File

@@ -20,13 +20,12 @@ package cmd
import (
"errors"
"fmt"
"log"
"regexp"
"strconv"
"time"
"github.com/araddon/dateparse"
"github.com/ijt/go-anytime"
"github.com/itlightning/dateparse"
modnow "github.com/jinzhu/now"
)
@@ -60,25 +59,31 @@ func (tp *TimestampProccessor) ProcessTimestamps() error {
case 1:
return tp.SingleTimestamp(tp.Args[0])
case 2:
return tp.Calc(tp.Args[0], tp.Args[1])
return tp.DualTimestamps(tp.Args[0], tp.Args[1])
}
return nil
}
func (tp *TimestampProccessor) SingleTimestamp(timestamp string) error {
ts, err := tp.Parse(timestamp)
// a post processor for ParseTimestamp() to apply custom time zone, if any
func (tp *TimestampProccessor) Parse(timestamp string) (time.Time, error) {
ts, err := tp.ParseTimestamp(timestamp)
if err != nil {
return err
return ts, err
}
tp.Print(ts)
if tp.TZ != "" {
// apply custom timezone
zone, _ := time.LoadLocation(tp.TZ)
ts = ts.In(zone)
}
return nil
return ts, nil
}
// Parse uses 3 different timestamp parser modules to provide maximum flexibility
func (tp *TimestampProccessor) Parse(timestamp string) (time.Time, error) {
func (tp *TimestampProccessor) ParseTimestamp(timestamp string) (time.Time, error) {
ts, err := anytime.Parse(timestamp, tp.Reference)
if err == nil {
return ts, nil
@@ -94,7 +99,18 @@ func (tp *TimestampProccessor) Parse(timestamp string) (time.Time, error) {
return dateparse.ParseAny(timestamp)
}
func (tp *TimestampProccessor) Calc(timestampA, timestampB string) error {
func (tp *TimestampProccessor) SingleTimestamp(timestamp string) error {
ts, err := tp.Parse(timestamp)
if err != nil {
return err
}
tp.Print(ts)
return nil
}
func (tp *TimestampProccessor) DualTimestamps(timestampA, timestampB string) error {
tsA, err := tp.Parse(timestampA)
if err != nil {
return err
@@ -112,6 +128,12 @@ func (tp *TimestampProccessor) Calc(timestampA, timestampB string) error {
return err
}
tp.CalcDiff(tsA, tsB)
return nil
}
func (tp *TimestampProccessor) CalcDiff(tsA time.Time, tsB time.Time) {
switch tp.Mode {
case ModeDiff:
var diff time.Duration
@@ -131,8 +153,6 @@ func (tp *TimestampProccessor) Calc(timestampA, timestampB string) error {
tp.Print(TPdatetime{TimestampProccessor: *tp, Data: sum})
}
return nil
}
func (tp *TimestampProccessor) CalcDuration(tsA time.Time, durB time.Duration) {
@@ -151,7 +171,7 @@ func (tp *TimestampProccessor) CalcDuration(tsA time.Time, durB time.Duration) {
func (tp *TimestampProccessor) Print(ts TimestampWriter) {
_, err := fmt.Fprintln(tp.Output, ts.String())
if err != nil {
log.Fatalf("failed to print to given output handle: %s", err)
Die("failed to print to given output handle", err)
}
}

View File

@@ -65,13 +65,15 @@ func TestParseTimestamps(t *testing.T) {
{`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},
{`Wed Sep 25 00:30:00 PM CEST 2025`, now},
{`Wed Sep 25 2025 13:30:00 GMT+0100 (GMT Daylight Time)`, 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)
tp := NewTP(&Config{Args: []string{tt.input}, Output: &writer, TZ: "UTC"}, now)
// writer.String()
ts, err := tp.Parse(tt.input)

View File

@@ -57,7 +57,7 @@ func (duration TPduration) String() string {
// 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)
return fmt.Sprintf("%.02f%s", duration.Data.Hours()/24, unit)
case "h", "hour", "hours":
return fmt.Sprintf("%.02f%s", duration.Data.Hours(), unit)
case "m", "min", "mins", "minutes":

81
cmd/writer_test.go Normal file
View File

@@ -0,0 +1,81 @@
/*
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"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestDuration(t *testing.T) {
var tests = []struct {
format string
duration time.Duration
want string
}{
{"day", time.Hour * 24, "1.00 days"},
{"dur", time.Hour * 24, "24h0m0s"},
{"hour", time.Hour * 24, "24.00 hours"},
{"min", time.Minute * 20, "20.00 minutes"},
{"min", time.Minute*20 + time.Second*30, "20.50 minutes"},
{"sec", time.Second * 30, "30.00 seconds"},
{"ms", time.Second * 30, "30000 milliseconds"},
}
for _, tt := range tests {
testname := fmt.Sprintf("formatduration-%s", tt.format)
t.Run(testname, func(t *testing.T) {
tpdur := TPduration{Data: tt.duration}
tpdur.Unit = true
tpdur.Format = tt.format
out := tpdur.String()
assert.Equal(t, tt.want, out)
})
}
}
func TestDatetime(t *testing.T) {
var now = time.Date(2025, 9, 25, 12, 30, 00, 0, time.UTC)
var tests = []struct {
format string
datetime time.Time
want string
}{
{"rfc3339", now, "2025-09-25T12:30:00Z"},
{"date", now, "2025-09-25"},
{"time", now, "12:30:00"},
{"unix", now, "1758803400"},
{"datetime", now, "2025-09-25 12:30:00 +0000 UTC"},
}
for _, tt := range tests {
testname := fmt.Sprintf("formatdatetime-%s", tt.format)
t.Run(testname, func(t *testing.T) {
tpdat := TPdatetime{Data: tt.datetime}
tpdat.Format = tt.format
out := tpdat.String()
assert.Equal(t, tt.want, out)
})
}
}