|
1 | 1 | # ISODURATION
|
2 | 2 | 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)
|
3 | 3 |
|
| 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 | + |
4 | 16 | ## Installation
|
5 | 17 | ```
|
6 | 18 | go get github.com/MyBlackJay/isoduration
|
7 | 19 | ```
|
8 | 20 |
|
| 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 | +``` |
9 | 136 |
|
0 commit comments