jsdur

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2023 License: MIT Imports: 4 Imported by: 0

README

jsdur - package with type Duration

Contains type Duration, that embeds standard time.Duration.

This type implements json and text Marshaler/Unmarshaler interfaces.

Marshaling rules
  • Zero duration will marshal into empty string.
  • Unmarshal from "0" or empty string leads to zero duration.
  • In other cases follow time.Duration Parse and String function behavior.
Example
package main

import (
    "github.com/ricdeau/jsdur"
    "encoding/json"
    "fmt"
)

type SomeStruct struct {
    Duration jsdur.Duration `json:"duration"`
}

func main() {
    var data SomeStruct
    _ = json.Unmarshal([]byte(`{"duration":"10h15m"}`), &data)
    fmt.Println(data.Duration)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Duration

type Duration struct {
	time.Duration
}

Duration struct embeds time.Duration and implements interfaces for json and text marshaling/unmarshalling.

func NewDuration

func NewDuration(d time.Duration) Duration

func (Duration) MarshalJSON

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

func (Duration) MarshalText

func (d Duration) MarshalText() (text []byte, err error)

func (Duration) String

func (d Duration) String() string

func (*Duration) UnmarshalJSON

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

func (*Duration) UnmarshalText

func (d *Duration) UnmarshalText(text []byte) (err error)

Jump to

Keyboard shortcuts

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