Coder Social home page Coder Social logo

iso8601's Introduction

iso8601 Duration parser

GoDoc GoReport

The current go native time.ParseDuration() does not support any ISO8601 duration strings.

This library parses any ISO8601 duration string into a native Go time.Duration object.

Should I use this?

Do you want to parse a full ISO8601 duration? If so, then probably not.
Do you want to parse ISO8601 durations that hardly ever reach '1D' and never reaches a higher duration part (e.g. month)? Then yes, you may want to use this.

Some APIs (for example) provide ISO8601 durations in their response even though the duration never reaches a value higher than a few days.
This project aims for those cases.

Why should I not use this for other use cases?

How long is a month? How long is a year?

Both, a month and a year, don't really have a hard value as a duration. But this project needs one for them regardless.
Currently a month is hard-coded as 30 days and a year received the duration of 365 days. This is not a perfect solution but will work for use cases where a duration hardly, if ever, reaches those values.

Installation

go get github.com/Achsion/iso8601

will resolve and add the package to the current development module, along with its dependencies.

Usage

package main

import "github.com/Achsion/iso8601/duration"

func main() {
	duration, err := duration.ParseToDuration("P1Y1M1DT1H1M1.1S")
}

Benchmark

Benchmarks created with

go test -bench=. -benchmem ./duration

v1.0.0

goos: linux
goarch: amd64
pkg: github.com/Achsion/iso8601/duration
cpu: AMD Ryzen 7 PRO 5875U with Radeon Graphics     
BenchmarkParseToDuration-16    	13843465	        85.54 ns/op	       0 B/op	       0 allocs/op
Older versions

v0.1.0

goos: linux
goarch: amd64
pkg: github.com/Achsion/iso8601/duration
cpu: AMD Ryzen 7 PRO 5875U with Radeon Graphics     
BenchmarkParseToDuration-16    	  878901	      1154 ns/op	    1655 B/op	       4 allocs/op

iso8601's People

Contributors

achsion avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.