2014-04-21 10:56:17 +08:00
|
|
|
package dateparse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-04-26 07:59:10 +08:00
|
|
|
"strconv"
|
2014-04-21 10:56:17 +08:00
|
|
|
"time"
|
|
|
|
"unicode"
|
|
|
|
)
|
|
|
|
|
2014-05-21 13:08:25 +08:00
|
|
|
type DateState int
|
|
|
|
|
2014-04-21 10:56:17 +08:00
|
|
|
const (
|
2017-06-27 08:45:07 +08:00
|
|
|
// C = Comma
|
|
|
|
// O = Colon
|
|
|
|
// E = Period/Dot
|
|
|
|
// A = Alpha
|
|
|
|
// N = Digits/Numeric
|
|
|
|
// S = Slash /
|
|
|
|
// P = Plus +
|
|
|
|
// m = Minus, Dash, -
|
|
|
|
// T = T
|
|
|
|
// Z = Z
|
|
|
|
// M = AM/PM
|
|
|
|
// W = Whitespace
|
2014-05-21 13:08:25 +08:00
|
|
|
ST_START DateState = iota
|
|
|
|
ST_DIGIT
|
|
|
|
ST_DIGITDASH
|
2017-07-27 07:55:21 +08:00
|
|
|
ST_DIGITDASHALPHA
|
2014-05-21 13:08:25 +08:00
|
|
|
ST_DIGITDASHWS
|
2017-07-21 09:41:09 +08:00
|
|
|
ST_DIGITDASHWSWS
|
|
|
|
ST_DIGITDASHWSWSAMPMMAYBE
|
|
|
|
ST_DIGITDASHWSWSOFFSET
|
|
|
|
ST_DIGITDASHWSWSOFFSETALPHA
|
|
|
|
ST_DIGITDASHWSWSOFFSETCOLONALPHA
|
|
|
|
ST_DIGITDASHWSWSOFFSETCOLON
|
|
|
|
ST_DIGITDASHWSOFFSET
|
|
|
|
ST_DIGITDASHWSWSALPHA
|
2017-04-12 06:30:42 +08:00
|
|
|
ST_DIGITDASHWSDOT
|
|
|
|
ST_DIGITDASHWSDOTALPHA
|
2017-07-21 09:41:09 +08:00
|
|
|
ST_DIGITDASHWSDOTOFFSET
|
|
|
|
ST_DIGITDASHWSDOTOFFSETALPHA
|
2014-05-21 13:08:25 +08:00
|
|
|
ST_DIGITDASHT
|
2017-06-26 10:09:06 +08:00
|
|
|
ST_DIGITDASHTZ
|
|
|
|
ST_DIGITDASHTZDIGIT
|
2017-07-12 04:40:58 +08:00
|
|
|
ST_DIGITDASHTDELTA
|
2017-07-18 05:46:18 +08:00
|
|
|
ST_DIGITDASHTDELTACOLON
|
2014-05-21 13:08:25 +08:00
|
|
|
ST_DIGITSLASH
|
|
|
|
ST_DIGITSLASHWS
|
|
|
|
ST_DIGITSLASHWSCOLON
|
2017-02-14 10:39:03 +08:00
|
|
|
ST_DIGITSLASHWSCOLONAMPM
|
2014-05-21 13:08:25 +08:00
|
|
|
ST_DIGITSLASHWSCOLONCOLON
|
2017-02-14 10:39:03 +08:00
|
|
|
ST_DIGITSLASHWSCOLONCOLONAMPM
|
2017-02-06 02:04:03 +08:00
|
|
|
ST_DIGITALPHA
|
2014-05-21 13:08:25 +08:00
|
|
|
ST_ALPHA
|
|
|
|
ST_ALPHAWS
|
2017-07-14 00:11:41 +08:00
|
|
|
ST_ALPHAWSDIGITCOMMA
|
2014-05-21 13:53:52 +08:00
|
|
|
ST_ALPHAWSALPHA
|
2017-06-27 08:45:07 +08:00
|
|
|
ST_ALPHAWSALPHACOLON
|
|
|
|
ST_ALPHAWSALPHACOLONOFFSET
|
|
|
|
ST_ALPHAWSALPHACOLONALPHA
|
|
|
|
ST_ALPHAWSALPHACOLONALPHAOFFSET
|
|
|
|
ST_ALPHAWSALPHACOLONALPHAOFFSETALPHA
|
2016-05-04 04:55:11 +08:00
|
|
|
ST_WEEKDAYCOMMA
|
2017-07-14 00:11:41 +08:00
|
|
|
ST_WEEKDAYCOMMADELTA
|
2016-05-04 04:55:11 +08:00
|
|
|
ST_WEEKDAYABBREVCOMMA
|
2017-07-14 00:11:41 +08:00
|
|
|
ST_WEEKDAYABBREVCOMMADELTA
|
2017-07-14 22:02:22 +08:00
|
|
|
ST_WEEKDAYABBREVCOMMADELTAZONE
|
2014-04-21 10:56:17 +08:00
|
|
|
)
|
|
|
|
|
2014-12-16 03:20:40 +08:00
|
|
|
var (
|
2017-07-14 00:11:41 +08:00
|
|
|
shortDates = []string{"01/02/2006", "1/2/2006", "06/01/02", "01/02/06", "1/2/06"}
|
2014-12-16 03:20:40 +08:00
|
|
|
)
|
|
|
|
|
2017-07-27 07:42:12 +08:00
|
|
|
// MustParse Parse a date, and panic if it can't be parsed
|
2016-09-19 02:46:39 +08:00
|
|
|
func MustParse(datestr string) time.Time {
|
2017-07-27 07:42:12 +08:00
|
|
|
t, err := parseTime(datestr, nil)
|
2016-09-19 02:46:39 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2017-07-27 07:42:12 +08:00
|
|
|
// ParseAny Given an unknown date format, detect the layout, parse.
|
2014-05-07 12:15:43 +08:00
|
|
|
func ParseAny(datestr string) (time.Time, error) {
|
2017-07-27 07:42:12 +08:00
|
|
|
return parseTime(datestr, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseIn Given an unknown date format, detect the layout,
|
|
|
|
// using given location, parse.
|
2017-07-27 08:08:58 +08:00
|
|
|
//
|
|
|
|
// If no recognized Timezone/Offset info exists in the datestring, it uses
|
|
|
|
// given location. IF there IS timezone/offset info it uses the given location
|
|
|
|
// info for any zone interpretation. That is, MST means one thing when using
|
|
|
|
// America/Denver and something else in other locations.
|
2017-07-27 07:42:12 +08:00
|
|
|
func ParseIn(datestr string, loc *time.Location) (time.Time, error) {
|
|
|
|
return parseTime(datestr, loc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseLocal Given an unknown date format, detect the layout,
|
|
|
|
// using time.Local, parse.
|
2017-07-27 08:08:58 +08:00
|
|
|
//
|
|
|
|
// If no recognized Timezone/Offset info exists in the datestring, it uses
|
|
|
|
// given location. IF there IS timezone/offset info it uses the given location
|
|
|
|
// info for any zone interpretation. That is, MST means one thing when using
|
|
|
|
// America/Denver and something else in other locations.
|
2017-07-27 07:42:12 +08:00
|
|
|
func ParseLocal(datestr string) (time.Time, error) {
|
|
|
|
return parseTime(datestr, time.Local)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parse(layout, datestr string, loc *time.Location) (time.Time, error) {
|
|
|
|
if loc == nil {
|
|
|
|
return time.Parse(layout, datestr)
|
|
|
|
}
|
|
|
|
return time.ParseInLocation(layout, datestr, loc)
|
|
|
|
}
|
2014-04-21 10:56:17 +08:00
|
|
|
|
2017-07-27 07:42:12 +08:00
|
|
|
func parseTime(datestr string, loc *time.Location) (time.Time, error) {
|
2014-05-21 13:08:25 +08:00
|
|
|
state := ST_START
|
2014-04-21 10:56:17 +08:00
|
|
|
|
2014-07-11 06:25:23 +08:00
|
|
|
firstSlash := 0
|
|
|
|
|
2014-06-16 03:11:29 +08:00
|
|
|
// General strategy is to read rune by rune through the date looking for
|
|
|
|
// certain hints of what type of date we are dealing with.
|
|
|
|
// Hopefully we only need to read about 5 or 6 bytes before
|
|
|
|
// we figure it out and then attempt a parse
|
2014-05-07 12:15:43 +08:00
|
|
|
iterRunes:
|
2014-04-21 10:56:17 +08:00
|
|
|
for i := 0; i < len(datestr); i++ {
|
2017-07-14 10:57:15 +08:00
|
|
|
r := rune(datestr[i])
|
|
|
|
// r, bytesConsumed := utf8.DecodeRuneInString(datestr[ri:])
|
|
|
|
// if bytesConsumed > 1 {
|
|
|
|
// ri += (bytesConsumed - 1)
|
|
|
|
// }
|
2014-05-07 12:15:43 +08:00
|
|
|
|
2014-04-21 10:56:17 +08:00
|
|
|
switch state {
|
2014-05-07 12:15:43 +08:00
|
|
|
case ST_START:
|
|
|
|
if unicode.IsDigit(r) {
|
2014-05-21 13:08:25 +08:00
|
|
|
state = ST_DIGIT
|
2014-05-07 12:15:43 +08:00
|
|
|
} else if unicode.IsLetter(r) {
|
2014-05-21 13:08:25 +08:00
|
|
|
state = ST_ALPHA
|
2014-04-21 10:56:17 +08:00
|
|
|
}
|
2014-05-21 13:08:25 +08:00
|
|
|
case ST_DIGIT: // starts digits
|
2014-05-07 12:15:43 +08:00
|
|
|
if unicode.IsDigit(r) {
|
|
|
|
continue
|
2017-02-06 02:04:03 +08:00
|
|
|
} else if unicode.IsLetter(r) {
|
|
|
|
state = ST_DIGITALPHA
|
|
|
|
continue
|
2014-04-21 10:56:17 +08:00
|
|
|
}
|
2014-05-07 12:15:43 +08:00
|
|
|
switch r {
|
2017-07-14 10:57:15 +08:00
|
|
|
case '-', '\u2212':
|
2014-05-21 13:08:25 +08:00
|
|
|
state = ST_DIGITDASH
|
2014-05-07 12:15:43 +08:00
|
|
|
case '/':
|
2014-05-21 13:08:25 +08:00
|
|
|
state = ST_DIGITSLASH
|
2014-07-11 06:25:23 +08:00
|
|
|
firstSlash = i
|
2014-04-25 08:51:56 +08:00
|
|
|
}
|
2014-05-21 13:08:25 +08:00
|
|
|
case ST_DIGITDASH: // starts digit then dash 02-
|
2014-05-07 12:15:43 +08:00
|
|
|
// 2006-01-02T15:04:05Z07:00
|
2017-06-26 10:09:06 +08:00
|
|
|
// 2017-06-25T17:46:57.45706582-07:00
|
2014-05-07 12:15:43 +08:00
|
|
|
// 2006-01-02T15:04:05.999999999Z07:00
|
2017-07-18 05:46:18 +08:00
|
|
|
// 2006-01-02T15:04:05+0000
|
2014-05-12 09:08:56 +08:00
|
|
|
// 2012-08-03 18:31:59.257000000
|
2014-05-07 12:15:43 +08:00
|
|
|
// 2014-04-26 17:24:37.3186369
|
2017-01-31 09:18:48 +08:00
|
|
|
// 2017-01-27 00:07:31.945167
|
2014-05-07 12:15:43 +08:00
|
|
|
// 2016-03-14 00:00:00.000
|
2014-05-11 23:28:23 +08:00
|
|
|
// 2014-05-11 08:20:13,787
|
2017-07-21 09:41:09 +08:00
|
|
|
// 2017-07-19 03:21:51+00:00
|
2014-05-07 12:15:43 +08:00
|
|
|
// 2006-01-02
|
2014-05-12 09:22:16 +08:00
|
|
|
// 2013-04-01 22:43:22
|
2014-05-07 12:15:43 +08:00
|
|
|
// 2014-04-26 05:24:37 PM
|
2017-07-27 07:55:21 +08:00
|
|
|
// 2013-Feb-03
|
2014-05-07 12:15:43 +08:00
|
|
|
switch {
|
|
|
|
case r == ' ':
|
2014-05-21 13:08:25 +08:00
|
|
|
state = ST_DIGITDASHWS
|
2014-05-07 12:15:43 +08:00
|
|
|
case r == 'T':
|
2014-05-21 13:08:25 +08:00
|
|
|
state = ST_DIGITDASHT
|
2017-07-27 07:55:21 +08:00
|
|
|
default:
|
|
|
|
if unicode.IsLetter(r) {
|
|
|
|
state = ST_DIGITDASHALPHA
|
|
|
|
break iterRunes
|
|
|
|
}
|
2014-04-25 08:51:56 +08:00
|
|
|
}
|
2017-07-21 09:41:09 +08:00
|
|
|
case ST_DIGITDASHWS:
|
2014-05-12 09:22:16 +08:00
|
|
|
// 2013-04-01 22:43:22
|
2014-05-11 23:28:23 +08:00
|
|
|
// 2014-05-11 08:20:13,787
|
2017-07-21 09:41:09 +08:00
|
|
|
// ST_DIGITDASHWSWS
|
|
|
|
// 2014-04-26 05:24:37 PM
|
|
|
|
// 2014-12-16 06:20:00 UTC
|
|
|
|
// 2015-02-18 00:12:00 +0000 UTC
|
|
|
|
// 2006-01-02 15:04:05 -0700
|
|
|
|
// 2006-01-02 15:04:05 -07:00
|
|
|
|
// ST_DIGITDASHWSOFFSET
|
|
|
|
// 2017-07-19 03:21:51+00:00
|
|
|
|
// ST_DIGITDASHWSDOT
|
|
|
|
// 2014-04-26 17:24:37.3186369
|
|
|
|
// 2017-01-27 00:07:31.945167
|
|
|
|
// 2012-08-03 18:31:59.257000000
|
|
|
|
// 2016-03-14 00:00:00.000
|
|
|
|
// ST_DIGITDASHWSDOTOFFSET
|
|
|
|
// 2017-01-27 00:07:31.945167 +0000
|
|
|
|
// 2016-03-14 00:00:00.000 +0000
|
|
|
|
// ST_DIGITDASHWSDOTOFFSETALPHA
|
|
|
|
// 2017-01-27 00:07:31.945167 +0000 UTC
|
|
|
|
// 2016-03-14 00:00:00.000 +0000 UTC
|
|
|
|
// ST_DIGITDASHWSDOTALPHA
|
|
|
|
// 2014-12-16 06:20:00.000 UTC
|
2014-05-07 12:15:43 +08:00
|
|
|
switch r {
|
2014-05-11 23:28:23 +08:00
|
|
|
case ',':
|
|
|
|
if len(datestr) == len("2014-05-11 08:20:13,787") {
|
2014-05-11 23:42:56 +08:00
|
|
|
// go doesn't seem to parse this one natively? or did i miss it?
|
2017-07-27 07:42:12 +08:00
|
|
|
t, err := parse("2006-01-02 03:04:05", datestr[:i], loc)
|
2017-07-14 10:57:15 +08:00
|
|
|
if err == nil {
|
2014-05-11 23:42:56 +08:00
|
|
|
ms, err := strconv.Atoi(datestr[i+1:])
|
|
|
|
if err == nil {
|
|
|
|
return time.Unix(0, t.UnixNano()+int64(ms)*1e6), nil
|
|
|
|
}
|
2014-05-11 23:28:23 +08:00
|
|
|
}
|
2017-07-14 10:57:15 +08:00
|
|
|
return t, err
|
2014-05-11 23:28:23 +08:00
|
|
|
}
|
2017-07-21 09:41:09 +08:00
|
|
|
case '-', '+':
|
|
|
|
state = ST_DIGITDASHWSOFFSET
|
2017-04-12 06:30:42 +08:00
|
|
|
case '.':
|
|
|
|
state = ST_DIGITDASHWSDOT
|
2017-07-21 09:41:09 +08:00
|
|
|
case ' ':
|
|
|
|
state = ST_DIGITDASHWSWS
|
|
|
|
}
|
|
|
|
|
|
|
|
case ST_DIGITDASHWSWS:
|
|
|
|
// ST_DIGITDASHWSWSALPHA
|
|
|
|
// 2014-12-16 06:20:00 UTC
|
|
|
|
// ST_DIGITDASHWSWSAMPMMAYBE
|
|
|
|
// 2014-04-26 05:24:37 PM
|
|
|
|
// ST_DIGITDASHWSWSOFFSET
|
|
|
|
// 2006-01-02 15:04:05 -0700
|
|
|
|
// ST_DIGITDASHWSWSOFFSETCOLON
|
|
|
|
// 2006-01-02 15:04:05 -07:00
|
|
|
|
// ST_DIGITDASHWSWSOFFSETCOLONALPHA
|
|
|
|
// 2015-02-18 00:12:00 +00:00 UTC
|
|
|
|
// ST_DIGITDASHWSWSOFFSETALPHA
|
|
|
|
// 2015-02-18 00:12:00 +0000 UTC
|
|
|
|
switch r {
|
|
|
|
case 'A', 'P':
|
|
|
|
state = ST_DIGITDASHWSWSAMPMMAYBE
|
|
|
|
case '+', '-':
|
|
|
|
state = ST_DIGITDASHWSWSOFFSET
|
2014-12-03 06:57:00 +08:00
|
|
|
default:
|
|
|
|
if unicode.IsLetter(r) {
|
|
|
|
// 2014-12-16 06:20:00 UTC
|
2017-07-21 09:41:09 +08:00
|
|
|
state = ST_DIGITDASHWSWSALPHA
|
2014-12-03 06:57:00 +08:00
|
|
|
break iterRunes
|
|
|
|
}
|
2014-05-07 12:15:43 +08:00
|
|
|
}
|
2017-07-21 09:41:09 +08:00
|
|
|
|
|
|
|
case ST_DIGITDASHWSWSAMPMMAYBE:
|
|
|
|
if r == 'M' {
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01-02 03:04:05 PM", datestr, loc)
|
2017-07-21 09:41:09 +08:00
|
|
|
}
|
|
|
|
state = ST_DIGITDASHWSWSALPHA
|
|
|
|
|
|
|
|
case ST_DIGITDASHWSWSOFFSET:
|
|
|
|
// ST_DIGITDASHWSWSOFFSET
|
|
|
|
// 2006-01-02 15:04:05 -0700
|
|
|
|
// ST_DIGITDASHWSWSOFFSETCOLON
|
|
|
|
// 2006-01-02 15:04:05 -07:00
|
|
|
|
// ST_DIGITDASHWSWSOFFSETCOLONALPHA
|
|
|
|
// 2015-02-18 00:12:00 +00:00 UTC
|
|
|
|
// ST_DIGITDASHWSWSOFFSETALPHA
|
|
|
|
// 2015-02-18 00:12:00 +0000 UTC
|
|
|
|
if r == ':' {
|
|
|
|
state = ST_DIGITDASHWSWSOFFSETCOLON
|
|
|
|
} else if unicode.IsLetter(r) {
|
|
|
|
// 2015-02-18 00:12:00 +0000 UTC
|
|
|
|
state = ST_DIGITDASHWSWSOFFSETALPHA
|
|
|
|
break iterRunes
|
|
|
|
}
|
|
|
|
|
|
|
|
case ST_DIGITDASHWSWSOFFSETCOLON:
|
|
|
|
// ST_DIGITDASHWSWSOFFSETCOLON
|
|
|
|
// 2006-01-02 15:04:05 -07:00
|
|
|
|
// ST_DIGITDASHWSWSOFFSETCOLONALPHA
|
|
|
|
// 2015-02-18 00:12:00 +00:00 UTC
|
|
|
|
if unicode.IsLetter(r) {
|
|
|
|
// 2015-02-18 00:12:00 +00:00 UTC
|
|
|
|
state = ST_DIGITDASHWSWSOFFSETCOLONALPHA
|
|
|
|
break iterRunes
|
|
|
|
}
|
|
|
|
|
2017-04-12 06:30:42 +08:00
|
|
|
case ST_DIGITDASHWSDOT:
|
|
|
|
// 2014-04-26 17:24:37.3186369
|
|
|
|
// 2017-01-27 00:07:31.945167
|
|
|
|
// 2012-08-03 18:31:59.257000000
|
|
|
|
// 2016-03-14 00:00:00.000
|
2017-07-21 09:41:09 +08:00
|
|
|
// ST_DIGITDASHWSDOTOFFSET
|
|
|
|
// 2017-01-27 00:07:31.945167 +0000
|
|
|
|
// 2016-03-14 00:00:00.000 +0000
|
|
|
|
// ST_DIGITDASHWSDOTOFFSETALPHA
|
|
|
|
// 2017-01-27 00:07:31.945167 +0000 UTC
|
|
|
|
// 2016-03-14 00:00:00.000 +0000 UTC
|
|
|
|
// ST_DIGITDASHWSDOTALPHA
|
|
|
|
// 2014-12-16 06:20:00.000 UTC
|
2017-04-12 06:30:42 +08:00
|
|
|
if unicode.IsLetter(r) {
|
|
|
|
// 2014-12-16 06:20:00.000 UTC
|
|
|
|
state = ST_DIGITDASHWSDOTALPHA
|
|
|
|
break iterRunes
|
|
|
|
} else if r == '+' || r == '-' {
|
2017-07-21 09:41:09 +08:00
|
|
|
state = ST_DIGITDASHWSDOTOFFSET
|
2017-04-12 06:30:42 +08:00
|
|
|
}
|
2017-07-21 09:41:09 +08:00
|
|
|
case ST_DIGITDASHWSDOTOFFSET:
|
|
|
|
// 2017-01-27 00:07:31.945167 +0000
|
|
|
|
// 2016-03-14 00:00:00.000 +0000
|
|
|
|
// ST_DIGITDASHWSDOTOFFSETALPHA
|
|
|
|
// 2017-01-27 00:07:31.945167 +0000 UTC
|
|
|
|
// 2016-03-14 00:00:00.000 +0000 UTC
|
2017-04-12 06:30:42 +08:00
|
|
|
if unicode.IsLetter(r) {
|
|
|
|
// 2014-12-16 06:20:00.000 UTC
|
2017-07-21 09:41:09 +08:00
|
|
|
// 2017-01-27 00:07:31.945167 +0000 UTC
|
|
|
|
// 2016-03-14 00:00:00.000 +0000 UTC
|
|
|
|
state = ST_DIGITDASHWSDOTOFFSETALPHA
|
2017-04-12 06:30:42 +08:00
|
|
|
break iterRunes
|
|
|
|
}
|
2014-05-21 13:08:25 +08:00
|
|
|
case ST_DIGITDASHT: // starts digit then dash 02- then T
|
2017-06-26 10:09:06 +08:00
|
|
|
// ST_DIGITDASHT
|
2015-06-26 05:09:39 +08:00
|
|
|
// 2006-01-02T15:04:05
|
2017-06-26 10:09:06 +08:00
|
|
|
// ST_DIGITDASHTZ
|
2014-10-03 05:39:24 +08:00
|
|
|
// 2006-01-02T15:04:05.999999999Z
|
|
|
|
// 2006-01-02T15:04:05.99999999Z
|
2014-10-03 05:47:22 +08:00
|
|
|
// 2006-01-02T15:04:05.9999999Z
|
|
|
|
// 2006-01-02T15:04:05.999999Z
|
|
|
|
// 2006-01-02T15:04:05.99999Z
|
|
|
|
// 2006-01-02T15:04:05.9999Z
|
|
|
|
// 2006-01-02T15:04:05.999Z
|
|
|
|
// 2006-01-02T15:04:05.99Z
|
2017-07-12 23:22:34 +08:00
|
|
|
// 2009-08-12T22:15Z
|
2017-06-26 10:09:06 +08:00
|
|
|
// ST_DIGITDASHTZDIGIT
|
|
|
|
// 2006-01-02T15:04:05.999999999Z07:00
|
|
|
|
// 2006-01-02T15:04:05Z07:00
|
|
|
|
// With another dash aka time-zone at end
|
2017-07-18 05:46:18 +08:00
|
|
|
// ST_DIGITDASHTDELTA
|
|
|
|
// ST_DIGITDASHTDELTACOLON
|
|
|
|
// 2017-06-25T17:46:57.45706582-07:00
|
|
|
|
// 2017-06-25T17:46:57+04:00
|
|
|
|
// 2006-01-02T15:04:05+0000
|
2017-07-14 10:57:15 +08:00
|
|
|
switch r {
|
|
|
|
case '-', '+':
|
2017-07-12 04:40:58 +08:00
|
|
|
state = ST_DIGITDASHTDELTA
|
2017-07-14 10:57:15 +08:00
|
|
|
case 'Z':
|
2017-06-26 10:09:06 +08:00
|
|
|
state = ST_DIGITDASHTZ
|
|
|
|
}
|
|
|
|
case ST_DIGITDASHTZ:
|
|
|
|
if unicode.IsDigit(r) {
|
|
|
|
state = ST_DIGITDASHTZDIGIT
|
2014-05-07 03:32:49 +08:00
|
|
|
}
|
2017-07-18 05:46:18 +08:00
|
|
|
case ST_DIGITDASHTDELTA:
|
|
|
|
if r == ':' {
|
|
|
|
state = ST_DIGITDASHTDELTACOLON
|
|
|
|
}
|
2014-05-21 13:08:25 +08:00
|
|
|
case ST_DIGITSLASH: // starts digit then slash 02/
|
2014-07-11 06:25:23 +08:00
|
|
|
// 2014/07/10 06:55:38.156283
|
2014-05-07 12:15:43 +08:00
|
|
|
// 03/19/2012 10:11:59
|
2014-05-12 04:33:13 +08:00
|
|
|
// 04/2/2014 03:00:37
|
2014-04-21 10:56:17 +08:00
|
|
|
// 3/1/2012 10:11:59
|
2014-04-28 04:26:17 +08:00
|
|
|
// 4/8/2014 22:05
|
2014-04-21 10:56:17 +08:00
|
|
|
// 3/1/2014
|
|
|
|
// 10/13/2014
|
|
|
|
// 01/02/2006
|
2016-01-06 03:34:31 +08:00
|
|
|
// 1/2/06
|
2014-05-07 12:15:43 +08:00
|
|
|
if unicode.IsDigit(r) || r == '/' {
|
|
|
|
continue
|
2014-04-28 05:33:33 +08:00
|
|
|
}
|
2014-05-07 12:15:43 +08:00
|
|
|
switch r {
|
|
|
|
case ' ':
|
2014-05-21 13:08:25 +08:00
|
|
|
state = ST_DIGITSLASHWS
|
2014-05-07 12:15:43 +08:00
|
|
|
}
|
2014-05-21 13:08:25 +08:00
|
|
|
case ST_DIGITSLASHWS: // starts digit then slash 02/ more digits/slashes then whitespace
|
2014-07-11 06:25:23 +08:00
|
|
|
// 2014/07/10 06:55:38.156283
|
2014-05-07 12:15:43 +08:00
|
|
|
// 03/19/2012 10:11:59
|
2014-05-12 04:33:13 +08:00
|
|
|
// 04/2/2014 03:00:37
|
2014-05-07 12:15:43 +08:00
|
|
|
// 3/1/2012 10:11:59
|
|
|
|
// 4/8/2014 22:05
|
|
|
|
switch r {
|
|
|
|
case ':':
|
2014-05-21 13:08:25 +08:00
|
|
|
state = ST_DIGITSLASHWSCOLON
|
2014-05-07 12:15:43 +08:00
|
|
|
}
|
2014-05-21 13:08:25 +08:00
|
|
|
case ST_DIGITSLASHWSCOLON: // starts digit then slash 02/ more digits/slashes then whitespace
|
2014-07-11 06:25:23 +08:00
|
|
|
// 2014/07/10 06:55:38.156283
|
2014-05-07 12:15:43 +08:00
|
|
|
// 03/19/2012 10:11:59
|
2014-05-21 13:08:25 +08:00
|
|
|
// 04/2/2014 03:00:37
|
2014-05-07 12:15:43 +08:00
|
|
|
// 3/1/2012 10:11:59
|
|
|
|
// 4/8/2014 22:05
|
2015-10-01 03:09:08 +08:00
|
|
|
// 3/1/2012 10:11:59 AM
|
2014-05-07 12:15:43 +08:00
|
|
|
switch r {
|
|
|
|
case ':':
|
2014-05-21 13:08:25 +08:00
|
|
|
state = ST_DIGITSLASHWSCOLONCOLON
|
2017-02-14 10:39:03 +08:00
|
|
|
case 'A', 'P':
|
|
|
|
state = ST_DIGITSLASHWSCOLONAMPM
|
2014-05-07 12:15:43 +08:00
|
|
|
}
|
2015-10-01 03:09:08 +08:00
|
|
|
case ST_DIGITSLASHWSCOLONCOLON: // starts digit then slash 02/ more digits/slashes then whitespace
|
|
|
|
// 2014/07/10 06:55:38.156283
|
|
|
|
// 03/19/2012 10:11:59
|
|
|
|
// 04/2/2014 03:00:37
|
|
|
|
// 3/1/2012 10:11:59
|
|
|
|
// 4/8/2014 22:05
|
|
|
|
// 3/1/2012 10:11:59 AM
|
|
|
|
switch r {
|
2017-02-14 10:39:03 +08:00
|
|
|
case 'A', 'P':
|
|
|
|
state = ST_DIGITSLASHWSCOLONCOLONAMPM
|
2015-10-01 03:09:08 +08:00
|
|
|
}
|
2017-02-06 02:04:03 +08:00
|
|
|
case ST_DIGITALPHA:
|
|
|
|
// 12 Feb 2006, 19:17
|
|
|
|
// 12 Feb 2006, 19:17:22
|
|
|
|
switch {
|
|
|
|
case len(datestr) == len("02 Jan 2006, 15:04"):
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("02 Jan 2006, 15:04", datestr, loc)
|
2017-02-06 02:04:03 +08:00
|
|
|
case len(datestr) == len("02 Jan 2006, 15:04:05"):
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("02 Jan 2006, 15:04:05", datestr, loc)
|
2017-02-06 02:04:03 +08:00
|
|
|
}
|
2014-05-21 13:08:25 +08:00
|
|
|
case ST_ALPHA: // starts alpha
|
2017-07-14 00:11:41 +08:00
|
|
|
// ST_ALPHAWS
|
|
|
|
// Mon Jan _2 15:04:05 2006
|
|
|
|
// Mon Jan _2 15:04:05 MST 2006
|
|
|
|
// Mon Jan 02 15:04:05 -0700 2006
|
|
|
|
// Mon Aug 10 15:44:11 UTC+0100 2015
|
|
|
|
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
|
|
|
|
// ST_ALPHAWSDIGITCOMMA
|
|
|
|
// May 8, 2009 5:57:51 PM
|
|
|
|
//
|
|
|
|
// ST_WEEKDAYCOMMA
|
|
|
|
// Monday, 02-Jan-06 15:04:05 MST
|
|
|
|
// ST_WEEKDAYCOMMADELTA
|
|
|
|
// Monday, 02 Jan 2006 15:04:05 -0700
|
|
|
|
// Monday, 02 Jan 2006 15:04:05 +0100
|
|
|
|
// ST_WEEKDAYABBREVCOMMA
|
|
|
|
// Mon, 02-Jan-06 15:04:05 MST
|
|
|
|
// Mon, 02 Jan 2006 15:04:05 MST
|
|
|
|
// ST_WEEKDAYABBREVCOMMADELTA
|
|
|
|
// Mon, 02 Jan 2006 15:04:05 -0700
|
|
|
|
// Thu, 13 Jul 2017 08:58:40 +0100
|
2017-07-14 22:02:22 +08:00
|
|
|
// ST_WEEKDAYABBREVCOMMADELTAZONE
|
|
|
|
// Tue, 11 Jul 2017 16:28:13 +0200 (CEST)
|
2014-05-07 12:15:43 +08:00
|
|
|
switch {
|
2017-07-14 00:11:41 +08:00
|
|
|
case unicode.IsLetter(r):
|
|
|
|
continue
|
2014-05-07 12:15:43 +08:00
|
|
|
case r == ' ':
|
2014-05-21 13:08:25 +08:00
|
|
|
state = ST_ALPHAWS
|
2014-05-21 13:53:52 +08:00
|
|
|
case r == ',':
|
2017-07-14 00:11:41 +08:00
|
|
|
if i == 3 {
|
2016-05-04 04:55:11 +08:00
|
|
|
state = ST_WEEKDAYABBREVCOMMA
|
2017-07-14 00:11:41 +08:00
|
|
|
} else {
|
|
|
|
state = ST_WEEKDAYCOMMA
|
2016-05-04 04:55:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case ST_WEEKDAYCOMMA: // Starts alpha then comma
|
2017-07-14 00:11:41 +08:00
|
|
|
// Mon, 02-Jan-06 15:04:05 MST
|
|
|
|
// Mon, 02 Jan 2006 15:04:05 MST
|
|
|
|
// ST_WEEKDAYCOMMADELTA
|
|
|
|
// Monday, 02 Jan 2006 15:04:05 -0700
|
|
|
|
// Monday, 02 Jan 2006 15:04:05 +0100
|
2016-05-04 04:55:11 +08:00
|
|
|
switch {
|
|
|
|
case r == '-':
|
|
|
|
if i < 15 {
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("Monday, 02-Jan-06 15:04:05 MST", datestr, loc)
|
2016-05-04 04:55:11 +08:00
|
|
|
} else {
|
2017-07-14 00:11:41 +08:00
|
|
|
state = ST_WEEKDAYCOMMADELTA
|
2016-05-04 04:55:11 +08:00
|
|
|
}
|
2017-07-14 00:11:41 +08:00
|
|
|
case r == '+':
|
2017-07-14 10:57:15 +08:00
|
|
|
state = ST_WEEKDAYCOMMADELTA
|
2016-05-04 04:55:11 +08:00
|
|
|
}
|
|
|
|
case ST_WEEKDAYABBREVCOMMA: // Starts alpha then comma
|
|
|
|
// Mon, 02-Jan-06 15:04:05 MST
|
2017-07-14 00:11:41 +08:00
|
|
|
// Mon, 02 Jan 2006 15:04:05 MST
|
|
|
|
// ST_WEEKDAYABBREVCOMMADELTA
|
|
|
|
// Mon, 02 Jan 2006 15:04:05 -0700
|
|
|
|
// Thu, 13 Jul 2017 08:58:40 +0100
|
2017-07-14 22:02:22 +08:00
|
|
|
// ST_WEEKDAYABBREVCOMMADELTAZONE
|
|
|
|
// Tue, 11 Jul 2017 16:28:13 +0200 (CEST)
|
2016-05-04 04:55:11 +08:00
|
|
|
switch {
|
|
|
|
case r == '-':
|
|
|
|
if i < 15 {
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("Mon, 02-Jan-06 15:04:05 MST", datestr, loc)
|
2016-05-04 04:55:11 +08:00
|
|
|
} else {
|
2017-07-14 00:11:41 +08:00
|
|
|
state = ST_WEEKDAYABBREVCOMMADELTA
|
2016-05-04 04:55:11 +08:00
|
|
|
}
|
2017-07-14 00:11:41 +08:00
|
|
|
case r == '+':
|
|
|
|
state = ST_WEEKDAYABBREVCOMMADELTA
|
2014-05-07 12:15:43 +08:00
|
|
|
}
|
2016-05-04 04:55:11 +08:00
|
|
|
|
2017-07-14 22:02:22 +08:00
|
|
|
case ST_WEEKDAYABBREVCOMMADELTA:
|
|
|
|
// ST_WEEKDAYABBREVCOMMADELTA
|
|
|
|
// Mon, 02 Jan 2006 15:04:05 -0700
|
|
|
|
// Thu, 13 Jul 2017 08:58:40 +0100
|
|
|
|
// ST_WEEKDAYABBREVCOMMADELTAZONE
|
|
|
|
// Tue, 11 Jul 2017 16:28:13 +0200 (CEST)
|
|
|
|
if r == '(' {
|
|
|
|
state = ST_WEEKDAYABBREVCOMMADELTAZONE
|
|
|
|
}
|
|
|
|
|
2014-05-21 13:08:25 +08:00
|
|
|
case ST_ALPHAWS: // Starts alpha then whitespace
|
2015-07-04 01:27:48 +08:00
|
|
|
// Mon Jan _2 15:04:05 2006
|
|
|
|
// Mon Jan _2 15:04:05 MST 2006
|
|
|
|
// Mon Jan 02 15:04:05 -0700 2006
|
|
|
|
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
|
2015-08-11 01:54:01 +08:00
|
|
|
// Mon Aug 10 15:44:11 UTC+0100 2015
|
2014-05-07 12:15:43 +08:00
|
|
|
switch {
|
|
|
|
case unicode.IsLetter(r):
|
2014-05-21 13:53:52 +08:00
|
|
|
state = ST_ALPHAWSALPHA
|
2017-07-14 00:11:41 +08:00
|
|
|
case unicode.IsDigit(r):
|
|
|
|
state = ST_ALPHAWSDIGITCOMMA
|
2014-05-21 13:53:52 +08:00
|
|
|
}
|
2016-05-04 04:02:30 +08:00
|
|
|
|
2017-07-14 00:11:41 +08:00
|
|
|
case ST_ALPHAWSDIGITCOMMA: // Starts Alpha, whitespace, digit, comma
|
2014-05-07 12:15:43 +08:00
|
|
|
// May 8, 2009 5:57:51 PM
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("Jan 2, 2006 3:04:05 PM", datestr, loc)
|
2017-07-12 23:22:34 +08:00
|
|
|
|
2017-06-27 08:45:07 +08:00
|
|
|
case ST_ALPHAWSALPHA: // Alpha, whitespace, alpha
|
2015-07-04 01:27:48 +08:00
|
|
|
// Mon Jan _2 15:04:05 2006
|
2017-06-27 08:45:07 +08:00
|
|
|
// Mon Jan 02 15:04:05 -0700 2006
|
2015-07-04 01:27:48 +08:00
|
|
|
// Mon Jan _2 15:04:05 MST 2006
|
2017-06-27 08:45:07 +08:00
|
|
|
// Mon Aug 10 15:44:11 UTC+0100 2015
|
|
|
|
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
|
|
|
|
if r == ':' {
|
|
|
|
state = ST_ALPHAWSALPHACOLON
|
|
|
|
}
|
|
|
|
case ST_ALPHAWSALPHACOLON: // Alpha, whitespace, alpha, :
|
|
|
|
// Mon Jan _2 15:04:05 2006
|
2015-07-04 01:27:48 +08:00
|
|
|
// Mon Jan 02 15:04:05 -0700 2006
|
2017-06-27 08:45:07 +08:00
|
|
|
// Mon Jan _2 15:04:05 MST 2006
|
2015-08-11 01:54:01 +08:00
|
|
|
// Mon Aug 10 15:44:11 UTC+0100 2015
|
2017-06-27 08:45:07 +08:00
|
|
|
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
|
|
|
|
if unicode.IsLetter(r) {
|
|
|
|
state = ST_ALPHAWSALPHACOLONALPHA
|
|
|
|
} else if r == '-' || r == '+' {
|
|
|
|
state = ST_ALPHAWSALPHACOLONOFFSET
|
|
|
|
}
|
|
|
|
case ST_ALPHAWSALPHACOLONALPHA: // Alpha, whitespace, alpha, :, alpha
|
|
|
|
// Mon Jan _2 15:04:05 MST 2006
|
|
|
|
// Mon Aug 10 15:44:11 UTC+0100 2015
|
|
|
|
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
|
|
|
|
if r == '+' {
|
|
|
|
state = ST_ALPHAWSALPHACOLONALPHAOFFSET
|
|
|
|
}
|
|
|
|
case ST_ALPHAWSALPHACOLONALPHAOFFSET: // Alpha, whitespace, alpha, : , alpha, offset, ?
|
|
|
|
// Mon Aug 10 15:44:11 UTC+0100 2015
|
|
|
|
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
|
|
|
|
if unicode.IsLetter(r) {
|
|
|
|
state = ST_ALPHAWSALPHACOLONALPHAOFFSETALPHA
|
2014-05-21 13:53:52 +08:00
|
|
|
}
|
2014-05-07 12:15:43 +08:00
|
|
|
default:
|
|
|
|
break iterRunes
|
2014-04-21 10:56:17 +08:00
|
|
|
}
|
2014-05-07 12:15:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch state {
|
2014-05-21 13:08:25 +08:00
|
|
|
case ST_DIGIT:
|
2014-05-07 12:15:43 +08:00
|
|
|
// unixy timestamps ish
|
2017-07-14 10:57:15 +08:00
|
|
|
// 1499979655583057426 nanoseconds
|
|
|
|
// 1499979795437000 micro-seconds
|
|
|
|
// 1499979795437 milliseconds
|
2015-08-19 06:50:04 +08:00
|
|
|
// 1384216367189
|
|
|
|
// 1332151919 seconds
|
|
|
|
// 20140601 yyyymmdd
|
2016-03-02 05:25:00 +08:00
|
|
|
// 2014 yyyy
|
2017-07-14 10:57:15 +08:00
|
|
|
if len(datestr) > len("1499979795437000") {
|
2014-04-26 07:59:10 +08:00
|
|
|
if nanoSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
|
|
|
|
return time.Unix(0, nanoSecs), nil
|
|
|
|
}
|
2017-07-14 10:57:15 +08:00
|
|
|
} else if len(datestr) > len("1499979795437") {
|
2014-05-07 12:15:43 +08:00
|
|
|
if microSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
|
|
|
|
return time.Unix(0, microSecs*1000), nil
|
|
|
|
}
|
2017-07-14 10:57:15 +08:00
|
|
|
} else if len(datestr) > len("1332151919") {
|
2014-10-08 09:30:17 +08:00
|
|
|
if miliSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
|
|
|
|
return time.Unix(0, miliSecs*1000*1000), nil
|
|
|
|
}
|
2015-08-19 06:50:04 +08:00
|
|
|
} else if len(datestr) == len("20140601") {
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("20060102", datestr, loc)
|
2016-03-02 05:25:00 +08:00
|
|
|
} else if len(datestr) == len("2014") {
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006", datestr, loc)
|
2014-04-26 07:59:10 +08:00
|
|
|
} else {
|
|
|
|
if secs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
|
|
|
|
return time.Unix(secs, 0), nil
|
|
|
|
}
|
|
|
|
}
|
2014-05-21 13:08:25 +08:00
|
|
|
case ST_DIGITDASH: // starts digit then dash 02-
|
2014-05-07 12:15:43 +08:00
|
|
|
// 2006-01-02
|
2016-03-02 05:25:00 +08:00
|
|
|
// 2006-01
|
2014-05-07 12:15:43 +08:00
|
|
|
if len(datestr) == len("2014-04-26") {
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01-02", datestr, loc)
|
2016-03-02 05:25:00 +08:00
|
|
|
} else if len(datestr) == len("2014-04") {
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01", datestr, loc)
|
2014-05-07 12:15:43 +08:00
|
|
|
}
|
2017-07-27 07:55:21 +08:00
|
|
|
case ST_DIGITDASHALPHA:
|
|
|
|
// 2013-Feb-03
|
|
|
|
return parse("2006-Jan-02", datestr, loc)
|
|
|
|
|
2017-07-12 04:40:58 +08:00
|
|
|
case ST_DIGITDASHTDELTA:
|
2017-07-18 05:46:18 +08:00
|
|
|
// 2006-01-02T15:04:05+0000
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01-02T15:04:05-0700", datestr, loc)
|
2017-07-18 05:46:18 +08:00
|
|
|
|
|
|
|
case ST_DIGITDASHTDELTACOLON:
|
2017-07-12 04:40:58 +08:00
|
|
|
// With another +/- time-zone at end
|
2017-07-12 02:00:40 +08:00
|
|
|
// 2006-01-02T15:04:05.999999999+07:00
|
2017-07-12 04:40:58 +08:00
|
|
|
// 2006-01-02T15:04:05.999999999-07:00
|
2017-07-12 02:00:40 +08:00
|
|
|
// 2006-01-02T15:04:05.999999+07:00
|
2017-07-12 04:40:58 +08:00
|
|
|
// 2006-01-02T15:04:05.999999-07:00
|
2017-07-12 02:00:40 +08:00
|
|
|
// 2006-01-02T15:04:05.999+07:00
|
2017-07-12 04:40:58 +08:00
|
|
|
// 2006-01-02T15:04:05.999-07:00
|
2017-07-12 02:00:40 +08:00
|
|
|
// 2006-01-02T15:04:05+07:00
|
2017-07-12 04:40:58 +08:00
|
|
|
// 2006-01-02T15:04:05-07:00
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01-02T15:04:05-07:00", datestr, loc)
|
2017-07-14 10:57:15 +08:00
|
|
|
|
2017-06-26 10:09:06 +08:00
|
|
|
case ST_DIGITDASHT: // starts digit then dash 02- then T
|
|
|
|
// 2006-01-02T15:04:05.999999
|
|
|
|
// 2006-01-02T15:04:05.999999
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01-02T15:04:05", datestr, loc)
|
2017-07-14 10:57:15 +08:00
|
|
|
|
|
|
|
case ST_DIGITDASHTZDIGIT:
|
|
|
|
// With a time-zone at end after Z
|
|
|
|
// 2006-01-02T15:04:05.999999999Z07:00
|
|
|
|
// 2006-01-02T15:04:05Z07:00
|
|
|
|
// RFC3339 = "2006-01-02T15:04:05Z07:00"
|
|
|
|
// RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
|
|
|
|
return time.Time{}, fmt.Errorf("RFC339 Dates may not contain both Z & Offset for %q see https://github.com/golang/go/issues/5294", datestr)
|
|
|
|
|
2017-06-26 10:09:06 +08:00
|
|
|
case ST_DIGITDASHTZ: // starts digit then dash 02- then T Then Z
|
|
|
|
// 2006-01-02T15:04:05.999999999Z
|
|
|
|
// 2006-01-02T15:04:05.99999999Z
|
|
|
|
// 2006-01-02T15:04:05.9999999Z
|
|
|
|
// 2006-01-02T15:04:05.999999Z
|
|
|
|
// 2006-01-02T15:04:05.99999Z
|
|
|
|
// 2006-01-02T15:04:05.9999Z
|
|
|
|
// 2006-01-02T15:04:05.999Z
|
|
|
|
// 2006-01-02T15:04:05.99Z
|
2017-07-12 23:22:34 +08:00
|
|
|
// 2009-08-12T22:15Z -- No seconds/milliseconds
|
|
|
|
switch len(datestr) {
|
|
|
|
case len("2009-08-12T22:15Z"):
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01-02T15:04Z", datestr, loc)
|
2017-07-12 23:22:34 +08:00
|
|
|
default:
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01-02T15:04:05Z", datestr, loc)
|
2017-06-26 10:09:06 +08:00
|
|
|
}
|
2014-05-21 13:08:25 +08:00
|
|
|
case ST_DIGITDASHWS: // starts digit then dash 02- then whitespace 1 << 2 << 5 + 3
|
2014-05-12 09:22:16 +08:00
|
|
|
// 2013-04-01 22:43:22
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01-02 15:04:05", datestr, loc)
|
2017-07-21 09:41:09 +08:00
|
|
|
|
|
|
|
case ST_DIGITDASHWSWSOFFSET:
|
2017-07-12 23:22:34 +08:00
|
|
|
// 2006-01-02 15:04:05 -0700
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01-02 15:04:05 -0700", datestr, loc)
|
2017-07-21 09:41:09 +08:00
|
|
|
|
|
|
|
case ST_DIGITDASHWSWSOFFSETCOLON:
|
2017-07-12 23:22:34 +08:00
|
|
|
// 2006-01-02 15:04:05 -07:00
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01-02 15:04:05 -07:00", datestr, loc)
|
2017-07-21 09:41:09 +08:00
|
|
|
|
|
|
|
case ST_DIGITDASHWSWSOFFSETALPHA:
|
|
|
|
// 2015-02-18 00:12:00 +0000 UTC
|
2017-07-27 07:42:12 +08:00
|
|
|
t, err := parse("2006-01-02 15:04:05 -0700 UTC", datestr, loc)
|
2017-07-21 09:41:09 +08:00
|
|
|
if err == nil {
|
|
|
|
return t, nil
|
2014-05-07 12:15:43 +08:00
|
|
|
}
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01-02 15:04:05 +0000 GMT", datestr, loc)
|
2017-07-21 09:41:09 +08:00
|
|
|
|
|
|
|
case ST_DIGITDASHWSWSOFFSETCOLONALPHA:
|
|
|
|
// 2015-02-18 00:12:00 +00:00 UTC
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01-02 15:04:05 -07:00 UTC", datestr, loc)
|
2017-07-21 09:41:09 +08:00
|
|
|
|
|
|
|
case ST_DIGITDASHWSOFFSET:
|
|
|
|
// 2017-07-19 03:21:51+00:00
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01-02 15:04:05-07:00", datestr, loc)
|
2017-07-21 09:41:09 +08:00
|
|
|
|
|
|
|
case ST_DIGITDASHWSWSALPHA:
|
2014-12-03 06:57:00 +08:00
|
|
|
// 2014-12-16 06:20:00 UTC
|
2017-07-27 07:42:12 +08:00
|
|
|
t, err := parse("2006-01-02 15:04:05 UTC", datestr, loc)
|
2017-07-21 09:41:09 +08:00
|
|
|
if err == nil {
|
|
|
|
return t, nil
|
|
|
|
}
|
2017-07-27 07:42:12 +08:00
|
|
|
t, err = parse("2006-01-02 15:04:05 GMT", datestr, loc)
|
2017-07-21 09:41:09 +08:00
|
|
|
if err == nil {
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
if len(datestr) > len("2006-01-02 03:04:05") {
|
2017-07-27 07:42:12 +08:00
|
|
|
t, err = parse("2006-01-02 03:04:05", datestr[:len("2006-01-02 03:04:05")], loc)
|
2017-07-12 23:22:34 +08:00
|
|
|
if err == nil {
|
|
|
|
return t, nil
|
2015-04-17 02:37:50 +08:00
|
|
|
}
|
2017-04-12 06:30:42 +08:00
|
|
|
}
|
2017-07-21 09:41:09 +08:00
|
|
|
|
2017-04-12 06:30:42 +08:00
|
|
|
case ST_DIGITDASHWSDOT:
|
|
|
|
// 2012-08-03 18:31:59.257000000
|
|
|
|
// 2014-04-26 17:24:37.3186369
|
|
|
|
// 2017-01-27 00:07:31.945167
|
|
|
|
// 2016-03-14 00:00:00.000
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01-02 15:04:05", datestr, loc)
|
2017-07-12 23:22:34 +08:00
|
|
|
|
2017-04-12 06:30:42 +08:00
|
|
|
case ST_DIGITDASHWSDOTALPHA:
|
|
|
|
// 2012-08-03 18:31:59.257000000 UTC
|
|
|
|
// 2014-04-26 17:24:37.3186369 UTC
|
|
|
|
// 2017-01-27 00:07:31.945167 UTC
|
|
|
|
// 2016-03-14 00:00:00.000 UTC
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01-02 15:04:05 UTC", datestr, loc)
|
2017-07-12 23:22:34 +08:00
|
|
|
|
2017-07-21 09:41:09 +08:00
|
|
|
case ST_DIGITDASHWSDOTOFFSET:
|
2017-04-12 06:30:42 +08:00
|
|
|
// 2012-08-03 18:31:59.257000000 +0000
|
|
|
|
// 2014-04-26 17:24:37.3186369 +0000
|
|
|
|
// 2017-01-27 00:07:31.945167 +0000
|
|
|
|
// 2016-03-14 00:00:00.000 +0000
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01-02 15:04:05 -0700", datestr, loc)
|
2017-07-12 23:22:34 +08:00
|
|
|
|
2017-07-21 09:41:09 +08:00
|
|
|
case ST_DIGITDASHWSDOTOFFSETALPHA:
|
2017-04-12 06:30:42 +08:00
|
|
|
// 2012-08-03 18:31:59.257000000 +0000 UTC
|
|
|
|
// 2014-04-26 17:24:37.3186369 +0000 UTC
|
|
|
|
// 2017-01-27 00:07:31.945167 +0000 UTC
|
|
|
|
// 2016-03-14 00:00:00.000 +0000 UTC
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006-01-02 15:04:05 -0700 UTC", datestr, loc)
|
2017-07-12 23:22:34 +08:00
|
|
|
|
2017-06-27 08:45:07 +08:00
|
|
|
case ST_ALPHAWSALPHACOLON:
|
|
|
|
// Mon Jan _2 15:04:05 2006
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse(time.ANSIC, datestr, loc)
|
2017-07-12 23:22:34 +08:00
|
|
|
|
2017-06-27 08:45:07 +08:00
|
|
|
case ST_ALPHAWSALPHACOLONOFFSET:
|
|
|
|
// Mon Jan 02 15:04:05 -0700 2006
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse(time.RubyDate, datestr, loc)
|
2017-07-12 23:22:34 +08:00
|
|
|
|
2017-06-27 08:45:07 +08:00
|
|
|
case ST_ALPHAWSALPHACOLONALPHA:
|
|
|
|
// Mon Jan _2 15:04:05 MST 2006
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse(time.UnixDate, datestr, loc)
|
2017-07-12 23:22:34 +08:00
|
|
|
|
2017-06-27 08:45:07 +08:00
|
|
|
case ST_ALPHAWSALPHACOLONALPHAOFFSET:
|
|
|
|
// Mon Aug 10 15:44:11 UTC+0100 2015
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("Mon Jan 02 15:04:05 MST-0700 2006", datestr, loc)
|
2017-07-12 23:22:34 +08:00
|
|
|
|
2017-06-27 08:45:07 +08:00
|
|
|
case ST_ALPHAWSALPHACOLONALPHAOFFSETALPHA:
|
|
|
|
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
|
|
|
|
if len(datestr) > len("Mon Jan 02 2006 15:04:05 MST-0700") {
|
|
|
|
// What effing time stamp is this?
|
|
|
|
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
|
|
|
|
dateTmp := datestr[:33]
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("Mon Jan 02 2006 15:04:05 MST-0700", dateTmp, loc)
|
2017-06-27 08:45:07 +08:00
|
|
|
}
|
2014-05-21 13:08:25 +08:00
|
|
|
case ST_DIGITSLASH: // starts digit then slash 02/ (but nothing else)
|
2014-05-07 12:15:43 +08:00
|
|
|
// 3/1/2014
|
|
|
|
// 10/13/2014
|
|
|
|
// 01/02/2006
|
2014-07-11 06:25:23 +08:00
|
|
|
// 2014/10/13
|
|
|
|
if firstSlash == 4 {
|
|
|
|
if len(datestr) == len("2006/01/02") {
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006/01/02", datestr, loc)
|
2014-05-07 12:15:43 +08:00
|
|
|
} else {
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("2006/1/2", datestr, loc)
|
2014-05-07 12:15:43 +08:00
|
|
|
}
|
|
|
|
} else {
|
2014-12-16 03:20:40 +08:00
|
|
|
for _, parseFormat := range shortDates {
|
2017-07-27 07:42:12 +08:00
|
|
|
if t, err := parse(parseFormat, datestr, loc); err == nil {
|
2014-07-11 06:25:23 +08:00
|
|
|
return t, nil
|
|
|
|
}
|
2014-05-07 12:15:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-14 10:39:03 +08:00
|
|
|
case ST_DIGITSLASHWSCOLON: // starts digit then slash 02/ more digits/slashes then whitespace
|
2014-05-07 12:15:43 +08:00
|
|
|
// 4/8/2014 22:05
|
2014-07-11 06:25:23 +08:00
|
|
|
// 04/08/2014 22:05
|
|
|
|
// 2014/4/8 22:05
|
|
|
|
// 2014/04/08 22:05
|
2017-02-14 08:49:48 +08:00
|
|
|
|
2014-07-11 06:25:23 +08:00
|
|
|
if firstSlash == 4 {
|
2017-07-14 10:57:15 +08:00
|
|
|
for _, layout := range []string{"2006/01/02 15:04", "2006/1/2 15:04", "2006/01/2 15:04", "2006/1/02 15:04"} {
|
2017-07-27 07:42:12 +08:00
|
|
|
if t, err := parse(layout, datestr, loc); err == nil {
|
2017-07-14 10:57:15 +08:00
|
|
|
return t, nil
|
2014-05-12 04:33:13 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-07 12:15:43 +08:00
|
|
|
} else {
|
2017-07-14 10:57:15 +08:00
|
|
|
for _, layout := range []string{"01/02/2006 15:04", "01/2/2006 15:04", "1/02/2006 15:04", "1/2/2006 15:04"} {
|
2017-07-27 07:42:12 +08:00
|
|
|
if t, err := parse(layout, datestr, loc); err == nil {
|
2017-07-14 10:57:15 +08:00
|
|
|
return t, nil
|
2014-07-11 06:25:23 +08:00
|
|
|
}
|
2014-05-07 12:15:43 +08:00
|
|
|
}
|
|
|
|
}
|
2017-02-14 10:39:03 +08:00
|
|
|
|
|
|
|
case ST_DIGITSLASHWSCOLONAMPM: // starts digit then slash 02/ more digits/slashes then whitespace
|
|
|
|
// 4/8/2014 22:05 PM
|
|
|
|
// 04/08/2014 22:05 PM
|
2017-07-14 10:57:15 +08:00
|
|
|
// 04/08/2014 1:05 PM
|
2017-02-14 10:39:03 +08:00
|
|
|
// 2014/4/8 22:05 PM
|
|
|
|
// 2014/04/08 22:05 PM
|
|
|
|
|
|
|
|
if firstSlash == 4 {
|
2017-07-14 10:57:15 +08:00
|
|
|
for _, layout := range []string{"2006/01/02 03:04 PM", "2006/01/2 03:04 PM", "2006/1/02 03:04 PM", "2006/1/2 03:04 PM",
|
|
|
|
"2006/01/02 3:04 PM", "2006/01/2 3:04 PM", "2006/1/02 3:04 PM", "2006/1/2 3:04 PM"} {
|
2017-07-27 07:42:12 +08:00
|
|
|
if t, err := parse(layout, datestr, loc); err == nil {
|
2017-07-14 10:57:15 +08:00
|
|
|
return t, nil
|
2017-02-14 10:39:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-07-14 10:57:15 +08:00
|
|
|
for _, layout := range []string{"01/02/2006 03:04 PM", "01/2/2006 03:04 PM", "1/02/2006 03:04 PM", "1/2/2006 03:04 PM",
|
|
|
|
"01/02/2006 3:04 PM", "01/2/2006 3:04 PM", "1/02/2006 3:04 PM", "1/2/2006 3:04 PM"} {
|
2017-07-27 07:42:12 +08:00
|
|
|
if t, err := parse(layout, datestr, loc); err == nil {
|
2017-07-14 10:57:15 +08:00
|
|
|
return t, nil
|
2017-02-14 10:39:03 +08:00
|
|
|
}
|
2017-07-14 10:57:15 +08:00
|
|
|
|
2017-02-14 10:39:03 +08:00
|
|
|
}
|
|
|
|
}
|
2017-02-14 08:49:48 +08:00
|
|
|
|
2017-02-14 10:39:03 +08:00
|
|
|
case ST_DIGITSLASHWSCOLONCOLON: // starts digit then slash 02/ more digits/slashes then whitespace double colons
|
2014-07-11 06:25:23 +08:00
|
|
|
// 2014/07/10 06:55:38.156283
|
2014-05-07 12:15:43 +08:00
|
|
|
// 03/19/2012 10:11:59
|
|
|
|
// 3/1/2012 10:11:59
|
2014-05-12 04:33:13 +08:00
|
|
|
// 03/1/2012 10:11:59
|
|
|
|
// 3/01/2012 10:11:59
|
2014-07-11 06:25:23 +08:00
|
|
|
if firstSlash == 4 {
|
2017-07-14 10:57:15 +08:00
|
|
|
for _, layout := range []string{"2006/01/02 15:04:05", "2006/1/02 15:04:05", "2006/01/2 15:04:05", "2006/1/2 15:04:05"} {
|
2017-07-27 07:42:12 +08:00
|
|
|
if t, err := parse(layout, datestr, loc); err == nil {
|
2017-07-14 10:57:15 +08:00
|
|
|
return t, nil
|
2014-05-12 04:33:13 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-07 12:15:43 +08:00
|
|
|
} else {
|
2017-07-14 10:57:15 +08:00
|
|
|
for _, layout := range []string{"01/02/2006 15:04:05", "1/02/2006 15:04:05", "01/2/2006 15:04:05", "1/2/2006 15:04:05"} {
|
2017-07-27 07:42:12 +08:00
|
|
|
if t, err := parse(layout, datestr, loc); err == nil {
|
2017-07-14 10:57:15 +08:00
|
|
|
return t, nil
|
2014-07-11 06:25:23 +08:00
|
|
|
}
|
2014-05-07 12:15:43 +08:00
|
|
|
}
|
|
|
|
}
|
2017-02-14 10:39:03 +08:00
|
|
|
|
|
|
|
case ST_DIGITSLASHWSCOLONCOLONAMPM: // starts digit then slash 02/ more digits/slashes then whitespace double colons
|
|
|
|
// 2014/07/10 06:55:38.156283 PM
|
|
|
|
// 03/19/2012 10:11:59 PM
|
|
|
|
// 3/1/2012 10:11:59 PM
|
|
|
|
// 03/1/2012 10:11:59 PM
|
|
|
|
// 3/01/2012 10:11:59 PM
|
|
|
|
|
|
|
|
if firstSlash == 4 {
|
2017-07-14 10:57:15 +08:00
|
|
|
for _, layout := range []string{"2006/01/02 03:04:05 PM", "2006/1/02 03:04:05 PM", "2006/01/2 03:04:05 PM", "2006/1/2 03:04:05 PM",
|
|
|
|
"2006/01/02 3:04:05 PM", "2006/1/02 3:04:05 PM", "2006/01/2 3:04:05 PM", "2006/1/2 3:04:05 PM"} {
|
2017-07-27 07:42:12 +08:00
|
|
|
if t, err := parse(layout, datestr, loc); err == nil {
|
2017-07-14 10:57:15 +08:00
|
|
|
return t, nil
|
2017-02-14 10:39:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-07-14 10:57:15 +08:00
|
|
|
for _, layout := range []string{"01/02/2006 03:04:05 PM", "1/02/2006 03:04:05 PM", "01/2/2006 03:04:05 PM", "1/2/2006 03:04:05 PM"} {
|
2017-07-27 07:42:12 +08:00
|
|
|
if t, err := parse(layout, datestr, loc); err == nil {
|
2017-07-14 10:57:15 +08:00
|
|
|
return t, nil
|
2017-02-14 10:39:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-14 08:49:48 +08:00
|
|
|
|
2017-07-14 00:11:41 +08:00
|
|
|
case ST_WEEKDAYCOMMADELTA:
|
|
|
|
// Monday, 02 Jan 2006 15:04:05 -0700
|
|
|
|
// Monday, 02 Jan 2006 15:04:05 +0100
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("Monday, 02 Jan 2006 15:04:05 -0700", datestr, loc)
|
2016-05-04 04:55:11 +08:00
|
|
|
case ST_WEEKDAYABBREVCOMMA: // Starts alpha then comma
|
2017-07-14 00:11:41 +08:00
|
|
|
// Mon, 02-Jan-06 15:04:05 MST
|
2016-05-04 04:55:11 +08:00
|
|
|
// Mon, 02 Jan 2006 15:04:05 MST
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("Mon, 02 Jan 2006 15:04:05 MST", datestr, loc)
|
2017-07-14 00:11:41 +08:00
|
|
|
case ST_WEEKDAYABBREVCOMMADELTA:
|
|
|
|
// Mon, 02 Jan 2006 15:04:05 -0700
|
|
|
|
// Thu, 13 Jul 2017 08:58:40 +0100
|
2017-07-12 23:22:34 +08:00
|
|
|
// RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("Mon, 02 Jan 2006 15:04:05 -0700", datestr, loc)
|
2017-07-14 22:02:22 +08:00
|
|
|
case ST_WEEKDAYABBREVCOMMADELTAZONE:
|
|
|
|
// Tue, 11 Jul 2017 16:28:13 +0200 (CEST)
|
2017-07-27 07:42:12 +08:00
|
|
|
return parse("Mon, 02 Jan 2006 15:04:05 -0700 (CEST)", datestr, loc)
|
2014-04-21 10:56:17 +08:00
|
|
|
}
|
|
|
|
|
2014-06-16 02:18:20 +08:00
|
|
|
return time.Time{}, fmt.Errorf("Could not find date format for %s", datestr)
|
2014-04-21 10:56:17 +08:00
|
|
|
}
|