forked from tada-team/dateparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
78 lines (72 loc) · 2.69 KB
/
time.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
package dateparse
import (
"fmt"
"regexp"
"strings"
"time"
)
var (
timePrefix = `(в|к|by|at)`
timeSuffix = `(утра|вечера|мин[у]?[т]?|a[.]?m|p[.]?m)`
)
var (
hhmmRegex = regexp.MustCompile(fmt.Sprintf(`%s?[" "]?%s[.:]%s`, timePrefix, hourHH, minuteMM))
hhRegex = regexp.MustCompile(fmt.Sprintf(`%s[" "]%s\s?%s?`, timePrefix, hourHH, timeSuffix))
baseTimeOrientationRegex = regexp.MustCompile(fmt.Sprintf(`%s?[" "]?%s`, datePrefix, durationSuffix))
)
func parseTime(s string, opts Opts) (t time.Time, st string) {
switch {
case hhmmRegex.MatchString(s):
return calculateTime(hhmmRegex.FindStringSubmatch(s), opts)
case hhRegex.MatchString(s):
return calculateTime(hhRegex.FindStringSubmatch(s), opts)
case baseTimeOrientationRegex.MatchString(s):
return calculateTime(baseTimeOrientationRegex.FindStringSubmatch(s), opts)
}
return opts.Now, st
}
func calculateTime(t []string, opts Opts) (time.Time, string) {
m := forceList(strings.Join(t[1:], ","))
switch len(m) {
case 4:
hour := forceInt(m[2])
minute := forceInt(m[3])
return getDate(opts.Now.Year(), int(opts.Now.Month()), opts.Now.Day(), hour, minute, 0, opts), t[0]
case 3:
hour := forceInt(m[1])
switch {
case strings.Contains(morning, m[2]):
if hour > 12 {
hour -= 12
}
return getDate(opts.Now.Year(), int(opts.Now.Month()), opts.Now.Day(), hour, 0, 0, opts), t[0]
case strings.Contains(evening, m[2]):
if hour < 12 {
hour += 12
}
return getDate(opts.Now.Year(), int(opts.Now.Month()), opts.Now.Day(), hour, 0, 0, opts), t[0]
}
minute := forceInt(m[2])
return getDate(opts.Now.Year(), int(opts.Now.Month()), opts.Now.Day(), hour, minute, 0, opts), t[0]
case 2:
switch {
case strings.Contains(noon, m[1]):
return getDate(opts.Now.Year(), int(opts.Now.Month()), opts.Now.Day(), 12, 0, 0, opts), t[0]
case strings.Contains(timePrefix, m[0]):
return getDate(opts.Now.Year(), int(opts.Now.Month()), opts.Now.Day(), forceInt(m[1]), 0, 0, opts), t[0]
}
return getDate(opts.Now.Year(), int(opts.Now.Month()), opts.Now.Day(), forceInt(m[0]), forceInt(m[1]), 0, opts), t[0]
case 1:
switch {
case strings.Contains(morning, m[0]):
return getDate(opts.Now.Year(), int(opts.Now.Month()), opts.Now.Day(), 10, 0, 0, opts), t[0]
case strings.Contains(evening, m[0]):
return getDate(opts.Now.Year(), int(opts.Now.Month()), opts.Now.Day(), 18, 0, 0, opts), t[0]
case strings.Contains(noon, m[0]):
return getDate(opts.Now.Year(), int(opts.Now.Month()), opts.Now.Day(), 12, 0, 0, opts), t[0]
case strings.Contains(midnight, m[0]):
return getDate(opts.Now.Year(), int(opts.Now.Month()), opts.Now.Day(), 0, 0, 0, opts), t[0]
}
}
return opts.Now, t[0]
}