as.go 24 KB

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