I have two configurations in the .env
file as following
MAX_TOKEN_EXPIRY_DAYS=30d
ACCESS_TOKEN_DURATION=30m
The struct in golang to load the config looks like following
type AppConfig struct {
MaxTokenExpiry time.Duration `mapstructure:"MAX_TOKEN_EXPIRY_DAYS"`
AccessTokenDuration time.Duration `mapstructure:"ACCESS_TOKEN_DURATION"`
}
Now the when I am trying to load the config, I am getting the following error
* error decoding 'MAX_TOKEN_EXPIRY_DAYS': time: unknown unit "d" in duration "30d"
This may mean that I the issue was with MAX_TOKEN_EXPIRY_DAYS=30d
line as it is not able to recognise the d
tag. But the m
tag in ACCESS_TOKEN_DURATION=30m
runs well as time.Duration
in golang is able to parse it well.
In the time package source code, I see the following constructs
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
Is there any way to denote days in the config?
It is because the ParseDuration called on your time string does not support d
as a unit suffix
Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
You are better off using the hours equivalent of 30d
i.e. 720h
to resolve the ambiguity.
Also see issue/explanation in golang/go for why the language designers decided to go with the choice. Why doesn't time.ParseDuration() support days? #17767