mirror of
https://codeberg.org/scip/ts.git
synced 2025-12-17 04:30:56 +01:00
Doc improvements (#6)
* add default datetime format, fix default format usage * fixed format tests * make reference time configurable * improve doc
This commit is contained in:
@@ -23,6 +23,7 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/knadh/koanf/providers/posflag"
|
||||
"github.com/knadh/koanf/v2"
|
||||
@@ -31,7 +32,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
VERSIONstring = "0.0.3"
|
||||
VERSIONstring = "0.0.4"
|
||||
Usage string = `This is ts, a timestamp tool.
|
||||
|
||||
Usage: ts <time string> [<time string>]
|
||||
@@ -76,8 +77,11 @@ noon Yesterday at 10:15am Mon, 02 Jan 2006 15:
|
||||
Example durations for second parameter:
|
||||
2d1h30m 2 days, one and a half hour
|
||||
30m 30 minutes`
|
||||
|
||||
ModeDiff int = iota
|
||||
ModeAdd
|
||||
|
||||
DefaultFormat string = "Mon Jan 02 15:04:05 MST 2006"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
@@ -92,7 +96,10 @@ type Config struct {
|
||||
Args []string
|
||||
Output io.Writer
|
||||
Mode int
|
||||
TZ string // for unit tests
|
||||
|
||||
// internal flags for [unit] tests
|
||||
tz string // has to be set directly in code
|
||||
refTime time.Time // must be set via env var $TSREFTIME
|
||||
}
|
||||
|
||||
func InitConfig(output io.Writer) (*Config, error) {
|
||||
@@ -127,11 +134,23 @@ func InitConfig(output io.Writer) (*Config, error) {
|
||||
}
|
||||
|
||||
// fetch values
|
||||
conf := &Config{Output: output}
|
||||
conf := &Config{Output: output, refTime: time.Now()}
|
||||
if err := kloader.Unmarshal("", &conf); err != nil {
|
||||
return nil, fmt.Errorf("error unmarshalling: %w", err)
|
||||
}
|
||||
|
||||
// check internal env var[s], if any
|
||||
reftime, present := os.LookupEnv("TSREFTIME")
|
||||
if present {
|
||||
// e.g: 2014-01-03T00:00:00+01:00
|
||||
ts, err := time.Parse(time.RFC3339Nano, reftime)
|
||||
if err != nil {
|
||||
os.Exit(Die("failed to set reference time from $TSREFTIME", err))
|
||||
}
|
||||
|
||||
conf.refTime = ts
|
||||
}
|
||||
|
||||
// want examples?
|
||||
if conf.Examples {
|
||||
_, err := fmt.Fprintln(output, Examples)
|
||||
|
||||
10
cmd/times.go
10
cmd/times.go
@@ -45,9 +45,10 @@ func NewTP(conf *Config, ref ...time.Time) *TimestampProccessor {
|
||||
|
||||
modnow.TimeFormats = append(modnow.TimeFormats, formats...)
|
||||
|
||||
tp := &TimestampProccessor{Config: *conf, Reference: time.Now()}
|
||||
tp := &TimestampProccessor{Config: *conf, Reference: conf.refTime}
|
||||
|
||||
if len(ref) == 1 {
|
||||
// overwritten externally by unit test
|
||||
tp.Reference = ref[0]
|
||||
}
|
||||
|
||||
@@ -73,9 +74,9 @@ func (tp *TimestampProccessor) Parse(timestamp string) (time.Time, error) {
|
||||
return ts, err
|
||||
}
|
||||
|
||||
if tp.TZ != "" {
|
||||
if tp.tz != "" {
|
||||
// apply custom timezone
|
||||
zone, _ := time.LoadLocation(tp.TZ)
|
||||
zone, _ := time.LoadLocation(tp.tz)
|
||||
ts = ts.In(zone)
|
||||
}
|
||||
|
||||
@@ -105,7 +106,8 @@ func (tp *TimestampProccessor) SingleTimestamp(timestamp string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
tp.Print(ts)
|
||||
tp.Print(TPdatetime{TimestampProccessor: *tp, Data: ts})
|
||||
//tp.Print(ts)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ func TestParseTimestamps(t *testing.T) {
|
||||
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, TZ: "UTC"}, now)
|
||||
tp := NewTP(&Config{Args: []string{tt.input}, Output: &writer, tz: "UTC"}, now)
|
||||
|
||||
// writer.String()
|
||||
ts, err := tp.Parse(tt.input)
|
||||
@@ -82,7 +82,7 @@ func TestParseTimestamps(t *testing.T) {
|
||||
|
||||
err = tp.ProcessTimestamps()
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, tt.want.String()+"\n", writer.String())
|
||||
assert.EqualValues(t, tt.want.Format(DefaultFormat)+"\n", writer.String())
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -148,7 +148,7 @@ func TestAddTimestamps(t *testing.T) {
|
||||
|
||||
err := tp.ProcessTimestamps()
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, tt.want.String()+"\n", writer.String())
|
||||
assert.EqualValues(t, tt.want.Format(DefaultFormat)+"\n", writer.String())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ func (datetime TPdatetime) String() string {
|
||||
case "datetime":
|
||||
fallthrough
|
||||
case "":
|
||||
return datetime.Data.String()
|
||||
return datetime.Data.Format(DefaultFormat)
|
||||
default:
|
||||
return datetime.Data.Format(datetime.Format)
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ func TestDatetime(t *testing.T) {
|
||||
{"date", now, "2025-09-25"},
|
||||
{"time", now, "12:30:00"},
|
||||
{"unix", now, "1758803400"},
|
||||
{"datetime", now, "2025-09-25 12:30:00 +0000 UTC"},
|
||||
{"datetime", now, "Thu Sep 25 12:30:00 UTC 2025"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
Reference in New Issue
Block a user