-
Notifications
You must be signed in to change notification settings - Fork 927
/
hour_minute.go
82 lines (71 loc) · 1.62 KB
/
hour_minute.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package br
import (
"regexp"
"strconv"
"time"
"github.com/botlabs-gg/yagpdb/v2/lib/when/rules"
"github.com/pkg/errors"
)
/*
{"5:30pm", 0, "5:30pm", 0},
{"5:30 pm", 0, "5:30 pm", 0},
{"7-10pm", 0, "7-10pm", 0},
{"5-30", 0, "5-30", 0},
{"05:30pm", 0, "05:30pm", 0},
{"05:30 pm", 0, "05:30 pm", 0},
{"05:30", 0, "05:30", 0},
{"05-30", 0, "05-30", 0},
{"7-10 pm", 0, "7-10 pm", 0},
{"11.10 pm", 0, "11.10 pm", 0},
https://play.golang.org/p/hXl7C8MWNr
*/
// 1. - int
// 2. - int
// 3. - ext?
func HourMinute(s rules.Strategy) rules.Rule {
return &rules.F{
RegExp: regexp.MustCompile("(?i)(?:\\W|^)" +
"((?:[0-1]{0,1}[0-9])|(?:2[0-3]))" +
"(?:\\:|:|\\-|h)" +
"((?:[0-5][0-9]))m*" +
"(?:\\s*(A\\.|P\\.|A\\.M\\.|P\\.M\\.|AM?|PM?))?" +
"(?:\\W|$)"),
Applier: func(m *rules.Match, c *rules.Context, o *rules.Options, ref time.Time) (bool, error) {
if (c.Hour != nil || c.Minute != nil) && s != rules.Override {
return false, nil
}
hour, err := strconv.Atoi(m.Captures[0])
if err != nil {
return false, errors.Wrap(err, "hour minute rule")
}
minutes, err := strconv.Atoi(m.Captures[1])
if err != nil {
return false, errors.Wrap(err, "hour minute rule")
}
if minutes > 59 {
return false, nil
}
c.Minute = &minutes
if m.Captures[2] != "" {
if hour > 12 {
return false, nil
}
switch m.Captures[2][0] {
case 65, 97: // am
c.Hour = &hour
case 80, 112: // pm
if hour < 12 {
hour += 12
}
c.Hour = &hour
}
} else {
if hour > 23 {
return false, nil
}
c.Hour = &hour
}
return true, nil
},
}
}