Documentation
¶
Overview ¶
Package timeutil provides JSON-serializable time utilities.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Duration ¶
Duration is a wrapper around time.Duration that supports JSON marshaling/unmarshaling.
When marshaling to JSON, it outputs the duration as a string using time.Duration.String(). When unmarshaling from JSON, it accepts both:
- String values that can be parsed by time.ParseDuration (e.g., "30s", "5m", "1h30m")
- Numeric values that represent nanoseconds as a float64
This makes it compatible with configuration files and APIs that need to represent durations in a human-readable format.
Example usage:
type Config struct { Timeout timeutil.Duration `json:"timeout"` } // JSON: {"timeout": "30s"} // or: {"timeout": 30000000000}
func (Duration) MarshalJSON ¶
MarshalJSON implements json.Marshaler. It marshals the duration as a string using time.Duration.String().
func (*Duration) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. It accepts both string values (parsed via time.ParseDuration) and numeric values (interpreted as nanoseconds).