-
Notifications
You must be signed in to change notification settings - Fork 927
/
hour.go
65 lines (56 loc) · 1.13 KB
/
hour.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 ru
import (
"regexp"
"strconv"
"time"
"github.com/botlabs-gg/yagpdb/v2/lib/when/rules"
"github.com/pkg/errors"
)
/*
"5pm"
"5 pm"
"5am"
"5pm"
"5A."
"5P."
"11 P.M."
https://play.golang.org/p/w2PeQ3l_rp
*/
func Hour(s rules.Strategy) rules.Rule {
return &rules.F{
RegExp: regexp.MustCompile("(?i)(?:\\W|^)" +
"(" + INTEGER_WORDS_PATTERN + "|\\d{1,2})" +
"(?:\\s*час(?:а|ов|ам)?)?(?:\\s*(утра|вечера|дня))" +
"(?:\\P{L}|$)"),
Applier: func(m *rules.Match, c *rules.Context, o *rules.Options, ref time.Time) (bool, error) {
if c.Hour != nil && s != rules.Override {
return false, nil
}
var hour int
var err error
if n, ok := INTEGER_WORDS[m.Captures[0]]; ok {
hour = n
} else {
hour, err = strconv.Atoi(m.Captures[0])
if err != nil {
return false, errors.Wrap(err, "hour rule")
}
}
if hour > 12 {
return false, nil
}
zero := 0
switch m.Captures[1] {
case "утра":
c.Hour = &hour
case "вечера", "дня":
if hour < 12 {
hour += 12
}
c.Hour = &hour
}
c.Minute = &zero
return true, nil
},
}
}