Parse IN location

This commit is contained in:
Aaron Raddon 2017-07-14 08:00:01 -07:00
parent 73f547e7b6
commit 45c3060e94
3 changed files with 102 additions and 87 deletions

View File

@ -2,7 +2,7 @@ Go Date Parser
---------------------------
Parse Any date format without knowing format in advance. Uses
a Scan/Lex based approach to minimize shotgun based parse attempts.
a byte scan state-machine based approach to minimize shotgun based parse attempts.
See [bench_test.go](https://github.com/araddon/dateparse/blob/master/bench_test.go) for performance comparison.

View File

@ -63,7 +63,7 @@ var (
shortDates = []string{"01/02/2006", "1/2/2006", "06/01/02", "01/02/06", "1/2/06"}
)
// Parse a date, and panic if it can't be parsed
// MustParse Parse a date, and panic if it can't be parsed
func MustParse(datestr string) time.Time {
t, err := ParseAny(datestr)
if err != nil {
@ -72,8 +72,17 @@ func MustParse(datestr string) time.Time {
return t
}
// Given an unknown date format, detect the type, parse, return time
// ParseAny Given an unknown format date string parse and return time
func ParseAny(datestr string) (time.Time, error) {
if time.Local == nil {
return ParseIn(datestr, time.UTC)
}
return ParseIn(datestr, time.Local)
}
// ParseIn Given an unknown format date string parse and return time
// using the given location as baseline
func ParseIn(datestr string, loc *time.Location) (time.Time, error) {
state := ST_START
@ -140,16 +149,16 @@ iterRunes:
switch r {
case 'A', 'P':
if len(datestr) == len("2014-04-26 03:24:37 PM") {
return time.Parse("2006-01-02 03:04:05 PM", datestr)
return time.ParseInLocation("2006-01-02 03:04:05 PM", datestr, loc)
}
case ',':
if len(datestr) == len("2014-05-11 08:20:13,787") {
// go doesn't seem to parse this one natively? or did i miss it?
t, err := time.Parse("2006-01-02 03:04:05", datestr[:i])
t, err := time.ParseInLocation("2006-01-02 03:04:05", datestr[:i], loc)
if err == nil {
ms, err := strconv.Atoi(datestr[i+1:])
if err == nil {
return time.Unix(0, t.UnixNano()+int64(ms)*1e6), nil
return time.Unix(0, t.UnixNano()+int64(ms)*1e6).In(loc), nil
}
}
return t, err
@ -272,9 +281,9 @@ iterRunes:
// 12 Feb 2006, 19:17:22
switch {
case len(datestr) == len("02 Jan 2006, 15:04"):
return time.Parse("02 Jan 2006, 15:04", datestr)
return time.ParseInLocation("02 Jan 2006, 15:04", datestr, loc)
case len(datestr) == len("02 Jan 2006, 15:04:05"):
return time.Parse("02 Jan 2006, 15:04:05", datestr)
return time.ParseInLocation("02 Jan 2006, 15:04:05", datestr, loc)
}
case ST_ALPHA: // starts alpha
// ST_ALPHAWS
@ -320,7 +329,7 @@ iterRunes:
switch {
case r == '-':
if i < 15 {
return time.Parse("Monday, 02-Jan-06 15:04:05 MST", datestr)
return time.ParseInLocation("Monday, 02-Jan-06 15:04:05 MST", datestr, loc)
} else {
state = ST_WEEKDAYCOMMADELTA
}
@ -338,7 +347,7 @@ iterRunes:
switch {
case r == '-':
if i < 15 {
return time.Parse("Mon, 02-Jan-06 15:04:05 MST", datestr)
return time.ParseInLocation("Mon, 02-Jan-06 15:04:05 MST", datestr, loc)
} else {
state = ST_WEEKDAYABBREVCOMMADELTA
}
@ -371,7 +380,7 @@ iterRunes:
case ST_ALPHAWSDIGITCOMMA: // Starts Alpha, whitespace, digit, comma
// May 8, 2009 5:57:51 PM
return time.Parse("Jan 2, 2006 3:04:05 PM", datestr)
return time.ParseInLocation("Jan 2, 2006 3:04:05 PM", datestr, loc)
case ST_ALPHAWSALPHA: // Alpha, whitespace, alpha
// Mon Jan _2 15:04:05 2006
@ -423,32 +432,32 @@ iterRunes:
// 2014 yyyy
if len(datestr) > len("1499979795437000") {
if nanoSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
return time.Unix(0, nanoSecs), nil
return time.Unix(0, nanoSecs).In(loc), nil
}
} else if len(datestr) > len("1499979795437") {
if microSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
return time.Unix(0, microSecs*1000), nil
return time.Unix(0, microSecs*1000).In(loc), nil
}
} else if len(datestr) > len("1332151919") {
if miliSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
return time.Unix(0, miliSecs*1000*1000), nil
return time.Unix(0, miliSecs*1000*1000).In(loc), nil
}
} else if len(datestr) == len("20140601") {
return time.Parse("20060102", datestr)
return time.ParseInLocation("20060102", datestr, loc)
} else if len(datestr) == len("2014") {
return time.Parse("2006", datestr)
return time.ParseInLocation("2006", datestr, loc)
} else {
if secs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
return time.Unix(secs, 0), nil
return time.Unix(secs, 0).In(loc), nil
}
}
case ST_DIGITDASH: // starts digit then dash 02-
// 2006-01-02
// 2006-01
if len(datestr) == len("2014-04-26") {
return time.Parse("2006-01-02", datestr)
return time.ParseInLocation("2006-01-02", datestr, loc)
} else if len(datestr) == len("2014-04") {
return time.Parse("2006-01", datestr)
return time.ParseInLocation("2006-01", datestr, loc)
}
case ST_DIGITDASHTDELTA:
// With another +/- time-zone at end
@ -460,12 +469,12 @@ iterRunes:
// 2006-01-02T15:04:05.999-07:00
// 2006-01-02T15:04:05+07:00
// 2006-01-02T15:04:05-07:00
return time.Parse("2006-01-02T15:04:05-07:00", datestr)
return time.ParseInLocation("2006-01-02T15:04:05-07:00", datestr, loc)
case ST_DIGITDASHT: // starts digit then dash 02- then T
// 2006-01-02T15:04:05.999999
// 2006-01-02T15:04:05.999999
return time.Parse("2006-01-02T15:04:05", datestr)
return time.ParseInLocation("2006-01-02T15:04:05", datestr, loc)
case ST_DIGITDASHTZDIGIT:
// With a time-zone at end after Z
@ -487,9 +496,9 @@ iterRunes:
// 2009-08-12T22:15Z -- No seconds/milliseconds
switch len(datestr) {
case len("2009-08-12T22:15Z"):
return time.Parse("2006-01-02T15:04Z", datestr)
return time.ParseInLocation("2006-01-02T15:04Z", datestr, loc)
default:
return time.Parse("2006-01-02T15:04:05Z", datestr)
return time.ParseInLocation("2006-01-02T15:04:05Z", datestr, loc)
}
case ST_DIGITDASHWS: // starts digit then dash 02- then whitespace 1 << 2 << 5 + 3
// 2013-04-01 22:43:22
@ -497,11 +506,11 @@ iterRunes:
// 2006-01-02 15:04:05 -07:00
switch len(datestr) {
case len("2006-01-02 15:04:05"):
return time.Parse("2006-01-02 15:04:05", datestr)
return time.ParseInLocation("2006-01-02 15:04:05", datestr, loc)
case len("2006-01-02 15:04:05 -0700"):
return time.Parse("2006-01-02 15:04:05 -0700", datestr)
return time.ParseInLocation("2006-01-02 15:04:05 -0700", datestr, loc)
case len("2006-01-02 15:04:05 -07:00"):
return time.Parse("2006-01-02 15:04:05 -07:00", datestr)
return time.ParseInLocation("2006-01-02 15:04:05 -07:00", datestr, loc)
}
case ST_DIGITDASHWSALPHA: // starts digit then dash 02- then whitespace 1 << 2 << 5 + 3
// 2014-12-16 06:20:00 UTC
@ -509,73 +518,73 @@ iterRunes:
// 2015-06-25 01:25:37.115208593 +0000 UTC
switch len(datestr) {
case len("2006-01-02 15:04:05 UTC"):
t, err := time.Parse("2006-01-02 15:04:05 UTC", datestr)
t, err := time.ParseInLocation("2006-01-02 15:04:05 UTC", datestr, loc)
if err == nil {
return t, nil
}
return time.Parse("2006-01-02 15:04:05 GMT", datestr)
return time.ParseInLocation("2006-01-02 15:04:05 GMT", datestr, loc)
case len("2015-02-18 00:12:00 +0000 UTC"):
t, err := time.Parse("2006-01-02 15:04:05 -0700 UTC", datestr)
t, err := time.ParseInLocation("2006-01-02 15:04:05 -0700 UTC", datestr, loc)
if err == nil {
return t, nil
}
return time.Parse("2006-01-02 15:04:05 -0700 GMT", datestr)
return time.ParseInLocation("2006-01-02 15:04:05 -0700 GMT", datestr, loc)
}
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
return time.Parse("2006-01-02 15:04:05", datestr)
return time.ParseInLocation("2006-01-02 15:04:05", datestr, loc)
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
return time.Parse("2006-01-02 15:04:05 UTC", datestr)
return time.ParseInLocation("2006-01-02 15:04:05 UTC", datestr, loc)
case ST_DIGITDASHWSDOTPLUS:
// 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
return time.Parse("2006-01-02 15:04:05 -0700", datestr)
return time.ParseInLocation("2006-01-02 15:04:05 -0700", datestr, loc)
case ST_DIGITDASHWSDOTPLUSALPHA:
// 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
return time.Parse("2006-01-02 15:04:05 -0700 UTC", datestr)
return time.ParseInLocation("2006-01-02 15:04:05 -0700 UTC", datestr, loc)
// if err == nil {
// return t, nil
// }
// return time.Parse("2006-01-02 15:04:05 -0700 GMT", datestr)
// return time.ParseInLocation("2006-01-02 15:04:05 -0700 GMT", datestr, loc)
case ST_ALPHAWSALPHACOLON:
// Mon Jan _2 15:04:05 2006
return time.Parse(time.ANSIC, datestr)
return time.ParseInLocation(time.ANSIC, datestr, loc)
case ST_ALPHAWSALPHACOLONOFFSET:
// Mon Jan 02 15:04:05 -0700 2006
return time.Parse(time.RubyDate, datestr)
return time.ParseInLocation(time.RubyDate, datestr, loc)
case ST_ALPHAWSALPHACOLONALPHA:
// Mon Jan _2 15:04:05 MST 2006
return time.Parse(time.UnixDate, datestr)
return time.ParseInLocation(time.UnixDate, datestr, loc)
case ST_ALPHAWSALPHACOLONALPHAOFFSET:
// Mon Aug 10 15:44:11 UTC+0100 2015
return time.Parse("Mon Jan 02 15:04:05 MST-0700 2006", datestr)
return time.ParseInLocation("Mon Jan 02 15:04:05 MST-0700 2006", datestr, loc)
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]
return time.Parse("Mon Jan 02 2006 15:04:05 MST-0700", dateTmp)
datestr = datestr[:33]
return time.ParseInLocation("Mon Jan 02 2006 15:04:05 MST-0700", datestr, loc)
}
case ST_DIGITSLASH: // starts digit then slash 02/ (but nothing else)
// 3/1/2014
@ -584,13 +593,13 @@ iterRunes:
// 2014/10/13
if firstSlash == 4 {
if len(datestr) == len("2006/01/02") {
return time.Parse("2006/01/02", datestr)
return time.ParseInLocation("2006/01/02", datestr, loc)
} else {
return time.Parse("2006/1/2", datestr)
return time.ParseInLocation("2006/1/2", datestr, loc)
}
} else {
for _, parseFormat := range shortDates {
if t, err := time.Parse(parseFormat, datestr); err == nil {
if t, err := time.ParseInLocation(parseFormat, datestr, loc); err == nil {
return t, nil
}
}
@ -604,13 +613,13 @@ iterRunes:
if firstSlash == 4 {
for _, layout := range []string{"2006/01/02 15:04", "2006/1/2 15:04", "2006/01/2 15:04", "2006/1/02 15:04"} {
if t, err := time.Parse(layout, datestr); err == nil {
if t, err := time.ParseInLocation(layout, datestr, loc); err == nil {
return t, nil
}
}
} else {
for _, layout := range []string{"01/02/2006 15:04", "01/2/2006 15:04", "1/02/2006 15:04", "1/2/2006 15:04"} {
if t, err := time.Parse(layout, datestr); err == nil {
if t, err := time.ParseInLocation(layout, datestr, loc); err == nil {
return t, nil
}
}
@ -626,14 +635,14 @@ iterRunes:
if firstSlash == 4 {
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"} {
if t, err := time.Parse(layout, datestr); err == nil {
if t, err := time.ParseInLocation(layout, datestr, loc); err == nil {
return t, nil
}
}
} else {
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"} {
if t, err := time.Parse(layout, datestr); err == nil {
if t, err := time.ParseInLocation(layout, datestr, loc); err == nil {
return t, nil
}
@ -648,13 +657,13 @@ iterRunes:
// 3/01/2012 10:11:59
if firstSlash == 4 {
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"} {
if t, err := time.Parse(layout, datestr); err == nil {
if t, err := time.ParseInLocation(layout, datestr, loc); err == nil {
return t, nil
}
}
} else {
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"} {
if t, err := time.Parse(layout, datestr); err == nil {
if t, err := time.ParseInLocation(layout, datestr, loc); err == nil {
return t, nil
}
}
@ -670,13 +679,13 @@ iterRunes:
if firstSlash == 4 {
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"} {
if t, err := time.Parse(layout, datestr); err == nil {
if t, err := time.ParseInLocation(layout, datestr, loc); err == nil {
return t, nil
}
}
} else {
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"} {
if t, err := time.Parse(layout, datestr); err == nil {
if t, err := time.ParseInLocation(layout, datestr, loc); err == nil {
return t, nil
}
}
@ -685,20 +694,20 @@ iterRunes:
case ST_WEEKDAYCOMMADELTA:
// Monday, 02 Jan 2006 15:04:05 -0700
// Monday, 02 Jan 2006 15:04:05 +0100
return time.Parse("Monday, 02 Jan 2006 15:04:05 -0700", datestr)
return time.ParseInLocation("Monday, 02 Jan 2006 15:04:05 -0700", datestr, loc)
case ST_WEEKDAYABBREVCOMMA: // Starts alpha then comma
// Mon, 02-Jan-06 15:04:05 MST
// Mon, 02 Jan 2006 15:04:05 MST
return time.Parse("Mon, 02 Jan 2006 15:04:05 MST", datestr)
return time.ParseInLocation("Mon, 02 Jan 2006 15:04:05 MST", datestr, loc)
case ST_WEEKDAYABBREVCOMMADELTA:
// Mon, 02 Jan 2006 15:04:05 -0700
// Thu, 13 Jul 2017 08:58:40 +0100
// RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
return time.Parse("Mon, 02 Jan 2006 15:04:05 -0700", datestr)
return time.ParseInLocation("Mon, 02 Jan 2006 15:04:05 -0700", datestr, loc)
case ST_WEEKDAYABBREVCOMMADELTAZONE:
// Tue, 11 Jul 2017 16:28:13 +0200 (CEST)
return time.Parse("Mon, 02 Jan 2006 15:04:05 -0700 (CEST)", datestr)
return time.ParseInLocation("Mon, 02 Jan 2006 15:04:05 -0700 (CEST)", datestr, loc)
}
return time.Time{}, fmt.Errorf("Could not find date format for %s", datestr)
return time.Time{}, fmt.Errorf("Could not find date format for %s", datestr, loc)
}

View File

@ -71,6 +71,8 @@ func TestParse(t *testing.T) {
t.Logf("you are testing and in MST %v", mstZone)
}
time.Local = time.UTC
zeroTime := time.Time{}.Unix()
ts, err := ParseAny("INVALID")
assert.Equal(t, zeroTime, ts.Unix())
@ -90,37 +92,46 @@ func TestParse(t *testing.T) {
ts = MustParse("Mon Jan 2 15:04:05 2006")
assert.Equal(t, "2006-01-02 15:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
// UnixDate = "Mon Jan _2 15:04:05 MST 2006"
ts = MustParse("Mon Jan 2 15:04:05 MST 2006")
// The time-zone of local machine appears to effect the results?
// Why is the zone/offset for MST not always the same depending on local time zone?
// Why is offset = 0 at all?
// https://play.golang.org/p/lSOT9AeNxz
// https://github.com/golang/go/issues/18012
_, offset := ts.Zone()
// WHY doesn't this work? seems to be underlying issue in go not finding
// the MST?
//assert.Equal(t, offset != 0, "Should have found zone/offset !=0 ", offset)
if offset == 0 {
assert.Equal(t, "2006-01-02 15:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
} else {
// for some reason i don't understand the offset is != 0
// IF you have your local time-zone set to US MST?
assert.Equal(t, "2006-01-02 22:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
}
// RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
ts = MustParse("Mon Jan 02 15:04:05 -0700 2006")
assert.Equal(t, "2006-01-02 22:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)), "%v")
// UnixDate = "Mon Jan _2 15:04:05 MST 2006"
ts = MustParse("Mon Jan 2 15:04:05 MST 2006")
// !!!!! The time-zone of local machine effects the results!
// https://play.golang.org/p/IDHRalIyXh
// https://github.com/golang/go/issues/18012
_, offset := ts.Zone()
assert.Equal(t, 0, offset, "Should have found zone/offset %v", offset)
assert.Equal(t, "2006-01-02 15:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
// Now lets set to mst
time.Local = mstZone
ts = MustParse("Mon Jan 2 15:04:05 MST 2006")
_, offset = ts.Zone()
assert.NotEqual(t, 0, offset, "Should have found zone/offset %v", offset)
assert.Equal(t, "2006-01-02 22:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
// Now Reset To UTC
time.Local = time.UTC
// RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
ts = MustParse("Monday, 02-Jan-06 15:04:05 MST")
_, offset = ts.Zone()
if offset == 0 {
assert.Equal(t, "2006-01-02 15:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
} else {
assert.Equal(t, "2006-01-02 22:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
}
assert.Equal(t, 0, offset, "Should have found zone/offset %v", offset)
assert.Equal(t, "2006-01-02 15:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
// Now lets set to mst
time.Local = mstZone
ts = MustParse("Monday, 02-Jan-06 15:04:05 MST")
_, offset = ts.Zone()
assert.NotEqual(t, 0, offset, "Should have found zone/offset %v", offset)
assert.Equal(t, "2006-01-02 22:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
// Now Reset To UTC
time.Local = time.UTC
ts = MustParse("Monday, 02-Jan-06 15:04:05 MST")
assert.Equal(t, "2006-01-02 15:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
@ -143,12 +154,7 @@ func TestParse(t *testing.T) {
assert.Equal(t, "2015-07-03 17:04:07 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
ts = MustParse("Mon, 02 Jan 2006 15:04:05 MST")
_, offset = ts.Zone()
if offset == 0 {
assert.Equal(t, "2006-01-02 15:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
} else {
assert.Equal(t, "2006-01-02 22:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
}
assert.Equal(t, "2006-01-02 15:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
ts = MustParse("Mon, 02-Jan-06 15:04:05 MST")
assert.Equal(t, "2006-01-02 15:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))