Skip to content

Commit 3ee8a64

Browse files
authored
Merge pull request #2 from MyBlackJay/DEVELOP
added docs, added functions for working with yaml
2 parents a196387 + bb4813d commit 3ee8a64

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

README.md

+127
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,136 @@
11
# ISODURATION
22
It's multifunctional and fast a Go module for parsing [ISO 8601 duration format](https://www.digi.com/resources/documentation/digidocs/90001488-13/reference/r_iso_8601_duration_format.htm)
33

4+
5+
## Why use?
6+
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.
7+
8+
## Features
9+
- fast parsing of raw strings in ISO 8601 duration format
10+
- convenient tools for obtaining and reverse conversion of time.Duration
11+
- possibility to get each period and time element in float64 format
12+
- human-readable errors open for import and comparison
13+
- yaml serialization and deserialization
14+
- json serialization and deserialization
15+
416
## Installation
517
```
618
go get github.com/MyBlackJay/isoduration
719
```
820

21+
## Example
22+
23+
#### [Code:](https://go.dev/play/p/SPc-oa4lcNi)
24+
```go
25+
package main
26+
27+
import (
28+
"encoding/json"
29+
"fmt"
30+
"github.com/MyBlackJay/isoduration"
31+
"gopkg.in/yaml.v3"
32+
"time"
33+
)
34+
35+
type Config struct {
36+
FlushThresholdDuration *isoduration.Duration `yaml:"flush_threshold_duration" json:"flush_threshold_duration"`
37+
}
38+
39+
type Main struct {
40+
Config Config `yaml:"config" json:"config"`
41+
}
42+
43+
func yamlToStruct() {
44+
yml := `
45+
config:
46+
flush_threshold_duration: PT30M30S
47+
`
48+
m := &Main{}
49+
50+
if err := yaml.Unmarshal([]byte(yml), m); err != nil {
51+
fmt.Println(err)
52+
return
53+
}
54+
55+
fmt.Println(m.Config.FlushThresholdDuration.ToTimeDuration())
56+
}
57+
58+
func jsonToStruct() {
59+
jsn := `{"config": {"flush_threshold_duration": "PT30M30S"}}`
60+
m := &Main{}
61+
62+
if err := json.Unmarshal([]byte(jsn), m); err != nil {
63+
fmt.Println(err)
64+
return
65+
}
66+
fmt.Println(m.Config.FlushThresholdDuration)
67+
}
68+
69+
func structToYaml() {
70+
tp := &Main{
71+
Config: Config{
72+
FlushThresholdDuration: isoduration.NewDuration(1, 1, 5, 0, 1, 30, 12, true),
73+
},
74+
}
75+
76+
if out, err := yaml.Marshal(tp); err != nil {
77+
fmt.Println(err)
78+
return
79+
} else {
80+
fmt.Println(string(out))
81+
}
82+
83+
}
84+
85+
func structToJson() {
86+
tp := &Main{
87+
Config: Config{
88+
FlushThresholdDuration: isoduration.NewDuration(1, 1, 5, 0, 1, 30, 12, true),
89+
},
90+
}
91+
92+
if out, err := json.Marshal(tp); err != nil {
93+
fmt.Println(err)
94+
return
95+
} else {
96+
fmt.Println(string(out))
97+
}
98+
}
99+
100+
func timeDelta() {
101+
str := "PT10H5M"
102+
strNeg := "-PT10H5M"
103+
now := time.Now()
104+
105+
td := time.Hour * 20
106+
107+
iso, _ := isoduration.ParseDuration(str)
108+
isoNeg, _ := isoduration.ParseDuration(strNeg)
109+
110+
fmt.Println(td - iso.ToTimeDuration())
111+
fmt.Println(td - isoNeg.ToTimeDuration())
112+
fmt.Println(now.Add(iso.ToTimeDuration()).UTC())
113+
fmt.Println(now.Add(isoNeg.ToTimeDuration()).UTC())
114+
115+
}
116+
117+
func fromTimeDuration() {
118+
dur := (time.Hour * isoduration.DayHours * isoduration.YearDays * 2) + (60 * time.Hour) + (30 * time.Second)
119+
v := isoduration.NewFromTimeDuration(dur)
120+
c := isoduration.FormatTimeDuration(dur)
121+
122+
fmt.Println(v, c)
123+
}
124+
125+
func main() {
126+
yamlToStruct() // 30m30s
127+
jsonToStruct() // 30m35s
128+
structToYaml() // config:
129+
// flush_threshold_duration: -P1Y1M5DT1H30M12S
130+
structToJson() // {"config":{"flush_threshold_duration":"-P1Y1M5DT1H30M12S"}}
131+
timeDelta() // 9h55m0s | 30h5m0s | 2024-01-19 21:25:38.660823592 +0000 UTC | 2024-01-19 01:15:38.660823592 +0000 UTC
132+
fromTimeDuration() // P2Y2DT12H30S P2Y2DT12H30S
133+
}
134+
135+
```
9136

types.go

+24
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,27 @@ func (d *Duration) UnmarshalJSON(source []byte) error {
205205
func (d *Duration) MarshalJSON() ([]byte, error) {
206206
return []byte("\"" + d.String() + "\""), nil
207207
}
208+
209+
// UnmarshalYAML designed to deserialize *Duration in a string in ISO 8601 duration format defined in user code via the gopkg.in/yaml.v3 library
210+
func (d *Duration) UnmarshalYAML(unmarshal func(interface{}) error) error {
211+
var str string
212+
if err := unmarshal(&str); err != nil {
213+
return IsNotIsoFormatError
214+
}
215+
216+
if str == "null" {
217+
return nil
218+
}
219+
220+
if parsed, err := ParseDuration(str); err == nil {
221+
*d = *parsed
222+
return nil
223+
} else {
224+
return err
225+
}
226+
}
227+
228+
// MarshalYAML designed to deserialize *Duration in a string in ISO 8601 duration format defined in user code via the gopkg.in/yaml.v3 library
229+
func (d *Duration) MarshalYAML() (interface{}, error) {
230+
return d.String(), nil
231+
}

0 commit comments

Comments
 (0)