34 lines
738 B
Go
34 lines
738 B
Go
|
package toml
|
||
|
|
||
|
import "time"
|
||
|
|
||
|
// Duration is a TOML wrapper type for time.Duration.
|
||
|
type Duration time.Duration
|
||
|
|
||
|
func (d Duration) String() string {
|
||
|
return time.Duration(d).String()
|
||
|
}
|
||
|
|
||
|
// UnmarshalText parses a TOML value into a duration value.
|
||
|
func (d *Duration) UnmarshalText(text []byte) error {
|
||
|
// Ignore if there is no value set.
|
||
|
if len(text) == 0 {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// Otherwise parse as a duration formatted string.
|
||
|
duration, err := time.ParseDuration(string(text))
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Set duration and return.
|
||
|
*d = Duration(duration)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// MarshalText converts a duration to a string for decoding toml
|
||
|
func (d Duration) MarshalText() (text []byte, err error) {
|
||
|
return []byte(d.String()), nil
|
||
|
}
|