-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdate.go
48 lines (42 loc) · 1.1 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
package journalskill
import (
"fmt"
"regexp"
"github.com/rickb777/date"
)
type DateType int
const (
DayDate = iota
MonthDate
YearDate
Invalid
)
var (
monthDateRegex = regexp.MustCompile(`^\d{4}-\d{2}(-XX)?$`)
yearDateRegex = regexp.MustCompile(`^\d{4}(-XX-XX)?$`)
xxDayDateRegex = regexp.MustCompile(`^XX\d{2}-\d{2}-\d{2}$`)
xxxxXXDayDateRegex = regexp.MustCompile(`^XXXX-XX-\d{2}$`)
)
func DateFrom(dateString string) (dayDate date.Date, monthDate string, dateType DateType) {
if dateString == "" {
return date.Date{}, "", Invalid
}
if monthDateRegex.MatchString(dateString) {
return date.Date{}, dateString[:7], MonthDate
}
if yearDateRegex.MatchString(dateString) {
return date.Date{}, "", YearDate
}
if xxxxXXDayDateRegex.MatchString(dateString) {
today := date.Today()
dateString = fmt.Sprintf("%04d-%02d-%v", today.Year(), today.Month(), dateString[8:])
}
if xxDayDateRegex.MatchString(dateString) {
dateString = "20" + dateString[2:]
}
entryDate, e := date.AutoParse(dateString)
if e != nil {
return date.Date{}, "", Invalid
}
return entryDate, "", DayDate
}