forked from tada-team/dateparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.go
190 lines (178 loc) · 8.91 KB
/
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package dateparse
import (
"fmt"
"regexp"
"strings"
"time"
)
var (
datePrefix = `(в|во|in|on|ровно|the|at)`
dateSuffix = `(-ого|-го|-ва|-его|th|числа|date|\\|/|года|years|[.])`
daySuffix = `(-ого|-го|-ва|-его|th|числа|date|\\)`
)
var (
baseDurRegex = regexp.MustCompile(fmt.Sprintf(`(%s)[" "/]`, duration))
baseDurOnlyRegex = regexp.MustCompile(fmt.Sprintf(`(%s)$`, duration))
baseDurTimeRegex = regexp.MustCompile(fmt.Sprintf(`(\d\d?\d?)[" "](%s)`, durationTime))
baseWeekOnlyRegex = regexp.MustCompile(fmt.Sprintf(`^(%s|%s)$`, weeks, shortWeeks))
baseWeekPrefixOnlyRegex = regexp.MustCompile(fmt.Sprintf(`^%s[" "](%s|%s)$`, datePrefix, weeks, shortWeeks))
baseWeekPrefixRegex = regexp.MustCompile(fmt.Sprintf(`^%s[" "](%s|%s)[" "]`, datePrefix, weeks, shortWeeks))
baseWeekRegex = regexp.MustCompile(fmt.Sprintf(`^(%s|%s)[" "]`, weeks, shortWeeks))
weekDurSuffixRegex = regexp.MustCompile(fmt.Sprintf(`%s[" "](%s)[" "]%s`, datePrefix, weeks, durationSuffix))
durSuffixWeekRegex = regexp.MustCompile(fmt.Sprintf(`%s?[" "]?%s[" "]%s[" "](%s)`, datePrefix, durationSuffix, datePrefix, weeks))
durPrefixWeekRegex = regexp.MustCompile(fmt.Sprintf(`%s[" "]%s[" "](%s)[" "]?%s?`, datePrefix, durPrefix, weeks, durationSuffix))
)
var (
ddRegex = regexp.MustCompile(fmt.Sprintf(`%s?[" "]?%s[" "]?%s`, datePrefix, dayDD, daySuffix))
ddmmRegex = regexp.MustCompile(fmt.Sprintf(`%s?[" "]?%s[/.]%s\s?%s?`, datePrefix, dayDD, monthMM, dateSuffix))
ddMonthRegex = regexp.MustCompile(fmt.Sprintf(`%s%s?[" "](%s)`, dayDD, dateSuffix, months))
ddmmyyyyRegex = regexp.MustCompile(fmt.Sprintf(`%s?[" "]?%s[/.]%s[/.]%s\s?%s?`, datePrefix, dayDD, monthMM, yearYYYY, dateSuffix))
ddMonthyyyyRegex = regexp.MustCompile(fmt.Sprintf(`%s?[" "]?%s[" "/.](%s)[" "/.]%s\s?%s?`, datePrefix, dayDD, months, yearYYYY, dateSuffix))
ddmmyyRegex = regexp.MustCompile(fmt.Sprintf(`%s?[" "]?%s[/.]%s[/.]%s\s?%s?`, datePrefix, dayDD, monthMM, yearYY, dateSuffix))
ddMonthyyRegex = regexp.MustCompile(fmt.Sprintf(`%s?[" "]?%s[" "/.](%s)[" "/.]%s\s?%s`, datePrefix, dayDD, months, yearYY, dateSuffix))
)
var (
durTimeRegex = regexp.MustCompile(fmt.Sprintf(`%s?[" "]?%s[" "](\d\d?\d?)[" "]?(%s)?`, datePrefix, durPrefix, durationTime))
durRegex = regexp.MustCompile(fmt.Sprintf(`%s?[" "]?%s[" "](%s)`, datePrefix, durPrefix, durationWds))
wdsRegex = regexp.MustCompile(fmt.Sprintf(`(%s)\b[" "/]?%s?`, durationWds, durationSuffix))
wdsSuffuxRegex = regexp.MustCompile(fmt.Sprintf(`(%s)[" "/]%s[" "]%s`, durationWds, datePrefix, durationSuffix))
)
var (
mmddyyyyRegex = regexp.MustCompile(fmt.Sprintf(`%s?[" "]?%s[/.]%s[/.]%s\s?%s?`, datePrefix, monthMM, dayDD, yearYYYY, dateSuffix))
mmddyyRegex = regexp.MustCompile(fmt.Sprintf(`%s?[" "]?%s[/.]%s[/.]%s\s?%s?`, datePrefix, monthMM, dayDD, yearYY, dateSuffix))
mmddRegex = regexp.MustCompile(fmt.Sprintf(`%s?[" "]?%s[/.]%s\s?%s?`, datePrefix, monthMM, dayDD, dateSuffix))
)
func parseDate(s string, opts Opts) (t time.Time, st string) {
switch {
case baseDurOnlyRegex.MatchString(s):
return calculateWordsDate(baseDurOnlyRegex.FindStringSubmatch(s), opts)
case baseWeekPrefixOnlyRegex.MatchString(s):
return calculateWeekDuration(baseWeekPrefixOnlyRegex.FindStringSubmatch(s), opts, 2)
case baseWeekOnlyRegex.MatchString(s):
return calculateWeekDuration(baseWeekOnlyRegex.FindStringSubmatch(s), opts, 1)
case wdsSuffuxRegex.MatchString(s):
return calculateWordsDate(wdsSuffuxRegex.FindStringSubmatch(s), opts)
case baseDurRegex.MatchString(s):
return calculateWordsDate(baseDurRegex.FindStringSubmatch(s), opts)
case weekDurSuffixRegex.MatchString(s):
return calculateWeekDuration(weekDurSuffixRegex.FindStringSubmatch(s), opts, 2)
case baseWeekPrefixRegex.MatchString(s):
return calculateWeekDuration(baseWeekPrefixRegex.FindStringSubmatch(s), opts, 2)
case baseWeekRegex.MatchString(s):
return calculateWeekDuration(baseWeekRegex.FindStringSubmatch(s), opts, 1)
case durTimeRegex.MatchString(s):
return calculateDuration(durTimeRegex.FindStringSubmatch(s), opts, 2)
case durRegex.MatchString(s):
return calculateDuration(durRegex.FindStringSubmatch(s), opts, 2)
case durPrefixWeekRegex.MatchString(s):
return calculateWeekDuration(durPrefixWeekRegex.FindStringSubmatch(s), opts, 3)
case durSuffixWeekRegex.MatchString(s):
return calculateWeekDuration(durSuffixWeekRegex.FindStringSubmatch(s), opts, -3)
case ddMonthyyyyRegex.MatchString(s):
return calculateFullDate(ddMonthyyyyRegex.FindStringSubmatch(s), opts, 2)
case ddMonthyyRegex.MatchString(s):
return calculateFullDate(ddMonthyyRegex.FindStringSubmatch(s), opts, 2)
case ddmmyyyyRegex.MatchString(s):
return calculateFullDate(ddmmyyyyRegex.FindStringSubmatch(s), opts, 2)
case mmddyyyyRegex.MatchString(s):
return calculateFullDate(mmddyyyyRegex.FindStringSubmatch(s), opts, 1)
case ddmmyyRegex.MatchString(s):
return calculateFullDate(ddmmyyRegex.FindStringSubmatch(s), opts, 2)
case mmddyyRegex.MatchString(s):
return calculateFullDate(mmddyyRegex.FindStringSubmatch(s), opts, 1)
case ddMonthRegex.MatchString(s):
return calculateDate(ddMonthRegex.FindStringSubmatch(s), opts, 2)
case ddmmRegex.MatchString(s):
return calculateDate(ddmmRegex.FindStringSubmatch(s), opts, 2)
case mmddRegex.MatchString(s):
return calculateDate(mmddRegex.FindStringSubmatch(s), opts, 1)
case baseDurTimeRegex.MatchString(s):
return calculateDuration(baseDurTimeRegex.FindStringSubmatch(s), opts, 1)
case wdsRegex.MatchString(s):
return calculateWordsDate(wdsRegex.FindStringSubmatch(s), opts)
case ddRegex.MatchString(s):
m := ddRegex.FindStringSubmatch(s)
day := forceInt(m[2])
return getDate(opts.Now.Year(), int(opts.Now.Month()), day, opts.TodayEndHour, 0, 0, opts), m[0]
}
return opts.Now, st
}
func getDate(year int, month int, day int, hour int, minute int, second int, opts Opts) time.Time {
return time.Date(year, time.Month(month), day, hour, minute, second, 0, opts.Now.Location())
}
func calculateDate(m []string, opts Opts, monthPosition int) (time.Time, string) {
m = forceList(strings.Join(m, ","))
dayPosition := 1
month := opts.Now.Month()
year := opts.Now.Year()
if monthPosition < 2 {
dayPosition += 1
}
day := forceInt(m[dayPosition])
if mth := parseMonth(m[monthPosition]); mth != 0 {
month = time.Month(mth)
} else {
month = time.Month(forceInt(m[monthPosition]))
}
if month < opts.Now.Month() {
year += 1
}
return getDate(year, int(month), day, opts.TodayEndHour, 0, 0, opts), m[0]
}
func calculateFullDate(m []string, opts Opts, monthPosition int) (time.Time, string) {
year := opts.Now.Year()
if len(m[4]) == 2 {
year = forceInt("20" + m[4][:2])
} else {
year = forceInt(m[4][:4])
}
date, _ := calculateDate(m, opts, monthPosition)
if date.Month() < opts.Now.Month() && year == opts.Now.Year() {
year += 1
}
return getDate(year, int(date.Month()), date.Day(), opts.TodayEndHour, 0, 0, opts), m[0]
}
func calculateWordsDate(m []string, opts Opts) (time.Time, string) {
m = forceList(strings.Join(m, ","))
date := getDate(opts.Now.Year(), int(opts.Now.Month()), opts.Now.Day(), opts.Now.Hour(), opts.Now.Minute(), 0, opts)
str := strings.Replace(m[0], "/", "", 1)
switch {
case strings.Contains(today, str) || strings.Contains(today, m[1]):
date = getDate(opts.Now.Year(), int(opts.Now.Month()), opts.Now.Day(), opts.TodayEndHour, 0, 0, opts)
case strings.Contains(tomorrow, str) || strings.Contains(tomorrow, m[1]):
date = getDate(opts.Now.Year(), int(opts.Now.Month()), opts.Now.Day()+1, opts.TodayEndHour, 0, 0, opts)
case strings.Contains(afterTomorrow, m[0]):
date = getDate(opts.Now.Year(), int(opts.Now.Month()), opts.Now.Day()+2, opts.TodayEndHour, 0, 0, opts)
case strings.Contains(afterAfterTomorrow, m[0]):
date = getDate(opts.Now.Year(), int(opts.Now.Month()), opts.Now.Day()+3, opts.TodayEndHour, 0, 0, opts)
case strings.Contains(yesterday, m[0]):
date = getDate(opts.Now.Year(), int(opts.Now.Month()), opts.Now.Day()+365, opts.TodayEndHour, 0, 0, opts)
}
if len(m) > 2 {
switch {
case strings.Contains(morning, m[2]):
date = getDate(date.Year(), int(date.Month()), date.Day(), 10, 0, 0, opts)
case strings.Contains(noon, m[2]):
date = getDate(date.Year(), int(date.Month()), date.Day(), 12, 0, 0, opts)
case strings.Contains(evening, m[2]):
date = getDate(date.Year(), int(date.Month()), date.Day(), 18, 0, 0, opts)
case strings.Contains(midnight, m[2]):
if date.Day() == opts.Now.Day() {
date = date.Add(24 * time.Hour)
}
date = getDate(date.Year(), int(date.Month()), date.Day(), 0, 0, 0, opts)
}
}
if len(m) > 3 {
switch {
case strings.Contains(noon, m[3]):
date = getDate(date.Year(), int(date.Month()), date.Day(), 12, 0, 0, opts)
case strings.Contains(midnight, m[3]):
if date.Day() == opts.Now.Day() {
date = date.Add(24 * time.Hour)
}
date = getDate(date.Year(), int(date.Month()), date.Day(), 0, 0, 0, opts)
}
}
return date, m[0]
}