be.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. package be
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type be 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. percentSuffix string
  19. perMille string
  20. timeSeparator string
  21. inifinity string
  22. currencies []string // idx = enum of currency code
  23. currencyPositiveSuffix string
  24. currencyNegativeSuffix string
  25. monthsAbbreviated []string
  26. monthsNarrow []string
  27. monthsWide []string
  28. daysAbbreviated []string
  29. daysNarrow []string
  30. daysShort []string
  31. daysWide []string
  32. periodsAbbreviated []string
  33. periodsNarrow []string
  34. periodsShort []string
  35. periodsWide []string
  36. erasAbbreviated []string
  37. erasNarrow []string
  38. erasWide []string
  39. timezones map[string]string
  40. }
  41. // New returns a new instance of translator for the 'be' locale
  42. func New() locales.Translator {
  43. return &be{
  44. locale: "be",
  45. pluralsCardinal: []locales.PluralRule{2, 4, 5, 6},
  46. pluralsOrdinal: []locales.PluralRule{4, 6},
  47. pluralsRange: []locales.PluralRule{2, 4, 5, 6},
  48. decimal: ",",
  49. group: " ",
  50. minus: "-",
  51. percent: "%",
  52. perMille: "‰",
  53. timeSeparator: ":",
  54. inifinity: "∞",
  55. 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", "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", "Br", "BYR", "BZD", "CAD", "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", "¥", "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", "NZD", "OMR", "PAB", "PEI", "PEN", "PES", "PGK", "PHP", "PKR", "PLN", "PLZ", "PTE", "PYG", "QAR", "RHD", "ROL", "RON", "RSD", "₽", "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", "NT$", "TZS", "UAH", "UAK", "UGS", "UGX", "$", "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"},
  56. percentSuffix: " ",
  57. currencyPositiveSuffix: " ",
  58. currencyNegativeSuffix: " ",
  59. monthsAbbreviated: []string{"", "сту", "лют", "сак", "кра", "мая", "чэр", "ліп", "жні", "вер", "кас", "ліс", "сне"},
  60. monthsNarrow: []string{"", "с", "л", "с", "к", "м", "ч", "л", "ж", "в", "к", "л", "с"},
  61. monthsWide: []string{"", "студзеня", "лютага", "сакавіка", "красавіка", "мая", "чэрвеня", "ліпеня", "жніўня", "верасня", "кастрычніка", "лістапада", "снежня"},
  62. daysAbbreviated: []string{"нд", "пн", "аў", "ср", "чц", "пт", "сб"},
  63. daysNarrow: []string{"н", "п", "а", "с", "ч", "п", "с"},
  64. daysShort: []string{"нд", "пн", "аў", "ср", "чц", "пт", "сб"},
  65. daysWide: []string{"нядзеля", "панядзелак", "аўторак", "серада", "чацвер", "пятніца", "субота"},
  66. periodsAbbreviated: []string{"AM", "PM"},
  67. periodsNarrow: []string{"am", "pm"},
  68. periodsWide: []string{"AM", "PM"},
  69. erasAbbreviated: []string{"да н.э.", "н.э."},
  70. erasNarrow: []string{"", ""},
  71. erasWide: []string{"да нараджэння Хрыстова", "ад нараджэння Хрыстова"},
  72. timezones: map[string]string{"AEST": "Стандартны час усходняй Аўстраліі", "NZST": "Стандартны час Новай Зеландыі", "AKST": "Стандартны час Аляскі", "WARST": "Летні час Заходняй Аргенціны", "HENOMX": "Паўночна-заходні мексіканскі летні час", "PST": "Ціхаакіянскі стандартны час", "ACDT": "Летні час цэнтральнай Аўстраліі", "ACWDT": "Заходні летні час Цэнтральнай Аўстраліі", "MEZ": "Цэнтральнаеўрапейскі стандартны час", "HNNOMX": "Паўночна-заходні мексіканскі стандартны час", "TMST": "Летні час Туркменістана", "JST": "Стандартны час Японіі", "HKST": "Летні час Ганконга", "IST": "Час Індыі", "CHADT": "Летні час Чатэма", "NZDT": "Летні час Новай Зеландыі", "EDT": "Паўночнаамерыканскі ўсходні летні час", "ChST": "Час Чамора", "CHAST": "Стандартны час Чатэма", "CDT": "Паўночнаамерыканскі цэнтральны летні час", "GFT": "Час Французскай Гвіяны", "ACWST": "Заходні стандартны час Цэнтральнай Аўстраліі", "MESZ": "Цэнтральнаеўрапейскі летні час", "MST": "MST", "OEZ": "Усходнееўрапейскі стандартны час", "HNCU": "Стандартны час Кубы", "WESZ": "Заходнееўрапейскі летні час", "HNEG": "Стандартны час Усходняй Грэнландыі", "HAT": "Ньюфаўндлендскі летні час", "AWST": "Стандартны час заходняй Аўстраліі", "ADT": "Атлантычны летні час", "JDT": "Летні час Японіі", "ACST": "Стандартны час цэнтральнай Аўстраліі", "ART": "Аргенцінскі стандартны час", "PDT": "Ціхаакіянскі летні час", "SAST": "Паўднёваафрыканскі час", "ECT": "Эквадорскі час", "MDT": "MDT", "HAST": "Гавайска-Алеуцкі стандартны час", "HECU": "Летні час Кубы", "HNPM": "Стандартны час Сен-П’ер і Мікелон", "CAT": "Цэнтральнаафрыканскі час", "BT": "Час Бутана", "WAT": "Заходнеафрыканскі стандартны час", "MYT": "Час Малайзіі", "WIT": "Усходнеінданезійскі час", "HADT": "Гавайска-Алеуцкі летні час", "HNPMX": "Мексіканскі ціхаакіянскі стандатны час", "COST": "Калумбійскі летні час", "GMT": "Час па Грынвічы", "AWDT": "Летні час заходняй Аўстраліі", "HEPMX": "Мексіканскі ціхаакіянскі летні час", "WIB": "Заходнеінданезійскі час", "LHDT": "Летні час Лорд-Хау", "WART": "Стандартны час Заходняй Аргенціны", "CLT": "Чылійскі стандартны час", "ARST": "Аргенцінскі летні час", "GYT": "Час Гаяны", "WEZ": "Заходнееўрапейскі стандартны час", "BOT": "Балівійскі час", "SGT": "Сінгапурскі час", "HEEG": "Летні час Усходняй Грэнландыі", "HEOG": "Летні час Заходняй Грэнландыі", "HNT": "Ньюфаўндлендскі стандартны час", "UYT": "Уругвайскі стандартны час", "WAST": "Заходнеафрыканскі летні час", "HKT": "Стандартны час Ганконга", "TMT": "Стандартны час Туркменістана", "AEDT": "Летні час усходняй Аўстраліі", "AKDT": "Летні час Аляскі", "EST": "Паўночнаамерыканскі ўсходні стандартны час", "WITA": "Цэнтральнаінданезійскі час", "EAT": "Усходнеафрыканскі час", "CLST": "Чылійскі летні час", "LHST": "Стандартны час Лорд-Хау", "∅∅∅": "Перуанскі летні час", "HNOG": "Стандартны час Заходняй Грэнландыі", "OESZ": "Усходнееўрапейскі летні час", "COT": "Калумбійскі стандартны час", "UYST": "Уругвайскі летні час", "CST": "Паўночнаамерыканскі цэнтральны стандартны час", "AST": "Атлантычны стандартны час", "VET": "Венесуэльскі час", "HEPM": "Стандартны летні час Сен-П’ер і Мікелон", "SRT": "Час Сурынама"},
  73. }
  74. }
  75. // Locale returns the current translators string locale
  76. func (be *be) Locale() string {
  77. return be.locale
  78. }
  79. // PluralsCardinal returns the list of cardinal plural rules associated with 'be'
  80. func (be *be) PluralsCardinal() []locales.PluralRule {
  81. return be.pluralsCardinal
  82. }
  83. // PluralsOrdinal returns the list of ordinal plural rules associated with 'be'
  84. func (be *be) PluralsOrdinal() []locales.PluralRule {
  85. return be.pluralsOrdinal
  86. }
  87. // PluralsRange returns the list of range plural rules associated with 'be'
  88. func (be *be) PluralsRange() []locales.PluralRule {
  89. return be.pluralsRange
  90. }
  91. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'be'
  92. func (be *be) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  93. n := math.Abs(num)
  94. nMod10 := math.Mod(n, 10)
  95. nMod100 := math.Mod(n, 100)
  96. if nMod10 == 1 && nMod100 != 11 {
  97. return locales.PluralRuleOne
  98. } else if nMod10 >= 2 && nMod10 <= 4 && (nMod100 < 12 || nMod100 > 14) {
  99. return locales.PluralRuleFew
  100. } else if (nMod10 == 0) || (nMod10 >= 5 && nMod10 <= 9) || (nMod100 >= 11 && nMod100 <= 14) {
  101. return locales.PluralRuleMany
  102. }
  103. return locales.PluralRuleOther
  104. }
  105. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'be'
  106. func (be *be) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  107. n := math.Abs(num)
  108. nMod10 := math.Mod(n, 10)
  109. nMod100 := math.Mod(n, 100)
  110. if (nMod10 == 2 || nMod10 == 3) && (nMod100 != 12 && nMod100 != 13) {
  111. return locales.PluralRuleFew
  112. }
  113. return locales.PluralRuleOther
  114. }
  115. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'be'
  116. func (be *be) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  117. start := be.CardinalPluralRule(num1, v1)
  118. end := be.CardinalPluralRule(num2, v2)
  119. if start == locales.PluralRuleOne && end == locales.PluralRuleOne {
  120. return locales.PluralRuleOne
  121. } else if start == locales.PluralRuleOne && end == locales.PluralRuleFew {
  122. return locales.PluralRuleFew
  123. } else if start == locales.PluralRuleOne && end == locales.PluralRuleMany {
  124. return locales.PluralRuleMany
  125. } else if start == locales.PluralRuleOne && end == locales.PluralRuleOther {
  126. return locales.PluralRuleOther
  127. } else if start == locales.PluralRuleFew && end == locales.PluralRuleOne {
  128. return locales.PluralRuleOne
  129. } else if start == locales.PluralRuleFew && end == locales.PluralRuleFew {
  130. return locales.PluralRuleFew
  131. } else if start == locales.PluralRuleFew && end == locales.PluralRuleMany {
  132. return locales.PluralRuleMany
  133. } else if start == locales.PluralRuleFew && end == locales.PluralRuleOther {
  134. return locales.PluralRuleOther
  135. } else if start == locales.PluralRuleMany && end == locales.PluralRuleOne {
  136. return locales.PluralRuleOne
  137. } else if start == locales.PluralRuleMany && end == locales.PluralRuleFew {
  138. return locales.PluralRuleFew
  139. } else if start == locales.PluralRuleMany && end == locales.PluralRuleMany {
  140. return locales.PluralRuleMany
  141. } else if start == locales.PluralRuleMany && end == locales.PluralRuleOther {
  142. return locales.PluralRuleOther
  143. } else if start == locales.PluralRuleOther && end == locales.PluralRuleOne {
  144. return locales.PluralRuleOne
  145. } else if start == locales.PluralRuleOther && end == locales.PluralRuleFew {
  146. return locales.PluralRuleFew
  147. } else if start == locales.PluralRuleOther && end == locales.PluralRuleMany {
  148. return locales.PluralRuleMany
  149. }
  150. return locales.PluralRuleOther
  151. }
  152. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  153. func (be *be) MonthAbbreviated(month time.Month) string {
  154. return be.monthsAbbreviated[month]
  155. }
  156. // MonthsAbbreviated returns the locales abbreviated months
  157. func (be *be) MonthsAbbreviated() []string {
  158. return be.monthsAbbreviated[1:]
  159. }
  160. // MonthNarrow returns the locales narrow month given the 'month' provided
  161. func (be *be) MonthNarrow(month time.Month) string {
  162. return be.monthsNarrow[month]
  163. }
  164. // MonthsNarrow returns the locales narrow months
  165. func (be *be) MonthsNarrow() []string {
  166. return be.monthsNarrow[1:]
  167. }
  168. // MonthWide returns the locales wide month given the 'month' provided
  169. func (be *be) MonthWide(month time.Month) string {
  170. return be.monthsWide[month]
  171. }
  172. // MonthsWide returns the locales wide months
  173. func (be *be) MonthsWide() []string {
  174. return be.monthsWide[1:]
  175. }
  176. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  177. func (be *be) WeekdayAbbreviated(weekday time.Weekday) string {
  178. return be.daysAbbreviated[weekday]
  179. }
  180. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  181. func (be *be) WeekdaysAbbreviated() []string {
  182. return be.daysAbbreviated
  183. }
  184. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  185. func (be *be) WeekdayNarrow(weekday time.Weekday) string {
  186. return be.daysNarrow[weekday]
  187. }
  188. // WeekdaysNarrow returns the locales narrow weekdays
  189. func (be *be) WeekdaysNarrow() []string {
  190. return be.daysNarrow
  191. }
  192. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  193. func (be *be) WeekdayShort(weekday time.Weekday) string {
  194. return be.daysShort[weekday]
  195. }
  196. // WeekdaysShort returns the locales short weekdays
  197. func (be *be) WeekdaysShort() []string {
  198. return be.daysShort
  199. }
  200. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  201. func (be *be) WeekdayWide(weekday time.Weekday) string {
  202. return be.daysWide[weekday]
  203. }
  204. // WeekdaysWide returns the locales wide weekdays
  205. func (be *be) WeekdaysWide() []string {
  206. return be.daysWide
  207. }
  208. // Decimal returns the decimal point of number
  209. func (be *be) Decimal() string {
  210. return be.decimal
  211. }
  212. // Group returns the group of number
  213. func (be *be) Group() string {
  214. return be.group
  215. }
  216. // Group returns the minus sign of number
  217. func (be *be) Minus() string {
  218. return be.minus
  219. }
  220. // FmtNumber returns 'num' with digits/precision of 'v' for 'be' and handles both Whole and Real numbers based on 'v'
  221. func (be *be) FmtNumber(num float64, v uint64) string {
  222. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  223. l := len(s) + 2 + 2*len(s[:len(s)-int(v)-1])/3
  224. count := 0
  225. inWhole := v == 0
  226. b := make([]byte, 0, l)
  227. for i := len(s) - 1; i >= 0; i-- {
  228. if s[i] == '.' {
  229. b = append(b, be.decimal[0])
  230. inWhole = true
  231. continue
  232. }
  233. if inWhole {
  234. if count == 3 {
  235. for j := len(be.group) - 1; j >= 0; j-- {
  236. b = append(b, be.group[j])
  237. }
  238. count = 1
  239. } else {
  240. count++
  241. }
  242. }
  243. b = append(b, s[i])
  244. }
  245. if num < 0 {
  246. b = append(b, be.minus[0])
  247. }
  248. // reverse
  249. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  250. b[i], b[j] = b[j], b[i]
  251. }
  252. return string(b)
  253. }
  254. // FmtPercent returns 'num' with digits/precision of 'v' for 'be' and handles both Whole and Real numbers based on 'v'
  255. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  256. func (be *be) FmtPercent(num float64, v uint64) string {
  257. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  258. l := len(s) + 5
  259. b := make([]byte, 0, l)
  260. for i := len(s) - 1; i >= 0; i-- {
  261. if s[i] == '.' {
  262. b = append(b, be.decimal[0])
  263. continue
  264. }
  265. b = append(b, s[i])
  266. }
  267. if num < 0 {
  268. b = append(b, be.minus[0])
  269. }
  270. // reverse
  271. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  272. b[i], b[j] = b[j], b[i]
  273. }
  274. b = append(b, be.percentSuffix...)
  275. b = append(b, be.percent...)
  276. return string(b)
  277. }
  278. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'be'
  279. func (be *be) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  280. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  281. symbol := be.currencies[currency]
  282. l := len(s) + len(symbol) + 4 + 2*len(s[:len(s)-int(v)-1])/3
  283. count := 0
  284. inWhole := v == 0
  285. b := make([]byte, 0, l)
  286. for i := len(s) - 1; i >= 0; i-- {
  287. if s[i] == '.' {
  288. b = append(b, be.decimal[0])
  289. inWhole = true
  290. continue
  291. }
  292. if inWhole {
  293. if count == 3 {
  294. for j := len(be.group) - 1; j >= 0; j-- {
  295. b = append(b, be.group[j])
  296. }
  297. count = 1
  298. } else {
  299. count++
  300. }
  301. }
  302. b = append(b, s[i])
  303. }
  304. if num < 0 {
  305. b = append(b, be.minus[0])
  306. }
  307. // reverse
  308. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  309. b[i], b[j] = b[j], b[i]
  310. }
  311. if int(v) < 2 {
  312. if v == 0 {
  313. b = append(b, be.decimal...)
  314. }
  315. for i := 0; i < 2-int(v); i++ {
  316. b = append(b, '0')
  317. }
  318. }
  319. b = append(b, be.currencyPositiveSuffix...)
  320. b = append(b, symbol...)
  321. return string(b)
  322. }
  323. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'be'
  324. // in accounting notation.
  325. func (be *be) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  326. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  327. symbol := be.currencies[currency]
  328. l := len(s) + len(symbol) + 4 + 2*len(s[:len(s)-int(v)-1])/3
  329. count := 0
  330. inWhole := v == 0
  331. b := make([]byte, 0, l)
  332. for i := len(s) - 1; i >= 0; i-- {
  333. if s[i] == '.' {
  334. b = append(b, be.decimal[0])
  335. inWhole = true
  336. continue
  337. }
  338. if inWhole {
  339. if count == 3 {
  340. for j := len(be.group) - 1; j >= 0; j-- {
  341. b = append(b, be.group[j])
  342. }
  343. count = 1
  344. } else {
  345. count++
  346. }
  347. }
  348. b = append(b, s[i])
  349. }
  350. if num < 0 {
  351. b = append(b, be.minus[0])
  352. }
  353. // reverse
  354. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  355. b[i], b[j] = b[j], b[i]
  356. }
  357. if int(v) < 2 {
  358. if v == 0 {
  359. b = append(b, be.decimal...)
  360. }
  361. for i := 0; i < 2-int(v); i++ {
  362. b = append(b, '0')
  363. }
  364. }
  365. if num < 0 {
  366. b = append(b, be.currencyNegativeSuffix...)
  367. b = append(b, symbol...)
  368. } else {
  369. b = append(b, be.currencyPositiveSuffix...)
  370. b = append(b, symbol...)
  371. }
  372. return string(b)
  373. }
  374. // FmtDateShort returns the short date representation of 't' for 'be'
  375. func (be *be) FmtDateShort(t time.Time) string {
  376. b := make([]byte, 0, 32)
  377. b = strconv.AppendInt(b, int64(t.Day()), 10)
  378. b = append(b, []byte{0x2e}...)
  379. if t.Month() < 10 {
  380. b = append(b, '0')
  381. }
  382. b = strconv.AppendInt(b, int64(t.Month()), 10)
  383. b = append(b, []byte{0x2e}...)
  384. if t.Year() > 9 {
  385. b = append(b, strconv.Itoa(t.Year())[2:]...)
  386. } else {
  387. b = append(b, strconv.Itoa(t.Year())[1:]...)
  388. }
  389. return string(b)
  390. }
  391. // FmtDateMedium returns the medium date representation of 't' for 'be'
  392. func (be *be) FmtDateMedium(t time.Time) string {
  393. b := make([]byte, 0, 32)
  394. b = strconv.AppendInt(b, int64(t.Day()), 10)
  395. b = append(b, []byte{0x2e}...)
  396. if t.Month() < 10 {
  397. b = append(b, '0')
  398. }
  399. b = strconv.AppendInt(b, int64(t.Month()), 10)
  400. b = append(b, []byte{0x2e}...)
  401. if t.Year() > 0 {
  402. b = strconv.AppendInt(b, int64(t.Year()), 10)
  403. } else {
  404. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  405. }
  406. return string(b)
  407. }
  408. // FmtDateLong returns the long date representation of 't' for 'be'
  409. func (be *be) FmtDateLong(t time.Time) string {
  410. b := make([]byte, 0, 32)
  411. b = strconv.AppendInt(b, int64(t.Day()), 10)
  412. b = append(b, []byte{0x20}...)
  413. b = append(b, be.monthsWide[t.Month()]...)
  414. b = append(b, []byte{0x20}...)
  415. if t.Year() > 0 {
  416. b = strconv.AppendInt(b, int64(t.Year()), 10)
  417. } else {
  418. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  419. }
  420. b = append(b, []byte{0x20, 0xd0, 0xb3}...)
  421. b = append(b, []byte{0x2e}...)
  422. return string(b)
  423. }
  424. // FmtDateFull returns the full date representation of 't' for 'be'
  425. func (be *be) FmtDateFull(t time.Time) string {
  426. b := make([]byte, 0, 32)
  427. b = append(b, be.daysWide[t.Weekday()]...)
  428. b = append(b, []byte{0x2c, 0x20}...)
  429. b = strconv.AppendInt(b, int64(t.Day()), 10)
  430. b = append(b, []byte{0x20}...)
  431. b = append(b, be.monthsWide[t.Month()]...)
  432. b = append(b, []byte{0x20}...)
  433. if t.Year() > 0 {
  434. b = strconv.AppendInt(b, int64(t.Year()), 10)
  435. } else {
  436. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  437. }
  438. b = append(b, []byte{0x20, 0xd0, 0xb3}...)
  439. b = append(b, []byte{0x2e}...)
  440. return string(b)
  441. }
  442. // FmtTimeShort returns the short time representation of 't' for 'be'
  443. func (be *be) FmtTimeShort(t time.Time) string {
  444. b := make([]byte, 0, 32)
  445. if t.Hour() < 10 {
  446. b = append(b, '0')
  447. }
  448. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  449. b = append(b, be.timeSeparator...)
  450. if t.Minute() < 10 {
  451. b = append(b, '0')
  452. }
  453. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  454. return string(b)
  455. }
  456. // FmtTimeMedium returns the medium time representation of 't' for 'be'
  457. func (be *be) FmtTimeMedium(t time.Time) string {
  458. b := make([]byte, 0, 32)
  459. if t.Hour() < 10 {
  460. b = append(b, '0')
  461. }
  462. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  463. b = append(b, be.timeSeparator...)
  464. if t.Minute() < 10 {
  465. b = append(b, '0')
  466. }
  467. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  468. b = append(b, be.timeSeparator...)
  469. if t.Second() < 10 {
  470. b = append(b, '0')
  471. }
  472. b = strconv.AppendInt(b, int64(t.Second()), 10)
  473. return string(b)
  474. }
  475. // FmtTimeLong returns the long time representation of 't' for 'be'
  476. func (be *be) FmtTimeLong(t time.Time) string {
  477. b := make([]byte, 0, 32)
  478. if t.Hour() < 10 {
  479. b = append(b, '0')
  480. }
  481. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  482. b = append(b, be.timeSeparator...)
  483. if t.Minute() < 10 {
  484. b = append(b, '0')
  485. }
  486. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  487. b = append(b, be.timeSeparator...)
  488. if t.Second() < 10 {
  489. b = append(b, '0')
  490. }
  491. b = strconv.AppendInt(b, int64(t.Second()), 10)
  492. b = append(b, []byte{0x20}...)
  493. tz, _ := t.Zone()
  494. b = append(b, tz...)
  495. return string(b)
  496. }
  497. // FmtTimeFull returns the full time representation of 't' for 'be'
  498. func (be *be) FmtTimeFull(t time.Time) string {
  499. b := make([]byte, 0, 32)
  500. if t.Hour() < 10 {
  501. b = append(b, '0')
  502. }
  503. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  504. b = append(b, be.timeSeparator...)
  505. if t.Minute() < 10 {
  506. b = append(b, '0')
  507. }
  508. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  509. b = append(b, be.timeSeparator...)
  510. if t.Second() < 10 {
  511. b = append(b, '0')
  512. }
  513. b = strconv.AppendInt(b, int64(t.Second()), 10)
  514. b = append(b, []byte{0x2c, 0x20}...)
  515. tz, _ := t.Zone()
  516. if btz, ok := be.timezones[tz]; ok {
  517. b = append(b, btz...)
  518. } else {
  519. b = append(b, tz...)
  520. }
  521. return string(b)
  522. }