@@ -33,18 +33,18 @@ type languageInfo struct {
3333// numberFormat directly maps the number format parser runtime required
3434// fields.
3535type 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.
17711784func (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}
0 commit comments