ast.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. package ast
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type ast 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 'ast' locale
  41. func New() locales.Translator {
  42. return &ast{
  43. locale: "ast",
  44. pluralsCardinal: []locales.PluralRule{2, 6},
  45. pluralsOrdinal: nil,
  46. pluralsRange: nil,
  47. decimal: "٫",
  48. group: "٬",
  49. minus: "‏-",
  50. percent: "٪",
  51. perMille: "؉",
  52. timeSeparator: ":",
  53. 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", "¥", "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", "SUR", "SVC", "SYP", "SZL", "฿", "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"},
  54. currencyPositiveSuffix: " ",
  55. currencyNegativeSuffix: " ",
  56. monthsAbbreviated: []string{"", "xin", "feb", "mar", "abr", "may", "xun", "xnt", "ago", "set", "och", "pay", "avi"},
  57. monthsNarrow: []string{"", "X", "F", "M", "A", "M", "X", "X", "A", "S", "O", "P", "A"},
  58. monthsWide: []string{"", "de xineru", "de febreru", "de marzu", "d’abril", "de mayu", "de xunu", "de xunetu", "d’agostu", "de setiembre", "d’ochobre", "de payares", "d’avientu"},
  59. daysAbbreviated: []string{"dom", "llu", "mar", "mié", "xue", "vie", "sáb"},
  60. daysNarrow: []string{"D", "L", "M", "M", "X", "V", "S"},
  61. daysShort: []string{"do", "ll", "ma", "mi", "xu", "vi", "sá"},
  62. daysWide: []string{"domingu", "llunes", "martes", "miércoles", "xueves", "vienres", "sábadu"},
  63. periodsAbbreviated: []string{"AM", "PM"},
  64. periodsNarrow: []string{"a", "p"},
  65. periodsWide: []string{"de la mañana", "de la tardi"},
  66. erasAbbreviated: []string{"a.C.", "d.C."},
  67. erasNarrow: []string{"aC", "dC"},
  68. erasWide: []string{"a.C.", "después de Cristu"},
  69. timezones: map[string]string{"CHAST": "Hora estándar de Chatham", "VET": "Hora de Venezuela", "AEST": "Hora estándar d’Australia del este", "AST": "Hora estándar del Atlánticu", "BT": "Hora de Bután", "PDT": "Hora braniega del Pacíficu norteamericanu", "HAT": "Hora braniega de Newfoundland", "EAT": "Hora d’África del este", "JST": "Hora estándar de Xapón", "JDT": "Hora braniega de Xapón", "HADT": "Hora braniega de Hawaii-Aleutianes", "WIB": "Hora d’Indonesia del oeste", "HNT": "Hora estándar de Newfoundland", "ADT": "Hora braniega del Atlánticu", "WAT": "Hora estándar d’África del oeste", "ChST": "Hora estándar de Chamorro", "∅∅∅": "Hora braniega de Brasilia", "OEZ": "Hora estándar d’Europa del Este", "AEDT": "Hora braniega d’Australia del este", "SRT": "Hora del Surinam", "SGT": "Hora estándar de Singapur", "IST": "Hora estándar de la India", "GFT": "Hora de La Guyana Francesa", "WESZ": "Hora braniega d’Europa Occidental", "WIT": "Hora d’Indonesia del este", "OESZ": "Hora braniega d’Europa del Este", "BOT": "Hora de Bolivia", "SAST": "Hora de Sudáfrica", "LHDT": "Hora braniega de Lord Howe", "NZDT": "Hora braniega de Nueva Zelanda", "WARST": "Hora braniega occidental d’Arxentina", "MDT": "Hora braniega de Macáu", "ART": "Hora estándar d’Arxentina", "EST": "Hora estándar del este norteamericanu", "MESZ": "Hora braniega d’Europa Central", "CHADT": "Hora braniega de Chatham", "LHST": "Hora estándar de Lord Howe", "CDT": "Hora braniega central norteamericana", "MEZ": "Hora estándar d’Europa Central", "CLST": "Hora braniega de Chile", "COT": "Hora estándar de Colombia", "MST": "Hora estándar de Macáu", "AKST": "Hora estándar d’Alaska", "ACST": "Hora estándar d’Australia central", "UYT": "Hora estándar del Uruguái", "ECT": "Hora d’Ecuador", "HKST": "Hora braniega de Ḥong Kong", "WART": "Hora estándar occidental d’Arxentina", "UYST": "Hora braniega del Uruguái", "ACWDT": "Hora braniega d’Australia central del oeste", "ARST": "Hora braniega d’Arxentina", "MYT": "Hora de Malasia", "PST": "Hora estándar del Pacíficu norteamericanu", "COST": "Hora braniega de Colombia", "HAST": "Hora estándar de Hawaii-Aleutianes", "TMST": "Hora braniega del Turkmenistán", "TMT": "Hora estándar del Turkmenistán", "ACDT": "Hora braniega d’Australia central", "AKDT": "Hora braniega d’Alaska", "AWST": "Hora estándar d’Australia del oeste", "EDT": "Hora braniega del este norteamericanu", "WITA": "Hora d’Indonesia central", "GMT": "Hora media de Greenwich", "WAST": "Hora braniega d’África del oeste", "HKT": "Hora estándar de Ḥong Kong", "ACWST": "Hora estándar d’Australia central del oeste", "CST": "Hora estándar central norteamericana", "NZST": "Hora estándar de Nueva Zelanda", "CLT": "Hora estándar de Chile", "WEZ": "Hora estándar d’Europa Occidental", "CAT": "Hora d’África central", "AWDT": "Hora braniega d’Australia del oeste", "GYT": "Hora de La Guyana"},
  70. }
  71. }
  72. // Locale returns the current translators string locale
  73. func (ast *ast) Locale() string {
  74. return ast.locale
  75. }
  76. // PluralsCardinal returns the list of cardinal plural rules associated with 'ast'
  77. func (ast *ast) PluralsCardinal() []locales.PluralRule {
  78. return ast.pluralsCardinal
  79. }
  80. // PluralsOrdinal returns the list of ordinal plural rules associated with 'ast'
  81. func (ast *ast) PluralsOrdinal() []locales.PluralRule {
  82. return ast.pluralsOrdinal
  83. }
  84. // PluralsRange returns the list of range plural rules associated with 'ast'
  85. func (ast *ast) PluralsRange() []locales.PluralRule {
  86. return ast.pluralsRange
  87. }
  88. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'ast'
  89. func (ast *ast) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  90. n := math.Abs(num)
  91. i := int64(n)
  92. if i == 1 && v == 0 {
  93. return locales.PluralRuleOne
  94. }
  95. return locales.PluralRuleOther
  96. }
  97. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'ast'
  98. func (ast *ast) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  99. return locales.PluralRuleUnknown
  100. }
  101. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'ast'
  102. func (ast *ast) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  103. return locales.PluralRuleUnknown
  104. }
  105. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  106. func (ast *ast) MonthAbbreviated(month time.Month) string {
  107. return ast.monthsAbbreviated[month]
  108. }
  109. // MonthsAbbreviated returns the locales abbreviated months
  110. func (ast *ast) MonthsAbbreviated() []string {
  111. return ast.monthsAbbreviated[1:]
  112. }
  113. // MonthNarrow returns the locales narrow month given the 'month' provided
  114. func (ast *ast) MonthNarrow(month time.Month) string {
  115. return ast.monthsNarrow[month]
  116. }
  117. // MonthsNarrow returns the locales narrow months
  118. func (ast *ast) MonthsNarrow() []string {
  119. return ast.monthsNarrow[1:]
  120. }
  121. // MonthWide returns the locales wide month given the 'month' provided
  122. func (ast *ast) MonthWide(month time.Month) string {
  123. return ast.monthsWide[month]
  124. }
  125. // MonthsWide returns the locales wide months
  126. func (ast *ast) MonthsWide() []string {
  127. return ast.monthsWide[1:]
  128. }
  129. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  130. func (ast *ast) WeekdayAbbreviated(weekday time.Weekday) string {
  131. return ast.daysAbbreviated[weekday]
  132. }
  133. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  134. func (ast *ast) WeekdaysAbbreviated() []string {
  135. return ast.daysAbbreviated
  136. }
  137. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  138. func (ast *ast) WeekdayNarrow(weekday time.Weekday) string {
  139. return ast.daysNarrow[weekday]
  140. }
  141. // WeekdaysNarrow returns the locales narrow weekdays
  142. func (ast *ast) WeekdaysNarrow() []string {
  143. return ast.daysNarrow
  144. }
  145. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  146. func (ast *ast) WeekdayShort(weekday time.Weekday) string {
  147. return ast.daysShort[weekday]
  148. }
  149. // WeekdaysShort returns the locales short weekdays
  150. func (ast *ast) WeekdaysShort() []string {
  151. return ast.daysShort
  152. }
  153. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  154. func (ast *ast) WeekdayWide(weekday time.Weekday) string {
  155. return ast.daysWide[weekday]
  156. }
  157. // WeekdaysWide returns the locales wide weekdays
  158. func (ast *ast) WeekdaysWide() []string {
  159. return ast.daysWide
  160. }
  161. // FmtNumber returns 'num' with digits/precision of 'v' for 'ast' and handles both Whole and Real numbers based on 'v'
  162. func (ast *ast) FmtNumber(num float64, v uint64) string {
  163. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  164. l := len(s) + 6 + 2*len(s[:len(s)-int(v)-1])/3
  165. count := 0
  166. inWhole := v == 0
  167. b := make([]byte, 0, l)
  168. for i := len(s) - 1; i >= 0; i-- {
  169. if s[i] == '.' {
  170. for j := len(ast.decimal) - 1; j >= 0; j-- {
  171. b = append(b, ast.decimal[j])
  172. }
  173. inWhole = true
  174. continue
  175. }
  176. if inWhole {
  177. if count == 3 {
  178. for j := len(ast.group) - 1; j >= 0; j-- {
  179. b = append(b, ast.group[j])
  180. }
  181. count = 1
  182. } else {
  183. count++
  184. }
  185. }
  186. b = append(b, s[i])
  187. }
  188. if num < 0 {
  189. for j := len(ast.minus) - 1; j >= 0; j-- {
  190. b = append(b, ast.minus[j])
  191. }
  192. }
  193. // reverse
  194. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  195. b[i], b[j] = b[j], b[i]
  196. }
  197. return string(b)
  198. }
  199. // FmtPercent returns 'num' with digits/precision of 'v' for 'ast' and handles both Whole and Real numbers based on 'v'
  200. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  201. func (ast *ast) FmtPercent(num float64, v uint64) string {
  202. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  203. l := len(s) + 8
  204. b := make([]byte, 0, l)
  205. for i := len(s) - 1; i >= 0; i-- {
  206. if s[i] == '.' {
  207. for j := len(ast.decimal) - 1; j >= 0; j-- {
  208. b = append(b, ast.decimal[j])
  209. }
  210. continue
  211. }
  212. b = append(b, s[i])
  213. }
  214. if num < 0 {
  215. for j := len(ast.minus) - 1; j >= 0; j-- {
  216. b = append(b, ast.minus[j])
  217. }
  218. }
  219. // reverse
  220. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  221. b[i], b[j] = b[j], b[i]
  222. }
  223. b = append(b, ast.percent...)
  224. return string(b)
  225. }
  226. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'ast'
  227. func (ast *ast) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  228. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  229. symbol := ast.currencies[currency]
  230. l := len(s) + len(symbol) + 8 + 2*len(s[:len(s)-int(v)-1])/3
  231. count := 0
  232. inWhole := v == 0
  233. b := make([]byte, 0, l)
  234. for i := len(s) - 1; i >= 0; i-- {
  235. if s[i] == '.' {
  236. for j := len(ast.decimal) - 1; j >= 0; j-- {
  237. b = append(b, ast.decimal[j])
  238. }
  239. inWhole = true
  240. continue
  241. }
  242. if inWhole {
  243. if count == 3 {
  244. for j := len(ast.group) - 1; j >= 0; j-- {
  245. b = append(b, ast.group[j])
  246. }
  247. count = 1
  248. } else {
  249. count++
  250. }
  251. }
  252. b = append(b, s[i])
  253. }
  254. if num < 0 {
  255. for j := len(ast.minus) - 1; j >= 0; j-- {
  256. b = append(b, ast.minus[j])
  257. }
  258. }
  259. // reverse
  260. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  261. b[i], b[j] = b[j], b[i]
  262. }
  263. if int(v) < 2 {
  264. if v == 0 {
  265. b = append(b, ast.decimal...)
  266. }
  267. for i := 0; i < 2-int(v); i++ {
  268. b = append(b, '0')
  269. }
  270. }
  271. b = append(b, ast.currencyPositiveSuffix...)
  272. b = append(b, symbol...)
  273. return string(b)
  274. }
  275. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'ast'
  276. // in accounting notation.
  277. func (ast *ast) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  278. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  279. symbol := ast.currencies[currency]
  280. l := len(s) + len(symbol) + 8 + 2*len(s[:len(s)-int(v)-1])/3
  281. count := 0
  282. inWhole := v == 0
  283. b := make([]byte, 0, l)
  284. for i := len(s) - 1; i >= 0; i-- {
  285. if s[i] == '.' {
  286. for j := len(ast.decimal) - 1; j >= 0; j-- {
  287. b = append(b, ast.decimal[j])
  288. }
  289. inWhole = true
  290. continue
  291. }
  292. if inWhole {
  293. if count == 3 {
  294. for j := len(ast.group) - 1; j >= 0; j-- {
  295. b = append(b, ast.group[j])
  296. }
  297. count = 1
  298. } else {
  299. count++
  300. }
  301. }
  302. b = append(b, s[i])
  303. }
  304. if num < 0 {
  305. for j := len(ast.minus) - 1; j >= 0; j-- {
  306. b = append(b, ast.minus[j])
  307. }
  308. }
  309. // reverse
  310. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  311. b[i], b[j] = b[j], b[i]
  312. }
  313. if int(v) < 2 {
  314. if v == 0 {
  315. b = append(b, ast.decimal...)
  316. }
  317. for i := 0; i < 2-int(v); i++ {
  318. b = append(b, '0')
  319. }
  320. }
  321. if num < 0 {
  322. b = append(b, ast.currencyNegativeSuffix...)
  323. b = append(b, symbol...)
  324. } else {
  325. b = append(b, ast.currencyPositiveSuffix...)
  326. b = append(b, symbol...)
  327. }
  328. return string(b)
  329. }
  330. // FmtDateShort returns the short date representation of 't' for 'ast'
  331. func (ast *ast) FmtDateShort(t time.Time) string {
  332. b := make([]byte, 0, 32)
  333. b = strconv.AppendInt(b, int64(t.Day()), 10)
  334. b = append(b, []byte{0x2f}...)
  335. b = strconv.AppendInt(b, int64(t.Month()), 10)
  336. b = append(b, []byte{0x2f}...)
  337. if t.Year() > 9 {
  338. b = append(b, strconv.Itoa(t.Year())[2:]...)
  339. } else {
  340. b = append(b, strconv.Itoa(t.Year())[1:]...)
  341. }
  342. return string(b)
  343. }
  344. // FmtDateMedium returns the medium date representation of 't' for 'ast'
  345. func (ast *ast) FmtDateMedium(t time.Time) string {
  346. b := make([]byte, 0, 32)
  347. b = strconv.AppendInt(b, int64(t.Day()), 10)
  348. b = append(b, []byte{0x20}...)
  349. b = append(b, ast.monthsAbbreviated[t.Month()]...)
  350. b = append(b, []byte{0x20}...)
  351. b = strconv.AppendInt(b, int64(t.Year()), 10)
  352. return string(b)
  353. }
  354. // FmtDateLong returns the long date representation of 't' for 'ast'
  355. func (ast *ast) FmtDateLong(t time.Time) string {
  356. b := make([]byte, 0, 32)
  357. b = strconv.AppendInt(b, int64(t.Day()), 10)
  358. b = append(b, []byte{0x20}...)
  359. b = append(b, ast.monthsWide[t.Month()]...)
  360. b = append(b, []byte{}...)
  361. b = append(b, []byte{0x27, 0x64, 0x65, 0x27, 0x20}...)
  362. b = strconv.AppendInt(b, int64(t.Year()), 10)
  363. return string(b)
  364. }
  365. // FmtDateFull returns the full date representation of 't' for 'ast'
  366. func (ast *ast) FmtDateFull(t time.Time) string {
  367. b := make([]byte, 0, 32)
  368. b = append(b, ast.daysWide[t.Weekday()]...)
  369. b = append(b, []byte{0x2c, 0x20}...)
  370. b = strconv.AppendInt(b, int64(t.Day()), 10)
  371. b = append(b, []byte{0x20}...)
  372. b = append(b, ast.monthsWide[t.Month()]...)
  373. b = append(b, []byte{}...)
  374. b = append(b, []byte{0x27, 0x64, 0x65, 0x27, 0x20}...)
  375. b = strconv.AppendInt(b, int64(t.Year()), 10)
  376. return string(b)
  377. }
  378. // FmtTimeShort returns the short time representation of 't' for 'ast'
  379. func (ast *ast) FmtTimeShort(t time.Time) string {
  380. b := make([]byte, 0, 32)
  381. if t.Hour() < 10 {
  382. b = append(b, '0')
  383. }
  384. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  385. b = append(b, ast.timeSeparator...)
  386. if t.Minute() < 10 {
  387. b = append(b, '0')
  388. }
  389. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  390. return string(b)
  391. }
  392. // FmtTimeMedium returns the medium time representation of 't' for 'ast'
  393. func (ast *ast) FmtTimeMedium(t time.Time) string {
  394. b := make([]byte, 0, 32)
  395. if t.Hour() < 10 {
  396. b = append(b, '0')
  397. }
  398. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  399. b = append(b, ast.timeSeparator...)
  400. if t.Minute() < 10 {
  401. b = append(b, '0')
  402. }
  403. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  404. b = append(b, ast.timeSeparator...)
  405. if t.Second() < 10 {
  406. b = append(b, '0')
  407. }
  408. b = strconv.AppendInt(b, int64(t.Second()), 10)
  409. return string(b)
  410. }
  411. // FmtTimeLong returns the long time representation of 't' for 'ast'
  412. func (ast *ast) FmtTimeLong(t time.Time) string {
  413. b := make([]byte, 0, 32)
  414. if t.Hour() < 10 {
  415. b = append(b, '0')
  416. }
  417. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  418. b = append(b, ast.timeSeparator...)
  419. if t.Minute() < 10 {
  420. b = append(b, '0')
  421. }
  422. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  423. b = append(b, ast.timeSeparator...)
  424. if t.Second() < 10 {
  425. b = append(b, '0')
  426. }
  427. b = strconv.AppendInt(b, int64(t.Second()), 10)
  428. b = append(b, []byte{0x20}...)
  429. tz, _ := t.Zone()
  430. b = append(b, tz...)
  431. return string(b)
  432. }
  433. // FmtTimeFull returns the full time representation of 't' for 'ast'
  434. func (ast *ast) FmtTimeFull(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, ast.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, ast.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. if btz, ok := ast.timezones[tz]; ok {
  453. b = append(b, btz...)
  454. } else {
  455. b = append(b, tz...)
  456. }
  457. return string(b)
  458. }