make reference time configurable

This commit is contained in:
2025-09-25 21:22:07 +02:00
parent 25138cc144
commit 908ededbcc
2 changed files with 17 additions and 4 deletions

View File

@@ -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)

View File

@@ -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]
}