mirror of
https://github.com/kerberos-io/onvif.git
synced 2026-08-23 15:08:33 +00:00
introduce iso8601_duration directly
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
iso8601 "github.com/yakovlevdmv/Golang-iso8601-duration"
|
||||
"github.com/use-go/onvif/xsd/iso8601"
|
||||
)
|
||||
|
||||
/*
|
||||
|
||||
104
xsd/iso8601/iso8601_duration.go
Normal file
104
xsd/iso8601/iso8601_duration.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user