Iso3339 support

This commit is contained in:
Aaron Raddon 2014-04-24 17:51:56 -07:00
parent fcbf9aa9db
commit b00a86af45
2 changed files with 26 additions and 3 deletions

View File

@ -156,6 +156,22 @@ func parseFeatures(datestr string) Features {
func ParseAny(datestr string) (time.Time, error) { func ParseAny(datestr string) (time.Time, error) {
f := parseFeatures(datestr) f := parseFeatures(datestr)
switch { switch {
case f.Has(HAS_T):
//RFC3339 = "2006-01-02T15:04:05Z07:00"
if len(datestr) == len(time.RFC3339) {
if t, err := time.Parse(time.RFC3339, datestr); err == nil {
return t, nil
} else {
u.Error(err)
}
} else if len(datestr) == len(time.RFC3339Nano) {
// RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
if t, err := time.Parse(time.RFC3339Nano, datestr); err == nil {
return t, nil
} else {
u.Error(err)
}
}
case f.Has(HAS_DASH) && !f.Has(HAS_SLASH): case f.Has(HAS_DASH) && !f.Has(HAS_SLASH):
switch { switch {
case f.Has(HAS_WHITESPACE) && f.Has(HAS_COLON): case f.Has(HAS_WHITESPACE) && f.Has(HAS_COLON):

View File

@ -1,6 +1,7 @@
package dateparse package dateparse
import ( import (
"fmt"
u "github.com/araddon/gou" u "github.com/araddon/gou"
"github.com/bmizerany/assert" "github.com/bmizerany/assert"
"testing" "testing"
@ -52,13 +53,19 @@ func init() {
func TestParse(t *testing.T) { func TestParse(t *testing.T) {
//f := parseFeatures("May 8, 2009 5:57:51 PM") //f := parseFeatures("May 8, 2009 5:57:51 PM")
ts, _ := ParseAny("May 8, 2009 5:57:51 PM") ts, err := ParseAny("May 8, 2009 5:57:51 PM")
assert.T(t, err == nil)
//u.Debug(" ", ts.Unix()) //u.Debug(" ", ts.Unix())
//u.Debugf("%v", f) //u.Debugf("%v", f)
//u.Debugf("%v", ts) //u.Debugf("%v", ts)
assert.T(t, ts.Unix() == 1241805471) assert.T(t, ts.In(time.UTC).Unix() == 1241805471)
ts, _ = ParseAny("03/19/2012 10:11:59") ts, _ = ParseAny("03/19/2012 10:11:59")
u.Debug(ts.Unix(), ts) //u.Debug(ts.Unix(), ts)
assert.T(t, ts.Unix() == 1332151919) assert.T(t, ts.Unix() == 1332151919)
ts2, err := ParseAny("2009-08-12T22:15:09-07:00")
assert.T(t, err == nil)
//u.Debug(ts2.In(time.UTC), " ", ts2.Unix())
assert.T(t, "2009-08-13 05:15:09 +0000 UTC" == fmt.Sprintf("%v", ts2.In(time.UTC)))
} }