ampm format

This commit is contained in:
Aaron Raddon 2014-05-06 18:14:58 -07:00
parent 36dfef3c74
commit a793d0cb31
2 changed files with 38 additions and 6 deletions

View File

@ -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)

View File

@ -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)))
// }