mirror of
https://github.com/araddon/dateparse.git
synced 2025-04-20 07:40:29 +08:00
merge master
This commit is contained in:
commit
de93f9e0ba
@ -69,6 +69,7 @@ var examples = []string{
|
|||||||
"Mon, 02 Jan 2006 15:04:05 MST",
|
"Mon, 02 Jan 2006 15:04:05 MST",
|
||||||
"Tue, 11 Jul 2017 16:28:13 +0200 (CEST)",
|
"Tue, 11 Jul 2017 16:28:13 +0200 (CEST)",
|
||||||
"Mon, 02 Jan 2006 15:04:05 -0700",
|
"Mon, 02 Jan 2006 15:04:05 -0700",
|
||||||
|
"Thu, 4 Jan 2018 17:53:36 +0000",
|
||||||
"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)",
|
||||||
"12 Feb 2006, 19:17",
|
"12 Feb 2006, 19:17",
|
||||||
@ -80,6 +81,7 @@ var examples = []string{
|
|||||||
"8/1/71",
|
"8/1/71",
|
||||||
"4/8/2014 22:05",
|
"4/8/2014 22:05",
|
||||||
"04/08/2014 22:05",
|
"04/08/2014 22:05",
|
||||||
|
"4/8/14 22:05",
|
||||||
"04/2/2014 03:00:51",
|
"04/2/2014 03:00:51",
|
||||||
"8/8/1965 12:00:00 AM",
|
"8/8/1965 12:00:00 AM",
|
||||||
"8/8/1965 01:00:01 PM",
|
"8/8/1965 01:00:01 PM",
|
||||||
@ -98,6 +100,8 @@ var examples = []string{
|
|||||||
"2014/4/02 03:00:51",
|
"2014/4/02 03:00:51",
|
||||||
"2012/03/19 10:11:59",
|
"2012/03/19 10:11:59",
|
||||||
"2012/03/19 10:11:59.3186369",
|
"2012/03/19 10:11:59.3186369",
|
||||||
|
// Chinese
|
||||||
|
"2014年04月08日",
|
||||||
// yyyy-mm-ddThh
|
// yyyy-mm-ddThh
|
||||||
"2006-01-02T15:04:05+0000",
|
"2006-01-02T15:04:05+0000",
|
||||||
"2009-08-12T22:15:09-07:00",
|
"2009-08-12T22:15:09-07:00",
|
||||||
@ -107,6 +111,7 @@ var examples = []string{
|
|||||||
"2014-04-26 17:24:37.3186369",
|
"2014-04-26 17:24:37.3186369",
|
||||||
"2012-08-03 18:31:59.257000000",
|
"2012-08-03 18:31:59.257000000",
|
||||||
"2014-04-26 17:24:37.123",
|
"2014-04-26 17:24:37.123",
|
||||||
|
"2013-04-01 22:43",
|
||||||
"2013-04-01 22:43:22",
|
"2013-04-01 22:43:22",
|
||||||
"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",
|
||||||
@ -176,6 +181,7 @@ func main() {
|
|||||||
| Mon, 02 Jan 2006 15:04:05 MST | 2006-01-02 15:04:05 +0000 MST |
|
| Mon, 02 Jan 2006 15:04:05 MST | 2006-01-02 15:04:05 +0000 MST |
|
||||||
| Tue, 11 Jul 2017 16:28:13 +0200 (CEST) | 2017-07-11 16:28:13 +0200 +0200 |
|
| Tue, 11 Jul 2017 16:28:13 +0200 (CEST) | 2017-07-11 16:28:13 +0200 +0200 |
|
||||||
| 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 |
|
||||||
|
| Thu, 4 Jan 2018 17:53:36 +0000 | 2018-01-04 17:53:36 +0000 UTC |
|
||||||
| 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 |
|
||||||
| 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 |
|
||||||
@ -186,6 +192,7 @@ func main() {
|
|||||||
| 8/1/71 | 1971-08-01 00:00:00 +0000 UTC |
|
| 8/1/71 | 1971-08-01 00:00:00 +0000 UTC |
|
||||||
| 4/8/2014 22:05 | 2014-04-08 22:05:00 +0000 UTC |
|
| 4/8/2014 22:05 | 2014-04-08 22:05:00 +0000 UTC |
|
||||||
| 04/08/2014 22:05 | 2014-04-08 22:05:00 +0000 UTC |
|
| 04/08/2014 22:05 | 2014-04-08 22:05:00 +0000 UTC |
|
||||||
|
| 4/8/14 22:05 | 2014-04-08 22:05:00 +0000 UTC |
|
||||||
| 04/2/2014 03:00:51 | 2014-04-02 03:00:51 +0000 UTC |
|
| 04/2/2014 03:00:51 | 2014-04-02 03:00:51 +0000 UTC |
|
||||||
| 8/8/1965 12:00:00 AM | 1965-08-08 00:00:00 +0000 UTC |
|
| 8/8/1965 12:00:00 AM | 1965-08-08 00:00:00 +0000 UTC |
|
||||||
| 8/8/1965 01:00:01 PM | 1965-08-08 13:00:01 +0000 UTC |
|
| 8/8/1965 01:00:01 PM | 1965-08-08 13:00:01 +0000 UTC |
|
||||||
@ -203,6 +210,7 @@ func main() {
|
|||||||
| 2014/4/02 03:00:51 | 2014-04-02 03:00:51 +0000 UTC |
|
| 2014/4/02 03:00:51 | 2014-04-02 03:00:51 +0000 UTC |
|
||||||
| 2012/03/19 10:11:59 | 2012-03-19 10:11:59 +0000 UTC |
|
| 2012/03/19 10:11:59 | 2012-03-19 10:11:59 +0000 UTC |
|
||||||
| 2012/03/19 10:11:59.3186369 | 2012-03-19 10:11:59.3186369 +0000 UTC |
|
| 2012/03/19 10:11:59.3186369 | 2012-03-19 10:11:59.3186369 +0000 UTC |
|
||||||
|
| 2014年04月08日 | 2014-04-08 00:00:00 +0000 UTC |
|
||||||
| 2006-01-02T15:04:05+0000 | 2006-01-02 15:04:05 +0000 UTC |
|
| 2006-01-02T15:04:05+0000 | 2006-01-02 15:04:05 +0000 UTC |
|
||||||
| 2009-08-12T22:15:09-07:00 | 2009-08-12 22:15:09 -0700 -0700 |
|
| 2009-08-12T22:15:09-07:00 | 2009-08-12 22:15:09 -0700 -0700 |
|
||||||
| 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 |
|
||||||
@ -210,6 +218,7 @@ func main() {
|
|||||||
| 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 |
|
||||||
| 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 |
|
||||||
| 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 |
|
||||||
|
| 2013-04-01 22:43 | 2013-04-01 22:43:00 +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-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 |
|
||||||
|
@ -56,13 +56,13 @@ func main() {
|
|||||||
|
|
||||||
for name, parser := range parsers {
|
for name, parser := range parsers {
|
||||||
time.Local = nil
|
time.Local = nil
|
||||||
table.AddRow(name, "time.Local = nil", parser(datestr, nil), parser(datestr, nil).In(time.UTC))
|
table.AddRow(name, "time.Local = nil", parser(datestr, nil, false), parser(datestr, nil, true))
|
||||||
if timezone != "" {
|
if timezone != "" {
|
||||||
time.Local = loc
|
time.Local = loc
|
||||||
table.AddRow(name, "time.Local = timezone arg", parser(datestr, loc), parser(datestr, loc).In(time.UTC))
|
table.AddRow(name, "time.Local = timezone arg", parser(datestr, loc, false), parser(datestr, loc, true))
|
||||||
}
|
}
|
||||||
time.Local = time.UTC
|
time.Local = time.UTC
|
||||||
table.AddRow(name, "time.Local = time.UTC", parser(datestr, time.UTC), parser(datestr, time.UTC).In(time.UTC))
|
table.AddRow(name, "time.Local = time.UTC", parser(datestr, time.UTC, false), parser(datestr, time.UTC, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(table.Render())
|
fmt.Println(table.Render())
|
||||||
@ -72,20 +72,38 @@ func stuff() (string, string) {
|
|||||||
return "more", "stuff"
|
return "more", "stuff"
|
||||||
}
|
}
|
||||||
|
|
||||||
type parser func(datestr string, loc *time.Location) time.Time
|
type parser func(datestr string, loc *time.Location, utc bool) string
|
||||||
|
|
||||||
func parseLocal(datestr string, loc *time.Location) time.Time {
|
func parseLocal(datestr string, loc *time.Location, utc bool) string {
|
||||||
time.Local = loc
|
time.Local = loc
|
||||||
t, _ := dateparse.ParseLocal(datestr)
|
t, err := dateparse.ParseLocal(datestr)
|
||||||
return t
|
if err != nil {
|
||||||
|
return err.Error()
|
||||||
|
}
|
||||||
|
if utc {
|
||||||
|
return t.In(time.UTC).String()
|
||||||
|
}
|
||||||
|
return t.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseIn(datestr string, loc *time.Location) time.Time {
|
func parseIn(datestr string, loc *time.Location, utc bool) string {
|
||||||
t, _ := dateparse.ParseIn(datestr, loc)
|
t, err := dateparse.ParseIn(datestr, loc)
|
||||||
return t
|
if err != nil {
|
||||||
|
return err.Error()
|
||||||
|
}
|
||||||
|
if utc {
|
||||||
|
return t.In(time.UTC).String()
|
||||||
|
}
|
||||||
|
return t.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAny(datestr string, loc *time.Location) time.Time {
|
func parseAny(datestr string, loc *time.Location, utc bool) string {
|
||||||
t, _ := dateparse.ParseAny(datestr)
|
t, err := dateparse.ParseAny(datestr)
|
||||||
return t
|
if err != nil {
|
||||||
|
return err.Error()
|
||||||
|
}
|
||||||
|
if utc {
|
||||||
|
return t.In(time.UTC).String()
|
||||||
|
}
|
||||||
|
return t.String()
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ var examples = []string{
|
|||||||
"Mon, 02 Jan 2006 15:04:05 MST",
|
"Mon, 02 Jan 2006 15:04:05 MST",
|
||||||
"Tue, 11 Jul 2017 16:28:13 +0200 (CEST)",
|
"Tue, 11 Jul 2017 16:28:13 +0200 (CEST)",
|
||||||
"Mon, 02 Jan 2006 15:04:05 -0700",
|
"Mon, 02 Jan 2006 15:04:05 -0700",
|
||||||
|
"Thu, 4 Jan 2018 17:53:36 +0000",
|
||||||
"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)",
|
||||||
"12 Feb 2006, 19:17",
|
"12 Feb 2006, 19:17",
|
||||||
@ -29,6 +30,7 @@ var examples = []string{
|
|||||||
"8/1/71",
|
"8/1/71",
|
||||||
"4/8/2014 22:05",
|
"4/8/2014 22:05",
|
||||||
"04/08/2014 22:05",
|
"04/08/2014 22:05",
|
||||||
|
"4/8/14 22:05",
|
||||||
"04/2/2014 03:00:51",
|
"04/2/2014 03:00:51",
|
||||||
"8/8/1965 12:00:00 AM",
|
"8/8/1965 12:00:00 AM",
|
||||||
"8/8/1965 01:00:01 PM",
|
"8/8/1965 01:00:01 PM",
|
||||||
@ -47,6 +49,8 @@ var examples = []string{
|
|||||||
"2014/4/02 03:00:51",
|
"2014/4/02 03:00:51",
|
||||||
"2012/03/19 10:11:59",
|
"2012/03/19 10:11:59",
|
||||||
"2012/03/19 10:11:59.3186369",
|
"2012/03/19 10:11:59.3186369",
|
||||||
|
// Chinese
|
||||||
|
"2014年04月08日",
|
||||||
// yyyy-mm-ddThh
|
// yyyy-mm-ddThh
|
||||||
"2006-01-02T15:04:05+0000",
|
"2006-01-02T15:04:05+0000",
|
||||||
"2009-08-12T22:15:09-07:00",
|
"2009-08-12T22:15:09-07:00",
|
||||||
@ -56,6 +60,7 @@ var examples = []string{
|
|||||||
"2014-04-26 17:24:37.3186369",
|
"2014-04-26 17:24:37.3186369",
|
||||||
"2012-08-03 18:31:59.257000000",
|
"2012-08-03 18:31:59.257000000",
|
||||||
"2014-04-26 17:24:37.123",
|
"2014-04-26 17:24:37.123",
|
||||||
|
"2013-04-01 22:43",
|
||||||
"2013-04-01 22:43:22",
|
"2013-04-01 22:43:22",
|
||||||
"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",
|
||||||
@ -125,6 +130,7 @@ func main() {
|
|||||||
| Mon, 02 Jan 2006 15:04:05 MST | 2006-01-02 15:04:05 +0000 MST |
|
| Mon, 02 Jan 2006 15:04:05 MST | 2006-01-02 15:04:05 +0000 MST |
|
||||||
| Tue, 11 Jul 2017 16:28:13 +0200 (CEST) | 2017-07-11 16:28:13 +0200 +0200 |
|
| Tue, 11 Jul 2017 16:28:13 +0200 (CEST) | 2017-07-11 16:28:13 +0200 +0200 |
|
||||||
| 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 |
|
||||||
|
| Thu, 4 Jan 2018 17:53:36 +0000 | 2018-01-04 17:53:36 +0000 UTC |
|
||||||
| 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 |
|
||||||
| 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 |
|
||||||
@ -135,6 +141,7 @@ func main() {
|
|||||||
| 8/1/71 | 1971-08-01 00:00:00 +0000 UTC |
|
| 8/1/71 | 1971-08-01 00:00:00 +0000 UTC |
|
||||||
| 4/8/2014 22:05 | 2014-04-08 22:05:00 +0000 UTC |
|
| 4/8/2014 22:05 | 2014-04-08 22:05:00 +0000 UTC |
|
||||||
| 04/08/2014 22:05 | 2014-04-08 22:05:00 +0000 UTC |
|
| 04/08/2014 22:05 | 2014-04-08 22:05:00 +0000 UTC |
|
||||||
|
| 4/8/14 22:05 | 2014-04-08 22:05:00 +0000 UTC |
|
||||||
| 04/2/2014 03:00:51 | 2014-04-02 03:00:51 +0000 UTC |
|
| 04/2/2014 03:00:51 | 2014-04-02 03:00:51 +0000 UTC |
|
||||||
| 8/8/1965 12:00:00 AM | 1965-08-08 00:00:00 +0000 UTC |
|
| 8/8/1965 12:00:00 AM | 1965-08-08 00:00:00 +0000 UTC |
|
||||||
| 8/8/1965 01:00:01 PM | 1965-08-08 13:00:01 +0000 UTC |
|
| 8/8/1965 01:00:01 PM | 1965-08-08 13:00:01 +0000 UTC |
|
||||||
@ -152,6 +159,7 @@ func main() {
|
|||||||
| 2014/4/02 03:00:51 | 2014-04-02 03:00:51 +0000 UTC |
|
| 2014/4/02 03:00:51 | 2014-04-02 03:00:51 +0000 UTC |
|
||||||
| 2012/03/19 10:11:59 | 2012-03-19 10:11:59 +0000 UTC |
|
| 2012/03/19 10:11:59 | 2012-03-19 10:11:59 +0000 UTC |
|
||||||
| 2012/03/19 10:11:59.3186369 | 2012-03-19 10:11:59.3186369 +0000 UTC |
|
| 2012/03/19 10:11:59.3186369 | 2012-03-19 10:11:59.3186369 +0000 UTC |
|
||||||
|
| 2014年04月08日 | 2014-04-08 00:00:00 +0000 UTC |
|
||||||
| 2006-01-02T15:04:05+0000 | 2006-01-02 15:04:05 +0000 UTC |
|
| 2006-01-02T15:04:05+0000 | 2006-01-02 15:04:05 +0000 UTC |
|
||||||
| 2009-08-12T22:15:09-07:00 | 2009-08-12 22:15:09 -0700 -0700 |
|
| 2009-08-12T22:15:09-07:00 | 2009-08-12 22:15:09 -0700 -0700 |
|
||||||
| 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 |
|
||||||
@ -159,6 +167,7 @@ func main() {
|
|||||||
| 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 |
|
||||||
| 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 |
|
||||||
| 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 |
|
||||||
|
| 2013-04-01 22:43 | 2013-04-01 22:43:00 +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-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 |
|
||||||
|
63
parseany.go
63
parseany.go
@ -8,15 +8,9 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
"unicode/utf8"
|
||||||
u "github.com/araddon/gou"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
u.SetupLogging("debug")
|
|
||||||
u.SetColorOutput()
|
|
||||||
}
|
|
||||||
|
|
||||||
type dateState int
|
type dateState int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -50,6 +44,8 @@ const (
|
|||||||
stateDigitSlashWSColonAMPM
|
stateDigitSlashWSColonAMPM
|
||||||
stateDigitSlashWSColonColon
|
stateDigitSlashWSColonColon
|
||||||
stateDigitSlashWSColonColonAMPM
|
stateDigitSlashWSColonColonAMPM
|
||||||
|
stateDigitChineseYear
|
||||||
|
stateDigitChineseYearWs
|
||||||
stateDigitAlpha
|
stateDigitAlpha
|
||||||
stateAlpha
|
stateAlpha
|
||||||
stateAlphaWS
|
stateAlphaWS
|
||||||
@ -134,11 +130,11 @@ func parseTime(datestr string, loc *time.Location) (time.Time, error) {
|
|||||||
// we figure it out and then attempt a parse
|
// we figure it out and then attempt a parse
|
||||||
iterRunes:
|
iterRunes:
|
||||||
for i := 0; i < len(datestr); i++ {
|
for i := 0; i < len(datestr); i++ {
|
||||||
r := rune(datestr[i])
|
//r := rune(datestr[i])
|
||||||
// r, bytesConsumed := utf8.DecodeRuneInString(datestr[ri:])
|
r, bytesConsumed := utf8.DecodeRuneInString(datestr[i:])
|
||||||
// if bytesConsumed > 1 {
|
if bytesConsumed > 1 {
|
||||||
// ri += (bytesConsumed - 1)
|
i += (bytesConsumed - 1)
|
||||||
// }
|
}
|
||||||
|
|
||||||
switch state {
|
switch state {
|
||||||
case stateStart:
|
case stateStart:
|
||||||
@ -151,6 +147,11 @@ iterRunes:
|
|||||||
if unicode.IsDigit(r) {
|
if unicode.IsDigit(r) {
|
||||||
continue
|
continue
|
||||||
} else if unicode.IsLetter(r) {
|
} else if unicode.IsLetter(r) {
|
||||||
|
if r == '年' {
|
||||||
|
// Chinese Year
|
||||||
|
state = stateDigitChineseYear
|
||||||
|
continue
|
||||||
|
}
|
||||||
state = stateDigitAlpha
|
state = stateDigitAlpha
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -387,6 +388,7 @@ iterRunes:
|
|||||||
// 04/2/2014 03:00:37
|
// 04/2/2014 03:00:37
|
||||||
// 3/1/2012 10:11:59
|
// 3/1/2012 10:11:59
|
||||||
// 4/8/2014 22:05
|
// 4/8/2014 22:05
|
||||||
|
// 4/8/14 22:05
|
||||||
switch r {
|
switch r {
|
||||||
case ':':
|
case ':':
|
||||||
state = stateDigitSlashWSColon
|
state = stateDigitSlashWSColon
|
||||||
@ -397,6 +399,7 @@ iterRunes:
|
|||||||
// 04/2/2014 03:00:37
|
// 04/2/2014 03:00:37
|
||||||
// 3/1/2012 10:11:59
|
// 3/1/2012 10:11:59
|
||||||
// 4/8/2014 22:05
|
// 4/8/2014 22:05
|
||||||
|
// 4/8/14 22:05
|
||||||
// 3/1/2012 10:11:59 AM
|
// 3/1/2012 10:11:59 AM
|
||||||
switch r {
|
switch r {
|
||||||
case ':':
|
case ':':
|
||||||
@ -410,11 +413,21 @@ iterRunes:
|
|||||||
// 04/2/2014 03:00:37
|
// 04/2/2014 03:00:37
|
||||||
// 3/1/2012 10:11:59
|
// 3/1/2012 10:11:59
|
||||||
// 4/8/2014 22:05
|
// 4/8/2014 22:05
|
||||||
|
// 4/8/14 22:05
|
||||||
// 3/1/2012 10:11:59 AM
|
// 3/1/2012 10:11:59 AM
|
||||||
switch r {
|
switch r {
|
||||||
case 'A', 'P':
|
case 'A', 'P':
|
||||||
state = stateDigitSlashWSColonColonAMPM
|
state = stateDigitSlashWSColonColonAMPM
|
||||||
}
|
}
|
||||||
|
case stateDigitChineseYear:
|
||||||
|
// stateDigitChineseYear
|
||||||
|
// 2014年04月08日
|
||||||
|
// weekday %Y年%m月%e日 %A %I:%M %p
|
||||||
|
// 2013年07月18日 星期四 10:27 上午
|
||||||
|
if r == ' ' {
|
||||||
|
state = stateDigitChineseYearWs
|
||||||
|
break
|
||||||
|
}
|
||||||
case stateDigitAlpha:
|
case stateDigitAlpha:
|
||||||
// 12 Feb 2006, 19:17
|
// 12 Feb 2006, 19:17
|
||||||
// 12 Feb 2006, 19:17:22
|
// 12 Feb 2006, 19:17:22
|
||||||
@ -488,6 +501,7 @@ iterRunes:
|
|||||||
// stateWeekdayAbbrevCommaOffset
|
// stateWeekdayAbbrevCommaOffset
|
||||||
// Mon, 02 Jan 2006 15:04:05 -0700
|
// Mon, 02 Jan 2006 15:04:05 -0700
|
||||||
// Thu, 13 Jul 2017 08:58:40 +0100
|
// Thu, 13 Jul 2017 08:58:40 +0100
|
||||||
|
// Thu, 4 Jan 2018 17:53:36 +0000
|
||||||
// stateWeekdayAbbrevCommaOffsetZone
|
// stateWeekdayAbbrevCommaOffsetZone
|
||||||
// Tue, 11 Jul 2017 16:28:13 +0200 (CEST)
|
// Tue, 11 Jul 2017 16:28:13 +0200 (CEST)
|
||||||
switch {
|
switch {
|
||||||
@ -504,6 +518,7 @@ iterRunes:
|
|||||||
// stateWeekdayAbbrevCommaOffset
|
// stateWeekdayAbbrevCommaOffset
|
||||||
// Mon, 02 Jan 2006 15:04:05 -0700
|
// Mon, 02 Jan 2006 15:04:05 -0700
|
||||||
// Thu, 13 Jul 2017 08:58:40 +0100
|
// Thu, 13 Jul 2017 08:58:40 +0100
|
||||||
|
// Thu, 4 Jan 2018 17:53:36 +0000
|
||||||
// stateWeekdayAbbrevCommaOffsetZone
|
// stateWeekdayAbbrevCommaOffsetZone
|
||||||
// Tue, 11 Jul 2017 16:28:13 +0200 (CEST)
|
// Tue, 11 Jul 2017 16:28:13 +0200 (CEST)
|
||||||
if r == '(' {
|
if r == '(' {
|
||||||
@ -669,7 +684,13 @@ iterRunes:
|
|||||||
}
|
}
|
||||||
case stateDigitDashWs: // starts digit then dash 02- then whitespace 1 << 2 << 5 + 3
|
case stateDigitDashWs: // starts digit then dash 02- then whitespace 1 << 2 << 5 + 3
|
||||||
// 2013-04-01 22:43:22
|
// 2013-04-01 22:43:22
|
||||||
return parse("2006-01-02 15:04:05", datestr, loc)
|
// 2013-04-01 22:43
|
||||||
|
switch len(datestr) {
|
||||||
|
case len("2013-04-01 22:43"):
|
||||||
|
return parse("2006-01-02 15:04", datestr, loc)
|
||||||
|
default:
|
||||||
|
return parse("2006-01-02 15:04:05", datestr, loc)
|
||||||
|
}
|
||||||
|
|
||||||
case stateDigitDashWsWsOffset:
|
case stateDigitDashWsWsOffset:
|
||||||
// 2006-01-02 15:04:05 -0700
|
// 2006-01-02 15:04:05 -0700
|
||||||
@ -814,7 +835,7 @@ iterRunes:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for _, layout := range []string{"01/02/2006 15:04", "01/2/2006 15:04", "1/02/2006 15:04", "1/2/2006 15:04"} {
|
for _, layout := range []string{"01/02/2006 15:04", "01/2/2006 15:04", "1/02/2006 15:04", "1/2/2006 15:04", "1/2/06 15:04", "01/02/06 15:04"} {
|
||||||
if t, err := parse(layout, datestr, loc); err == nil {
|
if t, err := parse(layout, datestr, loc); err == nil {
|
||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
@ -851,6 +872,7 @@ iterRunes:
|
|||||||
// 3/1/2012 10:11:59
|
// 3/1/2012 10:11:59
|
||||||
// 03/1/2012 10:11:59
|
// 03/1/2012 10:11:59
|
||||||
// 3/01/2012 10:11:59
|
// 3/01/2012 10:11:59
|
||||||
|
// 4/8/14 22:05
|
||||||
if part1Len == 4 {
|
if part1Len == 4 {
|
||||||
for _, layout := range []string{"2006/01/02 15:04:05", "2006/1/02 15:04:05", "2006/01/2 15:04:05", "2006/1/2 15:04:05"} {
|
for _, layout := range []string{"2006/01/02 15:04:05", "2006/1/02 15:04:05", "2006/01/2 15:04:05", "2006/1/2 15:04:05"} {
|
||||||
if t, err := parse(layout, datestr, loc); err == nil {
|
if t, err := parse(layout, datestr, loc); err == nil {
|
||||||
@ -886,7 +908,12 @@ iterRunes:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case stateDigitChineseYear:
|
||||||
|
// stateDigitChineseYear
|
||||||
|
// 2014年04月08日
|
||||||
|
return parse("2006年01月02日", datestr, loc)
|
||||||
|
case stateDigitChineseYearWs:
|
||||||
|
return parse("2006年01月02日 15:04:05", datestr, loc)
|
||||||
case stateWeekdayCommaOffset:
|
case stateWeekdayCommaOffset:
|
||||||
// Monday, 02 Jan 2006 15:04:05 -0700
|
// Monday, 02 Jan 2006 15:04:05 -0700
|
||||||
// Monday, 02 Jan 2006 15:04:05 +0100
|
// Monday, 02 Jan 2006 15:04:05 +0100
|
||||||
@ -899,6 +926,12 @@ iterRunes:
|
|||||||
// Mon, 02 Jan 2006 15:04:05 -0700
|
// Mon, 02 Jan 2006 15:04:05 -0700
|
||||||
// Thu, 13 Jul 2017 08:58:40 +0100
|
// Thu, 13 Jul 2017 08:58:40 +0100
|
||||||
// RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
|
// RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
|
||||||
|
//
|
||||||
|
// Thu, 4 Jan 2018 17:53:36 +0000
|
||||||
|
if len(datestr) > 10 && datestr[6] == ' ' {
|
||||||
|
// this really appears to be an invalid RFC1123 with non zero filled day
|
||||||
|
return parse("Mon, 2 Jan 2006 15:04:05 -0700", datestr, loc)
|
||||||
|
}
|
||||||
return parse("Mon, 02 Jan 2006 15:04:05 -0700", datestr, loc)
|
return parse("Mon, 02 Jan 2006 15:04:05 -0700", datestr, loc)
|
||||||
case stateWeekdayAbbrevCommaOffsetZone:
|
case stateWeekdayAbbrevCommaOffsetZone:
|
||||||
// Tue, 11 Jul 2017 16:28:13 +0200 (CEST)
|
// Tue, 11 Jul 2017 16:28:13 +0200 (CEST)
|
||||||
|
@ -14,7 +14,7 @@ import (
|
|||||||
// !!!!! The time-zone of local machine effects the results!
|
// !!!!! The time-zone of local machine effects the results!
|
||||||
// https://play.golang.org/p/IDHRalIyXh
|
// https://play.golang.org/p/IDHRalIyXh
|
||||||
// https://github.com/golang/go/issues/18012
|
// https://github.com/golang/go/issues/18012
|
||||||
func TestParseInLocation(t *testing.T) {
|
func TestInLocation(t *testing.T) {
|
||||||
|
|
||||||
denverLoc, err := time.LoadLocation("America/Denver")
|
denverLoc, err := time.LoadLocation("America/Denver")
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
@ -170,6 +170,9 @@ func TestParse(t *testing.T) {
|
|||||||
ts = MustParse("Mon, 02 Jan 2006 15:04:05 -0700")
|
ts = MustParse("Mon, 02 Jan 2006 15:04:05 -0700")
|
||||||
assert.Equal(t, "2006-01-02 22:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
assert.Equal(t, "2006-01-02 22:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||||
|
|
||||||
|
ts = MustParse("Thu, 4 Jan 2018 17:53:36 +0000")
|
||||||
|
assert.Equal(t, "2018-01-04 17:53:36 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||||
|
|
||||||
// not sure if this is anything close to a standard, never seen it before
|
// not sure if this is anything close to a standard, never seen it before
|
||||||
ts = MustParse("12 Feb 2006, 19:17")
|
ts = MustParse("12 Feb 2006, 19:17")
|
||||||
assert.Equal(t, "2006-02-12 19:17:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
assert.Equal(t, "2006-02-12 19:17:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||||
@ -180,6 +183,15 @@ func TestParse(t *testing.T) {
|
|||||||
ts = MustParse("2013-Feb-03")
|
ts = MustParse("2013-Feb-03")
|
||||||
assert.Equal(t, "2013-02-03 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
assert.Equal(t, "2013-02-03 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||||
|
|
||||||
|
//---------------------------------------------
|
||||||
|
// Chinese 2014年04月18日
|
||||||
|
|
||||||
|
ts = MustParse("2014年04月08日")
|
||||||
|
assert.Equal(t, "2014-04-08 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||||
|
|
||||||
|
ts = MustParse("2014年04月08日 19:17:22")
|
||||||
|
assert.Equal(t, "2014-04-08 19:17:22 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||||
|
|
||||||
//---------------------------------------------
|
//---------------------------------------------
|
||||||
// mm/dd/yyyy ?
|
// mm/dd/yyyy ?
|
||||||
|
|
||||||
@ -199,10 +211,18 @@ func TestParse(t *testing.T) {
|
|||||||
|
|
||||||
ts = MustParse("4/8/2014 22:05")
|
ts = MustParse("4/8/2014 22:05")
|
||||||
assert.Equal(t, "2014-04-08 22:05:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
assert.Equal(t, "2014-04-08 22:05:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||||
|
ts = MustParse("4/18/2014 22:05")
|
||||||
|
assert.Equal(t, "2014-04-18 22:05:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||||
ts = MustParse("04/08/2014 22:05")
|
ts = MustParse("04/08/2014 22:05")
|
||||||
assert.Equal(t, "2014-04-08 22:05:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
assert.Equal(t, "2014-04-08 22:05:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||||
|
|
||||||
|
ts = MustParse("4/8/14 22:05")
|
||||||
|
assert.Equal(t, "2014-04-08 22:05:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||||
|
ts = MustParse("4/18/14 22:05")
|
||||||
|
assert.Equal(t, "2014-04-18 22:05:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||||
|
ts = MustParse("10/18/14 22:05")
|
||||||
|
assert.Equal(t, "2014-10-18 22:05:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||||
|
|
||||||
ts = MustParse("04/2/2014 4:00:51")
|
ts = MustParse("04/2/2014 4:00:51")
|
||||||
assert.Equal(t, "2014-04-02 04:00:51 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
assert.Equal(t, "2014-04-02 04:00:51 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||||
|
|
||||||
@ -348,6 +368,9 @@ func TestParse(t *testing.T) {
|
|||||||
ts = MustParse("2012-08-03 18:31:59.257000000")
|
ts = MustParse("2012-08-03 18:31:59.257000000")
|
||||||
assert.Equal(t, "2012-08-03 18:31:59.257 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
assert.Equal(t, "2012-08-03 18:31:59.257 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||||
|
|
||||||
|
ts = MustParse("2013-04-01 22:43")
|
||||||
|
assert.Equal(t, "2013-04-01 22:43:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||||
|
|
||||||
ts = MustParse("2013-04-01 22:43:22")
|
ts = MustParse("2013-04-01 22:43:22")
|
||||||
assert.Equal(t, "2013-04-01 22:43:22 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
assert.Equal(t, "2013-04-01 22:43:22 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user