-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
65 lines (59 loc) · 1.84 KB
/
option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package datemath_parser
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
type DateMathParserOption func(*DateMathParser) error
func WithFormat(formats []string) DateMathParserOption {
return func(p *DateMathParser) error {
var parserFormats = []string{}
for _, format := range formats {
if jodaFormats, ok := BuiltInFormat[format]; ok {
parserFormats = append(parserFormats, jodaFormats...)
} else {
parserFormats = append(parserFormats, format)
}
}
p.Formats = parserFormats
return nil
}
}
var TimeZoneOffset = regexp.MustCompile(`(\+|-)(\d{1,2}):(\d{1,2})`)
func WithTimeZone(timeZone string) DateMathParserOption {
return func(p *DateMathParser) error {
if loc, err := time.LoadLocation(timeZone); err != nil {
var timeOffset = timeZone
if t, ok := abbrevTimeZone[strings.ToUpper(timeZone)]; ok {
timeOffset = t
} else if t, ok := fullNameTimeZone[strings.ToUpper(timeZone)]; ok {
timeOffset = t
}
var s = TimeZoneOffset.FindStringSubmatch(timeOffset)
if len(s) != 4 {
return fmt.Errorf("time zone: %s format is invalid, expect time offset format: (\\+|-)(\\d{1,2}):(\\d{1,2}) or time zone (abbreviation/full name) or IANA format", timeZone)
} else {
var flag = 1
if s[1] == "-" {
flag = -1
}
var hour, _ = strconv.Atoi(s[2])
if hour > 23 || hour < 0 {
return fmt.Errorf("time zone: %s is invalid, hour is out of range [0, 23]", timeZone)
}
var minute, _ = strconv.Atoi(s[3])
if minute > 59 || minute < 0 {
return fmt.Errorf("time zone: %s is invalid, minute is out of range [0, 59]", timeZone)
}
p.TimeZone = time.FixedZone("UTC",
flag*(hour*int(time.Hour)+minute*int(time.Minute))/int(time.Second))
return nil
}
} else {
p.TimeZone = loc
return nil
}
}
}