This closes #1661, fix incorrect time number format result

This commit is contained in:
xuri 2023-09-17 18:30:58 +08:00
parent e3b7dad69a
commit 744236b4b8
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
2 changed files with 29 additions and 13 deletions

View File

@ -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 {
@ -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
}
@ -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 {
@ -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
@ -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
for idx := i + 1; idx < len(tokens); idx++ {
var timePrevious, secondsNext bool
for idx := i - 1; idx >= 0; idx-- {
if tokens[idx].TType == nfp.TokenTypeDateTimes {
return strings.Contains(strings.ToUpper(tokens[idx].TValue), "S")
timePrevious = strings.ContainsAny(strings.ToUpper(tokens[idx].TValue), "HS")
break
}
if tokens[idx].TType == nfp.TokenTypeElapsedDateTimes {
timePrevious = true
break
}
}
return false
for idx := i + 1; idx < len(tokens); idx++ {
if tokens[idx].TType == nfp.TokenTypeDateTimes {
secondsNext = strings.Contains(strings.ToUpper(tokens[idx].TValue), "S")
break
}
}
return !(timePrevious || secondsNext)
}
// negativeHandler will be handling negative selection for a number format

View File

@ -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"},