timeutil

package
v1.90.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package timeutil adds utility functions to the standard time library.

The package provides a set of functions to work with time.Duration in human readable format in JSON.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Duration

type Duration time.Duration

Duration is an alias for the standard time.Duration.

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

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

func (d Duration) String() string

String returns a string representing the duration in the form "72h3m0.5s". It is a wrapper for time.Duration.String().

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(data []byte) error

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL