Skip to content

Commit aa3c79a

Browse files
committed
Support apply number format with the Japanese era years
1 parent 5fe30eb commit aa3c79a

File tree

2 files changed

+71
-31
lines changed

2 files changed

+71
-31
lines changed

numfmt.go

+58-31
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ type languageInfo struct {
3333
// numberFormat directly maps the number format parser runtime required
3434
// fields.
3535
type numberFormat struct {
36-
opts *Options
37-
cellType CellType
38-
section []nfp.Section
39-
t time.Time
40-
sectionIdx int
41-
date1904, isNumeric, hours, seconds, useMillisecond bool
42-
number float64
43-
ap, localCode, result, value, valueSectionType string
44-
switchArgument, currencyString string
45-
fracHolder, fracPadding, intHolder, intPadding, expBaseLen int
46-
percent int
47-
useCommaSep, usePointer, usePositive, useScientificNotation bool
36+
opts *Options
37+
cellType CellType
38+
section []nfp.Section
39+
t time.Time
40+
sectionIdx int
41+
date1904, isNumeric, hours, seconds, useMillisecond, useGannen bool
42+
number float64
43+
ap, localCode, result, value, valueSectionType string
44+
switchArgument, currencyString string
45+
fracHolder, fracPadding, intHolder, intPadding, expBaseLen int
46+
percent int
47+
useCommaSep, usePointer, usePositive, useScientificNotation bool
4848
}
4949

5050
// CultureName is the type of supported language country codes types for apply
@@ -797,6 +797,7 @@ var (
797797
"11": {tags: []string{"ja"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese},
798798
"411": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese},
799799
"800411": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese},
800+
"JP-X-GANNEN": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese},
800801
"JP-X-GANNEN,80": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese, useGannen: true},
801802
"12": {tags: []string{"ko"}, localMonth: localMonthsNameKorean, apFmt: apFmtKorean},
802803
"412": {tags: []string{"ko-KR"}, localMonth: localMonthsNameKorean, apFmt: apFmtKorean},
@@ -1344,7 +1345,7 @@ func (nf *numberFormat) dateTimeHandler() string {
13441345
if changeNumFmtCode, err := nf.currencyLanguageHandler(token); err != nil || changeNumFmtCode {
13451346
return nf.value
13461347
}
1347-
if !supportedLanguageInfo[nf.localCode].useGannen {
1348+
if !strings.EqualFold(nf.localCode, "JP-X-GANNEN") && !strings.EqualFold(nf.localCode, "JP-X-GANNEN,80") {
13481349
nf.result += nf.currencyString
13491350
}
13501351
}
@@ -1766,6 +1767,18 @@ func (nf *numberFormat) dateTimesHandler(i int, token nfp.Token) {
17661767
nf.secondsHandler(token)
17671768
}
17681769

1770+
// eraYear convert time to the Japanese era years.
1771+
func eraYear(t time.Time) (int, int) {
1772+
i, year := 0, -1
1773+
for i = len(japaneseEraYears) - 1; i > 0; i-- {
1774+
if y := japaneseEraYears[i]; t.After(y) {
1775+
year = t.Year() - y.Year() + 1
1776+
break
1777+
}
1778+
}
1779+
return i, year
1780+
}
1781+
17691782
// yearsHandler will be handling years in the date and times types tokens for a
17701783
// number format expression.
17711784
func (nf *numberFormat) yearsHandler(token nfp.Token) {
@@ -1778,24 +1791,38 @@ func (nf *numberFormat) yearsHandler(token nfp.Token) {
17781791
return
17791792
}
17801793
if strings.Contains(strings.ToUpper(token.TValue), "G") {
1781-
for i := len(japaneseEraYears) - 1; i > 0; i-- {
1782-
if y := japaneseEraYears[i]; nf.t.After(y) {
1783-
switch len(token.TValue) {
1784-
case 1:
1785-
nf.result += japaneseEraSymbols[i]
1786-
case 2:
1787-
nf.result += japaneseEraNames[i][:3]
1788-
default:
1789-
nf.result += japaneseEraNames[i]
1790-
}
1791-
year := nf.t.Year() - y.Year() + 1
1792-
if year == 1 && len(token.TValue) > 1 && supportedLanguageInfo[nf.localCode].useGannen {
1793-
nf.result += "\u5143"
1794-
break
1795-
}
1796-
nf.result += strconv.Itoa(year)
1797-
break
1798-
}
1794+
i, year := eraYear(nf.t)
1795+
if year == -1 {
1796+
return
1797+
}
1798+
nf.useGannen = supportedLanguageInfo[nf.localCode].useGannen
1799+
switch len(token.TValue) {
1800+
case 1:
1801+
nf.useGannen = false
1802+
nf.result += japaneseEraSymbols[i]
1803+
case 2:
1804+
nf.result += japaneseEraNames[i][:3]
1805+
default:
1806+
nf.result += japaneseEraNames[i]
1807+
}
1808+
return
1809+
}
1810+
if strings.Contains(strings.ToUpper(token.TValue), "E") {
1811+
_, year := eraYear(nf.t)
1812+
if year == -1 {
1813+
nf.result += strconv.Itoa(nf.t.Year())
1814+
return
1815+
}
1816+
if year == 1 && nf.useGannen {
1817+
nf.result += "\u5143"
1818+
return
1819+
}
1820+
if len(token.TValue) == 1 && !nf.useGannen {
1821+
nf.result += strconv.Itoa(year)
1822+
return
1823+
}
1824+
if len(token.TValue) == 2 {
1825+
nf.result += fmt.Sprintf("%02d", year)
17991826
}
18001827
}
18011828
}

numfmt_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,25 @@ func TestNumFmt(t *testing.T) {
420420
{"44835.18957170139", "[$-41E]mmmmm dd yyyy h:mm AM/PM", "\u0e15 01 2022 4:32 AM"},
421421
{"44866.18957170139", "[$-41E]mmmmm dd yyyy h:mm AM/PM", "\u0e1e 01 2022 4:32 AM"},
422422
{"44896.18957170139", "[$-41E]mmmmm dd yyyy h:mm AM/PM", "\u0e18 01 2022 4:32 AM"},
423+
{"100", "[$-411]ge\"\"m\"\"d\"\";@", "1900年4月9日"},
423424
{"43709", "[$-411]ge\"\"m\"\"d\"\";@", "R1年9月1日"},
424425
{"43709", "[$-411]gge\"\"m\"\"d\"\";@", "\u4EE41年9月1日"},
425426
{"43709", "[$-411]ggge\"\"m\"\"d\"\";@", "\u4EE4\u548C1年9月1日"},
427+
{"43709", "[$-411]gee\"\"m\"\"d\"\";@", "R01年9月1日"},
428+
{"43709", "[$-411]ggee\"\"m\"\"d\"\";@", "\u4EE401年9月1日"},
429+
{"43709", "[$-411]gggee\"\"m\"\"d\"\";@", "\u4EE4\u548C01年9月1日"},
430+
{"43709", "[$-ja-JP-x-gannen]ge\"\"m\"\"d\"\";@", "R1年9月1日"},
431+
{"43709", "[$-ja-JP-x-gannen]gge\"\"m\"\"d\"\";@", "\u4EE41年9月1日"},
432+
{"43709", "[$-ja-JP-x-gannen]ggge\"\"m\"\"d\"\";@", "\u4EE4\u548C1年9月1日"},
433+
{"43709", "[$-ja-JP-x-gannen]gee\"\"m\"\"d\"\";@", "R01年9月1日"},
434+
{"43709", "[$-ja-JP-x-gannen]ggee\"\"m\"\"d\"\";@", "\u4EE401年9月1日"},
435+
{"43709", "[$-ja-JP-x-gannen]gggee\"\"m\"\"d\"\";@", "\u4EE4\u548C01年9月1日"},
426436
{"43709", "[$-ja-JP-x-gannen,80]ge\"\"m\"\"d\"\";@", "R1年9月1日"},
427437
{"43709", "[$-ja-JP-x-gannen,80]gge\"\"m\"\"d\"\";@", "\u4EE4\u5143年9月1日"},
428438
{"43709", "[$-ja-JP-x-gannen,80]ggge\"\"m\"\"d\"\";@", "\u4EE4\u548C\u5143年9月1日"},
439+
{"43709", "[$-ja-JP-x-gannen,80]gee\"\"m\"\"d\"\";@", "R01年9月1日"},
440+
{"43709", "[$-ja-JP-x-gannen,80]ggee\"\"m\"\"d\"\";@", "\u4EE4\u5143年9月1日"},
441+
{"43709", "[$-ja-JP-x-gannen,80]gggee\"\"m\"\"d\"\";@", "\u4EE4\u548C\u5143年9月1日"},
429442
{"43466.189571759256", "[$-411]ge\"\"m\"\"d\"\";@", "H31年1月1日"},
430443
{"43466.189571759256", "[$-411]gge\"\"m\"\"d\"\";@", "\u5E7331年1月1日"},
431444
{"43466.189571759256", "[$-411]ggge\"\"m\"\"d\"\";@", "\u5E73\u621031年1月1日"},

0 commit comments

Comments
 (0)