-
Notifications
You must be signed in to change notification settings - Fork 921
/
exact_month_date.go
108 lines (86 loc) · 2.57 KB
/
exact_month_date.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package en
import (
"regexp"
"strconv"
"strings"
"time"
"github.com/botlabs-gg/yagpdb/v2/lib/when/rules"
)
// <[]string{"third of march", "third", "", "march", "", ""}>
// <[]string{"march third", "", "", "march", "third", ""}>
// <[]string{"march 3rd", "", "", "march", "3rd", ""}>
// <[]string{"3rd march", "3rd", "", "march", "", ""}>
// <[]string{"march 3", "", "", "march", "", "3"}>
// <[]string{"1st of september", "1st", "", "september", "", ""}>
// <[]string{"sept. 1st", "", "", "sept.", "1st", ""}>
// <[]string{"march 7th", "", "", "march", "7th", ""}>
// <[]string{"october 21st", "", "", "october", "21st", ""}>
// <[]string{"twentieth of december", "twentieth", "", "december", "", ""}>
// <[]string{"march 10th", "", "", "march", "10th", ""}>
// <[]string{"jan. 6", "", "", "jan.", "", "6"}>
// <[]string{"february", "", "", "february", "", ""}>
// <[]string{"october", "", "", "october", "", ""}>
// <[]string{"jul.", "", "", "jul.", "", ""}>
// <[]string{"june", "", "", "june", "", ""}>
// https://play.golang.org/p/Zfjl6ERNkq
// 1. - ordinal day?
// 2. - numeric day?
// 3. - month
// 4. - ordinal day?
// 5. - ordinal day?
func ExactMonthDate(s rules.Strategy) rules.Rule {
overwrite := s == rules.Override
return &rules.F{
RegExp: regexp.MustCompile("(?i)" +
"(?:\\W|^)" +
"(?:(?:(" + ORDINAL_WORDS_PATTERN[3:] + "(?:\\s+of)?|([0-9]+))\\s*)?" +
"(" + MONTH_OFFSET_PATTERN[3:] + // skip '(?:'
"(?:\\s*(?:(" + ORDINAL_WORDS_PATTERN[3:] + "|([0-9]+)))?" +
"(?:\\W|$)",
),
Applier: func(m *rules.Match, c *rules.Context, o *rules.Options, ref time.Time) (bool, error) {
_ = overwrite
ord1 := strings.ToLower(strings.TrimSpace(m.Captures[0]))
num1 := strings.ToLower(strings.TrimSpace(m.Captures[1]))
mon := strings.ToLower(strings.TrimSpace(m.Captures[2]))
ord2 := strings.ToLower(strings.TrimSpace(m.Captures[3]))
num2 := strings.ToLower(strings.TrimSpace(m.Captures[4]))
monInt, ok := MONTH_OFFSET[mon]
if !ok {
return false, nil
}
c.Month = &monInt
if ord1 != "" {
ordInt, ok := ORDINAL_WORDS[ord1]
if !ok {
return false, nil
}
c.Day = &ordInt
}
if num1 != "" {
n, err := strconv.ParseInt(num1, 10, 8)
if err != nil {
return false, nil
}
num := int(n)
c.Day = &num
}
if ord2 != "" {
ordInt, ok := ORDINAL_WORDS[ord2]
if !ok {
return false, nil
}
c.Day = &ordInt
}
if num2 != "" {
n, err := strconv.ParseInt(num2, 10, 8)
if err != nil {
return false, nil
}
num := int(n)
c.Day = &num
}
return true, nil
},
}
}