已验证 提交 aa3c79a8 编写于 作者: xurime's avatar xurime

Support apply number format with the Japanese era years

上级 5fe30eb4
...@@ -33,18 +33,18 @@ type languageInfo struct { ...@@ -33,18 +33,18 @@ type languageInfo struct {
// numberFormat directly maps the number format parser runtime required // numberFormat directly maps the number format parser runtime required
// fields. // fields.
type numberFormat struct { type numberFormat struct {
opts *Options opts *Options
cellType CellType cellType CellType
section []nfp.Section section []nfp.Section
t time.Time t time.Time
sectionIdx int sectionIdx int
date1904, isNumeric, hours, seconds, useMillisecond bool date1904, isNumeric, hours, seconds, useMillisecond, useGannen bool
number float64 number float64
ap, localCode, result, value, valueSectionType string ap, localCode, result, value, valueSectionType string
switchArgument, currencyString string switchArgument, currencyString string
fracHolder, fracPadding, intHolder, intPadding, expBaseLen int fracHolder, fracPadding, intHolder, intPadding, expBaseLen int
percent int percent int
useCommaSep, usePointer, usePositive, useScientificNotation bool useCommaSep, usePointer, usePositive, useScientificNotation bool
} }
// CultureName is the type of supported language country codes types for apply // CultureName is the type of supported language country codes types for apply
...@@ -797,6 +797,7 @@ var ( ...@@ -797,6 +797,7 @@ var (
"11": {tags: []string{"ja"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese}, "11": {tags: []string{"ja"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese},
"411": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese}, "411": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese},
"800411": {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}, "JP-X-GANNEN,80": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese, useGannen: true},
"12": {tags: []string{"ko"}, localMonth: localMonthsNameKorean, apFmt: apFmtKorean}, "12": {tags: []string{"ko"}, localMonth: localMonthsNameKorean, apFmt: apFmtKorean},
"412": {tags: []string{"ko-KR"}, localMonth: localMonthsNameKorean, apFmt: apFmtKorean}, "412": {tags: []string{"ko-KR"}, localMonth: localMonthsNameKorean, apFmt: apFmtKorean},
...@@ -1344,7 +1345,7 @@ func (nf *numberFormat) dateTimeHandler() string { ...@@ -1344,7 +1345,7 @@ func (nf *numberFormat) dateTimeHandler() string {
if changeNumFmtCode, err := nf.currencyLanguageHandler(token); err != nil || changeNumFmtCode { if changeNumFmtCode, err := nf.currencyLanguageHandler(token); err != nil || changeNumFmtCode {
return nf.value 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 nf.result += nf.currencyString
} }
} }
...@@ -1766,6 +1767,18 @@ func (nf *numberFormat) dateTimesHandler(i int, token nfp.Token) { ...@@ -1766,6 +1767,18 @@ func (nf *numberFormat) dateTimesHandler(i int, token nfp.Token) {
nf.secondsHandler(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 // yearsHandler will be handling years in the date and times types tokens for a
// number format expression. // number format expression.
func (nf *numberFormat) yearsHandler(token nfp.Token) { func (nf *numberFormat) yearsHandler(token nfp.Token) {
...@@ -1778,24 +1791,38 @@ func (nf *numberFormat) yearsHandler(token nfp.Token) { ...@@ -1778,24 +1791,38 @@ func (nf *numberFormat) yearsHandler(token nfp.Token) {
return return
} }
if strings.Contains(strings.ToUpper(token.TValue), "G") { if strings.Contains(strings.ToUpper(token.TValue), "G") {
for i := len(japaneseEraYears) - 1; i > 0; i-- { i, year := eraYear(nf.t)
if y := japaneseEraYears[i]; nf.t.After(y) { if year == -1 {
switch len(token.TValue) { return
case 1: }
nf.result += japaneseEraSymbols[i] nf.useGannen = supportedLanguageInfo[nf.localCode].useGannen
case 2: switch len(token.TValue) {
nf.result += japaneseEraNames[i][:3] case 1:
default: nf.useGannen = false
nf.result += japaneseEraNames[i] nf.result += japaneseEraSymbols[i]
} case 2:
year := nf.t.Year() - y.Year() + 1 nf.result += japaneseEraNames[i][:3]
if year == 1 && len(token.TValue) > 1 && supportedLanguageInfo[nf.localCode].useGannen { default:
nf.result += "\u5143" nf.result += japaneseEraNames[i]
break }
} return
nf.result += strconv.Itoa(year) }
break 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) { ...@@ -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"}, {"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"}, {"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"}, {"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]ge\"\"m\"\"d\"\";@", "R1年9月1日"},
{"43709", "[$-411]gge\"\"m\"\"d\"\";@", "\u4EE41年9月1日"}, {"43709", "[$-411]gge\"\"m\"\"d\"\";@", "\u4EE41年9月1日"},
{"43709", "[$-411]ggge\"\"m\"\"d\"\";@", "\u4EE4\u548C1年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]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]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]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]ge\"\"m\"\"d\"\";@", "H31年1月1日"},
{"43466.189571759256", "[$-411]gge\"\"m\"\"d\"\";@", "\u5E7331年1月1日"}, {"43466.189571759256", "[$-411]gge\"\"m\"\"d\"\";@", "\u5E7331年1月1日"},
{"43466.189571759256", "[$-411]ggge\"\"m\"\"d\"\";@", "\u5E73\u621031年1月1日"}, {"43466.189571759256", "[$-411]ggge\"\"m\"\"d\"\";@", "\u5E73\u621031年1月1日"},
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册