Code cleanup to reduce trial-error parsing

This commit is contained in:
Aaron Raddon 2016-05-03 13:55:11 -07:00
parent 7c2f96b241
commit a19b713c2e
2 changed files with 67 additions and 25 deletions

View File

@ -34,13 +34,17 @@ const (
ST_ALPHACOMMA ST_ALPHACOMMA
ST_ALPHACOMMADASH ST_ALPHACOMMADASH
ST_ALPHACOMMADASHDASH ST_ALPHACOMMADASHDASH
//ST_ALPHADIGIT ST_MONTHCOMMA
ST_WEEKDAYCOMMA
ST_WEEKDAYABBREVCOMMA
) )
var _ = u.EMPTY var _ = u.EMPTY
var ( var (
shortDates = []string{"01/02/2006", "1/2/2006", "06/01/02", "01/02/06", "1/2/06"} shortDates = []string{"01/02/2006", "1/2/2006", "06/01/02", "01/02/06", "1/2/06"}
weekdays = map[string]bool{"Monday": true, "Tuesday": true, "Wednesday": true, "Thursday": true, "Friday": true, "Saturday": true, "Sunday": true}
weekdayAbbrev = map[string]bool{"Mon": true, "Tue": true, "Wed": true, "Thu": true, "Fri": true, "Sat": true, "Sun": true}
) )
// Given an unknown date format, detect the type, parse, return time // Given an unknown date format, detect the type, parse, return time
@ -246,6 +250,7 @@ iterRunes:
// Mon Jan _2 15:04:05 MST 2006 // Mon Jan _2 15:04:05 MST 2006
// Mon Jan 02 15:04:05 -0700 2006 // Mon Jan 02 15:04:05 -0700 2006
// Monday, 02-Jan-06 15:04:05 MST // Monday, 02-Jan-06 15:04:05 MST
// Monday, 02 Jan 2006 15:04:05 -0700
// Mon, 02 Jan 2006 15:04:05 MST // Mon, 02 Jan 2006 15:04:05 MST
// Mon, 02 Jan 2006 15:04:05 -0700 // Mon, 02 Jan 2006 15:04:05 -0700
// Mon Aug 10 15:44:11 UTC+0100 2015 // Mon Aug 10 15:44:11 UTC+0100 2015
@ -257,10 +262,55 @@ iterRunes:
case r == ' ': case r == ' ':
state = ST_ALPHAWS state = ST_ALPHAWS
case r == ',': case r == ',':
state = ST_ALPHACOMMA
// case unicode.IsDigit(r): switch {
// state = ST_ALPHADIGIT case weekdays[datestr[:i]] == true:
state = ST_WEEKDAYCOMMA
case weekdayAbbrev[datestr[:i]] == true:
state = ST_WEEKDAYABBREVCOMMA
default:
state = ST_MONTHCOMMA
}
} }
case ST_WEEKDAYCOMMA: // Starts alpha then comma
// Monday, 02-Jan-06 15:04:05 MST
// Monday, 02 Jan 2006 15:04:05 -0700
switch {
case r == '-':
if i < 15 {
t, err := time.Parse("Monday, 02-Jan-06 15:04:05 MST", datestr)
if err == nil {
return t, nil
}
return time.Time{}, err
} else {
t, err := time.Parse("Monday, 02 Jan 2006 15:04:05 -0700", datestr)
if err == nil {
return t, nil
}
return time.Time{}, err
}
}
case ST_WEEKDAYABBREVCOMMA: // Starts alpha then comma
// Mon, 02-Jan-06 15:04:05 MST
// Mon, 02 Jan 2006 15:04:05 -0700
switch {
case r == '-':
if i < 15 {
t, err := time.Parse("Mon, 02-Jan-06 15:04:05 MST", datestr)
if err == nil {
return t, nil
}
return time.Time{}, err
} else {
t, err := time.Parse("Mon, 02 Jan 2006 15:04:05 -0700", datestr)
if err == nil {
return t, nil
}
return time.Time{}, err
}
}
case ST_ALPHAWS: // Starts alpha then whitespace case ST_ALPHAWS: // Starts alpha then whitespace
// 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
@ -279,41 +329,24 @@ iterRunes:
case ST_ALPHACOMMA: // Starts alpha then comma case ST_ALPHACOMMA: // Starts alpha then comma
// Mon, 02 Jan 2006 15:04:05 MST // Mon, 02 Jan 2006 15:04:05 MST
// Mon, 02 Jan 2006 15:04:05 -0700 // Mon, 02 Jan 2006 15:04:05 -0700
// Monday, 02-Jan-06 15:04:05 MST
// Monday, 02 Jan 2006 15:04:05 -0700
switch { switch {
case r == '-': case r == '-':
state = ST_ALPHACOMMADASH state = ST_ALPHACOMMADASH
} }
if t, err := time.Parse("Mon, 02 Jan 2006 15:04:05 MST", datestr); err == nil {
return t, nil
}
if t, err := time.Parse("Monday, 02 Jan 2006 15:04:05 MST", datestr); err == nil {
return t, nil
}
case ST_ALPHACOMMADASH: // Starts alpha then comma and one dash case ST_ALPHACOMMADASH: // Starts alpha then comma and one dash
// Mon, 02 Jan 2006 15:04:05 -0700 // Mon, 02 Jan 2006 15:04:05 -0700
// Monday, 02 Jan 2006 15:04:05 -0700
// Monday, 02-Jan-06 15:04:05 MST
switch { switch {
case r == '-': case r == '-':
state = ST_ALPHACOMMADASHDASH state = ST_ALPHACOMMADASHDASH
} }
t, err := time.Parse("Mon, 02 Jan 2006 15:04:05 -0700", datestr)
if err == nil {
return t, nil
}
if t, err := time.Parse("Monday, 02 Jan 2006 15:04:05 -0700", datestr); err == nil {
return t, nil
}
case ST_ALPHAWSCOMMA: // Starts Alpha, whitespace, digit, comma case ST_ALPHAWSCOMMA: // Starts Alpha, whitespace, digit, comma
// May 8, 2009 5:57:51 PM // May 8, 2009 5:57:51 PM
if t, err := time.Parse("Jan 2, 2006 3:04:05 PM", datestr); err == nil { t, err := time.Parse("Jan 2, 2006 3:04:05 PM", datestr)
if err == nil {
return t, nil return t, nil
} else {
//u.Error(err)
} }
return time.Time{}, err
case ST_ALPHAWSALPHA: // Starts Alpha, whitespace, alpha case ST_ALPHAWSALPHA: // Starts Alpha, whitespace, alpha
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time) // Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
// Mon Jan _2 15:04:05 2006 // Mon Jan _2 15:04:05 2006
@ -640,9 +673,17 @@ iterRunes:
} }
} }
} }
case ST_WEEKDAYABBREVCOMMA: // Starts alpha then comma
// Mon, 02 Jan 2006 15:04:05 MST
t, err := time.Parse("Mon, 02 Jan 2006 15:04:05 MST", datestr)
if err == nil {
return t, nil
}
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
// Jan 2, 2006 3:04:05 PM
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 {
return t, nil return t, nil
} else { } else {

View File

@ -57,6 +57,7 @@ import (
func init() { func init() {
u.SetupLogging("debug") u.SetupLogging("debug")
u.SetColorOutput()
} }
func TestParse(t *testing.T) { func TestParse(t *testing.T) {