From e6593ad04f6f694b95200b85eb0a90010c787c80 Mon Sep 17 00:00:00 2001 From: Edward Date: Fri, 1 May 2020 22:42:40 +0800 Subject: [PATCH] introduce iso8601_duration directly --- README.md | 5 -- xsd/built_in.go | 2 +- xsd/iso8601/iso8601_duration.go | 104 ++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 xsd/iso8601/iso8601_duration.go diff --git a/README.md b/README.md index b8a0c6c..3c64456 100644 --- a/README.md +++ b/README.md @@ -99,11 +99,6 @@ device.Authenticate("username", "password") resp, err := dev.CallMethod(createUsers) ``` -## TO TO - -+ [] merge Golang-iso8601-duration ,directly -+ .. - ## Great Thanks Modified from: [goonvif](https://github.com/yakovlevdmv/goonvif) diff --git a/xsd/built_in.go b/xsd/built_in.go index 51abc42..b2a015f 100644 --- a/xsd/built_in.go +++ b/xsd/built_in.go @@ -11,7 +11,7 @@ import ( "strings" "time" - iso8601 "github.com/yakovlevdmv/Golang-iso8601-duration" + "github.com/use-go/onvif/xsd/iso8601" ) /* diff --git a/xsd/iso8601/iso8601_duration.go b/xsd/iso8601/iso8601_duration.go new file mode 100644 index 0000000..799580a --- /dev/null +++ b/xsd/iso8601/iso8601_duration.go @@ -0,0 +1,104 @@ +package iso8601 + +import ( + "errors" + "regexp" +) + +//Duration of iso8601 +type Duration struct { + years string //= number of years + months string //= number of months + days string //= number of days + // Time section + hours string //= the number of hours + minutes string //= the number of minutes + seconds string //= the number of seconds +} + +//NewDuration return duration +func NewDuration(years, months, days, hours, minutes, seconds string) (*Duration, error) { + // Pattern for Years, Months, Days, Hours and Minutes components + pattern1 := "^$|[0-9]+" + // Pattern for Seconds component + pattern2 := "^$|[0-9]+(\\.[0-9]+)?" + + matched, err := regexp.MatchString(pattern1, years) + if err != nil { + return nil, err + } else if !matched { + return nil, errors.New("years value = " + years + " does not match pattern " + pattern1) + } + + matched, err = regexp.MatchString(pattern1, months) + if err != nil { + return nil, err + } else if !matched { + return nil, errors.New("months value = " + months + " does not match pattern " + pattern1) + } + + matched, err = regexp.MatchString(pattern1, days) + if err != nil { + return nil, err + } else if !matched { + return nil, errors.New("months value = " + days + " does not match pattern " + pattern1) + } + + matched, err = regexp.MatchString(pattern1, hours) + if err != nil { + return nil, err + } else if !matched { + return nil, errors.New("months value = " + hours + " does not match pattern " + pattern1) + } + + matched, err = regexp.MatchString(pattern1, minutes) + if err != nil { + return nil, err + } else if !matched { + return nil, errors.New("months value = " + minutes + " does not match pattern " + pattern1) + } + + matched, err = regexp.MatchString(pattern2, seconds) + if err != nil { + return nil, err + } else if !matched { + return nil, errors.New("years value = " + seconds + " does not match pattern " + pattern2) + } + + return &Duration{years: years, months: months, hours: hours, days: days, minutes: minutes, seconds: seconds}, nil +} + +//ISO8601Duration to string +func (duration Duration) ISO8601Duration() string { + var result string + result += "P" // time duration designator + //years + if duration.years != "" { + result += duration.years + "Y" + } + if duration.months != "" { + result += duration.months + "M" + } + if duration.days != "" { + result += duration.days + "D" + } + + if duration.hours != "" && duration.minutes != "" && duration.seconds != "" { + result += "T" + if duration.hours != "" { + result += duration.hours + "H" + } + if duration.minutes != "" { + result += duration.minutes + "M" + } + if duration.seconds != "" { + result += duration.seconds + "S" + } + } + + if len(result) == 1 { + result += "T0S" + } + + return result +}