-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_layouts.go
More file actions
47 lines (42 loc) · 1.13 KB
/
parser_layouts.go
File metadata and controls
47 lines (42 loc) · 1.13 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
package gotimeparser
import (
"fmt"
"time"
)
// DefaultAllLayouts returns a default list of ALL supported layouts.
// This function returns new copy of a slice.
func DefaultAllLayouts() []string {
return []string{
time.RFC3339Nano,
time.RFC3339,
time.RFC1123Z,
time.RFC1123,
time.RFC850,
time.RFC822Z,
time.RFC822,
time.Layout,
time.RubyDate,
time.UnixDate,
time.ANSIC,
time.StampNano,
time.StampMicro,
time.StampMilli,
time.Stamp,
time.Kitchen,
}
}
// ParseAllLayouts uses layouts from `DefaultAllLayouts` to parse the time from a provided input string.
func ParseAllLayouts(dateTime string) (time.Time, error) {
return ParseLayouts(DefaultAllLayouts(), dateTime)
}
// ParseLayouts parses time based on a list of provided layouts.
// If layouts is empty list or nil, the error with unknown layout will be returned.
func ParseLayouts(layouts []string, dateTime string) (time.Time, error) {
for _, layout := range layouts {
parsedTime, err := time.Parse(layout, dateTime)
if err == nil {
return parsedTime, nil
}
}
return time.Time{}, &UnknownLayoutError{fmt.Sprintf("could not parse time: %s", dateTime)}
}