Support apply number format with the Japanese era years
This commit is contained in:
parent
5fe30eb456
commit
aa3c79a811
89
numfmt.go
89
numfmt.go
|
@ -33,18 +33,18 @@ type languageInfo struct {
|
|||
// numberFormat directly maps the number format parser runtime required
|
||||
// fields.
|
||||
type numberFormat struct {
|
||||
opts *Options
|
||||
cellType CellType
|
||||
section []nfp.Section
|
||||
t time.Time
|
||||
sectionIdx int
|
||||
date1904, isNumeric, hours, seconds, useMillisecond bool
|
||||
number float64
|
||||
ap, localCode, result, value, valueSectionType string
|
||||
switchArgument, currencyString string
|
||||
fracHolder, fracPadding, intHolder, intPadding, expBaseLen int
|
||||
percent int
|
||||
useCommaSep, usePointer, usePositive, useScientificNotation bool
|
||||
opts *Options
|
||||
cellType CellType
|
||||
section []nfp.Section
|
||||
t time.Time
|
||||
sectionIdx int
|
||||
date1904, isNumeric, hours, seconds, useMillisecond, useGannen bool
|
||||
number float64
|
||||
ap, localCode, result, value, valueSectionType string
|
||||
switchArgument, currencyString string
|
||||
fracHolder, fracPadding, intHolder, intPadding, expBaseLen int
|
||||
percent int
|
||||
useCommaSep, usePointer, usePositive, useScientificNotation bool
|
||||
}
|
||||
|
||||
// CultureName is the type of supported language country codes types for apply
|
||||
|
@ -797,6 +797,7 @@ var (
|
|||
"11": {tags: []string{"ja"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese},
|
||||
"411": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese},
|
||||
"800411": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese},
|
||||
"JP-X-GANNEN": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese},
|
||||
"JP-X-GANNEN,80": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese, useGannen: true},
|
||||
"12": {tags: []string{"ko"}, localMonth: localMonthsNameKorean, apFmt: apFmtKorean},
|
||||
"412": {tags: []string{"ko-KR"}, localMonth: localMonthsNameKorean, apFmt: apFmtKorean},
|
||||
|
@ -1344,7 +1345,7 @@ func (nf *numberFormat) dateTimeHandler() string {
|
|||
if changeNumFmtCode, err := nf.currencyLanguageHandler(token); err != nil || changeNumFmtCode {
|
||||
return nf.value
|
||||
}
|
||||
if !supportedLanguageInfo[nf.localCode].useGannen {
|
||||
if !strings.EqualFold(nf.localCode, "JP-X-GANNEN") && !strings.EqualFold(nf.localCode, "JP-X-GANNEN,80") {
|
||||
nf.result += nf.currencyString
|
||||
}
|
||||
}
|
||||
|
@ -1766,6 +1767,18 @@ func (nf *numberFormat) dateTimesHandler(i int, token nfp.Token) {
|
|||
nf.secondsHandler(token)
|
||||
}
|
||||
|
||||
// eraYear convert time to the Japanese era years.
|
||||
func eraYear(t time.Time) (int, int) {
|
||||
i, year := 0, -1
|
||||
for i = len(japaneseEraYears) - 1; i > 0; i-- {
|
||||
if y := japaneseEraYears[i]; t.After(y) {
|
||||
year = t.Year() - y.Year() + 1
|
||||
break
|
||||
}
|
||||
}
|
||||
return i, year
|
||||
}
|
||||
|
||||
// yearsHandler will be handling years in the date and times types tokens for a
|
||||
// number format expression.
|
||||
func (nf *numberFormat) yearsHandler(token nfp.Token) {
|
||||
|
@ -1778,24 +1791,38 @@ func (nf *numberFormat) yearsHandler(token nfp.Token) {
|
|||
return
|
||||
}
|
||||
if strings.Contains(strings.ToUpper(token.TValue), "G") {
|
||||
for i := len(japaneseEraYears) - 1; i > 0; i-- {
|
||||
if y := japaneseEraYears[i]; nf.t.After(y) {
|
||||
switch len(token.TValue) {
|
||||
case 1:
|
||||
nf.result += japaneseEraSymbols[i]
|
||||
case 2:
|
||||
nf.result += japaneseEraNames[i][:3]
|
||||
default:
|
||||
nf.result += japaneseEraNames[i]
|
||||
}
|
||||
year := nf.t.Year() - y.Year() + 1
|
||||
if year == 1 && len(token.TValue) > 1 && supportedLanguageInfo[nf.localCode].useGannen {
|
||||
nf.result += "\u5143"
|
||||
break
|
||||
}
|
||||
nf.result += strconv.Itoa(year)
|
||||
break
|
||||
}
|
||||
i, year := eraYear(nf.t)
|
||||
if year == -1 {
|
||||
return
|
||||
}
|
||||
nf.useGannen = supportedLanguageInfo[nf.localCode].useGannen
|
||||
switch len(token.TValue) {
|
||||
case 1:
|
||||
nf.useGannen = false
|
||||
nf.result += japaneseEraSymbols[i]
|
||||
case 2:
|
||||
nf.result += japaneseEraNames[i][:3]
|
||||
default:
|
||||
nf.result += japaneseEraNames[i]
|
||||
}
|
||||
return
|
||||
}
|
||||
if strings.Contains(strings.ToUpper(token.TValue), "E") {
|
||||
_, year := eraYear(nf.t)
|
||||
if year == -1 {
|
||||
nf.result += strconv.Itoa(nf.t.Year())
|
||||
return
|
||||
}
|
||||
if year == 1 && nf.useGannen {
|
||||
nf.result += "\u5143"
|
||||
return
|
||||
}
|
||||
if len(token.TValue) == 1 && !nf.useGannen {
|
||||
nf.result += strconv.Itoa(year)
|
||||
return
|
||||
}
|
||||
if len(token.TValue) == 2 {
|
||||
nf.result += fmt.Sprintf("%02d", year)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -420,12 +420,25 @@ func TestNumFmt(t *testing.T) {
|
|||
{"44835.18957170139", "[$-41E]mmmmm dd yyyy h:mm AM/PM", "\u0e15 01 2022 4:32 AM"},
|
||||
{"44866.18957170139", "[$-41E]mmmmm dd yyyy h:mm AM/PM", "\u0e1e 01 2022 4:32 AM"},
|
||||
{"44896.18957170139", "[$-41E]mmmmm dd yyyy h:mm AM/PM", "\u0e18 01 2022 4:32 AM"},
|
||||
{"100", "[$-411]ge\"年\"m\"月\"d\"日\";@", "1900年4月9日"},
|
||||
{"43709", "[$-411]ge\"年\"m\"月\"d\"日\";@", "R1年9月1日"},
|
||||
{"43709", "[$-411]gge\"年\"m\"月\"d\"日\";@", "\u4EE41年9月1日"},
|
||||
{"43709", "[$-411]ggge\"年\"m\"月\"d\"日\";@", "\u4EE4\u548C1年9月1日"},
|
||||
{"43709", "[$-411]gee\"年\"m\"月\"d\"日\";@", "R01年9月1日"},
|
||||
{"43709", "[$-411]ggee\"年\"m\"月\"d\"日\";@", "\u4EE401年9月1日"},
|
||||
{"43709", "[$-411]gggee\"年\"m\"月\"d\"日\";@", "\u4EE4\u548C01年9月1日"},
|
||||
{"43709", "[$-ja-JP-x-gannen]ge\"年\"m\"月\"d\"日\";@", "R1年9月1日"},
|
||||
{"43709", "[$-ja-JP-x-gannen]gge\"年\"m\"月\"d\"日\";@", "\u4EE41年9月1日"},
|
||||
{"43709", "[$-ja-JP-x-gannen]ggge\"年\"m\"月\"d\"日\";@", "\u4EE4\u548C1年9月1日"},
|
||||
{"43709", "[$-ja-JP-x-gannen]gee\"年\"m\"月\"d\"日\";@", "R01年9月1日"},
|
||||
{"43709", "[$-ja-JP-x-gannen]ggee\"年\"m\"月\"d\"日\";@", "\u4EE401年9月1日"},
|
||||
{"43709", "[$-ja-JP-x-gannen]gggee\"年\"m\"月\"d\"日\";@", "\u4EE4\u548C01年9月1日"},
|
||||
{"43709", "[$-ja-JP-x-gannen,80]ge\"年\"m\"月\"d\"日\";@", "R1年9月1日"},
|
||||
{"43709", "[$-ja-JP-x-gannen,80]gge\"年\"m\"月\"d\"日\";@", "\u4EE4\u5143年9月1日"},
|
||||
{"43709", "[$-ja-JP-x-gannen,80]ggge\"年\"m\"月\"d\"日\";@", "\u4EE4\u548C\u5143年9月1日"},
|
||||
{"43709", "[$-ja-JP-x-gannen,80]gee\"年\"m\"月\"d\"日\";@", "R01年9月1日"},
|
||||
{"43709", "[$-ja-JP-x-gannen,80]ggee\"年\"m\"月\"d\"日\";@", "\u4EE4\u5143年9月1日"},
|
||||
{"43709", "[$-ja-JP-x-gannen,80]gggee\"年\"m\"月\"d\"日\";@", "\u4EE4\u548C\u5143年9月1日"},
|
||||
{"43466.189571759256", "[$-411]ge\"年\"m\"月\"d\"日\";@", "H31年1月1日"},
|
||||
{"43466.189571759256", "[$-411]gge\"年\"m\"月\"d\"日\";@", "\u5E7331年1月1日"},
|
||||
{"43466.189571759256", "[$-411]ggge\"年\"m\"月\"d\"日\";@", "\u5E73\u621031年1月1日"},
|
||||
|
|
Loading…
Reference in New Issue