Documentation ¶
Overview ¶
Package timeutil provides utility functions for working with time-related operations.
This package offers a collection of functions that facilitate working with time.Duration in a human-readable format when dealing with JSON.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Duration ¶
Duration is an alias for the standard time.Duration.
func (Duration) MarshalJSON ¶
MarshalJSON returns d as the JSON encoding of d. It encodes the time.Duration in human readable format (e.g.: 20s, 1h, ...).
Example ¶
package main import ( "encoding/json" "fmt" "log" "time" "github.com/Vonage/gosrvlib/pkg/timeutil" ) func main() { type testData struct { Time timeutil.Duration `json:"Time"` } data := testData{ Time: timeutil.Duration(7*time.Hour + 11*time.Minute + 13*time.Second), } enc, err := json.Marshal(data) if err != nil { log.Fatal(err) } fmt.Println(string(enc)) }
Output: {"Time":"7h11m13s"}
func (Duration) String ¶
String returns a string representing the duration in the form "72h3m0.5s". It is a wrapper for time.Duration.String().
func (*Duration) UnmarshalJSON ¶
UnmarshalJSON sets *d to a copy of data. It converts human readable time duration format (e.g.: 20s, 1h, ...) in standard time.Duration.
Example ¶
package main import ( "encoding/json" "fmt" "log" "github.com/Vonage/gosrvlib/pkg/timeutil" ) func main() { type testData struct { Time timeutil.Duration `json:"Time"` } enc := []byte(`{"Time":"7h11m13s"}`) var data testData err := json.Unmarshal(enc, &data) if err != nil { log.Fatal(err) } fmt.Println(data.Time.String()) }
Output: 7h11m13s