2 new date formats

This commit is contained in:
Aaron Raddon 2015-09-30 12:09:08 -07:00
parent b93aaa0470
commit eeec45e120
2 changed files with 37 additions and 3 deletions

View File

@ -2,11 +2,13 @@ package dateparse
import ( import (
"fmt" "fmt"
u "github.com/araddon/gou"
"strconv" "strconv"
"strings"
"time" "time"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
u "github.com/araddon/gou"
) )
type DateState int type DateState int
@ -24,6 +26,7 @@ const (
ST_DIGITSLASHWS ST_DIGITSLASHWS
ST_DIGITSLASHWSCOLON ST_DIGITSLASHWSCOLON
ST_DIGITSLASHWSCOLONCOLON ST_DIGITSLASHWSCOLONCOLON
ST_DIGITSLASHWSCOLONCOLONAMPM
ST_ALPHA ST_ALPHA
ST_ALPHAWS ST_ALPHAWS
ST_ALPHAWSCOMMA ST_ALPHAWSCOMMA
@ -220,10 +223,22 @@ iterRunes:
// 04/2/2014 03:00:37 // 04/2/2014 03:00:37
// 3/1/2012 10:11:59 // 3/1/2012 10:11:59
// 4/8/2014 22:05 // 4/8/2014 22:05
// 3/1/2012 10:11:59 AM
switch r { switch r {
case ':': case ':':
state = ST_DIGITSLASHWSCOLONCOLON state = ST_DIGITSLASHWSCOLONCOLON
} }
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 {
case 'A', 'P':
state = ST_DIGITSLASHWSCOLONCOLONAMPM
}
case ST_ALPHA: // starts alpha case ST_ALPHA: // starts alpha
// May 8, 2009 5:57:51 PM // May 8, 2009 5:57:51 PM
// Mon Jan _2 15:04:05 2006 // Mon Jan _2 15:04:05 2006
@ -427,6 +442,12 @@ iterRunes:
} else { } else {
return time.Time{}, err return time.Time{}, err
} }
case len("2015-09-30 18:48:56.35272715 +0000 UTC"):
if t, err := time.Parse("2006-01-02 15:04:05.00000000 +0000 UTC", datestr); err == nil {
return t, nil
} else {
return time.Time{}, err
}
case len("2015-06-25 01:25:37.115208593 +0000 UTC"): case len("2015-06-25 01:25:37.115208593 +0000 UTC"):
if t, err := time.Parse("2006-01-02 15:04:05.000000000 +0000 UTC", datestr); err == nil { if t, err := time.Parse("2006-01-02 15:04:05.000000000 +0000 UTC", datestr); err == nil {
return t, nil return t, nil
@ -518,13 +539,16 @@ iterRunes:
} }
} }
} }
case ST_DIGITSLASHWSCOLONCOLON: // starts digit then slash 02/ more digits/slashes then whitespace double colons case ST_DIGITSLASHWSCOLONCOLON, ST_DIGITSLASHWSCOLONCOLONAMPM: // starts digit then slash 02/ more digits/slashes then whitespace double colons
// 2014/07/10 06:55:38.156283 // 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 state == ST_DIGITSLASHWSCOLONCOLONAMPM {
datestr = strings.Replace(datestr, " AM", "", -1)
datestr = strings.Replace(datestr, " PM", "", -1)
}
if firstSlash == 4 { if firstSlash == 4 {
if len(datestr) == len("2014/07/10 06:55:38.156283") { 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 { if t, err := time.Parse("2006/01/02 15:04:05.000000", datestr); err == nil {
@ -586,6 +610,7 @@ iterRunes:
} }
} }
} }
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

@ -141,6 +141,10 @@ 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("8/8/1965 12:00:00 AM")
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC), " error ", err)
assert.T(t, "1965-08-08 12:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("4/02/2014 03:00:51") ts, err = ParseAny("4/02/2014 03:00:51")
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))
@ -222,6 +226,11 @@ 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, "2012-08-03 18:31:59.257 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) assert.T(t, "2012-08-03 18:31:59.257 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2015-09-30 18:48:56.35272715 +0000 UTC")
assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2015-09-30 18:48:56.35272715 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2012-08-03 18:31:59.257000000") ts, err = ParseAny("2012-08-03 18:31:59.257000000")
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))