@@ -57,11 +57,9 @@ type CultureName byte
57
57
const (
58
58
CultureNameUnknown CultureName = iota
59
59
CultureNameEnUS
60
+ CultureNameJaJP
60
61
CultureNameZhCN
61
62
CultureNameZhTW
62
- CultureNameJaJP
63
- CultureNameKoKR
64
- CultureNameThTH
65
63
)
66
64
67
65
var (
@@ -795,7 +793,7 @@ var (
795
793
31748 : {tags : []string {"zh-Hant" }, localMonth : localMonthsNameChinese3 , apFmt : nfp .AmPm [2 ], weekdayNames : weekdayNamesChinese , weekdayNamesAbbr : weekdayNamesChineseAbbr2 },
796
794
3076 : {tags : []string {"zh-HK" }, localMonth : localMonthsNameChinese2 , apFmt : nfp .AmPm [2 ], weekdayNames : weekdayNamesChinese , weekdayNamesAbbr : weekdayNamesChineseAbbr2 },
797
795
5124 : {tags : []string {"zh-MO" }, localMonth : localMonthsNameChinese3 , apFmt : nfp .AmPm [2 ], weekdayNames : weekdayNamesChinese , weekdayNamesAbbr : weekdayNamesChineseAbbr2 },
798
- 1028 : {tags : []string {"zh-TW" }, localMonth : localMonthsNameChinese3 , apFmt : nfp .AmPm [2 ], weekdayNames : weekdayNamesChinese , weekdayNamesAbbr : weekdayNamesChineseAbbr2 },
796
+ 1028 : {tags : []string {"zh-TW" }, localMonth : localMonthsNameChinese3 , apFmt : nfp .AmPm [2 ], weekdayNames : weekdayNamesChinese , weekdayNamesAbbr : weekdayNamesChineseAbbr2 , useGannen : true },
799
797
9 : {tags : []string {"en" }, localMonth : localMonthsNameEnglish , apFmt : nfp .AmPm [0 ], weekdayNames : weekdayNamesEnglish , weekdayNamesAbbr : weekdayNamesEnglishAbbr },
800
798
4096 : {tags : []string {
801
799
"aa" , "aa-DJ" , "aa-ER" , "aa-ER" , "aa-NA" , "agq" , "agq-CM" , "ak" , "ak-GH" , "sq-ML" ,
@@ -1172,6 +1170,10 @@ var (
1172
1170
"JA-JP-X-GANNEN" : {tags : []string {"ja-JP" }, localMonth : localMonthsNameChinese3 , apFmt : apFmtJapanese , weekdayNames : weekdayNamesJapanese , weekdayNamesAbbr : weekdayNamesJapaneseAbbr },
1173
1171
"JA-JP-X-GANNEN,80" : {tags : []string {"ja-JP" }, localMonth : localMonthsNameChinese3 , apFmt : apFmtJapanese , weekdayNames : weekdayNamesJapanese , weekdayNamesAbbr : weekdayNamesJapaneseAbbr , useGannen : true },
1174
1172
}
1173
+ // republicOfChinaYear defined start time of the Republic of China
1174
+ republicOfChinaYear = time .Date (1912 , time .January , 1 , 0 , 0 , 0 , 0 , time .UTC )
1175
+ // republicOfChinaEraName defined the Republic of China era name for the Republic of China calendar.
1176
+ republicOfChinaEraName = []string {"\u4e2d \u83ef \u6c11 \u570b " , "\u6c11 \u570b " , "\u524d " }
1175
1177
// japaneseEraYears list the Japanese era name periods.
1176
1178
japaneseEraYears = []time.Time {
1177
1179
time .Date (1868 , time .August , 8 , 0 , 0 , 0 , 0 , time .UTC ),
@@ -4638,6 +4640,21 @@ var (
4638
4640
return r .Replace (s )
4639
4641
},
4640
4642
}
4643
+ // langNumFmtFunc defines functions to apply language number format code.
4644
+ langNumFmtFunc = map [CultureName ]func (f * File , numFmtID int ) string {
4645
+ CultureNameEnUS : func (f * File , numFmtID int ) string {
4646
+ return f .langNumFmtFuncEnUS (numFmtID )
4647
+ },
4648
+ CultureNameJaJP : func (f * File , numFmtID int ) string {
4649
+ return f .langNumFmtFuncJaJP (numFmtID )
4650
+ },
4651
+ CultureNameZhCN : func (f * File , numFmtID int ) string {
4652
+ return f .langNumFmtFuncZhCN (numFmtID )
4653
+ },
4654
+ CultureNameZhTW : func (f * File , numFmtID int ) string {
4655
+ return f .langNumFmtFuncZhTW (numFmtID )
4656
+ },
4657
+ }
4641
4658
)
4642
4659
4643
4660
// getSupportedLanguageInfo returns language infomation by giving language code.
@@ -4698,6 +4715,42 @@ func (f *File) langNumFmtFuncEnUS(numFmtID int) string {
4698
4715
return ""
4699
4716
}
4700
4717
4718
+ // langNumFmtFuncJaJP returns number format code by given date and time pattern
4719
+ // for country code ja-jp.
4720
+ func (f * File ) langNumFmtFuncJaJP (numFmtID int ) string {
4721
+ if numFmtID == 30 && f .options .ShortDatePattern != "" {
4722
+ return f .options .ShortDatePattern
4723
+ }
4724
+ if (32 <= numFmtID && numFmtID <= 33 ) && f .options .LongTimePattern != "" {
4725
+ return f .options .LongTimePattern
4726
+ }
4727
+ return langNumFmt ["ja-jp" ][numFmtID ]
4728
+ }
4729
+
4730
+ // langNumFmtFuncZhCN returns number format code by given date and time pattern
4731
+ // for country code zh-cn.
4732
+ func (f * File ) langNumFmtFuncZhCN (numFmtID int ) string {
4733
+ if numFmtID == 30 && f .options .ShortDatePattern != "" {
4734
+ return f .options .ShortDatePattern
4735
+ }
4736
+ if (32 <= numFmtID && numFmtID <= 33 ) && f .options .LongTimePattern != "" {
4737
+ return f .options .LongTimePattern
4738
+ }
4739
+ return langNumFmt ["zh-cn" ][numFmtID ]
4740
+ }
4741
+
4742
+ // langNumFmtFuncZhTW returns number format code by given date and time pattern
4743
+ // for country code zh-tw.
4744
+ func (f * File ) langNumFmtFuncZhTW (numFmtID int ) string {
4745
+ if numFmtID == 30 && f .options .ShortDatePattern != "" {
4746
+ return f .options .ShortDatePattern
4747
+ }
4748
+ if (32 <= numFmtID && numFmtID <= 33 ) && f .options .LongTimePattern != "" {
4749
+ return f .options .LongTimePattern
4750
+ }
4751
+ return langNumFmt ["zh-tw" ][numFmtID ]
4752
+ }
4753
+
4701
4754
// checkDateTimePattern check and validate date and time options field value.
4702
4755
func (f * File ) checkDateTimePattern () error {
4703
4756
for _ , pattern := range []string {f .options .LongDatePattern , f .options .LongTimePattern , f .options .ShortDatePattern } {
@@ -4774,90 +4827,15 @@ func (f *File) extractNumFmtDecimal(fmtCode string) int {
4774
4827
return - 1
4775
4828
}
4776
4829
4777
- // langNumFmtFuncZhCN returns number format code by given date and time pattern
4778
- // for country code zh-cn.
4779
- func (f * File ) langNumFmtFuncZhCN (numFmtID int ) string {
4780
- if numFmtID == 30 && f .options .ShortDatePattern != "" {
4781
- return f .options .ShortDatePattern
4782
- }
4783
- if (32 <= numFmtID && numFmtID <= 33 ) && f .options .LongTimePattern != "" {
4784
- return f .options .LongTimePattern
4785
- }
4786
- return langNumFmt ["zh-cn" ][numFmtID ]
4787
- }
4788
-
4789
- // langNumFmtFuncZhTW returns number format code by given date and time pattern
4790
- // for country code zh-tw.
4791
- func (f * File ) langNumFmtFuncZhTW (numFmtID int ) string {
4792
- if numFmtID == 30 && f .options .ShortDatePattern != "" {
4793
- return f .options .ShortDatePattern
4794
- }
4795
- if (32 <= numFmtID && numFmtID <= 33 ) && f .options .LongTimePattern != "" {
4796
- return f .options .LongTimePattern
4797
- }
4798
- return langNumFmt ["zh-tw" ][numFmtID ]
4799
- }
4800
-
4801
- // langNumFmtFuncJaJP returns number format code by given date and time pattern
4802
- // for country code ja-jp.
4803
- func (f * File ) langNumFmtFuncJaJP (numFmtID int ) string {
4804
- if numFmtID == 30 && f .options .ShortDatePattern != "" {
4805
- return f .options .ShortDatePattern
4806
- }
4807
- if (32 <= numFmtID && numFmtID <= 33 ) && f .options .LongTimePattern != "" {
4808
- return f .options .LongTimePattern
4809
- }
4810
- return langNumFmt ["ja-jp" ][numFmtID ]
4811
- }
4812
-
4813
- // langNumFmtFuncKoKR returns number format code by given date and time pattern
4814
- // for country code ko-kr.
4815
- func (f * File ) langNumFmtFuncKoKR (numFmtID int ) string {
4816
- if numFmtID == 30 && f .options .ShortDatePattern != "" {
4817
- return f .options .ShortDatePattern
4818
- }
4819
- if (32 <= numFmtID && numFmtID <= 33 ) && f .options .LongTimePattern != "" {
4820
- return f .options .LongTimePattern
4821
- }
4822
- return langNumFmt ["ko-kr" ][numFmtID ]
4823
- }
4824
-
4825
- // langNumFmtFuncThTH returns number format code by given date and time pattern
4826
- // for country code th-th.
4827
- func (f * File ) langNumFmtFuncThTH (numFmtID int ) string {
4828
- if numFmtID == 71 && f .options .ShortDatePattern != "" {
4829
- return f .options .ShortDatePattern
4830
- }
4831
- if numFmtID == 76 && f .options .LongTimePattern != "" {
4832
- return f .options .LongTimePattern
4833
- }
4834
- return langNumFmt ["th-th" ][numFmtID ]
4835
- }
4836
-
4837
4830
// getBuiltInNumFmtCode convert number format index to number format code with
4838
4831
// specified locale and language.
4839
4832
func (f * File ) getBuiltInNumFmtCode (numFmtID int ) (string , bool ) {
4840
4833
if fmtCode , ok := builtInNumFmt [numFmtID ]; ok {
4841
4834
return fmtCode , true
4842
4835
}
4843
4836
if isLangNumFmt (numFmtID ) {
4844
- if f .options .CultureInfo == CultureNameEnUS {
4845
- return f .langNumFmtFuncEnUS (numFmtID ), true
4846
- }
4847
- if f .options .CultureInfo == CultureNameZhCN {
4848
- return f .langNumFmtFuncZhCN (numFmtID ), true
4849
- }
4850
- if f .options .CultureInfo == CultureNameZhTW {
4851
- return f .langNumFmtFuncZhTW (numFmtID ), true
4852
- }
4853
- if f .options .CultureInfo == CultureNameJaJP {
4854
- return f .langNumFmtFuncJaJP (numFmtID ), true
4855
- }
4856
- if f .options .CultureInfo == CultureNameKoKR {
4857
- return f .langNumFmtFuncKoKR (numFmtID ), true
4858
- }
4859
- if f .options .CultureInfo == CultureNameThTH {
4860
- return f .langNumFmtFuncThTH (numFmtID ), true
4837
+ if fn , ok := langNumFmtFunc [f .options .CultureInfo ]; ok {
4838
+ return fn (f , numFmtID ), true
4861
4839
}
4862
4840
}
4863
4841
return "" , false
@@ -6976,17 +6954,8 @@ func eraYear(t time.Time) (int, int) {
6976
6954
return i , year
6977
6955
}
6978
6956
6979
- // yearsHandler will be handling years in the date and times types tokens for a
6980
- // number format expression.
6981
- func (nf * numberFormat ) yearsHandler (token nfp.Token ) {
6982
- if strings .Contains (strings .ToUpper (token .TValue ), "Y" ) {
6983
- if len (token .TValue ) <= 2 {
6984
- nf .result += strconv .Itoa (nf .t .Year ())[2 :]
6985
- return
6986
- }
6987
- nf .result += strconv .Itoa (nf .t .Year ())
6988
- return
6989
- }
6957
+ // japaneseYearHandler handling the Japanease calendar years.
6958
+ func (nf * numberFormat ) japaneseYearHandler (token nfp.Token , langInfo languageInfo ) {
6990
6959
if strings .Contains (strings .ToUpper (token .TValue ), "G" ) {
6991
6960
i , year := eraYear (nf .t )
6992
6961
if year == - 1 {
@@ -7025,6 +6994,58 @@ func (nf *numberFormat) yearsHandler(token nfp.Token) {
7025
6994
}
7026
6995
}
7027
6996
6997
+ // republicOfChinaYearHandler handling the Republic of China calendar years.
6998
+ func (nf * numberFormat ) republicOfChinaYearHandler (token nfp.Token , langInfo languageInfo ) {
6999
+ if strings .Contains (strings .ToUpper (token .TValue ), "G" ) {
7000
+ year := nf .t .Year () - republicOfChinaYear .Year () + 1
7001
+ if year == 1 {
7002
+ nf .useGannen = langInfo .useGannen
7003
+ }
7004
+ var name string
7005
+ if name = republicOfChinaEraName [0 ]; len (token .TValue ) < 3 {
7006
+ name = republicOfChinaEraName [1 ]
7007
+ }
7008
+ if year < 0 {
7009
+ name += republicOfChinaEraName [2 ]
7010
+ }
7011
+ nf .result += name
7012
+ }
7013
+ if strings .Contains (strings .ToUpper (token .TValue ), "E" ) {
7014
+ year := nf .t .Year () - republicOfChinaYear .Year () + 1
7015
+ if year < 0 {
7016
+ year = republicOfChinaYear .Year () - nf .t .Year ()
7017
+ }
7018
+ if year == 1 && nf .useGannen {
7019
+ nf .result += "\u5143 "
7020
+ return
7021
+ }
7022
+ if len (token .TValue ) == 1 && ! nf .useGannen {
7023
+ nf .result += strconv .Itoa (year )
7024
+ return
7025
+ }
7026
+ }
7027
+ }
7028
+
7029
+ // yearsHandler will be handling years in the date and times types tokens for a
7030
+ // number format expression.
7031
+ func (nf * numberFormat ) yearsHandler (token nfp.Token ) {
7032
+ if strings .Contains (strings .ToUpper (token .TValue ), "Y" ) {
7033
+ if len (token .TValue ) <= 2 {
7034
+ nf .result += strconv .Itoa (nf .t .Year ())[2 :]
7035
+ return
7036
+ }
7037
+ nf .result += strconv .Itoa (nf .t .Year ())
7038
+ return
7039
+ }
7040
+ langInfo , _ := getSupportedLanguageInfo (nf .localCode )
7041
+ if inStrSlice (langInfo .tags , "zh-TW" , false ) != - 1 {
7042
+ nf .republicOfChinaYearHandler (token , langInfo )
7043
+ }
7044
+ if inStrSlice (langInfo .tags , "ja-JP" , false ) != - 1 {
7045
+ nf .japaneseYearHandler (token , langInfo )
7046
+ }
7047
+ }
7048
+
7028
7049
// daysHandler will be handling days in the date and times types tokens for a
7029
7050
// number format expression.
7030
7051
func (nf * numberFormat ) daysHandler (token nfp.Token ) {
0 commit comments