Skip to content

jsmcnair/duration

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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.

About

ISO 8601 duration parsing in Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%