update to handle yyyy/?? formats

This commit is contained in:
Aaron Raddon 2014-07-10 15:25:23 -07:00
parent 16ac80f2b1
commit 375d67e59f
2 changed files with 187 additions and 39 deletions

View File

@ -40,6 +40,8 @@ func ParseAny(datestr string) (time.Time, error) {
state := ST_START state := ST_START
firstSlash := 0
// General strategy is to read rune by rune through the date looking for // General strategy is to read rune by rune through the date looking for
// certain hints of what type of date we are dealing with. // certain hints of what type of date we are dealing with.
// Hopefully we only need to read about 5 or 6 bytes before // Hopefully we only need to read about 5 or 6 bytes before
@ -51,6 +53,7 @@ iterRunes:
i += (bytesConsumed - 1) i += (bytesConsumed - 1)
} }
//u.Infof("char=%s i=%d datestr=%s", r, i, datestr)
switch state { switch state {
case ST_START: case ST_START:
if unicode.IsDigit(r) { if unicode.IsDigit(r) {
@ -71,6 +74,7 @@ iterRunes:
state = ST_DIGITCOLON state = ST_DIGITCOLON
case '/': case '/':
state = ST_DIGITSLASH state = ST_DIGITSLASH
firstSlash = i
} }
case ST_DIGITDASH: // starts digit then dash 02- case ST_DIGITDASH: // starts digit then dash 02-
// 2006-01-02T15:04:05Z07:00 // 2006-01-02T15:04:05Z07:00
@ -135,6 +139,7 @@ iterRunes:
} }
} }
case ST_DIGITSLASH: // starts digit then slash 02/ case ST_DIGITSLASH: // starts digit then slash 02/
// 2014/07/10 06:55:38.156283
// 03/19/2012 10:11:59 // 03/19/2012 10:11:59
// 04/2/2014 03:00:37 // 04/2/2014 03:00:37
// 3/1/2012 10:11:59 // 3/1/2012 10:11:59
@ -150,6 +155,7 @@ iterRunes:
state = ST_DIGITSLASHWS state = ST_DIGITSLASHWS
} }
case ST_DIGITSLASHWS: // starts digit then slash 02/ more digits/slashes then whitespace case ST_DIGITSLASHWS: // starts digit then slash 02/ more digits/slashes then whitespace
// 2014/07/10 06:55:38.156283
// 03/19/2012 10:11:59 // 03/19/2012 10:11:59
// 04/2/2014 03:00:37 // 04/2/2014 03:00:37
// 3/1/2012 10:11:59 // 3/1/2012 10:11:59
@ -159,6 +165,7 @@ iterRunes:
state = ST_DIGITSLASHWSCOLON state = ST_DIGITSLASHWSCOLON
} }
case ST_DIGITSLASHWSCOLON: // starts digit then slash 02/ more digits/slashes then whitespace case ST_DIGITSLASHWSCOLON: // starts digit then slash 02/ more digits/slashes then whitespace
// 2014/07/10 06:55:38.156283
// 03/19/2012 10:11:59 // 03/19/2012 10:11:59
// 04/2/2014 03:00:37 // 04/2/2014 03:00:37
// 3/1/2012 10:11:59 // 3/1/2012 10:11:59
@ -315,7 +322,23 @@ iterRunes:
// 3/1/2014 // 3/1/2014
// 10/13/2014 // 10/13/2014
// 01/02/2006 // 01/02/2006
// 2014/10/13
if firstSlash == 4 {
if len(datestr) == len("2006/01/02") {
if t, err := time.Parse("2006/01/02", datestr); err == nil {
return t, nil
} else {
u.Errorf("hm: %v %s", err, datestr)
return time.Time{}, err
}
} else {
if t, err := time.Parse("2006/1/2", datestr); err == nil {
return t, nil
} else {
return time.Time{}, err
}
}
} else {
if len(datestr) == len("01/02/2006") { if len(datestr) == len("01/02/2006") {
if t, err := time.Parse("01/02/2006", datestr); err == nil { if t, err := time.Parse("01/02/2006", datestr); err == nil {
return t, nil return t, nil
@ -329,9 +352,38 @@ iterRunes:
return time.Time{}, err return time.Time{}, err
} }
} }
}
case ST_DIGITSLASHWSCOLON: // starts digit then slash 02/ more digits/slashes then whitespace case ST_DIGITSLASHWSCOLON: // starts digit then slash 02/ more digits/slashes then whitespace
// 4/8/2014 22:05 // 4/8/2014 22:05
// 04/08/2014 22:05
// 2014/4/8 22:05
// 2014/04/08 22:05
if firstSlash == 4 {
if len(datestr) == len("2006/01/02 15:04") {
if t, err := time.Parse("2006/01/02 15:04", datestr); err == nil {
return t, nil
} else {
return time.Time{}, err
}
} else if len(datestr) == len("2006/01/2 15:04") {
if t, err := time.Parse("2006/01/2 15:04", datestr); err == nil {
return t, nil
} else {
if t, err := time.Parse("2006/1/02 15:04", datestr); err == nil {
return t, nil
} else {
return time.Time{}, err
}
}
} else {
if t, err := time.Parse("2006/1/2 15:04", datestr); err == nil {
return t, nil
} else {
return time.Time{}, err
}
}
} else {
if len(datestr) == len("01/02/2006 15:04") { if len(datestr) == len("01/02/2006 15:04") {
if t, err := time.Parse("01/02/2006 15:04", datestr); err == nil { if t, err := time.Parse("01/02/2006 15:04", datestr); err == nil {
return t, nil return t, nil
@ -355,12 +407,52 @@ iterRunes:
return time.Time{}, err return time.Time{}, err
} }
} }
}
case ST_DIGITSLASHWSCOLONCOLON: // starts digit then slash 02/ more digits/slashes then whitespace double colons case ST_DIGITSLASHWSCOLONCOLON: // starts digit then slash 02/ more digits/slashes then whitespace double colons
// 2014/07/10 06:55:38.156283
// 03/19/2012 10:11:59 // 03/19/2012 10:11:59
// 3/1/2012 10:11:59 // 3/1/2012 10:11:59
// 03/1/2012 10:11:59 // 03/1/2012 10:11:59
// 3/01/2012 10:11:59 // 3/01/2012 10:11:59
if len(datestr) == len("01/02/2006 15:04:05") {
if firstSlash == 4 {
if len(datestr) == len("2014/07/10 06:55:38.156283") {
if t, err := time.Parse("2006/01/02 15:04:05.000000", datestr); err == nil {
return t, nil
} else {
return time.Time{}, err
}
} else if len(datestr) == len("2006/01/02 15:04:05") {
if t, err := time.Parse("2006/01/02 15:04:05", datestr); err == nil {
return t, nil
} else {
return time.Time{}, err
}
} else if len(datestr) == len("2006/01/2 15:04:05") {
if t, err := time.Parse("2006/01/2 15:04:05", datestr); err == nil {
return t, nil
} else {
if t, err := time.Parse("2006/1/02 15:04:05", datestr); err == nil {
return t, nil
} else {
return time.Time{}, err
}
}
} else {
if t, err := time.Parse("2006/1/2 15:04:05", datestr); err == nil {
return t, nil
} else {
return time.Time{}, err
}
}
} else {
if len(datestr) == len("07/10/2014 06:55:38.156283") {
if t, err := time.Parse("01/02/2006 15:04:05.000000", datestr); err == nil {
return t, nil
} else {
return time.Time{}, err
}
} else if len(datestr) == len("01/02/2006 15:04:05") {
if t, err := time.Parse("01/02/2006 15:04:05", datestr); err == nil { if t, err := time.Parse("01/02/2006 15:04:05", datestr); err == nil {
return t, nil return t, nil
} else { } else {
@ -383,6 +475,7 @@ iterRunes:
return time.Time{}, err return time.Time{}, err
} }
} }
}
case ST_ALPHACOMMA: // Starts alpha then comma but no DASH case ST_ALPHACOMMA: // Starts alpha then comma but no DASH
// Mon, 02 Jan 2006 15:04:05 MST // Mon, 02 Jan 2006 15:04:05 MST
if t, err := time.Parse("Jan 2, 2006 3:04:05 PM", datestr); err == nil { if t, err := time.Parse("Jan 2, 2006 3:04:05 PM", datestr); err == nil {

View File

@ -35,6 +35,7 @@ import (
Apr 7, 2014 4:58:55 PM Apr 7, 2014 4:58:55 PM
2014/07/10 06:55:38.156283
03/19/2012 10:11:59 03/19/2012 10:11:59
04/2/2014 03:00:37 04/2/2014 03:00:37
3/1/2014 3/1/2014
@ -89,10 +90,8 @@ func TestParse(t *testing.T) {
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC)) //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
//assert.T(t, "2006-01-02 15:04:05 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) //assert.T(t, "2006-01-02 15:04:05 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("03/19/2012 10:11:59") //---------------------------------------------
assert.Tf(t, err == nil, "%v", err) // mm/dd/yyyy ?
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2012-03-19 10:11:59 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("3/31/2014") ts, err = ParseAny("3/31/2014")
assert.Tf(t, err == nil, "%v", err) assert.Tf(t, err == nil, "%v", err)
@ -124,11 +123,61 @@ func TestParse(t *testing.T) {
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC)) //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2014-04-02 03:00:51 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) assert.T(t, "2014-04-02 03:00:51 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("1332151919") ts, err = ParseAny("03/19/2012 10:11:59")
assert.Tf(t, err == nil, "%v", err) assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC)) //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2012-03-19 10:11:59 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) assert.T(t, "2012-03-19 10:11:59 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("03/19/2012 10:11:59.3186369")
assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2012-03-19 10:11:59.3186369 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
//---------------------------------------------
// yyyy/mm/dd ?
ts, err = ParseAny("2014/3/31")
assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2014-03-31 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2014/03/31")
assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2014-03-31 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2014/4/8 22:05")
assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2014-04-08 22:05:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2014/04/08 22:05")
assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2014-04-08 22:05:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2014/04/2 03:00:51")
assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2014-04-02 03:00:51 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2014/4/02 03:00:51")
assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2014-04-02 03:00:51 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2012/03/19 10:11:59")
assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2012-03-19 10:11:59 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2012/03/19 10:11:59.3186369")
assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2012-03-19 10:11:59.3186369 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
//---------------------------------------------
// yyyy-mm-dd ?
ts, err = ParseAny("2009-08-12T22:15:09-07:00") ts, err = ParseAny("2009-08-12T22:15:09-07:00")
assert.Tf(t, err == nil, "%v", err) assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC)) //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
@ -168,6 +217,12 @@ func TestParse(t *testing.T) {
assert.Tf(t, err == nil, "%v", err) assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC)) //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2014-05-11 08:20:13.787 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) assert.T(t, "2014-05-11 08:20:13.787 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("1332151919")
assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2012-03-19 10:11:59 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
} }
// func TestWIP(t *testing.T) { // func TestWIP(t *testing.T) {