Skip to content

Commit

Permalink
This closes #1661, fix incorrect time number format result
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Sep 17, 2023
1 parent e3b7dad commit 744236b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
36 changes: 24 additions & 12 deletions numfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4939,6 +4939,9 @@ func (nf *numberFormat) numberHandler() string {
// positive numeric.
func (nf *numberFormat) dateTimeHandler() string {
nf.t, nf.hours, nf.seconds = timeFromExcelTime(nf.number, nf.date1904), false, false
if !nf.useMillisecond {
nf.t = nf.t.Add(time.Duration(math.Round(float64(nf.t.Nanosecond())/1e9)) * time.Second)
}
for i, token := range nf.section[nf.sectionIdx].Items {
if token.TType == nfp.TokenTypeCurrencyLanguage {
if changeNumFmtCode, err := nf.currencyLanguageHandler(token); err != nil || changeNumFmtCode {
Expand Down Expand Up @@ -6712,11 +6715,11 @@ func (nf *numberFormat) dateTimesHandler(i int, token nfp.Token) {
}
if strings.Contains(strings.ToUpper(token.TValue), "M") {
l := len(token.TValue)
if l == 1 && !nf.hours && !nf.secondsNext(i) {
if l == 1 && nf.isMonthToken(i) {
nf.result += strconv.Itoa(int(nf.t.Month()))
return
}
if l == 2 && !nf.hours && !nf.secondsNext(i) {
if l == 2 && nf.isMonthToken(i) {
nf.result += fmt.Sprintf("%02d", int(nf.t.Month()))
return
}
Expand Down Expand Up @@ -6837,8 +6840,7 @@ func (nf *numberFormat) daysHandler(token nfp.Token) {
// hoursHandler will be handling hours in the date and times types tokens for a
// number format expression.
func (nf *numberFormat) hoursHandler(i int, token nfp.Token) {
nf.hours = strings.Contains(strings.ToUpper(token.TValue), "H")
if nf.hours {
if nf.hours = strings.Contains(strings.ToUpper(token.TValue), "H"); nf.hours {
h := nf.t.Hour()
ap, ok := nf.apNext(i)
if ok {
Expand Down Expand Up @@ -6890,9 +6892,6 @@ func (nf *numberFormat) secondsHandler(token nfp.Token) {
if nf.seconds = strings.Contains(strings.ToUpper(token.TValue), "S"); !nf.seconds {
return
}
if !nf.useMillisecond {
nf.t = nf.t.Add(time.Duration(math.Round(float64(nf.t.Nanosecond())/1e9)) * time.Second)
}
if len(token.TValue) == 1 {
nf.result += strconv.Itoa(nf.t.Second())
return
Expand Down Expand Up @@ -6947,16 +6946,29 @@ func (nf *numberFormat) apNext(i int) ([]string, bool) {
return nil, false
}

// secondsNext detects if a token of type seconds exists after a given tokens
// list.
func (nf *numberFormat) secondsNext(i int) bool {
// isMonthToken detects if the given token represents minutes, if no hours and
// seconds tokens before the given token or not seconds after the given token,
// the current token is a minutes token.
func (nf *numberFormat) isMonthToken(i int) bool {
tokens := nf.section[nf.sectionIdx].Items
var timePrevious, secondsNext bool
for idx := i - 1; idx >= 0; idx-- {
if tokens[idx].TType == nfp.TokenTypeDateTimes {
timePrevious = strings.ContainsAny(strings.ToUpper(tokens[idx].TValue), "HS")
break
}
if tokens[idx].TType == nfp.TokenTypeElapsedDateTimes {
timePrevious = true
break
}
}
for idx := i + 1; idx < len(tokens); idx++ {
if tokens[idx].TType == nfp.TokenTypeDateTimes {
return strings.Contains(strings.ToUpper(tokens[idx].TValue), "S")
secondsNext = strings.Contains(strings.ToUpper(tokens[idx].TValue), "S")
break
}
}
return false
return !(timePrevious || secondsNext)
}

// negativeHandler will be handling negative selection for a number format
Expand Down
4 changes: 4 additions & 0 deletions numfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3518,6 +3518,10 @@ func TestNumFmt(t *testing.T) {
{"0.007", "[h]:mm:ss.00", "0:10:04.80"},
{"0.007", "[h]:mm:ss.000", "0:10:04.800"},
{"0.007", "[h]:mm:ss.0000", "0:10:04.800"},
{"0.3270833333", "[h]:mm", "7:51"},
{"0.5347222222", "[h]:mm", "12:50"},
{"0.5833333333", "[h]:mm", "14:00"},
{"0.5833333333", "hh", "14"},
{"123", "[h]:mm,:ss.0", "2952:00,:00.0"},
{"123", "yy-.dd", "00-.02"},
{"123", "[DBNum1][$-804]yyyy\"\"m\"\";@", "\u4e00\u4e5d\u25cb\u25cb\u5e74\u4e94\u6708"},
Expand Down

0 comments on commit 744236b

Please sign in to comment.