Skip to content

Commit

Permalink
change: failure of explicit type guess (--columns) falls back to text
Browse files Browse the repository at this point in the history
  • Loading branch information
ShuheiKubota committed Dec 30, 2022
1 parent 9bdf5b4 commit 6b00678
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ func TestGuess(t *testing.T) {
})

t.Run("GuessDatetime", func(t *testing.T) {
tst("20220304", "2022/03/04", "--columns", "#1:datetime")
tst("20220304", "20220304", "--columns", "#1:datetime")
tst("20220304 123456", "2022/03/04 12:34:56", "--columns", "#1:datetime")
tst("Feb 4, 2008 4:45pm", "2008/02/04 16:45:00", "--columns", `#1:datetime(Jan 2\, 2006 3:04pm)`, "-d", ";")
tst("true", "true", "--columns", "#1:datetime")
})
}

Expand Down
14 changes: 11 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func writeXlsx(f *excelize.File, sheet string, axis string, typ colType, value i
}

func (c globalCmd) guess(value string, col column, dateLayouts, timeLayouts []string) (colType, interface{}) {
typ, ival := c.guessByColType(value, col)
typ, ival := c.guessByColType(value, col, dateLayouts, timeLayouts)
if typ != typeUnknown {
return typ, ival
}
Expand Down Expand Up @@ -384,7 +384,7 @@ func (c globalCmd) guess(value string, col column, dateLayouts, timeLayouts []st
return typeUnknown, value
}

func (c globalCmd) guessByColType(value string, col column) (colType, interface{}) {
func (c globalCmd) guessByColType(value string, col column, dateLayouts, timeLayouts []string) (colType, interface{}) {
switch col.Type {
case typeText:
return typeText, value
Expand All @@ -396,18 +396,22 @@ func (c globalCmd) guessByColType(value string, col column) (colType, interface{

case typeDate:
ptns := translateDatePatterns(col.InputFormat)
ptns = append(ptns, dateLayouts...)
if t, ok := parseTime(value, ptns...); ok {
return typeDate, t
}

case typeTime:
ptns := translateTimePatterns(col.InputFormat)
ptns = append(ptns, timeLayouts...)
if t, ok := parseTime(value, ptns...); ok {
return typeTime, t
}

case typeDatetime:
if t, ok := parseTime(value, col.InputFormat); ok {
ptns := append([]string{}, col.InputFormat)
ptns = append(ptns, c.DatetimeFmt)
if t, ok := parseTime(value, ptns...); ok {
return typeDatetime, t
}

Expand All @@ -419,6 +423,10 @@ func (c globalCmd) guessByColType(value string, col column) (colType, interface{
default: // nop
}

if col.Type != typeUnknown {
return typeText, value
}

return typeUnknown, value
}

Expand Down

0 comments on commit 6b00678

Please sign in to comment.