bs.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. package bs
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type bs 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. currencyPositiveSuffix string
  23. currencyNegativeSuffix 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 'bs' locale
  41. func New() locales.Translator {
  42. return &bs{
  43. locale: "bs",
  44. pluralsCardinal: []locales.PluralRule{2, 4, 6},
  45. pluralsOrdinal: []locales.PluralRule{6},
  46. pluralsRange: []locales.PluralRule{2, 4, 6},
  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 ", "KM", "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 ", "BYR", "BZD", "CAD", "CDF", "CHE ", "CHF", "CHW ", "CLE ", "CLF ", "CLP", "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", "€", "FIM ", "FJD", "FKP", "FRF ", "GBP", "GEK ", "GEL", "GHC ", "GHS", "GIP", "GMD", "GNF", "GNS ", "GQE ", "GRD ", "GTQ", "GWE ", "GWP ", "GYD", "HKD", "HNL", "HRD ", "kn", "HTG", "HUF", "IDR", "IEP ", "ILP ", "ILR ", "ILS", "₹", "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", "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", "din.", "RUB", "RUR ", "RWF", "SAR", "SBD", "SCR", "SDD ", "SDG", "SDP ", "SEK", "SGD", "SHP", "SIT ", "SKK ", "SLL", "SOS", "SRD", "SRG ", "SSP", "STD", "SUR ", "SVC ", "SYP", "SZL", "฿", "TJR ", "TJS", "TMM ", "TMT", "TND", "TOP", "TPE ", "TRL ", "TRY", "TTD", "NT$", "TZS", "UAH", "UAK ", "UGS ", "UGX", "USD", "USN ", "USS ", "UYI ", "UYP ", "UYU", "UZS", "VEB ", "VEF", "₫", "VNN ", "VUV", "WST", "FCFA", "XAG ", "XAU ", "XBA ", "XBB ", "XBC ", "XBD ", "XCD", "XDR ", "XEU ", "XFO ", "XFU ", "CFA", "XPD ", "XPF", "XPT ", "XRE ", "XSU ", "XTS ", "XUA ", "XXX ", "YDD ", "YER", "YUD ", "YUM ", "YUN ", "YUR ", "ZAL ", "ZAR", "ZMK ", "ZMW", "ZRN ", "ZRZ ", "ZWD ", "ZWL ", "ZWR "},
  55. currencyPositiveSuffix: " ",
  56. currencyNegativeSuffix: " ",
  57. monthsAbbreviated: []string{"", "jan", "feb", "mar", "apr", "maj", "jun", "jul", "aug", "sep", "okt", "nov", "dec"},
  58. monthsNarrow: []string{"", "j", "f", "m", "a", "m", "j", "j", "a", "s", "o", "n", "d"},
  59. monthsWide: []string{"", "januar", "februar", "mart", "april", "maj", "juni", "juli", "august", "septembar", "oktobar", "novembar", "decembar"},
  60. daysAbbreviated: []string{"ned", "pon", "uto", "sri", "čet", "pet", "sub"},
  61. daysNarrow: []string{"N", "P", "U", "S", "Č", "P", "S"},
  62. daysShort: []string{"ned", "pon", "uto", "sri", "čet", "pet", "sub"},
  63. daysWide: []string{"nedjelja", "ponedjeljak", "utorak", "srijeda", "četvrtak", "petak", "subota"},
  64. periodsAbbreviated: []string{"prijepodne", "popodne"},
  65. periodsNarrow: []string{"prijepodne", "popodne"},
  66. periodsWide: []string{"prije podne", "popodne"},
  67. erasAbbreviated: []string{"p. n. e.", "n. e."},
  68. erasNarrow: []string{"pr.n.e.", "AD"},
  69. erasWide: []string{"Prije nove ere", "Nove ere"},
  70. timezones: map[string]string{"WARST": "Zapadnoargentinsko ljetno računanje vremena", "GMT": "Vrijeme po Grinviču", "WIB": "Zapadnoindonezijsko vrijeme", "AWDT": "Zapadnoaustralijsko ljetno računanje vremena", "MEZ": "Standardno centralnoevropsko vrijeme", "UYT": "Standardno urugvajsko vrijeme", "CLST": "Čileansko ljetno računanje vremena", "COT": "Standardno kolumbijsko vrijeme", "VET": "Venecuelansko vrijeme", "EAT": "Istočnoafričko vrijeme", "AEST": "Standardno istočnoaustralijsko vrijeme", "IST": "Standardno indijsko vrijeme", "WAST": "Zapadnoafričko ljetno računanje vremena", "SAST": "Standardno južnoafričko vrijeme", "AWST": "Standardno zapadnoaustralijsko vrijeme", "ACWST": "Standardno australijsko centralno zapadno vrijeme", "CHAST": "Standardno čatamsko vrijeme", "WEZ": "Standardno zapadnoevropsko vrijeme", "HAST": "Standardno havajsko-aleućansko vrijeme", "GFT": "Vrijeme Francuske Gvajane", "AKST": "Standardno aljaskansko vrijeme", "OEZ": "Standardno istočnoevropsko vrijeme", "ART": "Standardno argentinsko vrijeme", "JST": "Standardno japansko vrijeme", "NZDT": "Novozelandsko ljetno računanje vremena", "CST": "Standardno centralno vrijeme (SAD)", "AST": "Standardno atlantsko vrijeme", "OESZ": "Istočnoevropsko ljetno računanje vremena", "TMST": "Turkmenistansko ljetno računanje vremena", "COST": "Kolumbijsko ljetno računanje vremena", "WART": "Standardno zapadnoargentinsko vrijeme", "UYST": "Urugvajsko ljetno računanje vremena", "WESZ": "Zapadnoevropsko ljetno računanje vremena", "ECT": "Ekvadorsko vrijeme", "HNT": "Standardno njufaundlendsko vrijeme", "ACDT": "Centralnoaustralijsko ljetno računanje vremena", "LHST": "Standardno vrijeme na Ostrvu Lord Hau", "JDT": "Japansko ljetno računanje vremena", "BOT": "Bolivijsko vrijeme", "AEDT": "Istočnoaustralijsko ljetno računanje vremena", "ADT": "Ljetno računanje atlantskog vremena", "AKDT": "Aljaskansko ljetno računanje vremena", "CHADT": "Čatamsko ljetno računanje vremena", "CLT": "Standardno čileansko vrijeme", "HKST": "Hongkonško ljetno računanje vremena", "ChST": "Standardno čamorsko vrijeme", "WITA": "Centralnoindonezijsko vrijeme", "ARST": "Argentinsko ljetno računanje vremena", "ACWDT": "Australijsko centralno zapadno ljetno računanje vremena", "HADT": "Havajsko-aleućansko ljetno rečunanje vremena", "EDT": "Ljetno računanje istočnog vremena (SAD)", "CAT": "Centralnoafričko vrijeme", "HKT": "Standardno hongkonško vrijeme", "HAT": "Njufaundlendsko ljetno računanje vremena", "EST": "Standardno istočno vrijeme (SAD)", "WIT": "Istočnoindonezijsko vrijeme", "SGT": "Standardno singapursko vrijeme", "MDT": "Makao letnje računanje vremena", "PST": "Standardno pacifičko vrijeme (SAD)", "PDT": "Ljetno računanje pacifičkog vremena (SAD)", "NZST": "Standardno novozelandsko vrijeme", "CDT": "Ljetno računanje centralnog vremena (SAD)", "∅∅∅": "Brazilijsko ljetno računanje vremena", "GYT": "Gvajansko vrijeme", "SRT": "Surinamsko vrijeme", "ACST": "Standardno centralnoaustralijsko vrijeme", "BT": "Butansko vrijeme", "MST": "Makao standardno vreme", "TMT": "Standardno turkmenistansko vrijeme", "LHDT": "Ljetno računanje vremena na Ostrvu Lord Hau", "MYT": "Malezijsko vrijeme", "MESZ": "Centralnoevropsko ljetno računanje vremena", "WAT": "Standardno zapadnoafričko vrijeme"},
  71. }
  72. }
  73. // Locale returns the current translators string locale
  74. func (bs *bs) Locale() string {
  75. return bs.locale
  76. }
  77. // PluralsCardinal returns the list of cardinal plural rules associated with 'bs'
  78. func (bs *bs) PluralsCardinal() []locales.PluralRule {
  79. return bs.pluralsCardinal
  80. }
  81. // PluralsOrdinal returns the list of ordinal plural rules associated with 'bs'
  82. func (bs *bs) PluralsOrdinal() []locales.PluralRule {
  83. return bs.pluralsOrdinal
  84. }
  85. // PluralsRange returns the list of range plural rules associated with 'bs'
  86. func (bs *bs) PluralsRange() []locales.PluralRule {
  87. return bs.pluralsRange
  88. }
  89. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'bs'
  90. func (bs *bs) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  91. n := math.Abs(num)
  92. i := int64(n)
  93. f := locales.F(n, v)
  94. iMod10 := i % 10
  95. iMod100 := i % 100
  96. fMod10 := f % 10
  97. fMod100 := f % 100
  98. if (v == 0 && iMod10 == 1 && iMod100 != 11) || (fMod10 == 1 && fMod100 != 11) {
  99. return locales.PluralRuleOne
  100. } else if (v == 0 && iMod10 >= 2 && iMod10 <= 4 && iMod100 < 12 && iMod100 > 14) || (fMod10 >= 2 && fMod10 <= 4 && fMod100 < 12 && fMod100 > 14) {
  101. return locales.PluralRuleFew
  102. }
  103. return locales.PluralRuleOther
  104. }
  105. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'bs'
  106. func (bs *bs) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  107. return locales.PluralRuleOther
  108. }
  109. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'bs'
  110. func (bs *bs) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  111. start := bs.CardinalPluralRule(num1, v1)
  112. end := bs.CardinalPluralRule(num2, v2)
  113. if start == locales.PluralRuleOne && end == locales.PluralRuleOne {
  114. return locales.PluralRuleOne
  115. } else if start == locales.PluralRuleOne && end == locales.PluralRuleFew {
  116. return locales.PluralRuleFew
  117. } else if start == locales.PluralRuleOne && end == locales.PluralRuleOther {
  118. return locales.PluralRuleOther
  119. } else if start == locales.PluralRuleFew && end == locales.PluralRuleOne {
  120. return locales.PluralRuleOne
  121. } else if start == locales.PluralRuleFew && end == locales.PluralRuleFew {
  122. return locales.PluralRuleFew
  123. } else if start == locales.PluralRuleFew && end == locales.PluralRuleOther {
  124. return locales.PluralRuleOther
  125. } else if start == locales.PluralRuleOther && end == locales.PluralRuleOne {
  126. return locales.PluralRuleOne
  127. } else if start == locales.PluralRuleOther && end == locales.PluralRuleFew {
  128. return locales.PluralRuleFew
  129. }
  130. return locales.PluralRuleOther
  131. }
  132. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  133. func (bs *bs) MonthAbbreviated(month time.Month) string {
  134. return bs.monthsAbbreviated[month]
  135. }
  136. // MonthsAbbreviated returns the locales abbreviated months
  137. func (bs *bs) MonthsAbbreviated() []string {
  138. return bs.monthsAbbreviated[1:]
  139. }
  140. // MonthNarrow returns the locales narrow month given the 'month' provided
  141. func (bs *bs) MonthNarrow(month time.Month) string {
  142. return bs.monthsNarrow[month]
  143. }
  144. // MonthsNarrow returns the locales narrow months
  145. func (bs *bs) MonthsNarrow() []string {
  146. return bs.monthsNarrow[1:]
  147. }
  148. // MonthWide returns the locales wide month given the 'month' provided
  149. func (bs *bs) MonthWide(month time.Month) string {
  150. return bs.monthsWide[month]
  151. }
  152. // MonthsWide returns the locales wide months
  153. func (bs *bs) MonthsWide() []string {
  154. return bs.monthsWide[1:]
  155. }
  156. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  157. func (bs *bs) WeekdayAbbreviated(weekday time.Weekday) string {
  158. return bs.daysAbbreviated[weekday]
  159. }
  160. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  161. func (bs *bs) WeekdaysAbbreviated() []string {
  162. return bs.daysAbbreviated
  163. }
  164. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  165. func (bs *bs) WeekdayNarrow(weekday time.Weekday) string {
  166. return bs.daysNarrow[weekday]
  167. }
  168. // WeekdaysNarrow returns the locales narrow weekdays
  169. func (bs *bs) WeekdaysNarrow() []string {
  170. return bs.daysNarrow
  171. }
  172. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  173. func (bs *bs) WeekdayShort(weekday time.Weekday) string {
  174. return bs.daysShort[weekday]
  175. }
  176. // WeekdaysShort returns the locales short weekdays
  177. func (bs *bs) WeekdaysShort() []string {
  178. return bs.daysShort
  179. }
  180. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  181. func (bs *bs) WeekdayWide(weekday time.Weekday) string {
  182. return bs.daysWide[weekday]
  183. }
  184. // WeekdaysWide returns the locales wide weekdays
  185. func (bs *bs) WeekdaysWide() []string {
  186. return bs.daysWide
  187. }
  188. // FmtNumber returns 'num' with digits/precision of 'v' for 'bs' and handles both Whole and Real numbers based on 'v'
  189. func (bs *bs) FmtNumber(num float64, v uint64) string {
  190. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  191. l := len(s) + 2 + 1*len(s[:len(s)-int(v)-1])/3
  192. count := 0
  193. inWhole := v == 0
  194. b := make([]byte, 0, l)
  195. for i := len(s) - 1; i >= 0; i-- {
  196. if s[i] == '.' {
  197. b = append(b, bs.decimal[0])
  198. inWhole = true
  199. continue
  200. }
  201. if inWhole {
  202. if count == 3 {
  203. b = append(b, bs.group[0])
  204. count = 1
  205. } else {
  206. count++
  207. }
  208. }
  209. b = append(b, s[i])
  210. }
  211. if num < 0 {
  212. b = append(b, bs.minus[0])
  213. }
  214. // reverse
  215. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  216. b[i], b[j] = b[j], b[i]
  217. }
  218. return string(b)
  219. }
  220. // FmtPercent returns 'num' with digits/precision of 'v' for 'bs' and handles both Whole and Real numbers based on 'v'
  221. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  222. func (bs *bs) FmtPercent(num float64, v uint64) string {
  223. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  224. l := len(s) + 3
  225. b := make([]byte, 0, l)
  226. for i := len(s) - 1; i >= 0; i-- {
  227. if s[i] == '.' {
  228. b = append(b, bs.decimal[0])
  229. continue
  230. }
  231. b = append(b, s[i])
  232. }
  233. if num < 0 {
  234. b = append(b, bs.minus[0])
  235. }
  236. // reverse
  237. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  238. b[i], b[j] = b[j], b[i]
  239. }
  240. b = append(b, bs.percent...)
  241. return string(b)
  242. }
  243. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'bs'
  244. func (bs *bs) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  245. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  246. symbol := bs.currencies[currency]
  247. l := len(s) + len(symbol) + 4 + 1*len(s[:len(s)-int(v)-1])/3
  248. count := 0
  249. inWhole := v == 0
  250. b := make([]byte, 0, l)
  251. for i := len(s) - 1; i >= 0; i-- {
  252. if s[i] == '.' {
  253. b = append(b, bs.decimal[0])
  254. inWhole = true
  255. continue
  256. }
  257. if inWhole {
  258. if count == 3 {
  259. b = append(b, bs.group[0])
  260. count = 1
  261. } else {
  262. count++
  263. }
  264. }
  265. b = append(b, s[i])
  266. }
  267. if num < 0 {
  268. b = append(b, bs.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. if int(v) < 2 {
  275. if v == 0 {
  276. b = append(b, bs.decimal...)
  277. }
  278. for i := 0; i < 2-int(v); i++ {
  279. b = append(b, '0')
  280. }
  281. }
  282. b = append(b, bs.currencyPositiveSuffix...)
  283. b = append(b, symbol...)
  284. return string(b)
  285. }
  286. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'bs'
  287. // in accounting notation.
  288. func (bs *bs) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  289. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  290. symbol := bs.currencies[currency]
  291. l := len(s) + len(symbol) + 4 + 1*len(s[:len(s)-int(v)-1])/3
  292. count := 0
  293. inWhole := v == 0
  294. b := make([]byte, 0, l)
  295. for i := len(s) - 1; i >= 0; i-- {
  296. if s[i] == '.' {
  297. b = append(b, bs.decimal[0])
  298. inWhole = true
  299. continue
  300. }
  301. if inWhole {
  302. if count == 3 {
  303. b = append(b, bs.group[0])
  304. count = 1
  305. } else {
  306. count++
  307. }
  308. }
  309. b = append(b, s[i])
  310. }
  311. if num < 0 {
  312. b = append(b, bs.minus[0])
  313. }
  314. // reverse
  315. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  316. b[i], b[j] = b[j], b[i]
  317. }
  318. if int(v) < 2 {
  319. if v == 0 {
  320. b = append(b, bs.decimal...)
  321. }
  322. for i := 0; i < 2-int(v); i++ {
  323. b = append(b, '0')
  324. }
  325. }
  326. if num < 0 {
  327. b = append(b, bs.currencyNegativeSuffix...)
  328. b = append(b, symbol...)
  329. } else {
  330. b = append(b, bs.currencyPositiveSuffix...)
  331. b = append(b, symbol...)
  332. }
  333. return string(b)
  334. }
  335. // FmtDateShort returns the short date representation of 't' for 'bs'
  336. func (bs *bs) FmtDateShort(t time.Time) string {
  337. b := make([]byte, 0, 32)
  338. if t.Day() < 10 {
  339. b = append(b, '0')
  340. }
  341. b = strconv.AppendInt(b, int64(t.Day()), 10)
  342. b = append(b, []byte{0x2e}...)
  343. if t.Month() < 10 {
  344. b = append(b, '0')
  345. }
  346. b = strconv.AppendInt(b, int64(t.Month()), 10)
  347. b = append(b, []byte{0x2e}...)
  348. if t.Year() > 9 {
  349. b = append(b, strconv.Itoa(t.Year())[2:]...)
  350. } else {
  351. b = append(b, strconv.Itoa(t.Year())[1:]...)
  352. }
  353. b = append(b, []byte{0x2e}...)
  354. return string(b)
  355. }
  356. // FmtDateMedium returns the medium date representation of 't' for 'bs'
  357. func (bs *bs) FmtDateMedium(t time.Time) string {
  358. b := make([]byte, 0, 32)
  359. if t.Day() < 10 {
  360. b = append(b, '0')
  361. }
  362. b = strconv.AppendInt(b, int64(t.Day()), 10)
  363. b = append(b, []byte{0x2e, 0x20}...)
  364. b = append(b, bs.monthsAbbreviated[t.Month()]...)
  365. b = append(b, []byte{0x2e, 0x20}...)
  366. b = strconv.AppendInt(b, int64(t.Year()), 10)
  367. b = append(b, []byte{0x2e}...)
  368. return string(b)
  369. }
  370. // FmtDateLong returns the long date representation of 't' for 'bs'
  371. func (bs *bs) FmtDateLong(t time.Time) string {
  372. b := make([]byte, 0, 32)
  373. if t.Day() < 10 {
  374. b = append(b, '0')
  375. }
  376. b = strconv.AppendInt(b, int64(t.Day()), 10)
  377. b = append(b, []byte{0x2e, 0x20}...)
  378. b = append(b, bs.monthsWide[t.Month()]...)
  379. b = append(b, []byte{0x20}...)
  380. b = strconv.AppendInt(b, int64(t.Year()), 10)
  381. b = append(b, []byte{0x2e}...)
  382. return string(b)
  383. }
  384. // FmtDateFull returns the full date representation of 't' for 'bs'
  385. func (bs *bs) FmtDateFull(t time.Time) string {
  386. b := make([]byte, 0, 32)
  387. b = append(b, bs.daysWide[t.Weekday()]...)
  388. b = append(b, []byte{0x2c, 0x20}...)
  389. if t.Day() < 10 {
  390. b = append(b, '0')
  391. }
  392. b = strconv.AppendInt(b, int64(t.Day()), 10)
  393. b = append(b, []byte{0x2e, 0x20}...)
  394. b = append(b, bs.monthsWide[t.Month()]...)
  395. b = append(b, []byte{0x20}...)
  396. b = strconv.AppendInt(b, int64(t.Year()), 10)
  397. b = append(b, []byte{0x2e}...)
  398. return string(b)
  399. }
  400. // FmtTimeShort returns the short time representation of 't' for 'bs'
  401. func (bs *bs) FmtTimeShort(t time.Time) string {
  402. b := make([]byte, 0, 32)
  403. if t.Hour() < 10 {
  404. b = append(b, '0')
  405. }
  406. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  407. b = append(b, bs.timeSeparator...)
  408. if t.Minute() < 10 {
  409. b = append(b, '0')
  410. }
  411. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  412. return string(b)
  413. }
  414. // FmtTimeMedium returns the medium time representation of 't' for 'bs'
  415. func (bs *bs) FmtTimeMedium(t time.Time) string {
  416. b := make([]byte, 0, 32)
  417. if t.Hour() < 10 {
  418. b = append(b, '0')
  419. }
  420. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  421. b = append(b, bs.timeSeparator...)
  422. if t.Minute() < 10 {
  423. b = append(b, '0')
  424. }
  425. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  426. b = append(b, bs.timeSeparator...)
  427. if t.Second() < 10 {
  428. b = append(b, '0')
  429. }
  430. b = strconv.AppendInt(b, int64(t.Second()), 10)
  431. return string(b)
  432. }
  433. // FmtTimeLong returns the long time representation of 't' for 'bs'
  434. func (bs *bs) FmtTimeLong(t time.Time) string {
  435. b := make([]byte, 0, 32)
  436. if t.Hour() < 10 {
  437. b = append(b, '0')
  438. }
  439. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  440. b = append(b, bs.timeSeparator...)
  441. if t.Minute() < 10 {
  442. b = append(b, '0')
  443. }
  444. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  445. b = append(b, bs.timeSeparator...)
  446. if t.Second() < 10 {
  447. b = append(b, '0')
  448. }
  449. b = strconv.AppendInt(b, int64(t.Second()), 10)
  450. b = append(b, []byte{0x20}...)
  451. tz, _ := t.Zone()
  452. b = append(b, tz...)
  453. return string(b)
  454. }
  455. // FmtTimeFull returns the full time representation of 't' for 'bs'
  456. func (bs *bs) FmtTimeFull(t time.Time) string {
  457. b := make([]byte, 0, 32)
  458. if t.Hour() < 10 {
  459. b = append(b, '0')
  460. }
  461. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  462. b = append(b, bs.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, bs.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. tz, _ := t.Zone()
  474. if btz, ok := bs.timezones[tz]; ok {
  475. b = append(b, btz...)
  476. } else {
  477. b = append(b, tz...)
  478. }
  479. return string(b)
  480. }