added tests, add support for duration diff+add

This commit is contained in:
2025-09-24 13:55:33 +02:00
parent 0038ba2094
commit 47f4bd455e
7 changed files with 347 additions and 13 deletions

2
.gitignore vendored
View File

@@ -30,3 +30,5 @@ go.work.sum
# Editor/IDE
# .idea/
# .vscode/
ts

View File

@@ -2,7 +2,7 @@
generic cli timestamp parser and calculator tool
# Usage
## Usage
```default
This is ts, a timestamp tool.
@@ -21,6 +21,28 @@ Usage: ts <time string> [<time string>]
-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
The tool does not have any dependencies. Just download the binary for

View File

@@ -36,7 +36,7 @@ const (
Usage: ts <time string> [<time string>]
-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
For timestamps: datetime, rfc3339, date, time, unix, string
string is a strftime(1) format string. datetime is
@@ -71,7 +71,11 @@ noon Yesterday at 10:15am Mon, 02 Jan 2006 15:
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
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
ModeAdd
)
@@ -127,6 +131,15 @@ func InitConfig(output io.Writer) (*Config, error) {
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
if len(flagset.Args()) == 0 {
return nil, errors.New("no timestamp argument[s] specified.\n" + Usage)

View File

@@ -18,10 +18,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"fmt"
"io"
"log"
"os"
)
func Die(err error) int {
@@ -36,14 +34,6 @@ func Main(output io.Writer) int {
return Die(err)
}
if conf.Examples {
_, err := fmt.Fprintln(output, Examples)
if err != nil {
Die(err)
}
os.Exit(0)
}
tp := NewTP(conf)
if err := tp.ProcessTimestamps(); err != nil {

View File

@@ -18,8 +18,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"errors"
"fmt"
"log"
"regexp"
"strconv"
"time"
"github.com/ijt/go-anytime"
@@ -97,6 +100,13 @@ func (tp *TimestampProccessor) Calc(timestampA, timestampB string) error {
return err
}
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 {
return err
@@ -125,9 +135,64 @@ func (tp *TimestampProccessor) Calc(timestampA, timestampB string) error {
return nil
}
func (tp *TimestampProccessor) CalcDuration(tsA time.Time, durB time.Duration) {
var datetime time.Time
switch tp.Mode {
case ModeDiff:
datetime = tsA.Add(-durB)
case ModeAdd:
datetime = tsA.Add(durB)
}
tp.Print(TPdatetime{TimestampProccessor: *tp, Data: datetime})
}
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)
}
}
/*
We could use time.ParseDuration(), but this doesn't support days.
We could also use github.com/xhit/go-str2duration/v2, which does
the job, but it's just another dependency, just for this little
gem. And we don't need a time.Time value.
Convert a duration into seconds (int).
Valid time units are "s", "m", "h" and "d".
Valid inputs: 2h5m (2 hours and 5 min), 10d12h (10 and a half days)
*/
func duration2int(duration string) (time.Duration, error) {
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
}
}
}
if !found {
return 0, errors.New("failed to parse duration")
}
return time.Duration(seconds) * time.Second, nil
}

148
cmd/times_test.go Normal file
View File

@@ -0,0 +1,148 @@
/*
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"
"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
os.Setenv("TZ", "UTC")
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
View 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)
}
}