ks.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. package ks
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type ks struct {
  10. locale string
  11. pluralsCardinal []locales.PluralRule
  12. pluralsOrdinal []locales.PluralRule
  13. pluralsRange []locales.PluralRule
  14. decimal string
  15. group string
  16. minus string
  17. percent string
  18. perMille string
  19. timeSeparator string
  20. inifinity string
  21. currencies []string // idx = enum of currency code
  22. currencyPositivePrefix string
  23. currencyNegativePrefix string
  24. monthsAbbreviated []string
  25. monthsNarrow []string
  26. monthsWide []string
  27. daysAbbreviated []string
  28. daysNarrow []string
  29. daysShort []string
  30. daysWide []string
  31. periodsAbbreviated []string
  32. periodsNarrow []string
  33. periodsShort []string
  34. periodsWide []string
  35. erasAbbreviated []string
  36. erasNarrow []string
  37. erasWide []string
  38. timezones map[string]string
  39. }
  40. // New returns a new instance of translator for the 'ks' locale
  41. func New() locales.Translator {
  42. return &ks{
  43. locale: "ks",
  44. pluralsCardinal: []locales.PluralRule{2, 6},
  45. pluralsOrdinal: nil,
  46. pluralsRange: nil,
  47. decimal: ".",
  48. group: ",",
  49. minus: "‎-",
  50. percent: "%",
  51. perMille: "‰",
  52. timeSeparator: ":",
  53. inifinity: "∞",
  54. currencies: []string{"ADP", "AED", "AFA", "AFN", "ALK", "ALL", "AMD", "ANG", "AOA", "AOK", "AON", "AOR", "ARA", "ARL", "ARM", "ARP", "ARS", "ATS", "AUD", "AWG", "AZM", "AZN", "BAD", "BAM", "BAN", "BBD", "BDT", "BEC", "BEF", "BEL", "BGL", "BGM", "BGN", "BGO", "BHD", "BIF", "BMD", "BND", "BOB", "BOL", "BOP", "BOV", "BRB", "BRC", "BRE", "BRL", "BRN", "BRR", "BRZ", "BSD", "BTN", "BUK", "BWP", "BYB", "BYN", "BYR", "BZD", "CAD", "CDF", "CHE", "CHF", "CHW", "CLE", "CLF", "CLP", "CNH", "CNX", "CNY", "COP", "COU", "CRC", "CSD", "CSK", "CUC", "CUP", "CVE", "CYP", "CZK", "DDM", "DEM", "DJF", "DKK", "DOP", "DZD", "ECS", "ECV", "EEK", "EGP", "ERN", "ESA", "ESB", "ESP", "ETB", "EUR", "FIM", "FJD", "FKP", "FRF", "GBP", "GEK", "GEL", "GHC", "GHS", "GIP", "GMD", "GNF", "GNS", "GQE", "GRD", "GTQ", "GWE", "GWP", "GYD", "HKD", "HNL", "HRD", "HRK", "HTG", "HUF", "IDR", "IEP", "ILP", "ILR", "ILS", "INR", "IQD", "IRR", "ISJ", "ISK", "ITL", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRH", "KRO", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LTL", "LTT", "LUC", "LUF", "LUL", "LVL", "LVR", "LYD", "MAD", "MAF", "MCF", "MDC", "MDL", "MGA", "MGF", "MKD", "MKN", "MLF", "MMK", "MNT", "MOP", "MRO", "MTL", "MTP", "MUR", "MVP", "MVR", "MWK", "MXN", "MXP", "MXV", "MYR", "MZE", "MZM", "MZN", "NAD", "NGN", "NIC", "NIO", "NLG", "NOK", "NPR", "NZD", "OMR", "PAB", "PEI", "PEN", "PES", "PGK", "PHP", "PKR", "PLN", "PLZ", "PTE", "PYG", "QAR", "RHD", "ROL", "RON", "RSD", "RUB", "RUR", "RWF", "SAR", "SBD", "SCR", "SDD", "SDG", "SDP", "SEK", "SGD", "SHP", "SIT", "SKK", "SLL", "SOS", "SRD", "SRG", "SSP", "STD", "STN", "SUR", "SVC", "SYP", "SZL", "THB", "TJR", "TJS", "TMM", "TMT", "TND", "TOP", "TPE", "TRL", "TRY", "TTD", "TWD", "TZS", "UAH", "UAK", "UGS", "UGX", "USD", "USN", "USS", "UYI", "UYP", "UYU", "UZS", "VEB", "VEF", "VND", "VNN", "VUV", "WST", "XAF", "XAG", "XAU", "XBA", "XBB", "XBC", "XBD", "XCD", "XDR", "XEU", "XFO", "XFU", "XOF", "XPD", "XPF", "XPT", "XRE", "XSU", "XTS", "XUA", "XXX", "YDD", "YER", "YUD", "YUM", "YUN", "YUR", "ZAL", "ZAR", "ZMK", "ZMW", "ZRN", "ZRZ", "ZWD", "ZWL", "ZWR"},
  55. currencyPositivePrefix: " ",
  56. currencyNegativePrefix: " ",
  57. monthsNarrow: []string{"", "ج", "ف", "م", "ا", "م", "ج", "ج", "ا", "س", "س", "ا", "ن"},
  58. monthsWide: []string{"", "جنؤری", "فرؤری", "مارٕچ", "اپریل", "میٔ", "جوٗن", "جوٗلایی", "اگست", "ستمبر", "اکتوٗبر", "نومبر", "دسمبر"},
  59. daysAbbreviated: []string{"آتھوار", "ژٔنٛدٕروار", "بوٚموار", "بودوار", "برٛٮ۪سوار", "جُمہ", "بٹوار"},
  60. daysNarrow: []string{"ا", "ژ", "ب", "ب", "ب", "ج", "ب"},
  61. daysWide: []string{"اَتھوار", "ژٔنٛدرٕروار", "بوٚموار", "بودوار", "برٛٮ۪سوار", "جُمہ", "بٹوار"},
  62. erasAbbreviated: []string{"بی سی", "اے ڈی"},
  63. erasNarrow: []string{"", ""},
  64. erasWide: []string{"قبٕل مسیٖح", "عیٖسوی سنہٕ"},
  65. timezones: map[string]string{"UYST": "یوٗرٮ۪گوَے سَمَر ٹایِم", "CHAST": "کٮ۪تھَم سٹینڑاڑ ٹایِم", "ECT": "اِکویڑَر ٹایِم", "EST": "مشرقی سٹینڑاڑ ٹایِم", "WART": "مغربی ارجٮ۪نٹیٖنا سٹینڑاڑ ٹایِم", "EAT": "مشرقی افریٖقا ٹایِم", "HAST": "حَواے اٮ۪لیوٗٹِیَن سٹینڑاڑ ٹایِم", "COT": "کولومبِیا سٹینڑاڑ ٹایِم", "AEST": "آسٹریلِیَن مشرقی سٹینڑاڑ ٹایِم", "MST": "ماونٹین سٹینڑاڑ ٹایِم", "HNOG": "مغرِبی گریٖن لینڑُک سٹینڑاڑ ٹایِم", "IST": "ہِنٛدوستان", "COST": "کولومبِیا سَمَر ٹایِم", "CHADT": "چٮ۪تھَم سَمَر ٹایِم", "PST": "پیسِفِک سٹینڑاڑ ٹایِم", "BT": "بوٗٹان ٹایِم", "LHST": "لعاڑ حووے سٹینڑاڑ ٹایِم", "ART": "ارجٮ۪نٹیٖنا سٹینڑاڑ ٹایِم", "GYT": "گُیَنا ٹایِم", "CDT": "مرکزی ڈےلایِٔٹ ٹایِم", "NZST": "نِوزِلینڑ سٹینڑاڑ ٹایِم", "NZDT": "نِوزِلینڑ ڑےلایٔٹ ٹایِم", "MYT": "مَلیشِیا ٹایِم", "GFT": "فرٛٮ۪نٛچ گیوٗٮ۪نا ٹایِم", "JST": "جاپٲنۍ سٹینڑاڑ ٹایِم", "∅∅∅": "اٮ۪مَزَن سَمَر ٹایِم", "ChST": "کٮ۪مورو سٹینڑاڑ ٹایِم", "MESZ": "مرکزی یوٗرپی سَمَر ٹایِم", "HAT": "نیوٗ فاونڑ لینڑ ڑےلایِٔٹ ٹایِم", "ACWDT": "آسٹریلِیَن مرکزی مغربی ڈےلایِٔٹ ٹایِم", "HEOG": "مغرِبی گریٖن لینڑُک سَمَر ٹایِم", "SRT": "سُرِنام ٹایِم", "HECU": "کیوٗبا ڈےلایِٔٹ ٹایِم", "SGT": "سِنٛگاپوٗر ٹایِم", "JDT": "جاپٲنۍ ڑےلایِٔٹ ٹایِم", "EDT": "مشرقی ڈےلایِٔٹ ٹایِم", "HKST": "حانٛگ کانٛگ سَمَر ٹایِم", "PDT": "پیسِفِک ڈےلایِٔٹ ٹایِم", "MDT": "ماونٹین ڈےلایِٔٹ ٹایِم", "AEDT": "آسٹریلِیَن مشرقی ڈےلایِٔٹ ٹایِم", "TMT": "تُرکمٮ۪نِستان سٹینڑاڑ ٹایِم", "AKST": "اٮ۪لاسکا سٹینڑاڑ ٹایِم", "WITA": "مرکزی اِنڑونیشِیا ٹایِم", "HNNOMX": "HNNOMX", "CLT": "چِلی سٹینڑاڑ ٹایِم", "HEPMX": "HEPMX", "WEZ": "مغرِبی یوٗرپی سٹینڑاڑ ٹایِم", "AWDT": "آسٹریلِیَن مغرِبیٖ ڈےلایٔٹ ٹایِم", "SAST": "جنوٗبی افریقا ٹایِم", "VET": "وٮ۪نٮ۪زیوٗلا ٹایِم", "WIT": "مشرِقی اِنڑونیشِیا ٹایِم", "ARST": "ارجٮ۪نٹیٖنا سَمَر ٹایِم", "AWST": "آسٹریلِیَن مغرِبی سٹینڑاڑ ٹایِم", "HNPMX": "HNPMX", "WAST": "مغربی افریٖقا سَمَر ٹایِم", "ACST": "آسٹریلِیَن مرکزی سٹینڑاڑ ٹایِم", "HNT": "نیوٗ فاونڑلینڑ سٹینڑاڑ ٹایِم", "HENOMX": "HENOMX", "CAT": "مرکزی افریٖقا ٹایِم", "OESZ": "مشرقی یوٗرپی سَمَر ٹایِم", "GMT": "گرٛیٖن وِچ میٖن ٹایِم", "BOT": "بولِوِیا ٹایِم", "CST": "مرکزی سٹینڑاڑ ٹایِم", "AST": "اٮ۪ٹلانٹِک سٹینڑاڑ ٹایِم", "WIB": "مغرِبی اِنڑونیشِیا ٹایِم", "AKDT": "اٮ۪لاسکا ڈےلایِٔٹ ٹایِم", "MEZ": "مرکزی یوٗرپی سٹینڑاڑ ٹایِم", "HNPM": "سینٛٹ پَیری مِقیوٗلَن سٹینڑاڑ ٹایِم", "OEZ": "مشرقی یوٗرپی سٹینڑاڑ ٹایِم", "WESZ": "مغرِبی یوٗرِپی سَمَر ٹایِم", "ACDT": "آسٹریلِیَن مرکزی ڈےلایِٔٹ ٹایِم", "HNEG": "مشرِقی گریٖن لینڑُک سٹینڑاڑ ٹایِم", "LHDT": "لعاڑ ڑےلایٔٹ ٹایِم", "CLST": "چِلی سَمَر ٹایِم", "UYT": "یوٗرٮ۪گوَے سٹینڑاڑ ٹایِم", "ADT": "اٮ۪ٹلانٹِک ڈےلایِٔٹ ٹایِم", "TMST": "تُرکمٮ۪نِستان سَمَر ٹایِم", "HNCU": "کیوٗبا سٹینڑاڑ ٹایِم", "WARST": "مغربی ارجٮ۪نٹیٖنا سَمَر ٹایِم", "HKT": "حانگ کانٛگ سٹینڑاڑ ٹایِم", "HADT": "حَواے اٮ۪لیوٗٹِیَن سَمَر ٹایِم", "WAT": "مغربی افریٖقا سٹینڑاڑ ٹایِم", "HEPM": "سینٛٹ پَیری مِقیوٗلَن ڑےلایِٔٹ ٹایِم", "ACWST": "آسٹریلِیَن مرکزی مغربی سٹینڑاڑ ٹایِم", "HEEG": "مشرِقی گریٖن لینڑُک سَمَر ٹایِم"},
  66. }
  67. }
  68. // Locale returns the current translators string locale
  69. func (ks *ks) Locale() string {
  70. return ks.locale
  71. }
  72. // PluralsCardinal returns the list of cardinal plural rules associated with 'ks'
  73. func (ks *ks) PluralsCardinal() []locales.PluralRule {
  74. return ks.pluralsCardinal
  75. }
  76. // PluralsOrdinal returns the list of ordinal plural rules associated with 'ks'
  77. func (ks *ks) PluralsOrdinal() []locales.PluralRule {
  78. return ks.pluralsOrdinal
  79. }
  80. // PluralsRange returns the list of range plural rules associated with 'ks'
  81. func (ks *ks) PluralsRange() []locales.PluralRule {
  82. return ks.pluralsRange
  83. }
  84. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'ks'
  85. func (ks *ks) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  86. n := math.Abs(num)
  87. if n == 1 {
  88. return locales.PluralRuleOne
  89. }
  90. return locales.PluralRuleOther
  91. }
  92. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'ks'
  93. func (ks *ks) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  94. return locales.PluralRuleUnknown
  95. }
  96. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'ks'
  97. func (ks *ks) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  98. return locales.PluralRuleUnknown
  99. }
  100. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  101. func (ks *ks) MonthAbbreviated(month time.Month) string {
  102. return ks.monthsAbbreviated[month]
  103. }
  104. // MonthsAbbreviated returns the locales abbreviated months
  105. func (ks *ks) MonthsAbbreviated() []string {
  106. return nil
  107. }
  108. // MonthNarrow returns the locales narrow month given the 'month' provided
  109. func (ks *ks) MonthNarrow(month time.Month) string {
  110. return ks.monthsNarrow[month]
  111. }
  112. // MonthsNarrow returns the locales narrow months
  113. func (ks *ks) MonthsNarrow() []string {
  114. return ks.monthsNarrow[1:]
  115. }
  116. // MonthWide returns the locales wide month given the 'month' provided
  117. func (ks *ks) MonthWide(month time.Month) string {
  118. return ks.monthsWide[month]
  119. }
  120. // MonthsWide returns the locales wide months
  121. func (ks *ks) MonthsWide() []string {
  122. return ks.monthsWide[1:]
  123. }
  124. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  125. func (ks *ks) WeekdayAbbreviated(weekday time.Weekday) string {
  126. return ks.daysAbbreviated[weekday]
  127. }
  128. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  129. func (ks *ks) WeekdaysAbbreviated() []string {
  130. return ks.daysAbbreviated
  131. }
  132. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  133. func (ks *ks) WeekdayNarrow(weekday time.Weekday) string {
  134. return ks.daysNarrow[weekday]
  135. }
  136. // WeekdaysNarrow returns the locales narrow weekdays
  137. func (ks *ks) WeekdaysNarrow() []string {
  138. return ks.daysNarrow
  139. }
  140. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  141. func (ks *ks) WeekdayShort(weekday time.Weekday) string {
  142. return ks.daysShort[weekday]
  143. }
  144. // WeekdaysShort returns the locales short weekdays
  145. func (ks *ks) WeekdaysShort() []string {
  146. return ks.daysShort
  147. }
  148. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  149. func (ks *ks) WeekdayWide(weekday time.Weekday) string {
  150. return ks.daysWide[weekday]
  151. }
  152. // WeekdaysWide returns the locales wide weekdays
  153. func (ks *ks) WeekdaysWide() []string {
  154. return ks.daysWide
  155. }
  156. // Decimal returns the decimal point of number
  157. func (ks *ks) Decimal() string {
  158. return ks.decimal
  159. }
  160. // Group returns the group of number
  161. func (ks *ks) Group() string {
  162. return ks.group
  163. }
  164. // Group returns the minus sign of number
  165. func (ks *ks) Minus() string {
  166. return ks.minus
  167. }
  168. // FmtNumber returns 'num' with digits/precision of 'v' for 'ks' and handles both Whole and Real numbers based on 'v'
  169. func (ks *ks) FmtNumber(num float64, v uint64) string {
  170. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  171. l := len(s) + 5 + 1*len(s[:len(s)-int(v)-1])/3
  172. count := 0
  173. inWhole := v == 0
  174. inSecondary := false
  175. groupThreshold := 3
  176. b := make([]byte, 0, l)
  177. for i := len(s) - 1; i >= 0; i-- {
  178. if s[i] == '.' {
  179. b = append(b, ks.decimal[0])
  180. inWhole = true
  181. continue
  182. }
  183. if inWhole {
  184. if count == groupThreshold {
  185. b = append(b, ks.group[0])
  186. count = 1
  187. if !inSecondary {
  188. inSecondary = true
  189. groupThreshold = 2
  190. }
  191. } else {
  192. count++
  193. }
  194. }
  195. b = append(b, s[i])
  196. }
  197. if num < 0 {
  198. for j := len(ks.minus) - 1; j >= 0; j-- {
  199. b = append(b, ks.minus[j])
  200. }
  201. }
  202. // reverse
  203. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  204. b[i], b[j] = b[j], b[i]
  205. }
  206. return string(b)
  207. }
  208. // FmtPercent returns 'num' with digits/precision of 'v' for 'ks' and handles both Whole and Real numbers based on 'v'
  209. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  210. func (ks *ks) FmtPercent(num float64, v uint64) string {
  211. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  212. l := len(s) + 6
  213. b := make([]byte, 0, l)
  214. for i := len(s) - 1; i >= 0; i-- {
  215. if s[i] == '.' {
  216. b = append(b, ks.decimal[0])
  217. continue
  218. }
  219. b = append(b, s[i])
  220. }
  221. if num < 0 {
  222. for j := len(ks.minus) - 1; j >= 0; j-- {
  223. b = append(b, ks.minus[j])
  224. }
  225. }
  226. // reverse
  227. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  228. b[i], b[j] = b[j], b[i]
  229. }
  230. b = append(b, ks.percent...)
  231. return string(b)
  232. }
  233. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'ks'
  234. func (ks *ks) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  235. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  236. symbol := ks.currencies[currency]
  237. l := len(s) + len(symbol) + 7 + 1*len(s[:len(s)-int(v)-1])/3
  238. count := 0
  239. inWhole := v == 0
  240. inSecondary := false
  241. groupThreshold := 3
  242. b := make([]byte, 0, l)
  243. for i := len(s) - 1; i >= 0; i-- {
  244. if s[i] == '.' {
  245. b = append(b, ks.decimal[0])
  246. inWhole = true
  247. continue
  248. }
  249. if inWhole {
  250. if count == groupThreshold {
  251. b = append(b, ks.group[0])
  252. count = 1
  253. if !inSecondary {
  254. inSecondary = true
  255. groupThreshold = 2
  256. }
  257. } else {
  258. count++
  259. }
  260. }
  261. b = append(b, s[i])
  262. }
  263. for j := len(symbol) - 1; j >= 0; j-- {
  264. b = append(b, symbol[j])
  265. }
  266. for j := len(ks.currencyPositivePrefix) - 1; j >= 0; j-- {
  267. b = append(b, ks.currencyPositivePrefix[j])
  268. }
  269. if num < 0 {
  270. for j := len(ks.minus) - 1; j >= 0; j-- {
  271. b = append(b, ks.minus[j])
  272. }
  273. }
  274. // reverse
  275. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  276. b[i], b[j] = b[j], b[i]
  277. }
  278. if int(v) < 2 {
  279. if v == 0 {
  280. b = append(b, ks.decimal...)
  281. }
  282. for i := 0; i < 2-int(v); i++ {
  283. b = append(b, '0')
  284. }
  285. }
  286. return string(b)
  287. }
  288. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'ks'
  289. // in accounting notation.
  290. func (ks *ks) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  291. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  292. symbol := ks.currencies[currency]
  293. l := len(s) + len(symbol) + 7 + 1*len(s[:len(s)-int(v)-1])/3
  294. count := 0
  295. inWhole := v == 0
  296. inSecondary := false
  297. groupThreshold := 3
  298. b := make([]byte, 0, l)
  299. for i := len(s) - 1; i >= 0; i-- {
  300. if s[i] == '.' {
  301. b = append(b, ks.decimal[0])
  302. inWhole = true
  303. continue
  304. }
  305. if inWhole {
  306. if count == groupThreshold {
  307. b = append(b, ks.group[0])
  308. count = 1
  309. if !inSecondary {
  310. inSecondary = true
  311. groupThreshold = 2
  312. }
  313. } else {
  314. count++
  315. }
  316. }
  317. b = append(b, s[i])
  318. }
  319. if num < 0 {
  320. for j := len(symbol) - 1; j >= 0; j-- {
  321. b = append(b, symbol[j])
  322. }
  323. for j := len(ks.currencyNegativePrefix) - 1; j >= 0; j-- {
  324. b = append(b, ks.currencyNegativePrefix[j])
  325. }
  326. for j := len(ks.minus) - 1; j >= 0; j-- {
  327. b = append(b, ks.minus[j])
  328. }
  329. } else {
  330. for j := len(symbol) - 1; j >= 0; j-- {
  331. b = append(b, symbol[j])
  332. }
  333. for j := len(ks.currencyPositivePrefix) - 1; j >= 0; j-- {
  334. b = append(b, ks.currencyPositivePrefix[j])
  335. }
  336. }
  337. // reverse
  338. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  339. b[i], b[j] = b[j], b[i]
  340. }
  341. if int(v) < 2 {
  342. if v == 0 {
  343. b = append(b, ks.decimal...)
  344. }
  345. for i := 0; i < 2-int(v); i++ {
  346. b = append(b, '0')
  347. }
  348. }
  349. return string(b)
  350. }
  351. // FmtDateShort returns the short date representation of 't' for 'ks'
  352. func (ks *ks) FmtDateShort(t time.Time) string {
  353. b := make([]byte, 0, 32)
  354. b = strconv.AppendInt(b, int64(t.Month()), 10)
  355. b = append(b, []byte{0x2f}...)
  356. b = strconv.AppendInt(b, int64(t.Day()), 10)
  357. b = append(b, []byte{0x2f}...)
  358. if t.Year() > 9 {
  359. b = append(b, strconv.Itoa(t.Year())[2:]...)
  360. } else {
  361. b = append(b, strconv.Itoa(t.Year())[1:]...)
  362. }
  363. return string(b)
  364. }
  365. // FmtDateMedium returns the medium date representation of 't' for 'ks'
  366. func (ks *ks) FmtDateMedium(t time.Time) string {
  367. b := make([]byte, 0, 32)
  368. b = append(b, ks.monthsAbbreviated[t.Month()]...)
  369. b = append(b, []byte{0x20}...)
  370. b = strconv.AppendInt(b, int64(t.Day()), 10)
  371. b = append(b, []byte{0x2c, 0x20}...)
  372. if t.Year() > 0 {
  373. b = strconv.AppendInt(b, int64(t.Year()), 10)
  374. } else {
  375. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  376. }
  377. return string(b)
  378. }
  379. // FmtDateLong returns the long date representation of 't' for 'ks'
  380. func (ks *ks) FmtDateLong(t time.Time) string {
  381. b := make([]byte, 0, 32)
  382. b = append(b, ks.monthsWide[t.Month()]...)
  383. b = append(b, []byte{0x20}...)
  384. b = strconv.AppendInt(b, int64(t.Day()), 10)
  385. b = append(b, []byte{0x2c, 0x20}...)
  386. if t.Year() > 0 {
  387. b = strconv.AppendInt(b, int64(t.Year()), 10)
  388. } else {
  389. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  390. }
  391. return string(b)
  392. }
  393. // FmtDateFull returns the full date representation of 't' for 'ks'
  394. func (ks *ks) FmtDateFull(t time.Time) string {
  395. b := make([]byte, 0, 32)
  396. b = append(b, ks.daysWide[t.Weekday()]...)
  397. b = append(b, []byte{0x2c, 0x20}...)
  398. b = append(b, ks.monthsWide[t.Month()]...)
  399. b = append(b, []byte{0x20}...)
  400. b = strconv.AppendInt(b, int64(t.Day()), 10)
  401. b = append(b, []byte{0x2c, 0x20}...)
  402. if t.Year() > 0 {
  403. b = strconv.AppendInt(b, int64(t.Year()), 10)
  404. } else {
  405. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  406. }
  407. return string(b)
  408. }
  409. // FmtTimeShort returns the short time representation of 't' for 'ks'
  410. func (ks *ks) FmtTimeShort(t time.Time) string {
  411. b := make([]byte, 0, 32)
  412. h := t.Hour()
  413. if h > 12 {
  414. h -= 12
  415. }
  416. b = strconv.AppendInt(b, int64(h), 10)
  417. b = append(b, ks.timeSeparator...)
  418. if t.Minute() < 10 {
  419. b = append(b, '0')
  420. }
  421. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  422. b = append(b, []byte{0x20}...)
  423. if t.Hour() < 12 {
  424. b = append(b, ks.periodsAbbreviated[0]...)
  425. } else {
  426. b = append(b, ks.periodsAbbreviated[1]...)
  427. }
  428. return string(b)
  429. }
  430. // FmtTimeMedium returns the medium time representation of 't' for 'ks'
  431. func (ks *ks) FmtTimeMedium(t time.Time) string {
  432. b := make([]byte, 0, 32)
  433. h := t.Hour()
  434. if h > 12 {
  435. h -= 12
  436. }
  437. b = strconv.AppendInt(b, int64(h), 10)
  438. b = append(b, ks.timeSeparator...)
  439. if t.Minute() < 10 {
  440. b = append(b, '0')
  441. }
  442. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  443. b = append(b, ks.timeSeparator...)
  444. if t.Second() < 10 {
  445. b = append(b, '0')
  446. }
  447. b = strconv.AppendInt(b, int64(t.Second()), 10)
  448. b = append(b, []byte{0x20}...)
  449. if t.Hour() < 12 {
  450. b = append(b, ks.periodsAbbreviated[0]...)
  451. } else {
  452. b = append(b, ks.periodsAbbreviated[1]...)
  453. }
  454. return string(b)
  455. }
  456. // FmtTimeLong returns the long time representation of 't' for 'ks'
  457. func (ks *ks) FmtTimeLong(t time.Time) string {
  458. b := make([]byte, 0, 32)
  459. h := t.Hour()
  460. if h > 12 {
  461. h -= 12
  462. }
  463. b = strconv.AppendInt(b, int64(h), 10)
  464. b = append(b, ks.timeSeparator...)
  465. if t.Minute() < 10 {
  466. b = append(b, '0')
  467. }
  468. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  469. b = append(b, ks.timeSeparator...)
  470. if t.Second() < 10 {
  471. b = append(b, '0')
  472. }
  473. b = strconv.AppendInt(b, int64(t.Second()), 10)
  474. b = append(b, []byte{0x20}...)
  475. if t.Hour() < 12 {
  476. b = append(b, ks.periodsAbbreviated[0]...)
  477. } else {
  478. b = append(b, ks.periodsAbbreviated[1]...)
  479. }
  480. b = append(b, []byte{0x20}...)
  481. tz, _ := t.Zone()
  482. b = append(b, tz...)
  483. return string(b)
  484. }
  485. // FmtTimeFull returns the full time representation of 't' for 'ks'
  486. func (ks *ks) FmtTimeFull(t time.Time) string {
  487. b := make([]byte, 0, 32)
  488. h := t.Hour()
  489. if h > 12 {
  490. h -= 12
  491. }
  492. b = strconv.AppendInt(b, int64(h), 10)
  493. b = append(b, ks.timeSeparator...)
  494. if t.Minute() < 10 {
  495. b = append(b, '0')
  496. }
  497. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  498. b = append(b, ks.timeSeparator...)
  499. if t.Second() < 10 {
  500. b = append(b, '0')
  501. }
  502. b = strconv.AppendInt(b, int64(t.Second()), 10)
  503. b = append(b, []byte{0x20}...)
  504. if t.Hour() < 12 {
  505. b = append(b, ks.periodsAbbreviated[0]...)
  506. } else {
  507. b = append(b, ks.periodsAbbreviated[1]...)
  508. }
  509. b = append(b, []byte{0x20}...)
  510. tz, _ := t.Zone()
  511. if btz, ok := ks.timezones[tz]; ok {
  512. b = append(b, btz...)
  513. } else {
  514. b = append(b, tz...)
  515. }
  516. return string(b)
  517. }