mirror of
https://codeberg.org/scip/ts.git
synced 2025-12-17 20:41:03 +01:00
added more tests, fixed hour format output
This commit is contained in:
@@ -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) {
|
||||
|
||||
21
cmd/times.go
21
cmd/times.go
@@ -24,8 +24,8 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/araddon/dateparse"
|
||||
"github.com/ijt/go-anytime"
|
||||
"github.com/itlightning/dateparse"
|
||||
modnow "github.com/jinzhu/now"
|
||||
)
|
||||
|
||||
@@ -65,8 +65,25 @@ func (tp *TimestampProccessor) ProcessTimestamps() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Parse uses 3 different timestamp parser modules to provide maximum flexibility
|
||||
// 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 ts, err
|
||||
}
|
||||
|
||||
if tp.TZ != "" {
|
||||
// apply custom timezone
|
||||
zone, _ := time.LoadLocation(tp.TZ)
|
||||
ts = ts.In(zone)
|
||||
}
|
||||
|
||||
return ts, nil
|
||||
}
|
||||
|
||||
// Parse uses 3 different timestamp parser modules to provide maximum flexibility
|
||||
func (tp *TimestampProccessor) ParseTimestamp(timestamp string) (time.Time, error) {
|
||||
ts, err := anytime.Parse(timestamp, tp.Reference)
|
||||
if err == nil {
|
||||
return ts, nil
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
81
cmd/writer_test.go
Normal 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)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user