Skip to content

Commit

Permalink
Bug #4 fixed: AutoParse now checks for the string being blank (after …
Browse files Browse the repository at this point in the history
…trimming whitespace)
  • Loading branch information
rickb777 committed Jul 12, 2019
1 parent 1c9d14b commit 036c37a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
4 changes: 2 additions & 2 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ func TestInvalidDateText(t *testing.T) {
value string
want string
}{
{`not-a-date`, `Date.ParseISO: cannot parse not-a-date: incorrect syntax`},
{`215-08-15`, `Date.ParseISO: cannot parse 215-08-15: invalid year`},
{`not-a-date`, `Date.ParseISO: cannot parse "not-a-date": incorrect syntax`},
{`215-08-15`, `Date.ParseISO: cannot parse "215-08-15": invalid year`},
}
for _, c := range cases {
var d Date
Expand Down
23 changes: 15 additions & 8 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package date

import (
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -33,12 +34,18 @@ func MustAutoParse(value string) Date {
//
// * dd/mm/yyyy | dd.mm.yyyy (or any similar pattern)
//
// * surrounding whitespace is ignored
//
func AutoParse(value string) (Date, error) {
abs := strings.TrimSpace(value)
if len(abs) == 0 {
return Date{}, errors.New("Date.AutoParse: cannot parse a blank string")
}

sign := ""
if value[0] == '+' || value[0] == '-' {
abs = value[1:]
sign = value[:1]
if abs[0] == '+' || abs[0] == '-' {
sign = abs[:1]
abs = abs[1:]
}

if len(abs) >= 10 {
Expand Down Expand Up @@ -97,7 +104,7 @@ func MustParseISO(value string) Date {
// Background: https://en.wikipedia.org/wiki/ISO_8601#Dates
func ParseISO(value string) (Date, error) {
if len(value) < 8 {
return Date{}, fmt.Errorf("Date.ParseISO: cannot parse %s: incorrect length", value)
return Date{}, fmt.Errorf("Date.ParseISO: cannot parse %q: incorrect length", value)
}

abs := value
Expand All @@ -119,12 +126,12 @@ func ParseISO(value string) (Date, error) {
fd1 = 6
fd2 = 8
} else if abs[fm2] != '-' {
return Date{}, fmt.Errorf("Date.ParseISO: cannot parse %s: incorrect syntax", value)
return Date{}, fmt.Errorf("Date.ParseISO: cannot parse %q: incorrect syntax", value)
}
//fmt.Printf("%s %d %d %d %d %d\n", value, dash1, fm1, fm2, fd1, fd2)

if len(abs) != fd2 {
return Date{}, fmt.Errorf("Date.ParseISO: cannot parse %s: incorrect length", value)
return Date{}, fmt.Errorf("Date.ParseISO: cannot parse %q: incorrect length", value)
}

year, err := parseField(value, abs[:dash1], "year", 4, -1)
Expand Down Expand Up @@ -153,11 +160,11 @@ func ParseISO(value string) (Date, error) {

func parseField(value, field, name string, minLength, requiredLength int) (int, error) {
if (minLength > 0 && len(field) < minLength) || (requiredLength > 0 && len(field) != requiredLength) {
return 0, fmt.Errorf("Date.ParseISO: cannot parse %s: invalid %s", value, name)
return 0, fmt.Errorf("Date.ParseISO: cannot parse %q: invalid %s", value, name)
}
number, err := strconv.Atoi(field)
if err != nil {
return 0, fmt.Errorf("Date.ParseISO: cannot parse %s: invalid %s", value, name)
return 0, fmt.Errorf("Date.ParseISO: cannot parse %q: invalid %s", value, name)
}
return number, nil
}
Expand Down
10 changes: 7 additions & 3 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestAutoParse(t *testing.T) {
month time.Month
day int
}{
{"31/12/1969", 1969, time.December, 31},
{" 31/12/1969 ", 1969, time.December, 31},
{"1969/12/31", 1969, time.December, 31},
{"1969.12.31", 1969, time.December, 31},
{"1969-12-31", 1969, time.December, 31},
Expand All @@ -30,12 +30,12 @@ func TestAutoParse(t *testing.T) {
{"2004-03-01", 2004, time.March, 1},
{"0000-01-01", 0, time.January, 1},
{"+0001-02-03", 1, time.February, 3},
{"+00019-03-04", 19, time.March, 4},
{" +00019-03-04 ", 19, time.March, 4},
{"0100-04-05", 100, time.April, 5},
{"2000-05-06", 2000, time.May, 6},
{"+5000000-08-09", 5000000, time.August, 9},
{"-0001-09-11", -1, time.September, 11},
{"-0019-10-12", -19, time.October, 12},
{" -0019-10-12 ", -19, time.October, 12},
{"-00100-11-13", -100, time.November, 13},
{"-02000-12-14", -2000, time.December, 14},
{"-30000-02-15", -30000, time.February, 15},
Expand All @@ -44,6 +44,7 @@ func TestAutoParse(t *testing.T) {
{"12340506", 1234, time.May, 6},
{"+12340506", 1234, time.May, 6},
{"-00191012", -19, time.October, 12},
{" -00191012 ", -19, time.October, 12},
}
for _, c := range cases {
d := MustAutoParse(c.value)
Expand All @@ -69,6 +70,9 @@ func TestAutoParse(t *testing.T) {
"+10-11-12",
"+100-02-03",
"-123-05-06",
"--",
"",
" ",
}
for _, c := range badCases {
d, err := AutoParse(c)
Expand Down

0 comments on commit 036c37a

Please sign in to comment.