pax_global_header00006660000000000000000000000064124610627420014516gustar00rootroot0000000000000052 comment=2075bf119b58e5576c6ed9f867b8f3d17f2e54d4 iso8601-0.1.0/000077500000000000000000000000001246106274200126255ustar00rootroot00000000000000iso8601-0.1.0/LICENSE000066400000000000000000000020711246106274200136320ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 Dylan Meissner Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. iso8601-0.1.0/README.md000066400000000000000000000003041246106274200141010ustar00rootroot00000000000000 iso 8601 parser and formatter ============================= An [ISO8601](https://en.wikipedia.org/wiki/ISO_8601) Go utility. - *Time* is not yet implemented - *Duration* is mostly implemented iso8601-0.1.0/duration.go000066400000000000000000000041011246106274200147750ustar00rootroot00000000000000package iso8601 import ( "errors" "fmt" "regexp" "strconv" "time" ) var ( // ErrBadFormat is returned when parsing fails ErrBadFormat = errors.New("bad format string") // ErrNoMonth is raised when a month is in the format string ErrNoMonth = errors.New("no months allowed") full = regexp.MustCompile(`P((?P\d+)Y)?((?P\d+)M)?((?P\d+)D)?(T((?P\d+)H)?((?P\d+)M)?((?P\d+)S)?)?`) week = regexp.MustCompile(`P((?P\d+)W)`) ) // adapted from https://github.com/BrianHicks/finch/duration func ParseDuration(value string) (time.Duration, error) { var match []string var regex *regexp.Regexp if week.MatchString(value) { match = week.FindStringSubmatch(value) regex = week } else if full.MatchString(value) { match = full.FindStringSubmatch(value) regex = full } else { return time.Duration(0), ErrBadFormat } d := time.Duration(0) day := time.Hour * 24 week := day * 7 year := day * 365 for i, name := range regex.SubexpNames() { part := match[i] if i == 0 || name == "" || part == "" { continue } value, err := strconv.Atoi(part) if err != nil { return time.Duration(0), err } switch name { case "year": d += year * time.Duration(value) case "month": return time.Duration(0), ErrNoMonth case "week": d += week * time.Duration(value) case "day": d += day * time.Duration(value) case "hour": d += time.Hour * time.Duration(value) case "minute": d += time.Minute * time.Duration(value) case "second": d += time.Second * time.Duration(value) } } return d, nil } func FormatDuration(duration time.Duration) string { // we're not doing negative durations if duration.Seconds() <= 0 { return "PT0S" } hours := int(duration.Hours()) minutes := int(duration.Minutes()) - (hours * 60) seconds := int(duration.Seconds()) - (hours*3600 + minutes*60) // we're not doing Y,M,W s := "PT" if hours > 0 { s = fmt.Sprintf("%s%dH", s, hours) } if minutes > 0 { s = fmt.Sprintf("%s%dM", s, minutes) } if seconds > 0 { s = fmt.Sprintf("%s%dS", s, seconds) } return s } iso8601-0.1.0/duration_test.go000066400000000000000000000036641246106274200160510ustar00rootroot00000000000000package iso8601 import ( "testing" "time" ) func Test_parse_duration(t *testing.T) { var dur time.Duration var err error // test with bad format _, err = ParseDuration("asdf") if err != ErrBadFormat { t.Fatalf("Expected an ErrBadFormat") } // test with month _, err = ParseDuration("P1M") if err != ErrNoMonth { t.Fatalf("Expected an ErrNoMonth") } // test with good full string exp, _ := time.ParseDuration("51h4m5s") dur, err = ParseDuration("P2DT3H4M5S") if err != nil { t.Fatalf("Did not expect err: %v", err) } if dur.Hours() != exp.Hours() { t.Errorf("Expected %v hours, not %v", exp.Hours(), dur.Hours()) } if dur.Minutes() != exp.Minutes() { t.Errorf("Expected %v minutes, not %v", exp.Hours(), dur.Minutes()) } if dur.Seconds() != exp.Seconds() { t.Errorf("Expected 5 seconds, not %v", exp.Nanoseconds(), dur.Seconds()) } if dur.Nanoseconds() != exp.Nanoseconds() { t.Error("Expected %v nanoseconds, not %v", exp.Nanoseconds(), dur.Nanoseconds()) } // test with good week string dur, err = ParseDuration("P1W") if err != nil { t.Fatalf("Did not expect err: %v", err) } if dur.Hours() != 24*7 { t.Errorf("Expected 168 hours, not %d", dur.Hours()) } } func Test_format_duration(t *testing.T) { // Test complex duration with hours, minutes, seconds d := time.Duration(3701) * time.Second s := FormatDuration(d) if s != "PT1H1M41S" { t.Fatalf("bad ISO 8601 duration string: %s", s) } // Test only minutes duration d = time.Duration(20) * time.Minute s = FormatDuration(d) if s != "PT20M" { t.Fatalf("bad ISO 8601 duration string for 20M: %s", s) } // Test only seconds d = time.Duration(1) * time.Second s = FormatDuration(d) if s != "PT1S" { t.Fatalf("bad ISO 8601 duration string for 1S: %s", s) } // Test negative duration (unsupported) d = time.Duration(-1) * time.Second s = FormatDuration(d) if s != "PT0S" { t.Fatalf("bad ISO 8601 duration string for negative: %s", s) } }