forked from osteele/liquid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsedate.go
59 lines (51 loc) · 1.53 KB
/
parsedate.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
package values
import (
"reflect"
"time"
)
var zeroTime time.Time
var dateLayouts = []string{
// from the Go library
time.ANSIC, // "Mon Jan _2 15:04:05 2006"
time.UnixDate, // "Mon Jan _2 15:04:05 MST 2006"
time.RubyDate, // "Mon Jan 02 15:04:05 -0700 2006"
time.RFC822, // "02 Jan 06 15:04 MST"
time.RFC822Z, // "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
time.RFC850, // "Monday, 02-Jan-06 15:04:05 MST"
time.RFC1123, // "Mon, 02 Jan 2006 15:04:05 MST"
time.RFC1123Z, // "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
time.RFC3339, // "2006-01-02T15:04:05Z07:00"
// ISO 8601
"2006-01-02T15:04:05-07:00", // this is also XML Schema
"2006-01-02T15:04:05Z",
"2006-01-02",
"20060102T150405Z",
// from Ruby's Time.parse docs
"Mon, 02 Jan 2006 15:04:05 -0700", // "RFC822" -- but not really
// From Jekyll docs
"02 January 2006", // Jekyll long string
"02 Jan 2006", // Jekyll short string
// observed in the wild; plus some variants
"2006-01-02 15:04:05 -07:00",
"2006-01-02 15:04:05 -0700",
"2006-01-02 15:04:05 MST",
"2006-01-02 15:04:05",
"2006-01-02 15:04",
"January 2, 2006",
"January 2 2006",
"Jan 2, 2006",
"Jan 2 2006",
}
// ParseDate tries a few heuristics to parse a date from a string
func ParseDate(s string) (time.Time, error) {
if s == "now" {
return time.Now(), nil
}
for _, layout := range dateLayouts {
t, err := time.ParseInLocation(layout, s, time.Local)
if err == nil {
return t, nil
}
}
return zeroTime, conversionError("", s, reflect.TypeOf(zeroTime))
}