-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathslash_dmy.go
More file actions
96 lines (80 loc) · 1.7 KB
/
slash_dmy.go
File metadata and controls
96 lines (80 loc) · 1.7 KB
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
package common
import (
"regexp"
"strconv"
"time"
"github.com/olebedev/when/rules"
)
/*
- DD/MM/YYYY
- 11/3/2015
- 11/3/2015
- 11/3
also with "\", gift for windows' users
https://play.golang.org/p/29LkTfe1Xr
*/
func SlashDMY(s rules.Strategy) rules.Rule {
return &rules.F{
RegExp: regexp.MustCompile("(?i)(?:\\W|^)" +
"([0-3]{0,1}[0-9]{1})" +
"[\\/\\\\]" +
"([0-3]{0,1}[0-9]{1})" +
"(?:[\\/\\\\]" +
"((?:1|2)[0-9]{3})\\s*)?" +
"(?:\\W|$)"),
Applier: func(m *rules.Match, c *rules.Context, o *rules.Options, ref time.Time) (bool, error) {
if (c.Day != nil || c.Month != nil || c.Year != nil) && s != rules.Override {
return false, nil
}
day, _ := strconv.Atoi(m.Captures[0])
month, _ := strconv.Atoi(m.Captures[1])
year := -1
if m.Captures[2] != "" {
year, _ = strconv.Atoi(m.Captures[2])
}
if day == 0 {
return false, nil
}
if month > 12 {
return false, nil
}
if day > GetDays(ref.Year(), month) {
// invalid date: day is after last day of the month
return false, nil
}
WithYear:
if year != -1 {
c.Year = &year
c.Month = &month
c.Day = &day
return true, nil
}
if o.WantPast {
if month > int(ref.Month()) {
year = ref.Year() - 1
} else if month == int(ref.Month()) {
if day <= ref.Day() {
year = ref.Year()
} else {
year = ref.Year() - 1
}
} else {
year = ref.Year()
}
} else {
if month < int(ref.Month()) {
year = ref.Year() + 1
} else if month == int(ref.Month()) {
if day >= ref.Day() {
year = ref.Year()
} else {
year = ref.Year() + 1
}
} else {
year = ref.Year()
}
}
goto WithYear
},
}
}