new format april 3, 2018 closes #59

This commit is contained in:
Aaron Raddon 2018-05-23 21:11:15 -07:00
parent 1e64c9214a
commit 5025a49c02
4 changed files with 64 additions and 11 deletions

View File

@ -85,6 +85,7 @@ var examples = []string{
"Mon Aug 10 15:44:11 UTC+0100 2015",
"Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)",
"September 17, 2012 at 10:09am PST-08",
"October 7, 1970",
"12 Feb 2006, 19:17",
"12 Feb 2006 19:17",
"7 oct 70",
@ -207,6 +208,7 @@ func main() {
| 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 |
| September 17, 2012 at 10:09am PST-08 | 2012-09-17 10:09:00 -0800 PST |
| October 7, 1970 | 1970-10-07 00:00:00 +0000 UTC |
| 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 |
| 7 oct 70 | 1970-10-07 00:00:00 +0000 UTC |

View File

@ -24,6 +24,7 @@ var examples = []string{
"Mon Aug 10 15:44:11 UTC+0100 2015",
"Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)",
"September 17, 2012 at 10:09am PST-08",
"October 7, 1970",
"12 Feb 2006, 19:17",
"12 Feb 2006 19:17",
"7 oct 70",
@ -146,6 +147,7 @@ func main() {
| 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 |
| September 17, 2012 at 10:09am PST-08 | 2012-09-17 10:09:00 -0800 PST |
| October 7, 1970 | 1970-10-07 00:00:00 +0000 UTC |
| 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 |
| 7 oct 70 | 1970-10-07 00:00:00 +0000 UTC |

View File

@ -6,6 +6,7 @@ package dateparse
import (
"fmt"
"strconv"
"strings"
"time"
"unicode"
"unicode/utf8"
@ -16,6 +17,21 @@ import (
// gou.SetColorOutput()
// }
var months = []string{
"january",
"february",
"march",
"april",
"may",
"june",
"july",
"august",
"september",
"october",
"november",
"december",
}
type dateState uint8
type timeState uint8
@ -41,6 +57,7 @@ const (
dateAlphaWsDigitComma
dateAlphaWsDigitCommaWs
dateAlphaWsDigitCommaWsYear
dateAlphaWsMonth
dateAlphaWsAlpha
dateAlphaWsAlphaYearmaybe
dateWeekdayComma
@ -628,7 +645,10 @@ iterRunes:
// dateAlphaWSDigit
// May 8, 2009 5:57:51 PM
// oct 1, 1970
//
// dateAlphaWsMonth
// April 8, 2009
// dateAlphaWsMonthTime
// January 02, 2006 at 3:04pm MST-07
// dateWeekdayComma
// Monday, 02 Jan 2006 15:04:05 MST
// Monday, 02-Jan-06 15:04:05 MST
@ -643,6 +663,19 @@ iterRunes:
switch {
case r == ' ':
if i > 4 {
prefix := strings.ToLower(datestr[0:i])
for _, month := range months {
if prefix == month {
// len(" 31, 2018") = 9
if len(datestr[i:]) < 10 {
// April 8, 2009
p.dayi = i + 1
p.stateDate = dateAlphaWsMonth
break
}
}
}
if p.stateDate != dateAlphaWsMonth {
// September 17, 2012 at 5:00pm UTC-05
// This one doesn't follow standard parse methodologies. the "January"
// is difficult to use the format string replace method because of its variable-length (march, june)
@ -650,7 +683,10 @@ iterRunes:
p.format = []byte("January 02, 2006 at 3:04pm MST-07")
return p, nil
}
} else {
p.stateDate = dateAlphaWs
}
case r == ',':
// p.moi = 0
// p.molen = i
@ -667,6 +703,16 @@ iterRunes:
// just lay down the skip, no need to fill and then skip
}
}
case dateAlphaWsMonth:
// April 8, 2009
if r == ',' {
if i-p.dayi == 2 {
p.format = []byte("January 02, 2006")
return p, nil
}
p.format = []byte("January 2, 2006")
return p, nil
}
case dateWeekdayComma:
// Monday, 02 Jan 2006 15:04:05 MST
// Monday, 02 Jan 2006 15:04:05 -0700

View File

@ -13,8 +13,8 @@ func TestOne(t *testing.T) {
var ts time.Time
// {in: "2015-02-08 03:02:00 +0300 MSK m=+0.000000001", out: "2015-02-08 00:02:00 +0000 UTC"},
// {in: "2015-02-08 03:02:00.001 +0300 MSK m=+0.000000001", out: "2015-02-08 00:02:00.001 +0000 UTC"},
ts = MustParse("2018.05")
assert.Equal(t, "2018-05-01 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
ts = MustParse("April 3, 2018")
assert.Equal(t, "2018-04-03 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
}
type dateTest struct {
@ -58,9 +58,12 @@ var testInputs = []dateTest{
{in: "Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)", out: "2015-07-03 17:04:07 +0000 UTC"},
{in: "Fri Jul 3 2015 06:04:07 GMT+0100 (GMT Daylight Time)", out: "2015-07-03 05:04:07 +0000 UTC"},
{in: "Fri Jul 3 2015 06:04:07 PST-0700 (Pacific Daylight Time)", out: "2015-07-03 13:04:07 +0000 UTC"},
// Month dd, yyyy
// Month dd, yyyy time
{in: "September 17, 2012 at 5:00pm UTC-05", out: "2012-09-17 17:00:00 +0000 UTC"},
{in: "September 17, 2012 at 10:09am PST-08", out: "2012-09-17 18:09:00 +0000 UTC"},
// Monty dd, yyyy
{in: "September 17, 2012", out: "2012-09-17 00:00:00 +0000 UTC"},
{in: "May 7, 2012", out: "2012-05-07 00:00:00 +0000 UTC"},
// ?
{in: "Fri, 03 Jul 2015 08:08:08 MST", out: "2015-07-03 08:08:08 +0000 UTC"},
{in: "Fri, 03 Jul 2015 08:08:08 PST", out: "2015-07-03 15:08:08 +0000 UTC", loc: "America/Los_Angeles"},