add a new date format

This commit is contained in:
Aaron Raddon 2015-08-10 10:54:01 -07:00
parent 4f9dbf802b
commit ed500d214a
2 changed files with 23 additions and 6 deletions

View File

@ -232,6 +232,7 @@ iterRunes:
// Monday, 02-Jan-06 15:04:05 MST
// Mon, 02 Jan 2006 15:04:05 MST
// Mon, 02 Jan 2006 15:04:05 -0700
// Mon Aug 10 15:44:11 UTC+0100 2015
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
if unicode.IsLetter(r) {
continue
@ -250,6 +251,7 @@ iterRunes:
// Mon Jan _2 15:04:05 MST 2006
// Mon Jan 02 15:04:05 -0700 2006
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
// Mon Aug 10 15:44:11 UTC+0100 2015
switch {
// case r == ' ':
// state = ST_ALPHAWSWS
@ -286,26 +288,30 @@ iterRunes:
// Mon Jan _2 15:04:05 2006
// Mon Jan _2 15:04:05 MST 2006
// Mon Jan 02 15:04:05 -0700 2006
// Mon Aug 10 15:44:11 UTC+0100 2015
switch {
case len(datestr) == len("Mon Jan _2 15:04:05 2006"):
if t, err := time.Parse(time.ANSIC, datestr); err == nil {
return t, nil
} else {
//break iterRunes
return time.Time{}, err
}
case len(datestr) == len("Mon Jan _2 15:04:05 MST 2006"):
if t, err := time.Parse(time.UnixDate, datestr); err == nil {
return t, nil
} else {
//break iterRunes
return time.Time{}, err
}
case len(datestr) == len("Mon Jan 02 15:04:05 -0700 2006"):
if t, err := time.Parse(time.RubyDate, datestr); err == nil {
return t, nil
} else {
//break iterRunes
return time.Time{}, err
}
case len(datestr) == len("Mon Aug 10 15:44:11 UTC+0100 2015"):
if t, err := time.Parse("Mon Jan 02 15:04:05 MST-0700 2006", datestr); err == nil {
return t, nil
} else {
return time.Time{}, err
}
case len(datestr) > len("Mon Jan 02 2006 15:04:05 MST-0700"):
@ -315,9 +321,10 @@ iterRunes:
if t, err := time.Parse("Mon Jan 02 2006 15:04:05 MST-0700", dateTmp); err == nil {
return t, nil
} else {
//break iterRunes
return time.Time{}, err
}
default:
u.LogThrottle(u.WARN, 5, "ST_ALPHAWSALPHA case not found: %v", datestr)
}
default:
//u.Infof("no case for: %d", state)

View File

@ -2,10 +2,11 @@ package dateparse
import (
"fmt"
u "github.com/araddon/gou"
"github.com/bmizerany/assert"
"testing"
"time"
u "github.com/araddon/gou"
"github.com/bmizerany/assert"
)
/*
@ -59,6 +60,9 @@ func TestParse(t *testing.T) {
assert.T(t, ts.Unix() == zeroTime)
assert.T(t, err != nil)
//u.Debug(time.Now()) // 2015-08-10 10:23:17.675810275 -0700 PDT
//u.Debug(time.Now().In(time.UTC)) // 2015-08-10 17:23:17.675849992 +0000 UTC
ts, err = ParseAny("May 8, 2009 5:57:51 PM")
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2009-05-08 17:57:51 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
@ -84,6 +88,12 @@ func TestParse(t *testing.T) {
//u.Debug(fmt.Sprintf("%v", ts.In(time.UTC)), " ---- ", ts)
assert.T(t, "2006-01-02 15:04:05 +0000 MST" == fmt.Sprintf("%v", ts))
// Another weird one, year on the end after UTC?
ts, err = ParseAny("Mon Aug 10 15:44:11 UTC+0100 2015")
assert.T(t, err == nil)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2015-08-10 15:44:11 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
// Easily the worst Date format i have ever seen
// "Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)"
ts, err = ParseAny("Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)")