-
Notifications
You must be signed in to change notification settings - Fork 4
/
datetime.go
62 lines (51 loc) · 1.78 KB
/
datetime.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
package dateparse
import (
"math/rand"
"regexp"
"strings"
"time"
)
var dateTimeRegex, _ = joinRegexp([]*regexp.Regexp{baseDurOnlyRegex, baseWeekOnlyRegex, baseWeekPrefixRegex, baseWeekPrefixOnlyRegex,
baseDurRegex, baseWeekRegex, baseTimeOrientationRegex, durTimeRegex, baseDurTimeRegex, durRegex, wdsSuffuxRegex, wdsRegex,
ddRegex, ddmmRegex, ddMonthRegex, ddmmyyyyRegex, mmddyyyyRegex, mmddRegex, ddMonthyyyyRegex, ddmmyyRegex, mmddyyRegex,
ddMonthyyRegex, durPrefixWeekRegex, weekDurSuffixRegex, durSuffixWeekRegex, hhmmRegex, hhRegex, isoyyyymmddRegex, isoyymmddRegex, wdsTimeRegex}, "|")
func dateTimeParse(s string, opts Opts) (t time.Time, msg string) {
if dateTimeRegex.MatchString(s) {
marker := getMarker()
s = strings.ReplaceAll(s, "://", marker)
date, replacingDate := parseDate(s, opts)
s = strings.Replace(s, strings.TrimSpace(replacingDate), "", 1)
timeP, replacingTime := parseTime(s, opts)
if (timeP.Before(opts.Now) || timeP == opts.Now) && date == opts.Now {
date = date.Add(24 * time.Hour)
}
hour := timeP.Hour()
minute := timeP.Minute()
second := timeP.Second()
replacingTime = strings.TrimSpace(replacingTime)
if len(replacingTime) == 0 {
hour = date.Hour()
minute = date.Minute()
second = date.Second()
}
s = strings.Replace(s, replacingTime, "", 1)
s = strings.ReplaceAll(s, marker, "://")
return getDate(date.Year(), date.Month(), date.Day(), hour, minute, second, opts), strings.TrimSpace(s)
}
return
}
func joinRegexp(regexps []*regexp.Regexp, sep string) (*regexp.Regexp, error) {
var b strings.Builder
for i, re := range regexps {
if i > 0 {
b.WriteString(sep)
}
b.WriteString(re.String())
}
return regexp.Compile(b.String())
}
func getMarker() string {
b := make([]byte, 20)
rand.Read(b)
return string(b)
}