Merge branch 'master' of github.com:araddon/dateparse

This commit is contained in:
Aaron Raddon 2017-06-25 19:10:08 -07:00
commit f0482b5ca2
4 changed files with 145 additions and 122 deletions

View File

@ -68,6 +68,8 @@ var examples = []string{
"2014-12-16 06:20:00 UTC", "2014-12-16 06:20:00 UTC",
"2014-12-16 06:20:00 GMT", "2014-12-16 06:20:00 GMT",
"2014-04-26 05:24:37 PM", "2014-04-26 05:24:37 PM",
"2014-04-26 13:13:43 +0800",
"2014-04-26 13:13:44 +09:00",
"2014-04-26", "2014-04-26",
"2014-04", "2014-04",
"2014", "2014",
@ -96,9 +98,9 @@ func main() {
**outputs** **outputs**
``` ```
+-------------------------------------------------------+----------------------------------------+ +-------------------------------------------------------+------------------------------------------+
| Input | Parsed, and Output as %v | | Input | Parsed, and Output as %v |
+-------------------------------------------------------+----------------------------------------+ +-------------------------------------------------------+------------------------------------------+
| May 8, 2009 5:57:51 PM | 2009-05-08 17:57:51 +0000 UTC | | May 8, 2009 5:57:51 PM | 2009-05-08 17:57:51 +0000 UTC |
| Mon Jan 2 15:04:05 2006 | 2006-01-02 15:04:05 +0000 UTC | | Mon Jan 2 15:04:05 2006 | 2006-01-02 15:04:05 +0000 UTC |
| Mon Jan 2 15:04:05 MST 2006 | 2006-01-02 15:04:05 +0000 MST | | Mon Jan 2 15:04:05 MST 2006 | 2006-01-02 15:04:05 +0000 MST |
@ -109,8 +111,8 @@ func main() {
| 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 | | 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 +0000 |
| 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 +0000 |
| 3/31/2014 | 2014-03-31 00:00:00 +0000 UTC | | 3/31/2014 | 2014-03-31 00:00:00 +0000 UTC |
| 03/31/2014 | 2014-03-31 00:00:00 +0000 UTC | | 03/31/2014 | 2014-03-31 00:00:00 +0000 UTC |
| 08/21/71 | 1971-08-21 00:00:00 +0000 UTC | | 08/21/71 | 1971-08-21 00:00:00 +0000 UTC |
@ -139,14 +141,16 @@ func main() {
| 2009-08-12T22:15:09 | 2009-08-12 22:15:09 +0000 UTC | | 2009-08-12T22:15:09 | 2009-08-12 22:15:09 +0000 UTC |
| 2014-04-26 17:24:37.3186369 | 2014-04-26 17:24:37.3186369 +0000 UTC | | 2014-04-26 17:24:37.3186369 | 2014-04-26 17:24:37.3186369 +0000 UTC |
| 2014-04-26 17:24:37.318636 | 2014-04-26 17:24:37.318636 +0000 UTC | | 2014-04-26 17:24:37.318636 | 2014-04-26 17:24:37.318636 +0000 UTC |
| 2012-08-03 18:31:59.257000000 +0000 UTC | 2012-08-03 18:31:59.257 +0000 UTC | | 2012-08-03 18:31:59.257000000 +0000 UTC | 2012-08-03 18:31:59.257 +0000 +0000 |
| 2015-09-30 18:48:56.35272715 +0000 UTC | 2015-09-30 18:48:56.35272715 +0000 UTC | | 2015-09-30 18:48:56.35272715 +0000 UTC | 2015-09-30 18:48:56.35272715 +0000 +0000 |
| 2012-08-03 18:31:59.257000000 | 2012-08-03 18:31:59.257 +0000 UTC | | 2012-08-03 18:31:59.257000000 | 2012-08-03 18:31:59.257 +0000 UTC |
| 2013-04-01 22:43:22 | 2013-04-01 22:43:22 +0000 UTC | | 2013-04-01 22:43:22 | 2013-04-01 22:43:22 +0000 UTC |
| 2014-04-26 17:24:37.123 | 2014-04-26 17:24:37.123 +0000 UTC | | 2014-04-26 17:24:37.123 | 2014-04-26 17:24:37.123 +0000 UTC |
| 2014-12-16 06:20:00 UTC | 2014-12-16 06:20:00 +0000 UTC | | 2014-12-16 06:20:00 UTC | 2014-12-16 06:20:00 +0000 UTC |
| 2014-12-16 06:20:00 GMT | 2014-12-16 06:20:00 +0000 UTC | | 2014-12-16 06:20:00 GMT | 2014-12-16 06:20:00 +0000 UTC |
| 2014-04-26 05:24:37 PM | 2014-04-26 17:24:37 +0000 UTC | | 2014-04-26 05:24:37 PM | 2014-04-26 17:24:37 +0000 UTC |
| 2014-04-26 13:13:43 +0800 | 2014-04-26 13:13:43 +0800 +0800 |
| 2014-04-26 13:13:44 +09:00 | 2014-04-26 13:13:44 +0900 +0900 |
| 2014-04-26 | 2014-04-26 00:00:00 +0000 UTC | | 2014-04-26 | 2014-04-26 00:00:00 +0000 UTC |
| 2014-04 | 2014-04-01 00:00:00 +0000 UTC | | 2014-04 | 2014-04-01 00:00:00 +0000 UTC |
| 2014 | 2014-01-01 00:00:00 +0000 UTC | | 2014 | 2014-01-01 00:00:00 +0000 UTC |
@ -154,6 +158,6 @@ func main() {
| 20140601 | 2014-06-01 00:00:00 +0000 UTC | | 20140601 | 2014-06-01 00:00:00 +0000 UTC |
| 1332151919 | 2012-03-19 03:11:59 -0700 PDT | | 1332151919 | 2012-03-19 03:11:59 -0700 PDT |
| 1384216367189 | 2013-11-11 16:32:47.189 -0800 PST | | 1384216367189 | 2013-11-11 16:32:47.189 -0800 PST |
+-------------------------------------------------------+----------------------------------------+ +-------------------------------------------------------+------------------------------------------+
``` ```

View File

@ -58,6 +58,8 @@ var examples = []string{
"2014-12-16 06:20:00 UTC", "2014-12-16 06:20:00 UTC",
"2014-12-16 06:20:00 GMT", "2014-12-16 06:20:00 GMT",
"2014-04-26 05:24:37 PM", "2014-04-26 05:24:37 PM",
"2014-04-26 13:13:43 +0800",
"2014-04-26 13:13:44 +09:00",
"2014-04-26", "2014-04-26",
"2014-04", "2014-04",
"2014", "2014",
@ -84,9 +86,9 @@ func main() {
} }
/* /*
+-------------------------------------------------------+----------------------------------------+ +-------------------------------------------------------+------------------------------------------+
| Input | Parsed, and Output as %v | | Input | Parsed, and Output as %v |
+-------------------------------------------------------+----------------------------------------+ +-------------------------------------------------------+------------------------------------------+
| May 8, 2009 5:57:51 PM | 2009-05-08 17:57:51 +0000 UTC | | May 8, 2009 5:57:51 PM | 2009-05-08 17:57:51 +0000 UTC |
| Mon Jan 2 15:04:05 2006 | 2006-01-02 15:04:05 +0000 UTC | | Mon Jan 2 15:04:05 2006 | 2006-01-02 15:04:05 +0000 UTC |
| Mon Jan 2 15:04:05 MST 2006 | 2006-01-02 15:04:05 +0000 MST | | Mon Jan 2 15:04:05 MST 2006 | 2006-01-02 15:04:05 +0000 MST |
@ -97,8 +99,8 @@ func main() {
| 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 | | 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 +0000 |
| 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 +0000 |
| 3/31/2014 | 2014-03-31 00:00:00 +0000 UTC | | 3/31/2014 | 2014-03-31 00:00:00 +0000 UTC |
| 03/31/2014 | 2014-03-31 00:00:00 +0000 UTC | | 03/31/2014 | 2014-03-31 00:00:00 +0000 UTC |
| 08/21/71 | 1971-08-21 00:00:00 +0000 UTC | | 08/21/71 | 1971-08-21 00:00:00 +0000 UTC |
@ -127,14 +129,16 @@ func main() {
| 2009-08-12T22:15:09 | 2009-08-12 22:15:09 +0000 UTC | | 2009-08-12T22:15:09 | 2009-08-12 22:15:09 +0000 UTC |
| 2014-04-26 17:24:37.3186369 | 2014-04-26 17:24:37.3186369 +0000 UTC | | 2014-04-26 17:24:37.3186369 | 2014-04-26 17:24:37.3186369 +0000 UTC |
| 2014-04-26 17:24:37.318636 | 2014-04-26 17:24:37.318636 +0000 UTC | | 2014-04-26 17:24:37.318636 | 2014-04-26 17:24:37.318636 +0000 UTC |
| 2012-08-03 18:31:59.257000000 +0000 UTC | 2012-08-03 18:31:59.257 +0000 UTC | | 2012-08-03 18:31:59.257000000 +0000 UTC | 2012-08-03 18:31:59.257 +0000 +0000 |
| 2015-09-30 18:48:56.35272715 +0000 UTC | 2015-09-30 18:48:56.35272715 +0000 UTC | | 2015-09-30 18:48:56.35272715 +0000 UTC | 2015-09-30 18:48:56.35272715 +0000 +0000 |
| 2012-08-03 18:31:59.257000000 | 2012-08-03 18:31:59.257 +0000 UTC | | 2012-08-03 18:31:59.257000000 | 2012-08-03 18:31:59.257 +0000 UTC |
| 2013-04-01 22:43:22 | 2013-04-01 22:43:22 +0000 UTC | | 2013-04-01 22:43:22 | 2013-04-01 22:43:22 +0000 UTC |
| 2014-04-26 17:24:37.123 | 2014-04-26 17:24:37.123 +0000 UTC | | 2014-04-26 17:24:37.123 | 2014-04-26 17:24:37.123 +0000 UTC |
| 2014-12-16 06:20:00 UTC | 2014-12-16 06:20:00 +0000 UTC | | 2014-12-16 06:20:00 UTC | 2014-12-16 06:20:00 +0000 UTC |
| 2014-12-16 06:20:00 GMT | 2014-12-16 06:20:00 +0000 UTC | | 2014-12-16 06:20:00 GMT | 2014-12-16 06:20:00 +0000 UTC |
| 2014-04-26 05:24:37 PM | 2014-04-26 17:24:37 +0000 UTC | | 2014-04-26 05:24:37 PM | 2014-04-26 17:24:37 +0000 UTC |
| 2014-04-26 13:13:43 +0800 | 2014-04-26 13:13:43 +0800 +0800 |
| 2014-04-26 13:13:44 +09:00 | 2014-04-26 13:13:44 +0900 +0900 |
| 2014-04-26 | 2014-04-26 00:00:00 +0000 UTC | | 2014-04-26 | 2014-04-26 00:00:00 +0000 UTC |
| 2014-04 | 2014-04-01 00:00:00 +0000 UTC | | 2014-04 | 2014-04-01 00:00:00 +0000 UTC |
| 2014 | 2014-01-01 00:00:00 +0000 UTC | | 2014 | 2014-01-01 00:00:00 +0000 UTC |
@ -142,5 +146,5 @@ func main() {
| 20140601 | 2014-06-01 00:00:00 +0000 UTC | | 20140601 | 2014-06-01 00:00:00 +0000 UTC |
| 1332151919 | 2012-03-19 03:11:59 -0700 PDT | | 1332151919 | 2012-03-19 03:11:59 -0700 PDT |
| 1384216367189 | 2013-11-11 16:32:47.189 -0800 PST | | 1384216367189 | 2013-11-11 16:32:47.189 -0800 PST |
+-------------------------------------------------------+----------------------------------------+ +-------------------------------------------------------+------------------------------------------+
*/ */

View File

@ -621,11 +621,18 @@ iterRunes:
} }
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
// 2013-04-01 22:43:22 // 2013-04-01 22:43:22
if t, err := time.Parse("2006-01-02 15:04:05", datestr); err == nil { var t time.Time
return t, nil var err error
} else {
return time.Time{}, err switch len(datestr) {
case len("2006-01-02 15:04:05"):
t, err = time.Parse("2006-01-02 15:04:05", datestr)
case len("2006-01-02 15:04:05 -0700"):
t, err = time.Parse("2006-01-02 15:04:05 -0700", datestr)
case len("2006-01-02 15:04:05 -07:00"):
t, err = time.Parse("2006-01-02 15:04:05 -07:00", datestr)
} }
return t, err
case ST_DIGITDASHWSALPHA: // starts digit then dash 02- then whitespace 1 << 2 << 5 + 3 case ST_DIGITDASHWSALPHA: // starts digit then dash 02- then whitespace 1 << 2 << 5 + 3
// 2014-12-16 06:20:00 UTC // 2014-12-16 06:20:00 UTC
// 2015-02-18 00:12:00 +0000 UTC // 2015-02-18 00:12:00 +0000 UTC

View File

@ -389,6 +389,14 @@ func TestParse(t *testing.T) {
assertf(t, err == nil, "%v", err) assertf(t, err == nil, "%v", err)
assert(t, "2014-05-11 08:20:13.787 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) assert(t, "2014-05-11 08:20:13.787 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2014-05-11 08:20:13 +00:00")
assertf(t, err == nil, "%v", err)
assert(t, "2014-05-11 08:20:13 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2014-05-11 08:20:13 +0000")
assertf(t, err == nil, "%v", err)
assert(t, "2014-05-11 08:20:13 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
// yyyymmdd and similar // yyyymmdd and similar
ts, err = ParseAny("2014") ts, err = ParseAny("2014")
assertf(t, err == nil, "%v", err) assertf(t, err == nil, "%v", err)