fix time-zone of "Fri, 5 Jan 2018 14:09:32 -0700 (MST)" format closes #39

This commit is contained in:
Aaron Raddon 2018-02-09 18:08:04 -08:00
parent a7818d843a
commit 036f821411
2 changed files with 74 additions and 42 deletions

View File

@ -61,8 +61,10 @@ const (
stateAlphaWSAlphaColonAlphaOffset stateAlphaWSAlphaColonAlphaOffset
stateAlphaWSAlphaColonAlphaOffsetAlpha stateAlphaWSAlphaColonAlphaOffsetAlpha
stateWeekdayComma stateWeekdayComma
stateWeekdayCommaDash
stateWeekdayCommaOffset stateWeekdayCommaOffset
stateWeekdayAbbrevComma stateWeekdayAbbrevComma
stateWeekdayAbbrevCommaDash
stateWeekdayAbbrevCommaOffset stateWeekdayAbbrevCommaOffset
stateWeekdayAbbrevCommaOffsetZone stateWeekdayAbbrevCommaOffsetZone
) )
@ -127,6 +129,7 @@ func parseTime(datestr string, loc *time.Location) (time.Time, error) {
part1Len := 0 part1Len := 0
part2Len := 0 part2Len := 0
dayLen := 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.
@ -495,13 +498,16 @@ iterRunes:
// May 8, 2009 5:57:51 PM // May 8, 2009 5:57:51 PM
// //
// stateWeekdayComma // stateWeekdayComma
// Monday, 02 Jan 2006 15:04:05 MST
// stateWeekdayCommaDash
// Monday, 02-Jan-06 15:04:05 MST // Monday, 02-Jan-06 15:04:05 MST
// stateWeekdayCommaOffset // 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
// stateWeekdayAbbrevComma // stateWeekdayAbbrevComma
// Mon, 02-Jan-06 15:04:05 MST
// Mon, 02 Jan 2006 15:04:05 MST // Mon, 02 Jan 2006 15:04:05 MST
// stateWeekdayAbbrevCommaDash
// Mon, 02-Jan-06 15:04:05 MST
// 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
@ -513,47 +519,35 @@ iterRunes:
case r == ' ': case r == ' ':
state = stateAlphaWS state = stateAlphaWS
case r == ',': case r == ',':
part1Len = i
if i == 3 { if i == 3 {
state = stateWeekdayAbbrevComma state = stateWeekdayAbbrevComma
} else { } else {
state = stateWeekdayComma state = stateWeekdayComma
} }
i++
} }
case stateWeekdayComma: // Starts alpha then comma case stateWeekdayComma: // Starts alpha then comma
// Mon, 02-Jan-06 15:04:05 MST // Monday, 02 Jan 2006 15:04:05 MST
// Mon, 02 Jan 2006 15:04:05 MST // stateWeekdayCommaDash
// Monday, 02-Jan-06 15:04:05 MST
// stateWeekdayCommaOffset // 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
switch { switch {
case r == '-': case r == '-':
if i < 15 { if i < 15 {
for _, layout := range []string{ state = stateWeekdayCommaDash
"Monday, 02-Jan-06 15:04:05 MST", break iterRunes
"Monday, 02-Jan-06 15:4:05 MST",
"Monday, 02-Jan-06 15:04:5 MST",
"Monday, 02-Jan-06 15:4:5 MST",
"Monday, 2-Jan-06 15:04:05 MST",
"Monday, 2-Jan-06 15:4:05 MST",
"Monday, 2-Jan-06 15:4:5 MST",
"Monday, 2-Jan-06 15:04:5 MST",
"Monday, 2-Jan-6 15:04:05 MST",
"Monday, 2-Jan-6 15:4:05 MST",
"Monday, 2-Jan-6 15:4:5 MST",
"Monday, 2-Jan-6 15:04:5 MST",
} {
if t, err := parse(layout, datestr, loc); err == nil {
return t, nil
}
}
} }
state = stateWeekdayCommaOffset state = stateWeekdayCommaOffset
case r == '+': case r == '+':
state = stateWeekdayCommaOffset state = stateWeekdayCommaOffset
} }
case stateWeekdayAbbrevComma: // Starts alpha then comma case stateWeekdayAbbrevComma: // Starts alpha then comma
// Mon, 02-Jan-06 15:04:05 MST
// Mon, 02 Jan 2006 15:04:05 MST // Mon, 02 Jan 2006 15:04:05 MST
// stateWeekdayAbbrevCommaDash
// Mon, 02-Jan-06 15:04:05 MST
// 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
@ -561,26 +555,12 @@ iterRunes:
// stateWeekdayAbbrevCommaOffsetZone // stateWeekdayAbbrevCommaOffsetZone
// Tue, 11 Jul 2017 16:28:13 +0200 (CEST) // Tue, 11 Jul 2017 16:28:13 +0200 (CEST)
switch { switch {
case r == ' ' && dayLen == 0:
dayLen = i - part1Len - 2
case r == '-': case r == '-':
if i < 15 { if i < 15 {
for _, layout := range []string{ state = stateWeekdayAbbrevCommaDash
"Mon, 02-Jan-06 15:04:05 MST", break iterRunes
"Mon, 02-Jan-06 15:4:05 MST",
"Mon, 02-Jan-06 15:04:5 MST",
"Mon, 02-Jan-06 15:4:5 MST",
"Mon, 2-Jan-06 15:04:05 MST",
"Mon, 2-Jan-06 15:4:05 MST",
"Mon, 2-Jan-06 15:4:5 MST",
"Mon, 2-Jan-06 15:04:5 MST",
"Mon, 2-Jan-6 15:04:05 MST",
"Mon, 2-Jan-6 15:4:05 MST",
"Mon, 2-Jan-6 15:4:5 MST",
"Mon, 2-Jan-6 15:04:5 MST",
} {
if t, err := parse(layout, datestr, loc); err == nil {
return t, nil
}
}
} }
state = stateWeekdayAbbrevCommaOffset state = stateWeekdayAbbrevCommaOffset
case r == '+': case r == '+':
@ -1474,6 +1454,27 @@ iterRunes:
} }
} }
case stateWeekdayCommaDash:
// Monday, 02-Jan-06 15:04:05 MST
for _, layout := range []string{
"Monday, 02-Jan-06 15:04:05 MST",
"Monday, 02-Jan-06 15:4:05 MST",
"Monday, 02-Jan-06 15:04:5 MST",
"Monday, 02-Jan-06 15:4:5 MST",
"Monday, 2-Jan-06 15:04:05 MST",
"Monday, 2-Jan-06 15:4:05 MST",
"Monday, 2-Jan-06 15:4:5 MST",
"Monday, 2-Jan-06 15:04:5 MST",
"Monday, 2-Jan-6 15:04:05 MST",
"Monday, 2-Jan-6 15:4:05 MST",
"Monday, 2-Jan-6 15:4:5 MST",
"Monday, 2-Jan-6 15:04:5 MST",
} {
if t, err := parse(layout, datestr, loc); err == nil {
return t, nil
}
}
case stateWeekdayAbbrevComma: // Starts alpha then comma case stateWeekdayAbbrevComma: // Starts alpha then comma
// Mon, 02-Jan-06 15:04:05 MST // Mon, 02-Jan-06 15:04:05 MST
// Mon, 02 Jan 2006 15:04:05 MST // Mon, 02 Jan 2006 15:04:05 MST
@ -1488,6 +1489,28 @@ iterRunes:
} }
} }
case stateWeekdayAbbrevCommaDash:
// Mon, 02-Jan-06 15:04:05 MST
// Mon, 2-Jan-06 15:04:05 MST
for _, layout := range []string{
"Mon, 02-Jan-06 15:04:05 MST",
"Mon, 02-Jan-06 15:4:05 MST",
"Mon, 02-Jan-06 15:04:5 MST",
"Mon, 02-Jan-06 15:4:5 MST",
"Mon, 2-Jan-06 15:04:05 MST",
"Mon, 2-Jan-06 15:4:05 MST",
"Mon, 2-Jan-06 15:4:5 MST",
"Mon, 2-Jan-06 15:04:5 MST",
"Mon, 2-Jan-6 15:04:05 MST",
"Mon, 2-Jan-6 15:4:05 MST",
"Mon, 2-Jan-6 15:4:5 MST",
"Mon, 2-Jan-6 15:04:5 MST",
} {
if t, err := parse(layout, datestr, loc); err == nil {
return t, nil
}
}
case stateWeekdayAbbrevCommaOffset: case 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
@ -1507,7 +1530,10 @@ iterRunes:
case stateWeekdayAbbrevCommaOffsetZone: case stateWeekdayAbbrevCommaOffsetZone:
// Tue, 11 Jul 2017 16:28:13 +0200 (CEST) // Tue, 11 Jul 2017 16:28:13 +0200 (CEST)
return parse("Mon, _2 Jan 2006 15:04:05 -0700 (CEST)", datestr, loc) if dayLen == 1 {
return parse("Mon, 2 Jan 2006 15:04:05 -0700 (MST)", datestr, loc)
}
return parse("Mon, _2 Jan 2006 15:04:05 -0700 (MST)", datestr, loc)
} }
return time.Time{}, fmt.Errorf("Could not find date format for %s", datestr) return time.Time{}, fmt.Errorf("Could not find date format for %s", datestr)

