mirror of
https://codeberg.org/scip/ts.git
synced 2025-12-17 04:30:56 +01:00
make reference time configurable
This commit is contained in:
@@ -98,8 +98,8 @@ type Config struct {
|
|||||||
Mode int
|
Mode int
|
||||||
|
|
||||||
// internal flags for [unit] tests
|
// internal flags for [unit] tests
|
||||||
tz string
|
tz string // has to be set directly in code
|
||||||
refTime time.Time
|
refTime time.Time // must be set via env var $TSREFTIME
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitConfig(output io.Writer) (*Config, error) {
|
func InitConfig(output io.Writer) (*Config, error) {
|
||||||
@@ -134,11 +134,23 @@ func InitConfig(output io.Writer) (*Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// fetch values
|
// fetch values
|
||||||
conf := &Config{Output: output}
|
conf := &Config{Output: output, refTime: time.Now()}
|
||||||
if err := kloader.Unmarshal("", &conf); err != nil {
|
if err := kloader.Unmarshal("", &conf); err != nil {
|
||||||
return nil, fmt.Errorf("error unmarshalling: %w", err)
|
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?
|
// want examples?
|
||||||
if conf.Examples {
|
if conf.Examples {
|
||||||
_, err := fmt.Fprintln(output, Examples)
|
_, err := fmt.Fprintln(output, Examples)
|
||||||
|
|||||||
@@ -45,9 +45,10 @@ func NewTP(conf *Config, ref ...time.Time) *TimestampProccessor {
|
|||||||
|
|
||||||
modnow.TimeFormats = append(modnow.TimeFormats, formats...)
|
modnow.TimeFormats = append(modnow.TimeFormats, formats...)
|
||||||
|
|
||||||
tp := &TimestampProccessor{Config: *conf, Reference: time.Now()}
|
tp := &TimestampProccessor{Config: *conf, Reference: conf.refTime}
|
||||||
|
|
||||||
if len(ref) == 1 {
|
if len(ref) == 1 {
|
||||||
|
// overwritten externally by unit test
|
||||||
tp.Reference = ref[0]
|
tp.Reference = ref[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user