Code Monkey home page Code Monkey logo

isoduration's Introduction

ISODURATION

It's multifunctional and fast a Go module for parsing ISO 8601 duration format

Why use?

ISO 8601 duration format is a convenient format for recording the duration of something, for example, you can use this format in your configurations to indicate the duration of the wait or for time-based events.

Features

  • fast parsing of raw strings in ISO 8601 duration format
  • convenient tools for obtaining and reverse conversion of time.Duration
  • possibility to get each period and time element in float64 format
  • human-readable errors open for import and comparison
  • yaml serialization and deserialization
  • json serialization and deserialization

Installation

go get github.com/MyBlackJay/isoduration

Example

package main

import (
	"encoding/json"
	"fmt"
	"github.com/MyBlackJay/isoduration"
	"gopkg.in/yaml.v3"
	"time"
)

type Config struct {
	FlushThresholdDuration *isoduration.Duration `yaml:"flush_threshold_duration" json:"flush_threshold_duration"`
}

type Main struct {
	Config Config `yaml:"config" json:"config"`
}

func yamlToStruct() {
	yml := `
config:
  flush_threshold_duration: PT30M30S
`
	m := &Main{}

	if err := yaml.Unmarshal([]byte(yml), m); err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(m.Config.FlushThresholdDuration.ToTimeDuration())
}

func jsonToStruct() {
	jsn := `{"config": {"flush_threshold_duration": "PT30M30S"}}`
	m := &Main{}

	if err := json.Unmarshal([]byte(jsn), m); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(m.Config.FlushThresholdDuration)
}

func structToYaml() {
	tp := &Main{
		Config: Config{
			FlushThresholdDuration: isoduration.NewDuration(1, 1, 5, 0, 1, 30, 12, true),
		},
	}

	if out, err := yaml.Marshal(tp); err != nil {
		fmt.Println(err)
		return
	} else {
		fmt.Println(string(out))
	}

}

func structToJson() {
	tp := &Main{
		Config: Config{
			FlushThresholdDuration: isoduration.NewDuration(1, 1, 5, 0, 1, 30, 12, true),
		},
	}

	if out, err := json.Marshal(tp); err != nil {
		fmt.Println(err)
		return
	} else {
		fmt.Println(string(out))
	}
}

func timeDelta() {
	str := "PT10H5M"
	strNeg := "-PT10H5M"
	now := time.Now()

	td := time.Hour * 20

	iso, _ := isoduration.ParseDuration(str)
	isoNeg, _ := isoduration.ParseDuration(strNeg)

	fmt.Println(td - iso.ToTimeDuration())
	fmt.Println(td - isoNeg.ToTimeDuration())
	fmt.Println(now.Add(iso.ToTimeDuration()).UTC())
	fmt.Println(now.Add(isoNeg.ToTimeDuration()).UTC())

}

func fromTimeDuration() {
	dur := (time.Hour * isoduration.DayHours * isoduration.YearDays * 2) + (60 * time.Hour) + (30 * time.Second)
	v := isoduration.NewFromTimeDuration(dur)
	c := isoduration.FormatTimeDuration(dur)

	fmt.Println(v, c)
}

func main() {
	yamlToStruct() // 30m30s
	jsonToStruct() // 30m35s
	structToYaml() // config:
	//                  flush_threshold_duration: -P1Y1M5DT1H30M12S
	structToJson()     // {"config":{"flush_threshold_duration":"-P1Y1M5DT1H30M12S"}}
	timeDelta()        // 9h55m0s | 30h5m0s | 2024-01-19 21:25:38.660823592 +0000 UTC | 2024-01-19 01:15:38.660823592 +0000 UTC
	fromTimeDuration() // P2Y2DT12H30S P2Y2DT12H30S
}

isoduration's People

Contributors

myblackjay 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.