as.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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: nil,
  47. timeSeparator: ".",
  48. 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 ", "BAM ", "BAN ", "BBD ", "BDT ", "BEC ", "BEF ", "BEL ", "BGL ", "BGM ", "BGN ", "BGO ", "BHD ", "BIF ", "BMD ", "BND ", "BOB ", "BOL ", "BOP ", "BOV ", "BRB ", "BRC ", "BRE ", "BRL ", "BRN ", "BRR ", "BRZ ", "BSD ", "BTN ", "BUK ", "BWP ", "BYB ", "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 ", "EUR ", "FIM ", "FJD ", "FKP ", "FRF ", "GBP ", "GEK ", "GEL ", "GHC ", "GHS ", "GIP ", "GMD ", "GNF ", "GNS ", "GQE ", "GRD ", "GTQ ", "GWE ", "GWP ", "GYD ", "HKD ", "HNL ", "HRD ", "HRK ", "HTG ", "HUF ", "IDR ", "IEP ", "ILP ", "ILR ", "ILS ", "INR ", "IQD ", "IRR ", "ISJ ", "ISK ", "ITL ", "JMD ", "JOD ", "JPY ", "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 ", "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 ", "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 ", "THB ", "TJR ", "TJS ", "TMM ", "TMT ", "TND ", "TOP ", "TPE ", "TRL ", "TRY ", "TTD ", "TWD ", "TZS ", "UAH ", "UAK ", "UGS ", "UGX ", "USD ", "USN ", "USS ", "UYI ", "UYP ", "UYU ", "UZS ", "VEB ", "VEF ", "VND ", "VNN ", "VUV ", "WST ", "XAF ", "XAG ", "XAU ", "XBA ", "XBB ", "XBC ", "XBD ", "XCD ", "XDR ", "XEU ", "XFO ", "XFU ", "XOF ", "XPD ", "XPF ", "XPT ", "XRE ", "XSU ", "XTS ", "XUA ", "XXX ", "YDD ", "YER ", "YUD ", "YUM ", "YUN ", "YUR ", "ZAL ", "ZAR ", "ZMK ", "ZMW ", "ZRN ", "ZRZ ", "ZWD ", "ZWL ", "ZWR "},
  49. currencyPositivePrefix: " ",
  50. currencyNegativePrefix: " ",
  51. monthsAbbreviated: []string{"", "জানু", "ফেব্ৰু", "মাৰ্চ", "এপ্ৰিল", "মে", "জুন", "জুলাই", "আগ", "সেপ্ট", "অক্টো", "নভে", "ডিসে"},
  52. monthsWide: []string{"", "জানুৱাৰী", "ফেব্ৰুৱাৰী", "মাৰ্চ", "এপ্ৰিল", "মে", "জুন", "জুলাই", "আগষ্ট", "ছেপ্তেম্বৰ", "অক্টোবৰ", "নৱেম্বৰ", "ডিচেম্বৰ"},
  53. daysAbbreviated: []string{"ৰবি", "সোম", "মঙ্গল", "বুধ", "বৃহষ্পতি", "শুক্ৰ", "শনি"},
  54. daysWide: []string{"দেওবাৰ", "সোমবাৰ", "মঙ্গলবাৰ", "বুধবাৰ", "বৃহষ্পতিবাৰ", "শুক্ৰবাৰ", "শনিবাৰ"},
  55. periodsAbbreviated: []string{"পূৰ্বাহ্ণ", "অপৰাহ্ণ"},
  56. periodsWide: []string{"পূৰ্বাহ্ণ", "অপৰাহ্ণ"},
  57. timezones: map[string]string{"COST": "COST", "HKST": "HKST", "OEZ": "OEZ", "HNT": "HNT", "ACDT": "ACDT", "JST": "JST", "BT": "BT", "GMT": "GMT", "AKDT": "AKDT", "CDT": "CDT", "ACWDT": "ACWDT", "EST": "EST", "HAST": "HAST", "HADT": "HADT", "MEZ": "MEZ", "UYT": "UYT", "WART": "WART", "EAT": "EAT", "TMT": "TMT", "AKST": "AKST", "JDT": "JDT", "SRT": "SRT", "PST": "PST", "GYT": "GYT", "CHADT": "CHADT", "WAT": "WAT", "GFT": "GFT", "MDT": "MDT", "NZST": "NZST", "VET": "VET", "AEDT": "AEDT", "EDT": "EDT", "COT": "COT", "OESZ": "OESZ", "TMST": "TMST", "AWDT": "AWDT", "LHDT": "LHDT", "∅∅∅": "∅∅∅", "ADT": "ADT", "IST": "ভাৰতীয় সময়", "MST": "MST", "ACST": "ACST", "PDT": "PDT", "WARST": "WARST", "HAT": "HAT", "WIB": "WIB", "NZDT": "NZDT", "SGT": "SGT", "CHAST": "CHAST", "UYST": "UYST", "ChST": "ChST", "CAT": "CAT", "ECT": "ECT", "SAST": "SAST", "HKT": "HKT", "ART": "ART", "AWST": "AWST", "AST": "AST", "BOT": "BOT", "WITA": "WITA", "LHST": "LHST", "ARST": "ARST", "CST": "CST", "MESZ": "MESZ", "ACWST": "ACWST", "CLST": "CLST", "WAST": "WAST", "WESZ": "WESZ", "AEST": "AEST", "WIT": "WIT", "MYT": "MYT", "WEZ": "WEZ", "CLT": "CLT"},
  58. }
  59. }
  60. // Locale returns the current translators string locale
  61. func (as *as) Locale() string {
  62. return as.locale
  63. }
  64. // PluralsCardinal returns the list of cardinal plural rules associated with 'as'
  65. func (as *as) PluralsCardinal() []locales.PluralRule {
  66. return as.pluralsCardinal
  67. }
  68. // PluralsOrdinal returns the list of ordinal plural rules associated with 'as'
  69. func (as *as) PluralsOrdinal() []locales.PluralRule {
  70. return as.pluralsOrdinal
  71. }
  72. // PluralsRange returns the list of range plural rules associated with 'as'
  73. func (as *as) PluralsRange() []locales.PluralRule {
  74. return as.pluralsRange
  75. }
  76. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'as'
  77. func (as *as) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  78. n := math.Abs(num)
  79. i := int64(n)
  80. if (i == 0) || (n == 1) {
  81. return locales.PluralRuleOne
  82. }
  83. return locales.PluralRuleOther
  84. }
  85. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'as'
  86. func (as *as) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  87. n := math.Abs(num)
  88. if n == 1 || n == 5 || n == 7 || n == 8 || n == 9 || n == 10 {
  89. return locales.PluralRuleOne
  90. } else if n == 2 || n == 3 {
  91. return locales.PluralRuleTwo
  92. } else if n == 4 {
  93. return locales.PluralRuleFew
  94. } else if n == 6 {
  95. return locales.PluralRuleMany
  96. }
  97. return locales.PluralRuleOther
  98. }
  99. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'as'
  100. func (as *as) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  101. return locales.PluralRuleUnknown
  102. }
  103. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  104. func (as *as) MonthAbbreviated(month time.Month) string {
  105. return as.monthsAbbreviated[month]
  106. }
  107. // MonthsAbbreviated returns the locales abbreviated months
  108. func (as *as) MonthsAbbreviated() []string {
  109. return as.monthsAbbreviated[1:]
  110. }
  111. // MonthNarrow returns the locales narrow month given the 'month' provided
  112. func (as *as) MonthNarrow(month time.Month) string {
  113. return as.monthsNarrow[month]
  114. }
  115. // MonthsNarrow returns the locales narrow months
  116. func (as *as) MonthsNarrow() []string {
  117. return as.monthsNarrow[1:]
  118. }
  119. // MonthWide returns the locales wide month given the 'month' provided
  120. func (as *as) MonthWide(month time.Month) string {
  121. return as.monthsWide[month]
  122. }
  123. // MonthsWide returns the locales wide months
  124. func (as *as) MonthsWide() []string {
  125. return as.monthsWide[1:]
  126. }
  127. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  128. func (as *as) WeekdayAbbreviated(weekday time.Weekday) string {
  129. return as.daysAbbreviated[weekday]
  130. }
  131. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  132. func (as *as) WeekdaysAbbreviated() []string {
  133. return as.daysAbbreviated
  134. }
  135. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  136. func (as *as) WeekdayNarrow(weekday time.Weekday) string {
  137. return as.daysNarrow[weekday]
  138. }
  139. // WeekdaysNarrow returns the locales narrow weekdays
  140. func (as *as) WeekdaysNarrow() []string {
  141. return as.daysNarrow
  142. }
  143. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  144. func (as *as) WeekdayShort(weekday time.Weekday) string {
  145. return as.daysShort[weekday]
  146. }
  147. // WeekdaysShort returns the locales short weekdays
  148. func (as *as) WeekdaysShort() []string {
  149. return as.daysShort
  150. }
  151. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  152. func (as *as) WeekdayWide(weekday time.Weekday) string {
  153. return as.daysWide[weekday]
  154. }
  155. // WeekdaysWide returns the locales wide weekdays
  156. func (as *as) WeekdaysWide() []string {
  157. return as.daysWide
  158. }
  159. // FmtNumber returns 'num' with digits/precision of 'v' for 'as' and handles both Whole and Real numbers based on 'v'
  160. func (as *as) FmtNumber(num float64, v uint64) string {
  161. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  162. l := len(s) + 0 + 0*len(s[:len(s)-int(v)-1])/3
  163. count := 0
  164. inWhole := v == 0
  165. inSecondary := false
  166. groupThreshold := 3
  167. b := make([]byte, 0, l)
  168. for i := len(s) - 1; i >= 0; i-- {
  169. if s[i] == '.' {
  170. b = append(b, as.decimal[0])
  171. inWhole = true
  172. continue
  173. }
  174. if inWhole {
  175. if count == groupThreshold {
  176. b = append(b, as.group[0])
  177. count = 1
  178. if !inSecondary {
  179. inSecondary = true
  180. groupThreshold = 2
  181. }
  182. } else {
  183. count++
  184. }
  185. }
  186. b = append(b, s[i])
  187. }
  188. if num < 0 {
  189. b = append(b, as.minus[0])
  190. }
  191. // reverse
  192. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  193. b[i], b[j] = b[j], b[i]
  194. }
  195. return string(b)
  196. }
  197. // FmtPercent returns 'num' with digits/precision of 'v' for 'as' and handles both Whole and Real numbers based on 'v'
  198. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  199. func (as *as) FmtPercent(num float64, v uint64) string {
  200. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  201. l := len(s) + 0
  202. b := make([]byte, 0, l)
  203. for i := len(s) - 1; i >= 0; i-- {
  204. if s[i] == '.' {
  205. b = append(b, as.decimal[0])
  206. continue
  207. }
  208. b = append(b, s[i])
  209. }
  210. if num < 0 {
  211. b = append(b, as.minus[0])
  212. }
  213. // reverse
  214. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  215. b[i], b[j] = b[j], b[i]
  216. }
  217. b = append(b, as.percent...)
  218. return string(b)
  219. }
  220. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'as'
  221. func (as *as) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  222. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  223. symbol := as.currencies[currency]
  224. l := len(s) + len(symbol) + 2 + 0*len(s[:len(s)-int(v)-1])/3
  225. count := 0
  226. inWhole := v == 0
  227. inSecondary := false
  228. groupThreshold := 3
  229. b := make([]byte, 0, l)
  230. for i := len(s) - 1; i >= 0; i-- {
  231. if s[i] == '.' {
  232. b = append(b, as.decimal[0])
  233. inWhole = true
  234. continue
  235. }
  236. if inWhole {
  237. if count == groupThreshold {
  238. b = append(b, as.group[0])
  239. count = 1
  240. if !inSecondary {
  241. inSecondary = true
  242. groupThreshold = 2
  243. }
  244. } else {
  245. count++
  246. }
  247. }
  248. b = append(b, s[i])
  249. }
  250. for j := len(symbol) - 1; j >= 0; j-- {
  251. b = append(b, symbol[j])
  252. }
  253. for j := len(as.currencyPositivePrefix) - 1; j >= 0; j-- {
  254. b = append(b, as.currencyPositivePrefix[j])
  255. }
  256. if num < 0 {
  257. b = append(b, as.minus[0])
  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, as.decimal...)
  266. }
  267. for i := 0; i < 2-int(v); i++ {
  268. b = append(b, '0')
  269. }
  270. }
  271. return string(b)
  272. }
  273. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'as'
  274. // in accounting notation.
  275. func (as *as) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  276. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  277. symbol := as.currencies[currency]
  278. l := len(s) + len(symbol) + 2 + 0*len(s[:len(s)-int(v)-1])/3
  279. count := 0
  280. inWhole := v == 0
  281. inSecondary := false
  282. groupThreshold := 3
  283. b := make([]byte, 0, l)
  284. for i := len(s) - 1; i >= 0; i-- {
  285. if s[i] == '.' {
  286. b = append(b, as.decimal[0])
  287. inWhole = true
  288. continue
  289. }
  290. if inWhole {
  291. if count == groupThreshold {
  292. b = append(b, as.group[0])
  293. count = 1
  294. if !inSecondary {
  295. inSecondary = true
  296. groupThreshold = 2
  297. }
  298. } else {
  299. count++
  300. }
  301. }
  302. b = append(b, s[i])
  303. }
  304. if num < 0 {
  305. for j := len(symbol) - 1; j >= 0; j-- {
  306. b = append(b, symbol[j])
  307. }
  308. for j := len(as.currencyNegativePrefix) - 1; j >= 0; j-- {
  309. b = append(b, as.currencyNegativePrefix[j])
  310. }
  311. b = append(b, as.minus[0])
  312. } else {
  313. for j := len(symbol) - 1; j >= 0; j-- {
  314. b = append(b, symbol[j])
  315. }
  316. for j := len(as.currencyPositivePrefix) - 1; j >= 0; j-- {
  317. b = append(b, as.currencyPositivePrefix[j])
  318. }
  319. }
  320. // reverse
  321. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  322. b[i], b[j] = b[j], b[i]
  323. }
  324. if int(v) < 2 {
  325. if v == 0 {
  326. b = append(b, as.decimal...)
  327. }
  328. for i := 0; i < 2-int(v); i++ {
  329. b = append(b, '0')
  330. }
  331. }
  332. return string(b)
  333. }
  334. // FmtDateShort returns the short date representation of 't' for 'as'
  335. func (as *as) FmtDateShort(t time.Time) string {
  336. b := make([]byte, 0, 32)
  337. b = strconv.AppendInt(b, int64(t.Day()), 10)
  338. b = append(b, []byte{0x2d}...)
  339. b = strconv.AppendInt(b, int64(t.Month()), 10)
  340. b = append(b, []byte{0x2d}...)
  341. b = strconv.AppendInt(b, int64(t.Year()), 10)
  342. return string(b)
  343. }
  344. // FmtDateMedium returns the medium date representation of 't' for 'as'
  345. func (as *as) FmtDateMedium(t time.Time) string {
  346. b := make([]byte, 0, 32)
  347. if t.Day() < 10 {
  348. b = append(b, '0')
  349. }
  350. b = strconv.AppendInt(b, int64(t.Day()), 10)
  351. b = append(b, []byte{0x2d}...)
  352. if t.Month() < 10 {
  353. b = append(b, '0')
  354. }
  355. b = strconv.AppendInt(b, int64(t.Month()), 10)
  356. b = append(b, []byte{0x2d}...)
  357. b = strconv.AppendInt(b, int64(t.Year()), 10)
  358. return string(b)
  359. }
  360. // FmtDateLong returns the long date representation of 't' for 'as'
  361. func (as *as) FmtDateLong(t time.Time) string {
  362. b := make([]byte, 0, 32)
  363. b = strconv.AppendInt(b, int64(t.Day()), 10)
  364. b = append(b, []byte{0x20}...)
  365. b = append(b, as.monthsWide[t.Month()]...)
  366. b = append(b, []byte{0x2c, 0x20}...)
  367. b = strconv.AppendInt(b, int64(t.Year()), 10)
  368. return string(b)
  369. }
  370. // FmtDateFull returns the full date representation of 't' for 'as'
  371. func (as *as) FmtDateFull(t time.Time) string {
  372. b := make([]byte, 0, 32)
  373. b = append(b, as.daysWide[t.Weekday()]...)
  374. b = append(b, []byte{0x2c, 0x20}...)
  375. b = strconv.AppendInt(b, int64(t.Day()), 10)
  376. b = append(b, []byte{0x20}...)
  377. b = append(b, as.monthsWide[t.Month()]...)
  378. b = append(b, []byte{0x2c, 0x20}...)
  379. b = strconv.AppendInt(b, int64(t.Year()), 10)
  380. return string(b)
  381. }
  382. // FmtTimeShort returns the short time representation of 't' for 'as'
  383. func (as *as) FmtTimeShort(t time.Time) string {
  384. b := make([]byte, 0, 32)
  385. h := t.Hour()
  386. if h > 12 {
  387. h -= 12
  388. }
  389. b = strconv.AppendInt(b, int64(h), 10)
  390. b = append(b, []byte{0x2e}...)
  391. if t.Minute() < 10 {
  392. b = append(b, '0')
  393. }
  394. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  395. b = append(b, []byte{0x2e, 0x20}...)
  396. if t.Hour() < 12 {
  397. b = append(b, as.periodsAbbreviated[0]...)
  398. } else {
  399. b = append(b, as.periodsAbbreviated[1]...)
  400. }
  401. return string(b)
  402. }
  403. // FmtTimeMedium returns the medium time representation of 't' for 'as'
  404. func (as *as) FmtTimeMedium(t time.Time) string {
  405. b := make([]byte, 0, 32)
  406. h := t.Hour()
  407. if h > 12 {
  408. h -= 12
  409. }
  410. b = strconv.AppendInt(b, int64(h), 10)
  411. b = append(b, []byte{0x2e}...)
  412. if t.Minute() < 10 {
  413. b = append(b, '0')
  414. }
  415. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  416. b = append(b, []byte{0x2e}...)
  417. if t.Second() < 10 {
  418. b = append(b, '0')
  419. }
  420. b = strconv.AppendInt(b, int64(t.Second()), 10)
  421. b = append(b, []byte{0x20}...)
  422. if t.Hour() < 12 {
  423. b = append(b, as.periodsAbbreviated[0]...)
  424. } else {
  425. b = append(b, as.periodsAbbreviated[1]...)
  426. }
  427. return string(b)
  428. }
  429. // FmtTimeLong returns the long time representation of 't' for 'as'
  430. func (as *as) FmtTimeLong(t time.Time) string {
  431. b := make([]byte, 0, 32)
  432. h := t.Hour()
  433. if h > 12 {
  434. h -= 12
  435. }
  436. b = strconv.AppendInt(b, int64(h), 10)
  437. b = append(b, []byte{0x2e}...)
  438. if t.Minute() < 10 {
  439. b = append(b, '0')
  440. }
  441. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  442. b = append(b, []byte{0x2e}...)
  443. if t.Second() < 10 {
  444. b = append(b, '0')
  445. }
  446. b = strconv.AppendInt(b, int64(t.Second()), 10)
  447. b = append(b, []byte{0x20}...)
  448. if t.Hour() < 12 {
  449. b = append(b, as.periodsAbbreviated[0]...)
  450. } else {
  451. b = append(b, as.periodsAbbreviated[1]...)
  452. }
  453. b = append(b, []byte{0x20}...)
  454. tz, _ := t.Zone()
  455. b = append(b, tz...)
  456. return string(b)
  457. }
  458. // FmtTimeFull returns the full time representation of 't' for 'as'
  459. func (as *as) FmtTimeFull(t time.Time) string {
  460. b := make([]byte, 0, 32)
  461. h := t.Hour()
  462. if h > 12 {
  463. h -= 12
  464. }
  465. b = strconv.AppendInt(b, int64(h), 10)
  466. b = append(b, []byte{0x2e}...)
  467. if t.Minute() < 10 {
  468. b = append(b, '0')
  469. }
  470. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  471. b = append(b, []byte{0x2e}...)
  472. if t.Second() < 10 {
  473. b = append(b, '0')
  474. }
  475. b = strconv.AppendInt(b, int64(t.Second()), 10)
  476. b = append(b, []byte{0x20}...)
  477. if t.Hour() < 12 {
  478. b = append(b, as.periodsAbbreviated[0]...)
  479. } else {
  480. b = append(b, as.periodsAbbreviated[1]...)
  481. }
  482. b = append(b, []byte{0x20}...)
  483. tz, _ := t.Zone()
  484. if btz, ok := as.timezones[tz]; ok {
  485. b = append(b, btz...)
  486. } else {
  487. b = append(b, tz...)
  488. }
  489. return string(b)
  490. }