add YYYY-MM and YYYY date formats

This commit is contained in:
Michael Camilleri 2016-03-01 13:25:00 -08:00
parent fbec5be882
commit 5a2b0cd269
2 changed files with 29 additions and 1 deletions

View File

@ -356,6 +356,7 @@ iterRunes:
// 1384216367189 // 1384216367189
// 1332151919 seconds // 1332151919 seconds
// 20140601 yyyymmdd // 20140601 yyyymmdd
// 2014 yyyy
if len(datestr) >= len("13980450781991351") { if len(datestr) >= len("13980450781991351") {
if nanoSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil { if nanoSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
return time.Unix(0, nanoSecs), nil return time.Unix(0, nanoSecs), nil
@ -380,6 +381,12 @@ iterRunes:
} else { } else {
return time.Time{}, err return time.Time{}, err
} }
} else if len(datestr) == len("2014") {
if t, err := time.Parse("2006", datestr); err == nil {
return t, nil
} else {
return time.Time{}, err
}
} else { } else {
if secs, err := strconv.ParseInt(datestr, 10, 64); err == nil { if secs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
return time.Unix(secs, 0), nil return time.Unix(secs, 0), nil
@ -389,12 +396,19 @@ iterRunes:
} }
case ST_DIGITDASH: // starts digit then dash 02- case ST_DIGITDASH: // starts digit then dash 02-
// 2006-01-02 // 2006-01-02
// 2006-01
if len(datestr) == len("2014-04-26") { if len(datestr) == len("2014-04-26") {
if t, err := time.Parse("2006-01-02", datestr); err == nil { if t, err := time.Parse("2006-01-02", datestr); err == nil {
return t, nil return t, nil
} else { } else {
return time.Time{}, err return time.Time{}, err
} }
} else if len(datestr) == len("2014-04") {
if t, err := time.Parse("2006-01", datestr); err == nil {
return t, nil
} else {
return time.Time{}, err
}
} }
case ST_DIGITDASHWS: // starts digit then dash 02- then whitespace 1 << 2 << 5 + 3 case ST_DIGITDASHWS: // starts digit then dash 02- then whitespace 1 << 2 << 5 + 3
// 2014-04-26 17:24:37.3186369 // 2014-04-26 17:24:37.3186369

View File

@ -49,6 +49,10 @@ import (
2006-01-02 2006-01-02
2014-05-11 08:20:13,787 // i couldn't find parser for this in go? 2014-05-11 08:20:13,787 // i couldn't find parser for this in go?
// only day or year level resolution
2006-01
2006
*/ */
func init() { func init() {
@ -267,12 +271,22 @@ func TestParse(t *testing.T) {
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC)) //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2014-04-26 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) assert.T(t, "2014-04-26 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2014-04")
assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2014-04-01 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2014-05-11 08:20:13,787") ts, err = ParseAny("2014-05-11 08:20:13,787")
assert.Tf(t, err == nil, "%v", err) assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC)) //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2014-05-11 08:20:13.787 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) assert.T(t, "2014-05-11 08:20:13.787 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
// yyyy mm dd // yyyymmdd and similar
ts, err = ParseAny("2014")
assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2014-01-01 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("20140601") ts, err = ParseAny("20140601")
assert.Tf(t, err == nil, "%v", err) assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC)) //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))