Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions parseany.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,14 @@ func ParseIn(datestr string, loc *time.Location, opts ...ParserOption) (time.Tim
// Set Location to time.Local. Same as ParseIn Location but lazily uses
// the global time.Local variable for Location argument.
//
// denverLoc, _ := time.LoadLocation("America/Denver")
// time.Local = denverLoc
// denverLoc, _ := time.LoadLocation("America/Denver")
// time.Local = denverLoc
//
// t, err := dateparse.ParseLocal("3/1/2014")
// t, err := dateparse.ParseLocal("3/1/2014")
//
// Equivalent to:
//
// t, err := dateparse.ParseIn("3/1/2014", denverLoc)
//
// t, err := dateparse.ParseIn("3/1/2014", denverLoc)
func ParseLocal(datestr string, opts ...ParserOption) (time.Time, error) {
p, err := parseTime(datestr, time.Local, opts...)
if err != nil {
Expand All @@ -204,9 +203,8 @@ func MustParse(datestr string, opts ...ParserOption) time.Time {
// ParseFormat parse's an unknown date-time string and returns a layout
// string that can parse this (and exact same format) other date-time strings.
//
// layout, err := dateparse.ParseFormat("2013-02-01 00:00:00")
// // layout = "2006-01-02 15:04:05"
//
// layout, err := dateparse.ParseFormat("2013-02-01 00:00:00")
// // layout = "2006-01-02 15:04:05"
func ParseFormat(datestr string, opts ...ParserOption) (string, error) {
p, err := parseTime(datestr, nil, opts...)
if err != nil {
Expand Down Expand Up @@ -426,6 +424,11 @@ iterRunes:
p.setMonth()
default:
if unicode.IsLetter(r) {
// 2013-Feb-03 starts the month on the letter.
// 2021-032T already has digits in the month field.
if i > p.moi {
return nil, unknownErr(datestr)
}
p.stateDate = dateYearDashAlphaDash
}
}
Expand Down Expand Up @@ -1785,6 +1788,12 @@ iterRunes:

case dateYearDash:
// 2006-01
// A longer field is left in the layout. time.Parse then reads
// 03 as an hour and 2 as a day, so 2021-032 becomes
// 2 January 2021 at 03:00.
if !p.yearMonthLenOK() {
return nil, unknownErr(datestr)
}
return p, nil

case dateYearDashDash:
Expand Down Expand Up @@ -1850,6 +1859,9 @@ iterRunes:
// 2014.05
p.molen = i - p.moi
p.setMonth()
if p.yearlen == 4 && p.dayi == 0 && !p.yearMonthLenOK() {
return nil, unknownErr(datestr)
}
return p, nil

case dateDigitDotDot:
Expand Down Expand Up @@ -1919,13 +1931,19 @@ iterRunes:

case dateDigitYearSlash:
// 2014/10/13
if p.dayi == 0 && !p.yearMonthLenOK() {
return nil, unknownErr(datestr)
}
return p, nil

case dateDigitColon:
// 3:1:2014
// 10:13:2014
// 01:02:2006
// 2014:10:13
if p.yearlen == 4 && p.dayi == 0 && !p.yearMonthLenOK() {
return nil, unknownErr(datestr)
}
return p, nil

case dateDigitChineseYear:
Expand Down Expand Up @@ -2044,6 +2062,10 @@ func (p *parser) set(start int, val string) {
p.format[start+i] = byte(r)
}
}
func (p *parser) yearMonthLenOK() bool {
return p.molen == 1 || p.molen == 2
}

func (p *parser) setMonth() {
if p.molen == 2 {
p.set(p.moi, "01")
Expand Down
7 changes: 7 additions & 0 deletions parseany_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,13 @@ var testParseErrors = []dateTest{
{in: "29-06-2016", err: true},
// this is just testing the empty space up front
{in: " 2018-01-02 17:08:09 -07:00", err: true},
// 2021-032 was 2 January 2021 at 03:00. The extra digit stayed in the layout.
{in: "2021-032", err: true},
{in: "2021.032", err: true},
{in: "2021/032", err: true},
{in: "2021:032", err: true},
{in: "2021-999", err: true},
{in: "2021-032T00:00:00Z", err: true},
}

func TestParseErrors(t *testing.T) {
Expand Down