-
Notifications
You must be signed in to change notification settings - Fork 0
/
times.go
65 lines (57 loc) · 1.05 KB
/
times.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
63
64
65
/*
* Copyright (c) 2022, Dana Burkart <dana.burkart@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
package parser
import (
"errors"
"fmt"
"time"
"unicode"
"unicode/utf8"
)
var numberFormats = [...]string{
time.RFC3339,
time.RFC3339Nano,
time.RFC822,
time.RFC822Z,
time.Layout,
"2006/01/02",
"02/01/2006",
}
var letterFormats = [...]string{
"Jan 02, 2006",
time.RFC850,
time.UnixDate,
time.RFC1123,
time.RFC1123Z,
time.Stamp,
}
func ParseVagueDateTime(some string) (time.Time, error) {
first, _ := utf8.DecodeRuneInString(some)
var theFmt string
var found time.Time
switch {
case unicode.IsDigit(first):
for _, theFmt = range numberFormats {
tm, err := time.Parse(theFmt, some)
if err == nil {
found = tm
break
}
}
default:
for _, theFmt := range letterFormats {
tm, err := time.Parse(theFmt, some)
if err == nil {
found = tm
break
}
}
}
if found.IsZero() {
return found, errors.New(fmt.Sprintf("Specified time '%s' did not match a known timestamp", some))
}
return found, nil
}