bn.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. package bn
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type bn 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. monthsAbbreviated []string
  23. monthsNarrow []string
  24. monthsWide []string
  25. daysAbbreviated []string
  26. daysNarrow []string
  27. daysShort []string
  28. daysWide []string
  29. periodsAbbreviated []string
  30. periodsNarrow []string
  31. periodsShort []string
  32. periodsWide []string
  33. erasAbbreviated []string
  34. erasNarrow []string
  35. erasWide []string
  36. timezones map[string]string
  37. }
  38. // New returns a new instance of translator for the 'bn' locale
  39. func New() locales.Translator {
  40. return &bn{
  41. locale: "bn",
  42. pluralsCardinal: []locales.PluralRule{2, 6},
  43. pluralsOrdinal: []locales.PluralRule{2, 3, 4, 5, 6},
  44. pluralsRange: []locales.PluralRule{2, 6},
  45. decimal: ".",
  46. group: ",",
  47. minus: "-",
  48. percent: "%",
  49. perMille: "‰",
  50. timeSeparator: ":",
  51. inifinity: "∞",
  52. currencies: []string{"ADP", "AED", "AFA", "AFN", "ALK", "ALL", "AMD", "ANG", "AOA", "AOK", "AON", "AOR", "ARA", "ARL", "ARM", "ARP", "ARS", "ATS", "A$", "AWG", "AZM", "AZN", "BAD", "BAM", "BAN", "BBD", "৳", "BEC", "BEF", "BEL", "BGL", "BGM", "BGN", "BGO", "BHD", "BIF", "BMD", "BND", "BOB", "BOL", "BOP", "BOV", "BRB", "BRC", "BRE", "R$", "BRN", "BRR", "BRZ", "BSD", "BTN", "BUK", "BWP", "BYB", "BYN", "BYR", "BZD", "CA$", "CDF", "CHE", "CHF", "CHW", "CLE", "CLF", "CLP", "CNH", "CNX", "CN¥", "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", "€", "FIM", "FJD", "FKP", "FRF", "£", "GEK", "GEL", "GHC", "GHS", "GIP", "GMD", "GNF", "GNS", "GQE", "GRD", "GTQ", "GWE", "GWP", "GYD", "HK$", "HNL", "HRD", "HRK", "HTG", "HUF", "IDR", "IEP", "ILP", "ILR", "₪", "₹", "IQD", "IRR", "ISJ", "ISK", "ITL", "JMD", "JOD", "JP¥", "KES", "KGS", "KHR", "KMF", "KPW", "KRH", "KRO", "₩", "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", "MX$", "MXP", "MXV", "MYR", "MZE", "MZM", "MZN", "NAD", "NGN", "NIC", "NIO", "NLG", "NOK", "NPR", "NZ$", "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", "฿", "TJR", "TJS", "TMM", "TMT", "TND", "TOP", "TPE", "TRL", "TRY", "TTD", "NT$", "TZS", "UAH", "UAK", "UGS", "UGX", "US$", "USN", "USS", "UYI", "UYP", "UYU", "UZS", "VEB", "VEF", "₫", "VNN", "VUV", "WST", "FCFA", "XAG", "XAU", "XBA", "XBB", "XBC", "XBD", "EC$", "XDR", "XEU", "XFO", "XFU", "CFA", "XPD", "CFPF", "XPT", "XRE", "XSU", "XTS", "XUA", "XXX", "YDD", "YER", "YUD", "YUM", "YUN", "YUR", "ZAL", "ZAR", "ZMK", "ZMW", "ZRN", "ZRZ", "ZWD", "ZWL", "ZWR"},
  53. monthsAbbreviated: []string{"", "জানু", "ফেব", "মার্চ", "এপ্রিল", "মে", "জুন", "জুলাই", "আগস্ট", "সেপ্টেম্বর", "অক্টোবর", "নভেম্বর", "ডিসেম্বর"},
  54. monthsNarrow: []string{"", "জা", "ফে", "মা", "এ", "মে", "জুন", "জু", "আ", "সে", "অ", "ন", "ডি"},
  55. monthsWide: []string{"", "জানুয়ারী", "ফেব্রুয়ারী", "মার্চ", "এপ্রিল", "মে", "জুন", "জুলাই", "আগস্ট", "সেপ্টেম্বর", "অক্টোবর", "নভেম্বর", "ডিসেম্বর"},
  56. daysAbbreviated: []string{"রবি", "সোম", "মঙ্গল", "বুধ", "বৃহস্পতি", "শুক্র", "শনি"},
  57. daysNarrow: []string{"র", "সো", "ম", "বু", "বৃ", "শু", "শ"},
  58. daysShort: []string{"রঃ", "সোঃ", "মঃ", "বুঃ", "বৃঃ", "শুঃ", "শোঃ"},
  59. daysWide: []string{"রবিবার", "সোমবার", "মঙ্গলবার", "বুধবার", "বৃহস্পতিবার", "শুক্রবার", "শনিবার"},
  60. periodsAbbreviated: []string{"AM", "PM"},
  61. periodsNarrow: []string{"AM", "PM"},
  62. periodsWide: []string{"AM", "PM"},
  63. erasAbbreviated: []string{"খ্রিস্টপূর্ব", "খৃষ্টাব্দ"},
  64. erasNarrow: []string{"", ""},
  65. erasWide: []string{"খ্রিস্টপূর্ব", "খ্রীষ্টাব্দ"},
  66. timezones: map[string]string{"HNOG": "পশ্চিম গ্রীনল্যান্ড মানক সময়", "HKST": "হং কং গ্রীষ্মকালীন সময়", "VET": "ভেনেজুয়েলা সময়", "HNNOMX": "উত্তরপশ্চিম মেক্সিকোর মানক সময়", "EAT": "পূর্ব আফ্রিকা সময়", "PDT": "প্রশান্ত মহাসাগরীয় অঞ্চলের দিনের সময়", "AEDT": "অস্ট্রেলীয় পূর্ব দিবালোক সময়", "AKDT": "আলাস্কা দিবালোক সময়", "CLT": "চিলি মানক সময়", "TMST": "তুর্কমেনিস্তান গ্রীষ্মকালীন সময়", "AWDT": "অস্ট্রেলীয় পশ্চিমি দিবালোক সময়", "NZST": "নিউজিল্যান্ড মানক সময়", "WITA": "কেন্দ্রীয় ইন্দোনেশিয়া সময়", "HNCU": "কিউবা মানক সময়", "CDT": "কেন্দ্রীয় দিবালোক সময়", "ACWDT": "অস্ট্রেলীয় কেন্দ্রীয় পশ্চিমি দিবালোক সময়", "IST": "ভারতীয় মানক সময়", "CLST": "চিলি গ্রীষ্মকালীন সময়", "OESZ": "পূর্ব ইউরোপীয় গ্রীষ্মকালীন সময়", "CHADT": "চ্যাথাম দিবালোক সময়", "WIB": "পশ্চিমী ইন্দোনেশিয়া সময়", "EST": "পূর্বাঞ্চলের প্রমাণ সময়", "BT": "ভুটান সময়", "ACWST": "অস্ট্রেলীয় কেন্দ্রীয় পশ্চিমি মানক সময়", "AST": "অতলান্তিক মানক সময়", "AEST": "অস্ট্রেলীয় পূর্ব মানক সময়", "MDT": "পার্বত্য অঞ্চলের দিনের সময়", "HAT": "নিউফাউন্ডল্যান্ড দিবালোক সময়", "ART": "আর্জেন্টিনা মানক সময়", "COST": "কোলোম্বিয়া গ্রীষ্মকালীন সময়", "UYST": "উরুগুয়ে গ্রীষ্মকালীন সময়", "SAST": "দক্ষিণ আফ্রিকা মানক সময়", "WAST": "পশ্চিম আফ্রিকা গ্রীষ্মকালীন সময়", "HEEG": "পূর্ব গ্রীনল্যান্ড গ্রীষ্মকালীন সময়", "WAT": "পশ্চিম আফ্রিকা মানক সময়", "WEZ": "পশ্চিম ইউরোপীয় মানক সময়", "CAT": "মধ্য আফ্রিকা সময়", "JDT": "জাপান দিবালোক সময়", "GMT": "গ্রীনিচ মিন টাইম", "HNPM": "সেন্ট পিয়ের ও মিকেলন মানক সময়", "ARST": "আর্জেন্টিনা গ্রীষ্মকালীন সময়", "HNPMX": "মেক্সিকান প্রশান্ত মহসাগরীয় মানক সময়", "AKST": "আলাস্কা মানক সময়", "ACST": "অস্ট্রেলীয় কেন্দ্রীয় মানক সময়", "MEZ": "মধ্য ইউরোপীয় মানক সময়", "HKT": "হং কং মানক সময়", "SRT": "সুরিনাম সময়", "WIT": "পূর্ব ইন্দোনেশিয়া সময়", "TMT": "তুর্কমেনিস্তান মানক সময়", "AWST": "অস্ট্রেলীয় পশ্চিমি মানক সময়", "JST": "জাপান মানক সময়", "ACDT": "অস্ট্রেলীয় কেন্দ্রীয় দিবালোক সময়", "HNT": "নিউফাউন্ডল্যান্ড মানক সময়", "HAST": "হাওয়াই-আলেউত মানক সময়", "NZDT": "নিউজিল্যান্ড দিবালোক সময়", "BOT": "বোলিভিয়া সময়", "GFT": "ফরাসি গায়ানা সময়", "WESZ": "পশ্চিম ইউরোপীয় গ্রীষ্মকালীন সময়", "EDT": "পূর্বাঞ্চলের দিবালোক সময়", "HNEG": "পূর্ব গ্রীনল্যান্ড মানক সময়", "MESZ": "মধ্য ইউরোপীয় গ্রীষ্মকালীন সময়", "LHST": "লর্ড হাওয়ে মানক মসয়", "ChST": "চামেরো মানক সময়", "PST": "প্রশান্ত মহাসাগরীয় অঞ্চলের মানক সময়", "MST": "পার্বত্য অঞ্চলের প্রমাণ সময়", "LHDT": "লর্ড হাওয়ে দিবালোক মসয়", "HEPM": "সেন্ট পিয়ের ও মিকেলন দিবালোক সময়", "MYT": "মালয়েশিয়া সময়", "HECU": "কিউবা দিবালোক সময়", "CST": "কেন্দ্রীয় মানক সময়", "HEPMX": "মেক্সিকান প্রশান্ত মহাসাগরীয় দিবালোক সময়", "CHAST": "চ্যাথাম মানক সময়", "HEOG": "পশ্চিম গ্রীনল্যান্ড গ্রীষ্মকালীন সময়", "UYT": "উরুগুয়ে মানক সময়", "HENOMX": "উত্তরপশ্চিম মেক্সিকোর দিনের সময়", "OEZ": "পূর্ব ইউরোপীয় মানক সময়", "HADT": "হাওয়াই-আলেউত দিবালোক সময়", "GYT": "গুয়ানা সময়", "ADT": "অতলান্তিক দিবালোক সময়", "SGT": "সিঙ্গাপুর মানক সময়", "WARST": "পশ্চিমি আর্জেনটিনা গ্রীষ্মকালীন সময়", "COT": "কোলোম্বিয়া মানক সময়", "∅∅∅": "ব্রাসিলিয়া গ্রীষ্মকালীন সময়", "ECT": "ইকুয়েডর সময়", "WART": "পশ্চিমি আর্জেনটিনার প্রমাণ সময়"},
  67. }
  68. }
  69. // Locale returns the current translators string locale
  70. func (bn *bn) Locale() string {
  71. return bn.locale
  72. }
  73. // PluralsCardinal returns the list of cardinal plural rules associated with 'bn'
  74. func (bn *bn) PluralsCardinal() []locales.PluralRule {
  75. return bn.pluralsCardinal
  76. }
  77. // PluralsOrdinal returns the list of ordinal plural rules associated with 'bn'
  78. func (bn *bn) PluralsOrdinal() []locales.PluralRule {
  79. return bn.pluralsOrdinal
  80. }
  81. // PluralsRange returns the list of range plural rules associated with 'bn'
  82. func (bn *bn) PluralsRange() []locales.PluralRule {
  83. return bn.pluralsRange
  84. }
  85. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'bn'
  86. func (bn *bn) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  87. n := math.Abs(num)
  88. i := int64(n)
  89. if (i == 0) || (n == 1) {
  90. return locales.PluralRuleOne
  91. }
  92. return locales.PluralRuleOther
  93. }
  94. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'bn'
  95. func (bn *bn) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  96. n := math.Abs(num)
  97. if n == 1 || n == 5 || n == 7 || n == 8 || n == 9 || n == 10 {
  98. return locales.PluralRuleOne
  99. } else if n == 2 || n == 3 {
  100. return locales.PluralRuleTwo
  101. } else if n == 4 {
  102. return locales.PluralRuleFew
  103. } else if n == 6 {
  104. return locales.PluralRuleMany
  105. }
  106. return locales.PluralRuleOther
  107. }
  108. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'bn'
  109. func (bn *bn) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  110. start := bn.CardinalPluralRule(num1, v1)
  111. end := bn.CardinalPluralRule(num2, v2)
  112. if start == locales.PluralRuleOne && end == locales.PluralRuleOne {
  113. return locales.PluralRuleOne
  114. } else if start == locales.PluralRuleOne && end == locales.PluralRuleOther {
  115. return locales.PluralRuleOther
  116. }
  117. return locales.PluralRuleOther
  118. }
  119. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  120. func (bn *bn) MonthAbbreviated(month time.Month) string {
  121. return bn.monthsAbbreviated[month]
  122. }
  123. // MonthsAbbreviated returns the locales abbreviated months
  124. func (bn *bn) MonthsAbbreviated() []string {
  125. return bn.monthsAbbreviated[1:]
  126. }
  127. // MonthNarrow returns the locales narrow month given the 'month' provided
  128. func (bn *bn) MonthNarrow(month time.Month) string {
  129. return bn.monthsNarrow[month]
  130. }
  131. // MonthsNarrow returns the locales narrow months
  132. func (bn *bn) MonthsNarrow() []string {
  133. return bn.monthsNarrow[1:]
  134. }
  135. // MonthWide returns the locales wide month given the 'month' provided
  136. func (bn *bn) MonthWide(month time.Month) string {
  137. return bn.monthsWide[month]
  138. }
  139. // MonthsWide returns the locales wide months
  140. func (bn *bn) MonthsWide() []string {
  141. return bn.monthsWide[1:]
  142. }
  143. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  144. func (bn *bn) WeekdayAbbreviated(weekday time.Weekday) string {
  145. return bn.daysAbbreviated[weekday]
  146. }
  147. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  148. func (bn *bn) WeekdaysAbbreviated() []string {
  149. return bn.daysAbbreviated
  150. }
  151. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  152. func (bn *bn) WeekdayNarrow(weekday time.Weekday) string {
  153. return bn.daysNarrow[weekday]
  154. }
  155. // WeekdaysNarrow returns the locales narrow weekdays
  156. func (bn *bn) WeekdaysNarrow() []string {
  157. return bn.daysNarrow
  158. }
  159. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  160. func (bn *bn) WeekdayShort(weekday time.Weekday) string {
  161. return bn.daysShort[weekday]
  162. }
  163. // WeekdaysShort returns the locales short weekdays
  164. func (bn *bn) WeekdaysShort() []string {
  165. return bn.daysShort
  166. }
  167. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  168. func (bn *bn) WeekdayWide(weekday time.Weekday) string {
  169. return bn.daysWide[weekday]
  170. }
  171. // WeekdaysWide returns the locales wide weekdays
  172. func (bn *bn) WeekdaysWide() []string {
  173. return bn.daysWide
  174. }
  175. // Decimal returns the decimal point of number
  176. func (bn *bn) Decimal() string {
  177. return bn.decimal
  178. }
  179. // Group returns the group of number
  180. func (bn *bn) Group() string {
  181. return bn.group
  182. }
  183. // Group returns the minus sign of number
  184. func (bn *bn) Minus() string {
  185. return bn.minus
  186. }
  187. // FmtNumber returns 'num' with digits/precision of 'v' for 'bn' and handles both Whole and Real numbers based on 'v'
  188. func (bn *bn) FmtNumber(num float64, v uint64) string {
  189. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  190. l := len(s) + 2 + 1*len(s[:len(s)-int(v)-1])/3
  191. count := 0
  192. inWhole := v == 0
  193. inSecondary := false
  194. groupThreshold := 3
  195. b := make([]byte, 0, l)
  196. for i := len(s) - 1; i >= 0; i-- {
  197. if s[i] == '.' {
  198. b = append(b, bn.decimal[0])
  199. inWhole = true
  200. continue
  201. }
  202. if inWhole {
  203. if count == groupThreshold {
  204. b = append(b, bn.group[0])
  205. count = 1
  206. if !inSecondary {
  207. inSecondary = true
  208. groupThreshold = 2
  209. }
  210. } else {
  211. count++
  212. }
  213. }
  214. b = append(b, s[i])
  215. }
  216. if num < 0 {
  217. b = append(b, bn.minus[0])
  218. }
  219. // reverse
  220. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  221. b[i], b[j] = b[j], b[i]
  222. }
  223. return string(b)
  224. }
  225. // FmtPercent returns 'num' with digits/precision of 'v' for 'bn' and handles both Whole and Real numbers based on 'v'
  226. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  227. func (bn *bn) FmtPercent(num float64, v uint64) string {
  228. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  229. l := len(s) + 3
  230. b := make([]byte, 0, l)
  231. for i := len(s) - 1; i >= 0; i-- {
  232. if s[i] == '.' {
  233. b = append(b, bn.decimal[0])
  234. continue
  235. }
  236. b = append(b, s[i])
  237. }
  238. if num < 0 {
  239. b = append(b, bn.minus[0])
  240. }
  241. // reverse
  242. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  243. b[i], b[j] = b[j], b[i]
  244. }
  245. b = append(b, bn.percent...)
  246. return string(b)
  247. }
  248. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'bn'
  249. func (bn *bn) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  250. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  251. symbol := bn.currencies[currency]
  252. l := len(s) + len(symbol) + 2 + 1*len(s[:len(s)-int(v)-1])/3
  253. count := 0
  254. inWhole := v == 0
  255. inSecondary := false
  256. groupThreshold := 3
  257. b := make([]byte, 0, l)
  258. for i := len(s) - 1; i >= 0; i-- {
  259. if s[i] == '.' {
  260. b = append(b, bn.decimal[0])
  261. inWhole = true
  262. continue
  263. }
  264. if inWhole {
  265. if count == groupThreshold {
  266. b = append(b, bn.group[0])
  267. count = 1
  268. if !inSecondary {
  269. inSecondary = true
  270. groupThreshold = 2
  271. }
  272. } else {
  273. count++
  274. }
  275. }
  276. b = append(b, s[i])
  277. }
  278. if num < 0 {
  279. b = append(b, bn.minus[0])
  280. }
  281. // reverse
  282. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  283. b[i], b[j] = b[j], b[i]
  284. }
  285. if int(v) < 2 {
  286. if v == 0 {
  287. b = append(b, bn.decimal...)
  288. }
  289. for i := 0; i < 2-int(v); i++ {
  290. b = append(b, '0')
  291. }
  292. }
  293. b = append(b, symbol...)
  294. return string(b)
  295. }
  296. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'bn'
  297. // in accounting notation.
  298. func (bn *bn) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  299. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  300. symbol := bn.currencies[currency]
  301. l := len(s) + len(symbol) + 2 + 1*len(s[:len(s)-int(v)-1])/3
  302. count := 0
  303. inWhole := v == 0
  304. inSecondary := false
  305. groupThreshold := 3
  306. b := make([]byte, 0, l)
  307. for i := len(s) - 1; i >= 0; i-- {
  308. if s[i] == '.' {
  309. b = append(b, bn.decimal[0])
  310. inWhole = true
  311. continue
  312. }
  313. if inWhole {
  314. if count == groupThreshold {
  315. b = append(b, bn.group[0])
  316. count = 1
  317. if !inSecondary {
  318. inSecondary = true
  319. groupThreshold = 2
  320. }
  321. } else {
  322. count++
  323. }
  324. }
  325. b = append(b, s[i])
  326. }
  327. if num < 0 {
  328. b = append(b, bn.minus[0])
  329. }
  330. // reverse
  331. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  332. b[i], b[j] = b[j], b[i]
  333. }
  334. if int(v) < 2 {
  335. if v == 0 {
  336. b = append(b, bn.decimal...)
  337. }
  338. for i := 0; i < 2-int(v); i++ {
  339. b = append(b, '0')
  340. }
  341. }
  342. if num < 0 {
  343. b = append(b, symbol...)
  344. } else {
  345. b = append(b, symbol...)
  346. }
  347. return string(b)
  348. }
  349. // FmtDateShort returns the short date representation of 't' for 'bn'
  350. func (bn *bn) FmtDateShort(t time.Time) string {
  351. b := make([]byte, 0, 32)
  352. b = strconv.AppendInt(b, int64(t.Day()), 10)
  353. b = append(b, []byte{0x2f}...)
  354. b = strconv.AppendInt(b, int64(t.Month()), 10)
  355. b = append(b, []byte{0x2f}...)
  356. if t.Year() > 9 {
  357. b = append(b, strconv.Itoa(t.Year())[2:]...)
  358. } else {
  359. b = append(b, strconv.Itoa(t.Year())[1:]...)
  360. }
  361. return string(b)
  362. }
  363. // FmtDateMedium returns the medium date representation of 't' for 'bn'
  364. func (bn *bn) FmtDateMedium(t time.Time) string {
  365. b := make([]byte, 0, 32)
  366. b = strconv.AppendInt(b, int64(t.Day()), 10)
  367. b = append(b, []byte{0x20}...)
  368. b = append(b, bn.monthsAbbreviated[t.Month()]...)
  369. b = append(b, []byte{0x2c, 0x20}...)
  370. if t.Year() > 0 {
  371. b = strconv.AppendInt(b, int64(t.Year()), 10)
  372. } else {
  373. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  374. }
  375. return string(b)
  376. }
  377. // FmtDateLong returns the long date representation of 't' for 'bn'
  378. func (bn *bn) FmtDateLong(t time.Time) string {
  379. b := make([]byte, 0, 32)
  380. b = strconv.AppendInt(b, int64(t.Day()), 10)
  381. b = append(b, []byte{0x20}...)
  382. b = append(b, bn.monthsWide[t.Month()]...)
  383. b = append(b, []byte{0x2c, 0x20}...)
  384. if t.Year() > 0 {
  385. b = strconv.AppendInt(b, int64(t.Year()), 10)
  386. } else {
  387. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  388. }
  389. return string(b)
  390. }
  391. // FmtDateFull returns the full date representation of 't' for 'bn'
  392. func (bn *bn) FmtDateFull(t time.Time) string {
  393. b := make([]byte, 0, 32)
  394. b = append(b, bn.daysWide[t.Weekday()]...)
  395. b = append(b, []byte{0x2c, 0x20}...)
  396. b = strconv.AppendInt(b, int64(t.Day()), 10)
  397. b = append(b, []byte{0x20}...)
  398. b = append(b, bn.monthsWide[t.Month()]...)
  399. b = append(b, []byte{0x2c, 0x20}...)
  400. if t.Year() > 0 {
  401. b = strconv.AppendInt(b, int64(t.Year()), 10)
  402. } else {
  403. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  404. }
  405. return string(b)
  406. }
  407. // FmtTimeShort returns the short time representation of 't' for 'bn'
  408. func (bn *bn) FmtTimeShort(t time.Time) string {
  409. b := make([]byte, 0, 32)
  410. h := t.Hour()
  411. if h > 12 {
  412. h -= 12
  413. }
  414. b = strconv.AppendInt(b, int64(h), 10)
  415. b = append(b, bn.timeSeparator...)
  416. if t.Minute() < 10 {
  417. b = append(b, '0')
  418. }
  419. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  420. b = append(b, []byte{0x20}...)
  421. if t.Hour() < 12 {
  422. b = append(b, bn.periodsAbbreviated[0]...)
  423. } else {
  424. b = append(b, bn.periodsAbbreviated[1]...)
  425. }
  426. return string(b)
  427. }
  428. // FmtTimeMedium returns the medium time representation of 't' for 'bn'
  429. func (bn *bn) FmtTimeMedium(t time.Time) string {
  430. b := make([]byte, 0, 32)
  431. h := t.Hour()
  432. if h > 12 {
  433. h -= 12
  434. }
  435. b = strconv.AppendInt(b, int64(h), 10)
  436. b = append(b, bn.timeSeparator...)
  437. if t.Minute() < 10 {
  438. b = append(b, '0')
  439. }
  440. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  441. b = append(b, bn.timeSeparator...)
  442. if t.Second() < 10 {
  443. b = append(b, '0')
  444. }
  445. b = strconv.AppendInt(b, int64(t.Second()), 10)
  446. b = append(b, []byte{0x20}...)
  447. if t.Hour() < 12 {
  448. b = append(b, bn.periodsAbbreviated[0]...)
  449. } else {
  450. b = append(b, bn.periodsAbbreviated[1]...)
  451. }
  452. return string(b)
  453. }
  454. // FmtTimeLong returns the long time representation of 't' for 'bn'
  455. func (bn *bn) FmtTimeLong(t time.Time) string {
  456. b := make([]byte, 0, 32)
  457. h := t.Hour()
  458. if h > 12 {
  459. h -= 12
  460. }
  461. b = strconv.AppendInt(b, int64(h), 10)
  462. b = append(b, bn.timeSeparator...)
  463. if t.Minute() < 10 {
  464. b = append(b, '0')
  465. }
  466. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  467. b = append(b, bn.timeSeparator...)
  468. if t.Second() < 10 {
  469. b = append(b, '0')
  470. }
  471. b = strconv.AppendInt(b, int64(t.Second()), 10)
  472. b = append(b, []byte{0x20}...)
  473. if t.Hour() < 12 {
  474. b = append(b, bn.periodsAbbreviated[0]...)
  475. } else {
  476. b = append(b, bn.periodsAbbreviated[1]...)
  477. }
  478. b = append(b, []byte{0x20}...)
  479. tz, _ := t.Zone()
  480. b = append(b, tz...)
  481. return string(b)
  482. }
  483. // FmtTimeFull returns the full time representation of 't' for 'bn'
  484. func (bn *bn) FmtTimeFull(t time.Time) string {
  485. b := make([]byte, 0, 32)
  486. h := t.Hour()
  487. if h > 12 {
  488. h -= 12
  489. }
  490. b = strconv.AppendInt(b, int64(h), 10)
  491. b = append(b, bn.timeSeparator...)
  492. if t.Minute() < 10 {
  493. b = append(b, '0')
  494. }
  495. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  496. b = append(b, bn.timeSeparator...)
  497. if t.Second() < 10 {
  498. b = append(b, '0')
  499. }
  500. b = strconv.AppendInt(b, int64(t.Second()), 10)
  501. b = append(b, []byte{0x20}...)
  502. if t.Hour() < 12 {
  503. b = append(b, bn.periodsAbbreviated[0]...)
  504. } else {
  505. b = append(b, bn.periodsAbbreviated[1]...)
  506. }
  507. b = append(b, []byte{0x20}...)
  508. tz, _ := t.Zone()
  509. if btz, ok := bn.timezones[tz]; ok {
  510. b = append(b, btz...)
  511. } else {
  512. b = append(b, tz...)
  513. }
  514. return string(b)
  515. }