From a793d0cb317320e65251fb978617357bc36380ad Mon Sep 17 00:00:00 2001 From: Aaron Raddon Date: Tue, 6 May 2014 18:14:58 -0700 Subject: [PATCH] ampm format --- parseany.go | 23 ++++++++++++++++++----- parseany_test.go | 21 ++++++++++++++++++++- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/parseany.go b/parseany.go index 22452a5..3f0b38e 100644 --- a/parseany.go +++ b/parseany.go @@ -116,7 +116,7 @@ func parseFeatures(datestr string) Features { features[HAS_NUMERIC] = HAS_NUMERIC } else if char == ' ' { //u.Info("is whitespace") - state = s_WHITESPACE + //state = s_WHITESPACE } else { //u.Error("unrecognized input? ", char, " ", string(char)) } @@ -136,13 +136,14 @@ func parseFeatures(datestr string) Features { } lexeme = lexeme + string(char) case S_NUMERIC: + //u.Infof("numeric: %v", string(char)) if unicode.IsLetter(char) { features[HAS_ALPHA] = HAS_ALPHA } else if unicode.IsNumber(char) { features[HAS_NUMERIC] = HAS_NUMERIC } else if char == ' ' { //u.Info("is whitespace") - state = s_WHITESPACE + //state = s_WHITESPACE } else { //u.Error("unrecognized input? ", char, " ", string(char)) } @@ -157,6 +158,8 @@ func parseFeatures(datestr string) Features { // Given an unknown date format, detect the type, parse, return time func ParseAny(datestr string) (time.Time, error) { f := parseFeatures(datestr) + //u.Infof("%s alpha?%v colon?%v whitespace?%v dash?%v slash?%v AMPM?%v", datestr, f.Has(HAS_ALPHA), + // f.Has(HAS_COLON), f.Has(HAS_WHITESPACE), f.Has(HAS_DASH), f.Has(HAS_SLASH), f.Has(HAS_AMPM)) switch { case f.Has(HAS_T): //RFC3339 = "2006-01-02T15:04:05Z07:00" @@ -176,7 +179,7 @@ func ParseAny(datestr string) (time.Time, error) { } case f.Has(HAS_DASH) && !f.Has(HAS_SLASH): switch { - case f.Has(HAS_WHITESPACE) && f.Has(HAS_COLON): + case f.Has(HAS_WHITESPACE) && f.Has(HAS_COLON) && !f.Has(HAS_ALPHA): //2014-04-26 05:24:37.3186369 //2006-01-02 15:04:05.000 //2006-01-02 @@ -187,8 +190,18 @@ func ParseAny(datestr string) (time.Time, error) { } else { u.Error(err) } - } else if len(datestr) == len("2014-04-26 05:24:37.3186369") { - if t, err := time.Parse("2006-01-02 03:04:05.000", datestr); err == nil { + } else if len(datestr) == len("2014-04-26 05:24:37.000") { + if t, err := time.Parse("2006-01-02 15:04:05.000", datestr); err == nil { + return t, nil + } else { + u.Error(err) + } + } + + case f.Has(HAS_ALPHA): + //2006-01-02 03:04:05 PM + if len(datestr) == len("2014-04-26 03:24:37 PM") { + if t, err := time.Parse("2006-01-02 03:04:05 PM", datestr); err == nil { return t, nil } else { u.Error(err) diff --git a/parseany_test.go b/parseany_test.go index 38881b3..cc35d17 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -94,7 +94,26 @@ func TestParse(t *testing.T) { //2014-04-26 05:24:37.3186369 ts, err = ParseAny("2014-04-26 17:24:37.3186369") assert.T(t, err == nil) - u.Debug(ts.Unix(), ts) + //u.Debug(ts.Unix(), ts) assert.T(t, "2014-04-26 17:24:37.3186369 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) + //2014-04-26 17:24:37.123 + ts, err = ParseAny("2014-04-26 17:24:37.123") + assert.T(t, err == nil) + u.Debugf("unix=%v ts='%v'", ts.Unix(), ts) + assert.T(t, "2014-04-26 17:24:37.123 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) + + //2014-04-26 05:24:37 PM + ts, err = ParseAny("2014-04-26 05:24:37 PM") + assert.T(t, err == nil) + u.Debug(ts.Unix(), ts) + assert.T(t, "2014-04-26 17:24:37 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) } + +// func TestParseAMPM(t *testing.T) { +// //2014-04-26 05:24:37 PM +// ts, err := ParseAny("2014-04-26 05:24:37 PM") +// assert.T(t, err == nil) +// u.Debug(ts.Unix(), ts) +// assert.T(t, "2014-04-26 17:24:37 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) +// }