From 908ededbcc597b7ced3fe03932ff3a8b4d44b58e Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Thu, 25 Sep 2025 21:22:07 +0200 Subject: [PATCH] make reference time configurable --- cmd/config.go | 18 +++++++++++++++--- cmd/times.go | 3 ++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 7bb6e66..80ab7c8 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -98,8 +98,8 @@ type Config struct { Mode int // internal flags for [unit] tests - tz string - refTime time.Time + 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) { @@ -134,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) diff --git a/cmd/times.go b/cmd/times.go index f4b6c57..13cf7bd 100644 --- a/cmd/times.go +++ b/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] }