Skip to content

Commit

Permalink
fix number format convert issue
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed May 15, 2021
1 parent 9a54595 commit f838861
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
17 changes: 11 additions & 6 deletions styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -1009,22 +1009,23 @@ func parseTime(v string, format string) string {
}
// It is the presence of the "am/pm" indicator that determines if this is
// a 12 hour or 24 hours time format, not the number of 'h' characters.
var padding bool
if val.Hour() == 0 && !strings.Contains(format, "hh") && !strings.Contains(format, "HH") {
padding = true
}
if is12HourTime(format) {
goFmt = strings.Replace(goFmt, "hh", "3", 1)
goFmt = strings.Replace(goFmt, "h", "3", 1)
goFmt = strings.Replace(goFmt, "HH", "3", 1)
goFmt = strings.Replace(goFmt, "H", "3", 1)
} else {
goFmt = strings.Replace(goFmt, "hh", "15", 1)
if 0 < val.Hour() && val.Hour() < 12 {
goFmt = strings.Replace(goFmt, "h", "3", 1)
} else {
goFmt = strings.Replace(goFmt, "h", "15", 1)
}
goFmt = strings.Replace(goFmt, "HH", "15", 1)
if 0 < val.Hour() && val.Hour() < 12 {
goFmt = strings.Replace(goFmt, "h", "3", 1)
goFmt = strings.Replace(goFmt, "H", "3", 1)
} else {
goFmt = strings.Replace(goFmt, "h", "15", 1)
goFmt = strings.Replace(goFmt, "H", "15", 1)
}
}
Expand All @@ -1046,7 +1047,11 @@ func parseTime(v string, format string) string {
goFmt = strings.Replace(goFmt, "[3]", "3", 1)
goFmt = strings.Replace(goFmt, "[15]", "15", 1)
}
return val.Format(goFmt)
s := val.Format(goFmt)
if padding {
s = strings.Replace(s, "00:", "0:", 1)
}
return s
}

// is12HourTime checks whether an Excel time format string is a 12 hours form.
Expand Down
8 changes: 8 additions & 0 deletions styles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ func TestParseTime(t *testing.T) {
assert.Equal(t, "2019-03-04 05:05:42", parseTime("43528.2123", "YYYY-MM-DD hh:mm:ss"))
assert.Equal(t, "2019-03-04 05:05:42", parseTime("43528.2123", "YYYY-MM-DD hh:mm:ss;YYYY-MM-DD hh:mm:ss"))
assert.Equal(t, "3/4/2019 5:5:42", parseTime("43528.2123", "M/D/YYYY h:m:s"))
assert.Equal(t, "3/4/2019 0:5:42", parseTime("43528.003958333335", "m/d/yyyy h:m:s"))
assert.Equal(t, "3/4/2019 0:05:42", parseTime("43528.003958333335", "M/D/YYYY h:mm:s"))
assert.Equal(t, "0:05", parseTime("43528.003958333335", "h:mm"))
assert.Equal(t, "0:0", parseTime("6.9444444444444444E-5", "h:m"))
assert.Equal(t, "0:00", parseTime("6.9444444444444444E-5", "h:mm"))
assert.Equal(t, "0:0", parseTime("6.9444444444444444E-5", "h:m"))
assert.Equal(t, "12:1", parseTime("0.50070601851851848", "h:m"))
assert.Equal(t, "23:30", parseTime("0.97952546296296295", "h:m"))
assert.Equal(t, "March", parseTime("43528", "mmmm"))
assert.Equal(t, "Monday", parseTime("43528", "dddd"))
}
Expand Down

0 comments on commit f838861

Please sign in to comment.