View File

@ -38,6 +38,9 @@ func TestInLocation(t *testing.T) {
assert.Equal(t, "UTC", zone, "Should have found zone = UTC %v", zone) assert.Equal(t, "UTC", zone, "Should have found zone = UTC %v", zone)
assert.Equal(t, "2013-02-01 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) assert.Equal(t, "2013-02-01 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
ts = MustParse("Tue, 5 Jul 2017 16:28:13 -0700 (MST)")
assert.Equal(t, "2017-07-05 23:28:13 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
// Now we are going to use ParseIn() and see that it gives different answer // Now we are going to use ParseIn() and see that it gives different answer
// with different zone, offset // with different zone, offset
time.Local = nil time.Local = nil
@ -178,6 +181,9 @@ func TestParse(t *testing.T) {
ts = MustParse("Tue, 11 Jul 2017 16:28:13 +0200 (CEST)") ts = MustParse("Tue, 11 Jul 2017 16:28:13 +0200 (CEST)")
assert.Equal(t, "2017-07-11 14:28:13 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) assert.Equal(t, "2017-07-11 14:28:13 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
ts = MustParse("Tue, 5 Jul 2017 16:28:13 -0700 (CEST)")
assert.Equal(t, "2017-07-05 23:28:13 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
ts = MustParse("Thu, 13 Jul 2017 08:58:40 +0100") ts = MustParse("Thu, 13 Jul 2017 08:58:40 +0100")
assert.Equal(t, "2017-07-13 07:58:40 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) assert.Equal(t, "2017-07-13 07:58:40 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))