mm.dd.yyyy format closes #34 (pr #35)

This commit is contained in:
Aaron Raddon 2018-01-11 13:21:04 -08:00 committed by GitHub
parent 031c380915
commit afa6a0f934
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 7 deletions

View File

@ -127,6 +127,10 @@ var examples = []string{
"2014-04", "2014-04",
"2014", "2014",
"2014-05-11 08:20:13,787", "2014-05-11 08:20:13,787",
// mm.dd.yy
"3.31.2014",
"03.31.2014",
"08.21.71",
// yyyymmdd and similar // yyyymmdd and similar
"20140601", "20140601",
// unix seconds, ms // unix seconds, ms
@ -230,6 +234,9 @@ func main() {
| 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 |
| 2014-05-11 08:20:13,787 | 2014-05-11 08:20:13.787 +0000 UTC | | 2014-05-11 08:20:13,787 | 2014-05-11 08:20:13.787 +0000 UTC |
| 3.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 |
| 20140601 | 2014-06-01 00:00:00 +0000 UTC | | 20140601 | 2014-06-01 00:00:00 +0000 UTC |
| 1332151919 | 2012-03-19 10:11:59 +0000 UTC | | 1332151919 | 2012-03-19 10:11:59 +0000 UTC |
| 1384216367189 | 2013-11-12 00:32:47.189 +0000 UTC | | 1384216367189 | 2013-11-12 00:32:47.189 +0000 UTC |

View File

@ -76,6 +76,10 @@ var examples = []string{
"2014-04", "2014-04",
"2014", "2014",
"2014-05-11 08:20:13,787", "2014-05-11 08:20:13,787",
// mm.dd.yy
"3.31.2014",
"03.31.2014",
"08.21.71",
// yyyymmdd and similar // yyyymmdd and similar
"20140601", "20140601",
// unix seconds, ms // unix seconds, ms
@ -179,6 +183,9 @@ func main() {
| 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 |
| 2014-05-11 08:20:13,787 | 2014-05-11 08:20:13.787 +0000 UTC | | 2014-05-11 08:20:13,787 | 2014-05-11 08:20:13.787 +0000 UTC |
| 3.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 |
| 20140601 | 2014-06-01 00:00:00 +0000 UTC | | 20140601 | 2014-06-01 00:00:00 +0000 UTC |
| 1332151919 | 2012-03-19 10:11:59 +0000 UTC | | 1332151919 | 2012-03-19 10:11:59 +0000 UTC |
| 1384216367189 | 2013-11-12 00:32:47.189 +0000 UTC | | 1384216367189 | 2013-11-12 00:32:47.189 +0000 UTC |

View File

@ -36,6 +36,8 @@ const (
stateDigitDashTZDigit stateDigitDashTZDigit
stateDigitDashTOffset stateDigitDashTOffset
stateDigitDashTOffsetColon stateDigitDashTOffsetColon
stateDigitDot
stateDigitDotDot
stateDigitSlash stateDigitSlash
stateDigitSlashWS stateDigitSlashWS
stateDigitSlashWSColon stateDigitSlashWSColon
@ -119,7 +121,8 @@ func parse(layout, datestr string, loc *time.Location) (time.Time, error) {
func parseTime(datestr string, loc *time.Location) (time.Time, error) { func parseTime(datestr string, loc *time.Location) (time.Time, error) {
state := stateStart state := stateStart
firstSlash := 0 part1Len := 0
part2Len := 0
// General strategy is to read rune by rune through the date looking for // General strategy is to read rune by rune through the date looking for
// certain hints of what type of date we are dealing with. // certain hints of what type of date we are dealing with.
@ -157,7 +160,10 @@ iterRunes:
state = stateDigitDash state = stateDigitDash
case '/': case '/':
state = stateDigitSlash state = stateDigitSlash
firstSlash = i part1Len = i
case '.':
state = stateDigitDot
part1Len = i
} }
case stateDigitDash: // starts digit then dash 02- case stateDigitDash: // starts digit then dash 02-
// 2006-01-02T15:04:05Z07:00 // 2006-01-02T15:04:05Z07:00
@ -431,6 +437,14 @@ iterRunes:
case len(datestr) == len("02 Jan 2006, 15:04:05"): case len(datestr) == len("02 Jan 2006, 15:04:05"):
return parse("02 Jan 2006, 15:04:05", datestr, loc) return parse("02 Jan 2006, 15:04:05", datestr, loc)
} }
case stateDigitDot:
// 3.31.2014
if r == '.' {
state = stateDigitDotDot
part2Len = i
}
case stateDigitDotDot:
// iterate all the way through
case stateAlpha: // starts alpha case stateAlpha: // starts alpha
// stateAlphaWS // stateAlphaWS
// Mon Jan _2 15:04:05 2006 // Mon Jan _2 15:04:05 2006
@ -747,6 +761,26 @@ iterRunes:
// 2016-03-14 00:00:00.000 +0000 UTC // 2016-03-14 00:00:00.000 +0000 UTC
return parse("2006-01-02 15:04:05 -0700 UTC", datestr, loc) return parse("2006-01-02 15:04:05 -0700 UTC", datestr, loc)
case stateDigitDotDot:
//u.Infof("%s lty?%v", datestr, len(datestr)-part2Len)
switch {
case len(datestr) == len("01.02.2006"):
return parse("01.02.2006", datestr, loc)
case len(datestr)-part2Len == 3:
for _, layout := range []string{"01.02.06", "1.02.06", "01.2.06", "1.2.06"} {
if t, err := parse(layout, datestr, loc); err == nil {
return t, nil
}
}
default:
for _, layout := range []string{"1.02.2006", "01.2.2006", "1.2.2006"} {
if t, err := parse(layout, datestr, loc); err == nil {
return t, nil
}
}
}
case stateAlphaWSAlphaColon: case stateAlphaWSAlphaColon:
// Mon Jan _2 15:04:05 2006 // Mon Jan _2 15:04:05 2006
return parse(time.ANSIC, datestr, loc) return parse(time.ANSIC, datestr, loc)
@ -776,7 +810,7 @@ iterRunes:
// 10/13/2014 // 10/13/2014
// 01/02/2006 // 01/02/2006
// 2014/10/13 // 2014/10/13
if firstSlash == 4 { if part1Len == 4 {
if len(datestr) == len("2006/01/02") { if len(datestr) == len("2006/01/02") {
return parse("2006/01/02", datestr, loc) return parse("2006/01/02", datestr, loc)
} }
@ -794,7 +828,7 @@ iterRunes:
// 2014/4/8 22:05 // 2014/4/8 22:05
// 2014/04/08 22:05 // 2014/04/08 22:05
if firstSlash == 4 { if part1Len == 4 {
for _, layout := range []string{"2006/01/02 15:04", "2006/1/2 15:04", "2006/01/2 15:04", "2006/1/02 15:04"} { for _, layout := range []string{"2006/01/02 15:04", "2006/1/2 15:04", "2006/01/2 15:04", "2006/1/02 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
@ -815,7 +849,7 @@ iterRunes:
// 2014/4/8 22:05 PM // 2014/4/8 22:05 PM
// 2014/04/08 22:05 PM // 2014/04/08 22:05 PM
if firstSlash == 4 { if part1Len == 4 {
for _, layout := range []string{"2006/01/02 03:04 PM", "2006/01/2 03:04 PM", "2006/1/02 03:04 PM", "2006/1/2 03:04 PM", for _, layout := range []string{"2006/01/02 03:04 PM", "2006/01/2 03:04 PM", "2006/1/02 03:04 PM", "2006/1/2 03:04 PM",
"2006/01/02 3:04 PM", "2006/01/2 3:04 PM", "2006/1/02 3:04 PM", "2006/1/2 3:04 PM"} { "2006/01/02 3:04 PM", "2006/01/2 3:04 PM", "2006/1/02 3:04 PM", "2006/1/2 3:04 PM"} {
if t, err := parse(layout, datestr, loc); err == nil { if t, err := parse(layout, datestr, loc); err == nil {
@ -839,7 +873,7 @@ iterRunes:
// 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 // 4/8/14 22:05
if firstSlash == 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 {
return t, nil return t, nil
@ -860,7 +894,7 @@ iterRunes:
// 03/1/2012 10:11:59 PM // 03/1/2012 10:11:59 PM
// 3/01/2012 10:11:59 PM // 3/01/2012 10:11:59 PM
if firstSlash == 4 { if part1Len == 4 {
for _, layout := range []string{"2006/01/02 03:04:05 PM", "2006/1/02 03:04:05 PM", "2006/01/2 03:04:05 PM", "2006/1/2 03:04:05 PM", for _, layout := range []string{"2006/01/02 03:04:05 PM", "2006/1/02 03:04:05 PM", "2006/01/2 03:04:05 PM", "2006/1/2 03:04:05 PM",
"2006/01/02 3:04:05 PM", "2006/1/02 3:04:05 PM", "2006/01/2 3:04:05 PM", "2006/1/2 3:04:05 PM"} { "2006/01/02 3:04:05 PM", "2006/1/02 3:04:05 PM", "2006/01/2 3:04:05 PM", "2006/1/2 3:04:05 PM"} {
if t, err := parse(layout, datestr, loc); err == nil { if t, err := parse(layout, datestr, loc); err == nil {

View File

@ -466,6 +466,21 @@ func TestParse(t *testing.T) {
ts = MustParse("2016-06-21T19:55:00.799+0100") ts = MustParse("2016-06-21T19:55:00.799+0100")
assert.Equal(t, "2016-06-21 18:55:00.799 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) assert.Equal(t, "2016-06-21 18:55:00.799 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
//---------------------------------------------
// mm.dd.yyyy
// mm.dd.yy
ts = MustParse("3.31.2014")
assert.Equal(t, "2014-03-31 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
ts = MustParse("03.31.2014")
assert.Equal(t, "2014-03-31 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
ts = MustParse("08.21.71")
assert.Equal(t, "1971-08-21 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
//---------------------------------------------
// yyyymmdd and similar // yyyymmdd and similar
ts = MustParse("2014") ts = MustParse("2014")
assert.Equal(t, "2014-01-01 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) assert.Equal(t, "2014-01-01 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))