duration

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2023 License: MIT Imports: 6 Imported by: 17

README

duration

Go Reference

It's a Go module for parsing ISO 8601 durations and converting them to the often much more useful time.Duration.

why?

ISO 8601 is a pretty common standard and sometimes these durations show up in the wild.

installation

go get github.com/sosodev/duration

usage

package main

import (
	"fmt"
	"time"
	"github.com/sosodev/duration"
)

func main() {
	d, err := duration.Parse("P3Y6M4DT12H30M5.5S")
	if err != nil {
		panic(err)
	}
	
	fmt.Println(d.Years) // 3
	fmt.Println(d.Months) // 6
	fmt.Println(d.Days) // 4
	fmt.Println(d.Hours) // 12
	fmt.Println(d.Minutes) // 30
	fmt.Println(d.Seconds) // 5.5
	
	d, err = duration.Parse("T33.3S")
	if err != nil {
		panic(err)
	}
	
	fmt.Println(d.ToTimeDuration() == time.Second*33+time.Millisecond*300) // true
}

correctness

This module aims to implement the ISO 8601 duration specification correctly. It properly supports fractional units and has unit tests that assert the correctness of it's parsing and conversion to a time.Duration.

With that said durations with months or years specified will be converted to time.Duration with a little fuzziness. Since I couldn't find a standard value, and they obviously vary, for those I used 2.628e+15 nanoseconds for a month and 3.154e+16 nanoseconds for a year.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnexpectedInput is returned when an input in the duration string does not match expectations
	ErrUnexpectedInput = errors.New("unexpected input")
)

Functions

func Format added in v1.1.0

func Format(d time.Duration) string

Format formats the given time.Duration into an ISO 8601 duration string (e.g. P1DT6H5M), negative durations are prefixed with a minus sign, for a zero duration "PT0S" is returned. Note that for *Duration's with period values of a month or year that the duration becomes a bit fuzzy since obviously those things vary month to month and year to year

Types

type Duration

type Duration struct {
	Years    float64
	Months   float64
	Weeks    float64
	Days     float64
	Hours    float64
	Minutes  float64
	Seconds  float64
	Negative bool
}

Duration holds all the smaller units that make up the duration

func FromTimeDuration added in v1.1.0

func FromTimeDuration(d time.Duration) *Duration

FromTimeDuration converts the given time.Duration into duration.Duration. Note that for *Duration's with period values of a month or year that the duration becomes a bit fuzzy since obviously those things vary month to month and year to year

func Parse

func Parse(d string) (*Duration, error)

Parse attempts to parse the given duration string into a *Duration, if parsing fails an error is returned instead

func (Duration) MarshalJSON added in v1.2.0

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

func (*Duration) String

func (duration *Duration) String() string

String returns the ISO8601 duration string for the *Duration

func (*Duration) ToTimeDuration

func (duration *Duration) ToTimeDuration() time.Duration

ToTimeDuration converts the *Duration to the standard library's time.Duration. Note that for *Duration's with period values of a month or year that the duration becomes a bit fuzzy since obviously those things vary month to month and year to year

func (*Duration) UnmarshalJSON added in v1.2.0

func (duration *Duration) UnmarshalJSON(source []byte) error

Jump to

Keyboard shortcuts

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