cy.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. package cy
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type cy 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. currencyNegativePrefix 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 'cy' locale
  41. func New() locales.Translator {
  42. return &cy{
  43. locale: "cy",
  44. pluralsCardinal: []locales.PluralRule{1, 2, 3, 4, 5, 6},
  45. pluralsOrdinal: []locales.PluralRule{1, 2, 3, 4, 5, 6},
  46. pluralsRange: []locales.PluralRule{4, 5, 6, 2, 3},
  47. timeSeparator: ":",
  48. 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", "R$", "BRN", "BRR", "BRZ", "BSD", "BTN", "BUK", "BWP", "BYB ", "BYR", "BZD", "CA$", "CDF", "CHE", "CHF", "CHW", "CLE", "CLF", "CLP", "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", "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", "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", "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"},
  49. currencyNegativePrefix: "(",
  50. currencyNegativeSuffix: ")",
  51. monthsAbbreviated: []string{"", "Ion", "Chwef", "Maw", "Ebrill", "Mai", "Meh", "Gorff", "Awst", "Medi", "Hyd", "Tach", "Rhag"},
  52. monthsNarrow: []string{"", "I", "Ch", "M", "E", "M", "M", "G", "A", "M", "H", "T", "Rh"},
  53. monthsWide: []string{"", "Ionawr", "Chwefror", "Mawrth", "Ebrill", "Mai", "Mehefin", "Gorffennaf", "Awst", "Medi", "Hydref", "Tachwedd", "Rhagfyr"},
  54. daysAbbreviated: []string{"Sul", "Llun", "Maw", "Mer", "Iau", "Gwen", "Sad"},
  55. daysNarrow: []string{"S", "Ll", "M", "M", "I", "G", "S"},
  56. daysShort: []string{"Su", "Ll", "Ma", "Me", "Ia", "Gw", "Sa"},
  57. daysWide: []string{"Dydd Sul", "Dydd Llun", "Dydd Mawrth", "Dydd Mercher", "Dydd Iau", "Dydd Gwener", "Dydd Sadwrn"},
  58. periodsAbbreviated: []string{"AM", "PM"},
  59. periodsNarrow: []string{"AM", "PM"},
  60. periodsWide: []string{"AM", "PM"},
  61. erasAbbreviated: []string{"CC", "OC"},
  62. erasNarrow: []string{"C", "O"},
  63. erasWide: []string{"Cyn Crist", "Oed Crist"},
  64. timezones: map[string]string{"OESZ": "Amser Haf Dwyrain Ewrop", "HAT": "Amser Haf Newfoundland", "TMST": "Amser Haf Tyrcmenistan", "GYT": "Amser Guyana", "CST": "Amser Safonol Canolbarth Gogledd America", "AEDT": "Amser Haf Dwyrain Awstralia", "AST": "Amser Safonol Cefnfor yr Iwerydd", "HKST": "Amser Haf Hong Kong", "WITA": "Amser Canolbarth Indonesia", "EAT": "Amser Dwyrain Affrica", "AKDT": "Amser Haf Alaska", "ECT": "Amser Ecuador", "IST": "Amser India", "SRT": "Amser Suriname", "LHDT": "Amser Haf yr Arglwydd Howe", "BOT": "Amser Bolivia", "WARST": "Amser Haf Gorllewin Ariannin", "GMT": "Amser Safonol Greenwich", "MDT": "MDT", "ART": "Amser Safonol Ariannin", "AWDT": "Amser Haf Gorllewin Awstralia", "ACST": "Amser Safonol Canolbarth Awstralia", "TMT": "Amser Safonol Tyrcmenistan", "AKST": "Amser Safonol Alaska", "VET": "Amser Venezuela", "EST": "Amser Safonol Dwyrain Gogledd America", "HAST": "Amser Safonol Hawaii-Aleutian", "MESZ": "Amser Haf Canolbarth Ewrop", "NZST": "Amser Safonol Seland Newydd", "UYST": "Amser Haf Uruguay", "WEZ": "Amser Safonol Gorllewin Ewrop", "WESZ": "Amser Haf Gorllewin Ewrop", "WIT": "Amser Dwyrain Indonesia", "HNT": "Amser Safonol Newfoundland", "CLST": "Amser Haf Chile", "WAST": "Amser Haf Gorllewin Affrica", "OEZ": "Amser Safonol Dwyrain Ewrop", "CDT": "Amser Haf Canolbarth Gogledd America", "ACWDT": "Amser Haf Canolbarth Gorllewin Awstralia", "ChST": "Amser Chamorro", "HADT": "Amser Haf Hawaii-Aleutian", "WAT": "Amser Safonol Gorllewin Affrica", "BT": "Amser Bhutan", "COT": "Amser Safonol Colombia", "MST": "MST", "∅∅∅": "Amser Haf Amazonas", "PDT": "Amser Haf Cefnfor Tawel Gogledd America", "HKT": "Amser Safonol Hong Kong", "ARST": "Amser Haf Ariannin", "ADT": "Amser Haf Cefnfor yr Iwerydd", "JST": "Amser Safonol Siapan", "MEZ": "Amser Safonol Canolbarth Ewrop", "CHAST": "Amser Safonol Chatham", "CHADT": "Amser Haf Chatham", "EDT": "Amser Haf Dwyrain Gogledd America", "SGT": "Amser Singapore", "WART": "Amser Safonol Gorllewin Ariannin", "JDT": "Amser Haf Siapan", "MYT": "Amser Malaysia", "NZDT": "Amser Haf Seland Newydd", "ACWST": "Amser Safonol Canolbarth Gorllewin Awstralia", "COST": "Amser Haf Colombia", "PST": "Amser Safonol Cefnfor Tawel Gogledd America", "LHST": "Amser Safonol yr Arglwydd Howe", "CLT": "Amser Safonol Chile", "SAST": "Amser Safonol De Affrica", "AWST": "Amser Safonol Gorllewin Awstralia", "UYT": "Amser Safonol Uruguay", "AEST": "Amser Safonol Dwyrain Awstralia", "CAT": "Amser Canolbarth Affrica", "WIB": "Amser Gorllewin Indonesia", "GFT": "Amser Guyane Ffrengig", "ACDT": "Amser Haf Canolbarth Awstralia"},
  65. }
  66. }
  67. // Locale returns the current translators string locale
  68. func (cy *cy) Locale() string {
  69. return cy.locale
  70. }
  71. // PluralsCardinal returns the list of cardinal plural rules associated with 'cy'
  72. func (cy *cy) PluralsCardinal() []locales.PluralRule {
  73. return cy.pluralsCardinal
  74. }
  75. // PluralsOrdinal returns the list of ordinal plural rules associated with 'cy'
  76. func (cy *cy) PluralsOrdinal() []locales.PluralRule {
  77. return cy.pluralsOrdinal
  78. }
  79. // PluralsRange returns the list of range plural rules associated with 'cy'
  80. func (cy *cy) PluralsRange() []locales.PluralRule {
  81. return cy.pluralsRange
  82. }
  83. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'cy'
  84. func (cy *cy) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  85. n := math.Abs(num)
  86. if n == 0 {
  87. return locales.PluralRuleZero
  88. } else if n == 1 {
  89. return locales.PluralRuleOne
  90. } else if n == 2 {
  91. return locales.PluralRuleTwo
  92. } else if n == 3 {
  93. return locales.PluralRuleFew
  94. } else if n == 6 {
  95. return locales.PluralRuleMany
  96. }
  97. return locales.PluralRuleOther
  98. }
  99. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'cy'
  100. func (cy *cy) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  101. n := math.Abs(num)
  102. if n == 0 || n == 7 || n == 8 || n == 9 {
  103. return locales.PluralRuleZero
  104. } else if n == 1 {
  105. return locales.PluralRuleOne
  106. } else if n == 2 {
  107. return locales.PluralRuleTwo
  108. } else if n == 3 || n == 4 {
  109. return locales.PluralRuleFew
  110. } else if n == 5 || n == 6 {
  111. return locales.PluralRuleMany
  112. }
  113. return locales.PluralRuleOther
  114. }
  115. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'cy'
  116. func (cy *cy) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  117. start := cy.CardinalPluralRule(num1, v1)
  118. end := cy.CardinalPluralRule(num2, v2)
  119. if start == locales.PluralRuleZero && end == locales.PluralRuleOne {
  120. return locales.PluralRuleOne
  121. } else if start == locales.PluralRuleZero && end == locales.PluralRuleTwo {
  122. return locales.PluralRuleTwo
  123. } else if start == locales.PluralRuleZero && end == locales.PluralRuleFew {
  124. return locales.PluralRuleFew
  125. } else if start == locales.PluralRuleZero && end == locales.PluralRuleMany {
  126. return locales.PluralRuleMany
  127. } else if start == locales.PluralRuleZero && end == locales.PluralRuleOther {
  128. return locales.PluralRuleOther
  129. } else if start == locales.PluralRuleOne && end == locales.PluralRuleTwo {
  130. return locales.PluralRuleTwo
  131. } else if start == locales.PluralRuleOne && end == locales.PluralRuleFew {
  132. return locales.PluralRuleFew
  133. } else if start == locales.PluralRuleOne && end == locales.PluralRuleMany {
  134. return locales.PluralRuleMany
  135. } else if start == locales.PluralRuleOne && end == locales.PluralRuleOther {
  136. return locales.PluralRuleOther
  137. } else if start == locales.PluralRuleTwo && end == locales.PluralRuleFew {
  138. return locales.PluralRuleFew
  139. } else if start == locales.PluralRuleTwo && end == locales.PluralRuleMany {
  140. return locales.PluralRuleMany
  141. } else if start == locales.PluralRuleTwo && end == locales.PluralRuleOther {
  142. return locales.PluralRuleOther
  143. } else if start == locales.PluralRuleFew && end == locales.PluralRuleMany {
  144. return locales.PluralRuleMany
  145. } else if start == locales.PluralRuleFew && end == locales.PluralRuleOther {
  146. return locales.PluralRuleOther
  147. } else if start == locales.PluralRuleMany && end == locales.PluralRuleOther {
  148. return locales.PluralRuleOther
  149. } else if start == locales.PluralRuleOther && end == locales.PluralRuleOne {
  150. return locales.PluralRuleOne
  151. } else if start == locales.PluralRuleOther && end == locales.PluralRuleTwo {
  152. return locales.PluralRuleTwo
  153. } else if start == locales.PluralRuleOther && end == locales.PluralRuleFew {
  154. return locales.PluralRuleFew
  155. } else if start == locales.PluralRuleOther && end == locales.PluralRuleMany {
  156. return locales.PluralRuleMany
  157. }
  158. return locales.PluralRuleOther
  159. }
  160. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  161. func (cy *cy) MonthAbbreviated(month time.Month) string {
  162. return cy.monthsAbbreviated[month]
  163. }
  164. // MonthsAbbreviated returns the locales abbreviated months
  165. func (cy *cy) MonthsAbbreviated() []string {
  166. return cy.monthsAbbreviated[1:]
  167. }
  168. // MonthNarrow returns the locales narrow month given the 'month' provided
  169. func (cy *cy) MonthNarrow(month time.Month) string {
  170. return cy.monthsNarrow[month]
  171. }
  172. // MonthsNarrow returns the locales narrow months
  173. func (cy *cy) MonthsNarrow() []string {
  174. return cy.monthsNarrow[1:]
  175. }
  176. // MonthWide returns the locales wide month given the 'month' provided
  177. func (cy *cy) MonthWide(month time.Month) string {
  178. return cy.monthsWide[month]
  179. }
  180. // MonthsWide returns the locales wide months
  181. func (cy *cy) MonthsWide() []string {
  182. return cy.monthsWide[1:]
  183. }
  184. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  185. func (cy *cy) WeekdayAbbreviated(weekday time.Weekday) string {
  186. return cy.daysAbbreviated[weekday]
  187. }
  188. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  189. func (cy *cy) WeekdaysAbbreviated() []string {
  190. return cy.daysAbbreviated
  191. }
  192. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  193. func (cy *cy) WeekdayNarrow(weekday time.Weekday) string {
  194. return cy.daysNarrow[weekday]
  195. }
  196. // WeekdaysNarrow returns the locales narrow weekdays
  197. func (cy *cy) WeekdaysNarrow() []string {
  198. return cy.daysNarrow
  199. }
  200. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  201. func (cy *cy) WeekdayShort(weekday time.Weekday) string {
  202. return cy.daysShort[weekday]
  203. }
  204. // WeekdaysShort returns the locales short weekdays
  205. func (cy *cy) WeekdaysShort() []string {
  206. return cy.daysShort
  207. }
  208. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  209. func (cy *cy) WeekdayWide(weekday time.Weekday) string {
  210. return cy.daysWide[weekday]
  211. }
  212. // WeekdaysWide returns the locales wide weekdays
  213. func (cy *cy) WeekdaysWide() []string {
  214. return cy.daysWide
  215. }
  216. // FmtNumber returns 'num' with digits/precision of 'v' for 'cy' and handles both Whole and Real numbers based on 'v'
  217. func (cy *cy) FmtNumber(num float64, v uint64) string {
  218. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  219. l := len(s) + 0 + 0*len(s[:len(s)-int(v)-1])/3
  220. count := 0
  221. inWhole := v == 0
  222. b := make([]byte, 0, l)
  223. for i := len(s) - 1; i >= 0; i-- {
  224. if s[i] == '.' {
  225. b = append(b, cy.decimal[0])
  226. inWhole = true
  227. continue
  228. }
  229. if inWhole {
  230. if count == 3 {
  231. b = append(b, cy.group[0])
  232. count = 1
  233. } else {
  234. count++
  235. }
  236. }
  237. b = append(b, s[i])
  238. }
  239. if num < 0 {
  240. b = append(b, cy.minus[0])
  241. }
  242. // reverse
  243. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  244. b[i], b[j] = b[j], b[i]
  245. }
  246. return string(b)
  247. }
  248. // FmtPercent returns 'num' with digits/precision of 'v' for 'cy' and handles both Whole and Real numbers based on 'v'
  249. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  250. func (cy *cy) FmtPercent(num float64, v uint64) string {
  251. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  252. l := len(s) + 0
  253. b := make([]byte, 0, l)
  254. for i := len(s) - 1; i >= 0; i-- {
  255. if s[i] == '.' {
  256. b = append(b, cy.decimal[0])
  257. continue
  258. }
  259. b = append(b, s[i])
  260. }
  261. if num < 0 {
  262. b = append(b, cy.minus[0])
  263. }
  264. // reverse
  265. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  266. b[i], b[j] = b[j], b[i]
  267. }
  268. b = append(b, cy.percent...)
  269. return string(b)
  270. }
  271. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'cy'
  272. func (cy *cy) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  273. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  274. symbol := cy.currencies[currency]
  275. l := len(s) + len(symbol) + 0 + 0*len(s[:len(s)-int(v)-1])/3
  276. count := 0
  277. inWhole := v == 0
  278. b := make([]byte, 0, l)
  279. for i := len(s) - 1; i >= 0; i-- {
  280. if s[i] == '.' {
  281. b = append(b, cy.decimal[0])
  282. inWhole = true
  283. continue
  284. }
  285. if inWhole {
  286. if count == 3 {
  287. b = append(b, cy.group[0])
  288. count = 1
  289. } else {
  290. count++
  291. }
  292. }
  293. b = append(b, s[i])
  294. }
  295. for j := len(symbol) - 1; j >= 0; j-- {
  296. b = append(b, symbol[j])
  297. }
  298. if num < 0 {
  299. b = append(b, cy.minus[0])
  300. }
  301. // reverse
  302. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  303. b[i], b[j] = b[j], b[i]
  304. }
  305. if int(v) < 2 {
  306. if v == 0 {
  307. b = append(b, cy.decimal...)
  308. }
  309. for i := 0; i < 2-int(v); i++ {
  310. b = append(b, '0')
  311. }
  312. }
  313. return string(b)
  314. }
  315. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'cy'
  316. // in accounting notation.
  317. func (cy *cy) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  318. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  319. symbol := cy.currencies[currency]
  320. l := len(s) + len(symbol) + 2 + 0*len(s[:len(s)-int(v)-1])/3
  321. count := 0
  322. inWhole := v == 0
  323. b := make([]byte, 0, l)
  324. for i := len(s) - 1; i >= 0; i-- {
  325. if s[i] == '.' {
  326. b = append(b, cy.decimal[0])
  327. inWhole = true
  328. continue
  329. }
  330. if inWhole {
  331. if count == 3 {
  332. b = append(b, cy.group[0])
  333. count = 1
  334. } else {
  335. count++
  336. }
  337. }
  338. b = append(b, s[i])
  339. }
  340. if num < 0 {
  341. for j := len(symbol) - 1; j >= 0; j-- {
  342. b = append(b, symbol[j])
  343. }
  344. b = append(b, cy.currencyNegativePrefix[0])
  345. } else {
  346. for j := len(symbol) - 1; j >= 0; j-- {
  347. b = append(b, symbol[j])
  348. }
  349. }
  350. // reverse
  351. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  352. b[i], b[j] = b[j], b[i]
  353. }
  354. if int(v) < 2 {
  355. if v == 0 {
  356. b = append(b, cy.decimal...)
  357. }
  358. for i := 0; i < 2-int(v); i++ {
  359. b = append(b, '0')
  360. }
  361. }
  362. if num < 0 {
  363. b = append(b, cy.currencyNegativeSuffix...)
  364. }
  365. return string(b)
  366. }
  367. // FmtDateShort returns the short date representation of 't' for 'cy'
  368. func (cy *cy) FmtDateShort(t time.Time) string {
  369. b := make([]byte, 0, 32)
  370. if t.Day() < 10 {
  371. b = append(b, '0')
  372. }
  373. b = strconv.AppendInt(b, int64(t.Day()), 10)
  374. b = append(b, []byte{0x2f}...)
  375. if t.Month() < 10 {
  376. b = append(b, '0')
  377. }
  378. b = strconv.AppendInt(b, int64(t.Month()), 10)
  379. b = append(b, []byte{0x2f}...)
  380. if t.Year() > 9 {
  381. b = append(b, strconv.Itoa(t.Year())[2:]...)
  382. } else {
  383. b = append(b, strconv.Itoa(t.Year())[1:]...)
  384. }
  385. return string(b)
  386. }
  387. // FmtDateMedium returns the medium date representation of 't' for 'cy'
  388. func (cy *cy) FmtDateMedium(t time.Time) string {
  389. b := make([]byte, 0, 32)
  390. b = strconv.AppendInt(b, int64(t.Day()), 10)
  391. b = append(b, []byte{0x20}...)
  392. b = append(b, cy.monthsAbbreviated[t.Month()]...)
  393. b = append(b, []byte{0x20}...)
  394. b = strconv.AppendInt(b, int64(t.Year()), 10)
  395. return string(b)
  396. }
  397. // FmtDateLong returns the long date representation of 't' for 'cy'
  398. func (cy *cy) FmtDateLong(t time.Time) string {
  399. b := make([]byte, 0, 32)
  400. b = strconv.AppendInt(b, int64(t.Day()), 10)
  401. b = append(b, []byte{0x20}...)
  402. b = append(b, cy.monthsWide[t.Month()]...)
  403. b = append(b, []byte{0x20}...)
  404. b = strconv.AppendInt(b, int64(t.Year()), 10)
  405. return string(b)
  406. }
  407. // FmtDateFull returns the full date representation of 't' for 'cy'
  408. func (cy *cy) FmtDateFull(t time.Time) string {
  409. b := make([]byte, 0, 32)
  410. b = append(b, cy.daysWide[t.Weekday()]...)
  411. b = append(b, []byte{0x2c, 0x20}...)
  412. b = strconv.AppendInt(b, int64(t.Day()), 10)
  413. b = append(b, []byte{0x20}...)
  414. b = append(b, cy.monthsWide[t.Month()]...)
  415. b = append(b, []byte{0x20}...)
  416. b = strconv.AppendInt(b, int64(t.Year()), 10)
  417. return string(b)
  418. }
  419. // FmtTimeShort returns the short time representation of 't' for 'cy'
  420. func (cy *cy) FmtTimeShort(t time.Time) string {
  421. b := make([]byte, 0, 32)
  422. if t.Hour() < 10 {
  423. b = append(b, '0')
  424. }
  425. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  426. b = append(b, cy.timeSeparator...)
  427. if t.Minute() < 10 {
  428. b = append(b, '0')
  429. }
  430. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  431. return string(b)
  432. }
  433. // FmtTimeMedium returns the medium time representation of 't' for 'cy'
  434. func (cy *cy) FmtTimeMedium(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, cy.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, cy.timeSeparator...)
  446. if t.Second() < 10 {
  447. b = append(b, '0')
  448. }
  449. b = strconv.AppendInt(b, int64(t.Second()), 10)
  450. return string(b)
  451. }
  452. // FmtTimeLong returns the long time representation of 't' for 'cy'
  453. func (cy *cy) FmtTimeLong(t time.Time) string {
  454. b := make([]byte, 0, 32)
  455. if t.Hour() < 10 {
  456. b = append(b, '0')
  457. }
  458. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  459. b = append(b, cy.timeSeparator...)
  460. if t.Minute() < 10 {
  461. b = append(b, '0')
  462. }
  463. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  464. b = append(b, cy.timeSeparator...)
  465. if t.Second() < 10 {
  466. b = append(b, '0')
  467. }
  468. b = strconv.AppendInt(b, int64(t.Second()), 10)
  469. b = append(b, []byte{0x20}...)
  470. tz, _ := t.Zone()
  471. b = append(b, tz...)
  472. return string(b)
  473. }
  474. // FmtTimeFull returns the full time representation of 't' for 'cy'
  475. func (cy *cy) FmtTimeFull(t time.Time) string {
  476. b := make([]byte, 0, 32)
  477. if t.Hour() < 10 {
  478. b = append(b, '0')
  479. }
  480. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  481. b = append(b, cy.timeSeparator...)
  482. if t.Minute() < 10 {
  483. b = append(b, '0')
  484. }
  485. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  486. b = append(b, cy.timeSeparator...)
  487. if t.Second() < 10 {
  488. b = append(b, '0')
  489. }
  490. b = strconv.AppendInt(b, int64(t.Second()), 10)
  491. b = append(b, []byte{0x20}...)
  492. tz, _ := t.Zone()
  493. if btz, ok := cy.timezones[tz]; ok {
  494. b = append(b, btz...)
  495. } else {
  496. b = append(b, tz...)
  497. }
  498. return string(b)
  499. }