Documentation
¶
Overview ¶
Package dur provides a wrapper dur.Duration around time.Duration, with sensible JSON/YAML marshaling/unmarshaling support.
Unmarshaling supports standard time.Duration formats string formats such as "5s, ""1h30m", all parsed by time.ParseDuration. It also supports integer and float values which are interpreted as seconds, rather than nanoseconds, like the regular time.Duration does.
Marshaling always outputs a string, using the standard time.Duration format, by calling time.Duration(d).String().
Index ¶
Examples ¶
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 implements JSON and YAML marshaler and unmarshaler interfaces.
When unmarshaling, string values in JSON and YAML are parsed using time.ParseDuration. Numeric values are parsed as number of seconds.
When marshaling, the duration is formatted as a string using time.Duration's String method.
func Parse ¶
Parse parses given interface to a Duration.
If the interface is a string, it will be parsed using time.Parse. If the interface is a int or float64, it will be parsed as a number of seconds.
func (Duration) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface, returning the duration as a string in the format "1h2m3s", same as time.Duration.String().
Example ¶
package main
import (
"encoding/json"
"fmt"
"time"
"github.com/jimeh/go-tyme/dur"
)
func main() {
type Connection struct {
Timeout dur.Duration `json:"timeout"`
}
conn := Connection{Timeout: dur.Duration(5 * time.Second)}
b, _ := json.Marshal(conn)
fmt.Println(string(b))
}
Output: {"timeout":"5s"}
func (Duration) MarshalYAML ¶
MarshalJSON implements the yaml.Marshaler interface, returning the duration as a string in the format 1h2m3s, same as time.Duration.String().
Example ¶
package main
import (
"fmt"
"time"
"github.com/jimeh/go-tyme/dur"
"gopkg.in/yaml.v3"
)
func main() {
type Connection struct {
Timeout dur.Duration `yaml:"timeout"`
}
conn := Connection{Timeout: dur.Duration(5 * time.Second)}
b, _ := yaml.Marshal(conn)
fmt.Println(string(b))
}
Output: timeout: 5s
func (*Duration) UnmarshalJSON ¶
UmarshalJSON implements the json.Unmarshaler interface. Supports string, numeric JSON types, converting them to a Duration using ParseDuration.
Example ¶
package main
import (
"encoding/json"
"fmt"
"time"
"github.com/jimeh/go-tyme/dur"
)
func main() {
type Connection struct {
Timeout dur.Duration `json:"timeout"`
}
conn := Connection{}
_ = json.Unmarshal([]byte(`{"timeout": "10s"}`), &conn)
fmt.Printf("%+v (%+v)\n", conn.Timeout, time.Duration(conn.Timeout))
_ = json.Unmarshal([]byte(`{"timeout": 5}`), &conn)
fmt.Printf("%+v (%+v)\n", conn.Timeout, time.Duration(conn.Timeout))
_ = json.Unmarshal([]byte(`{"timeout": 0.5}`), &conn)
fmt.Printf("%+v (%+v)\n", conn.Timeout, time.Duration(conn.Timeout))
}
Output: 10000000000 (10s) 5000000000 (5s) 500000000 (500ms)
func (*Duration) UnmarshalYAML ¶
UmarshalYAML implements the yaml.Unmarshaler interface. Supports string, int and float YAML types, converting them to a Duration using ParseDuration.
Example ¶
package main
import (
"fmt"
"time"
"github.com/jimeh/go-tyme/dur"
"gopkg.in/yaml.v3"
)
func main() {
type Connection struct {
Timeout dur.Duration `yaml:"timeout"`
}
conn := Connection{}
_ = yaml.Unmarshal([]byte(`timeout: 10s`), &conn)
fmt.Printf("%+v (%+v)\n", conn.Timeout, time.Duration(conn.Timeout))
_ = yaml.Unmarshal([]byte(`timeout: 5`), &conn)
fmt.Printf("%+v (%+v)\n", conn.Timeout, time.Duration(conn.Timeout))
_ = yaml.Unmarshal([]byte(`timeout: 0.5`), &conn)
fmt.Printf("%+v (%+v)\n", conn.Timeout, time.Duration(conn.Timeout))
}
Output: 10000000000 (10s) 5000000000 (5s) 500000000 (500ms)