New date format

This commit is contained in:
Aaron Raddon 2017-02-05 10:04:03 -08:00
parent 2943ab2f5b
commit ebdd2c1b80
3 changed files with 31 additions and 0 deletions

View File

@ -17,6 +17,7 @@ var examples = []string{
"Mon Aug 10 15:44:11 UTC+0100 2015", "Mon Aug 10 15:44:11 UTC+0100 2015",
"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, 02 Jan 2006 15:04:05 -0700", "Mon, 02 Jan 2006 15:04:05 -0700",
"12 Feb 2006, 19:17",
"2015-02-18 00:12:00 +0000 GMT", "2015-02-18 00:12:00 +0000 GMT",
"2015-02-18 00:12:00 +0000 UTC", "2015-02-18 00:12:00 +0000 UTC",
"3/31/2014", "3/31/2014",
@ -91,6 +92,7 @@ func main() {
| Mon Aug 10 15:44:11 UTC+0100 2015 | 2015-08-10 15:44:11 +0000 UTC | | Mon Aug 10 15:44:11 UTC+0100 2015 | 2015-08-10 15:44:11 +0000 UTC |
| Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time) | 2015-07-03 18:04:07 +0100 GMT | | Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time) | 2015-07-03 18:04:07 +0100 GMT |
| Mon, 02 Jan 2006 15:04:05 -0700 | 2006-01-02 15:04:05 -0700 -0700 | | Mon, 02 Jan 2006 15:04:05 -0700 | 2006-01-02 15:04:05 -0700 -0700 |
| 12 Feb 2006, 19:17 | 2006-02-12 19:17:00 +0000 UTC |
| 2015-02-18 00:12:00 +0000 GMT | 2015-02-18 00:12:00 +0000 UTC | | 2015-02-18 00:12:00 +0000 GMT | 2015-02-18 00:12:00 +0000 UTC |
| 2015-02-18 00:12:00 +0000 UTC | 2015-02-18 00:12:00 +0000 UTC | | 2015-02-18 00:12:00 +0000 UTC | 2015-02-18 00:12:00 +0000 UTC |
| 3/31/2014 | 2014-03-31 00:00:00 +0000 UTC | | 3/31/2014 | 2014-03-31 00:00:00 +0000 UTC |

View File

@ -27,6 +27,7 @@ const (
ST_DIGITSLASHWSCOLON ST_DIGITSLASHWSCOLON
ST_DIGITSLASHWSCOLONCOLON ST_DIGITSLASHWSCOLONCOLON
ST_DIGITSLASHWSCOLONCOLONAMPM ST_DIGITSLASHWSCOLONCOLONAMPM
ST_DIGITALPHA
ST_ALPHA ST_ALPHA
ST_ALPHAWS ST_ALPHAWS
ST_ALPHAWSCOMMA ST_ALPHAWSCOMMA
@ -85,6 +86,9 @@ iterRunes:
case ST_DIGIT: // starts digits case ST_DIGIT: // starts digits
if unicode.IsDigit(r) { if unicode.IsDigit(r) {
continue continue
} else if unicode.IsLetter(r) {
state = ST_DIGITALPHA
continue
} }
switch r { switch r {
case ',': case ',':
@ -255,6 +259,25 @@ iterRunes:
case 'A', 'P': case 'A', 'P':
state = ST_DIGITSLASHWSCOLONCOLONAMPM state = ST_DIGITSLASHWSCOLONCOLONAMPM
} }
case ST_DIGITALPHA:
// 12 Feb 2006, 19:17
// 12 Feb 2006, 19:17:22
switch {
case len(datestr) == len("02 Jan 2006, 15:04"):
if t, err := time.Parse("02 Jan 2006, 15:04", datestr); err == nil {
return t, nil
} else {
return time.Time{}, err
}
case len(datestr) == len("02 Jan 2006, 15:04:05"):
if t, err := time.Parse("02 Jan 2006, 15:04:05", datestr); err == nil {
return t, nil
} else {
return time.Time{}, err
}
default:
//u.LogThrottle(u.WARN, 5, "ST_ALPHAWSALPHA case not found: %v", datestr)
}
case ST_ALPHA: // starts alpha case ST_ALPHA: // starts alpha
// 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

View File

@ -117,6 +117,12 @@ 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, "2006-01-02 22:04:05 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) assert.T(t, "2006-01-02 22:04:05 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
// not sure if this is anything close to a standard, never seen it before
ts, err = ParseAny("12 Feb 2006, 19:17")
assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2006-02-12 19:17:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2015-02-18 00:12:00 +0000 GMT") ts, err = ParseAny("2015-02-18 00:12:00 +0000 GMT")
assert.Tf(t, err == nil, "%v", err) assert.Tf(t, err == nil, "%v", err)
assert.T(t, "2015-02-18 00:12:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) assert.T(t, "2015-02-18 00:12:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))