Skip to content

Commit

Permalink
Merge pull request #781 from benedictjohannes/allow-timefmt-in-multis…
Browse files Browse the repository at this point in the history
…ection-numfmt

allow recognition of time format in multisection numFormat
  • Loading branch information
tealeg authored Sep 14, 2023
2 parents ac8326d + 1dc43d5 commit 61c977e
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions format_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,6 @@ func parseFullNumberFormatString(numFmt string) *parsedNumberFormat {
parsedNumFmt := &parsedNumberFormat{
numFmt: numFmt,
}
if isTimeFormat(numFmt) {
// Time formats cannot have multiple groups separated by semicolons, there is only one format.
// Strings are unaffected by the time format.
parsedNumFmt.isTimeFormat = true
parsedNumFmt.textFormat, _ = parseNumberFormatSection("general")
return parsedNumFmt
}

var fmtOptions []*formatOptions
formats, err := splitFormatOnSemicolon(numFmt)
Expand All @@ -277,6 +270,7 @@ func parseFullNumberFormatString(numFmt string) *parsedNumberFormat {

if len(fmtOptions) == 1 {
// If there is only one option, it is used for all
parsedNumFmt.isTimeFormat = fmtOptions[0].isTimeFormat
parsedNumFmt.positiveFormat = fmtOptions[0]
parsedNumFmt.negativeFormat = fmtOptions[0]
parsedNumFmt.zeroFormat = fmtOptions[0]
Expand All @@ -291,6 +285,7 @@ func parseFullNumberFormatString(numFmt string) *parsedNumberFormat {
// When negative numbers now have their own format, they should become positive before having the format applied.
// The format will contain a negative sign if it is desired, but they may be colored red or wrapped in
// parenthesis instead.
parsedNumFmt.isTimeFormat = fmtOptions[0].isTimeFormat
parsedNumFmt.negativeFormatExpectsPositive = true
parsedNumFmt.positiveFormat = fmtOptions[0]
parsedNumFmt.negativeFormat = fmtOptions[1]
Expand All @@ -299,6 +294,7 @@ func parseFullNumberFormatString(numFmt string) *parsedNumberFormat {
} else if len(fmtOptions) == 3 {
// If there are three formats, the first is used for positive, the second gets used as a negative format,
// the third is for negative, and strings are not formatted.
parsedNumFmt.isTimeFormat = fmtOptions[0].isTimeFormat
parsedNumFmt.negativeFormatExpectsPositive = true
parsedNumFmt.positiveFormat = fmtOptions[0]
parsedNumFmt.negativeFormat = fmtOptions[1]
Expand All @@ -307,6 +303,7 @@ func parseFullNumberFormatString(numFmt string) *parsedNumberFormat {
} else {
// With four options, the first is positive, the second is negative, the third is zero, and the fourth is strings
// Negative numbers should be still become positive before having the negative formatting applied.
parsedNumFmt.isTimeFormat = fmtOptions[0].isTimeFormat
parsedNumFmt.negativeFormatExpectsPositive = true
parsedNumFmt.positiveFormat = fmtOptions[0]
parsedNumFmt.negativeFormat = fmtOptions[1]
Expand Down Expand Up @@ -375,6 +372,13 @@ func parseNumberFormatSection(fullFormat string) (*formatOptions, error) {
reducedFormatString: "general",
}, nil
}
if isTimeFormat(reducedFormat) {
return &formatOptions{
fullFormatString: fullFormat,
isTimeFormat: true,
reducedFormatString: reducedFormat,
}, nil
}

prefix, reducedFormat, showPercent1, err := parseLiterals(reducedFormat)
if err != nil {
Expand Down Expand Up @@ -519,7 +523,7 @@ func (fullFormat *parsedNumberFormat) parseTime(value string, date1904 bool) (st
return value, err
}
val := TimeFromExcelTime(f, date1904)
format := fullFormat.numFmt
format := fullFormat.positiveFormat.fullFormatString
// Replace Excel placeholders with Go time placeholders.
// For example, replace yyyy with 2006. These are in a specific order,
// due to the fact that m is used in month, minute, and am/pm. It would
Expand Down

0 comments on commit 61c977e

Please sign in to comment.