mgo.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. package mgo
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type mgo 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 'mgo' locale
  41. func New() locales.Translator {
  42. return &mgo{
  43. locale: "mgo",
  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. inifinity: "∞",
  54. currencies: []string{"ADP", "AED", "AFA", "AFN", "ALK", "ALL", "AMD", "ANG", "AOA", "AOK", "AON", "AOR", "ARA", "ARL", "ARM", "ARP", "ARS", "ATS", "AUD", "AWG", "AZM", "AZN", "BAD", "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", "BYN", "BYR", "BZD", "CAD", "CDF", "CHE", "CHF", "CHW", "CLE", "CLF", "CLP", "CNH", "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", "STN", "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", "FCFA", "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"},
  55. currencyPositivePrefix: " ",
  56. currencyNegativePrefix: " ",
  57. monthsAbbreviated: []string{"", "mbegtug", "imeg àbùbì", "imeg mbəŋchubi", "iməg ngwə̀t", "iməg fog", "iməg ichiibɔd", "iməg àdùmbə̀ŋ", "iməg ichika", "iməg kud", "iməg tèsiʼe", "iməg zò", "iməg krizmed"},
  58. monthsNarrow: []string{"", "M1", "A2", "M3", "N4", "F5", "I6", "A7", "I8", "K9", "10", "11", "12"},
  59. monthsWide: []string{"", "iməg mbegtug", "imeg àbùbì", "imeg mbəŋchubi", "iməg ngwə̀t", "iməg fog", "iməg ichiibɔd", "iməg àdùmbə̀ŋ", "iməg ichika", "iməg kud", "iməg tèsiʼe", "iməg zò", "iməg krizmed"},
  60. daysAbbreviated: []string{"Aneg 1", "Aneg 2", "Aneg 3", "Aneg 4", "Aneg 5", "Aneg 6", "Aneg 7"},
  61. daysNarrow: []string{"A1", "A2", "A3", "A4", "A5", "A6", "A7"},
  62. daysShort: []string{"1", "2", "3", "4", "5", "6", "7"},
  63. daysWide: []string{"Aneg 1", "Aneg 2", "Aneg 3", "Aneg 4", "Aneg 5", "Aneg 6", "Aneg 7"},
  64. periodsAbbreviated: []string{"AM", "PM"},
  65. periodsWide: []string{"AM", "PM"},
  66. erasAbbreviated: []string{"BCE", "CE"},
  67. erasNarrow: []string{"", ""},
  68. erasWide: []string{"", ""},
  69. timezones: map[string]string{"SGT": "SGT", "EST": "EST", "ACDT": "ACDT", "IST": "IST", "WARST": "WARST", "CLT": "CLT", "HNOG": "HNOG", "HENOMX": "HENOMX", "OEZ": "OEZ", "HEPMX": "HEPMX", "PDT": "PDT", "AST": "AST", "BOT": "BOT", "AKST": "AKST", "LHST": "LHST", "VET": "VET", "TMST": "TMST", "CHAST": "CHAST", "ADT": "ADT", "MYT": "MYT", "JST": "JST", "∅∅∅": "∅∅∅", "COST": "COST", "ACWST": "ACWST", "HEPM": "HEPM", "HNNOMX": "HNNOMX", "AWST": "AWST", "AWDT": "AWDT", "SAST": "SAST", "JDT": "JDT", "ECT": "ECT", "EDT": "EDT", "CHADT": "CHADT", "NZDT": "NZDT", "GMT": "GMT", "CST": "CST", "AEST": "AEST", "AKDT": "AKDT", "HNPM": "HNPM", "PST": "PST", "WEZ": "WEZ", "HEEG": "HEEG", "MESZ": "MESZ", "LHDT": "LHDT", "ART": "ART", "ARST": "ARST", "WESZ": "WESZ", "WAT": "WAT", "WIB": "WIB", "GFT": "GFT", "HKT": "HKT", "HNT": "HNT", "GYT": "GYT", "HNPMX": "HNPMX", "AEDT": "AEDT", "HNEG": "HNEG", "MST": "MST", "MDT": "MDT", "CAT": "CAT", "CLST": "CLST", "UYST": "UYST", "COT": "COT", "ACST": "ACST", "HEOG": "HEOG", "WART": "WART", "HAT": "HAT", "EAT": "EAT", "HAST": "HAST", "WAST": "WAST", "NZST": "NZST", "HKST": "HKST", "UYT": "UYT", "ChST": "ChST", "HECU": "HECU", "BT": "BT", "TMT": "TMT", "OESZ": "OESZ", "HADT": "HADT", "HNCU": "HNCU", "CDT": "CDT", "ACWDT": "ACWDT", "MEZ": "MEZ", "WITA": "WITA", "SRT": "SRT", "WIT": "WIT"},
  70. }
  71. }
  72. // Locale returns the current translators string locale
  73. func (mgo *mgo) Locale() string {
  74. return mgo.locale
  75. }
  76. // PluralsCardinal returns the list of cardinal plural rules associated with 'mgo'
  77. func (mgo *mgo) PluralsCardinal() []locales.PluralRule {
  78. return mgo.pluralsCardinal
  79. }
  80. // PluralsOrdinal returns the list of ordinal plural rules associated with 'mgo'
  81. func (mgo *mgo) PluralsOrdinal() []locales.PluralRule {
  82. return mgo.pluralsOrdinal
  83. }
  84. // PluralsRange returns the list of range plural rules associated with 'mgo'
  85. func (mgo *mgo) PluralsRange() []locales.PluralRule {
  86. return mgo.pluralsRange
  87. }
  88. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'mgo'
  89. func (mgo *mgo) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  90. n := math.Abs(num)
  91. if n == 1 {
  92. return locales.PluralRuleOne
  93. }
  94. return locales.PluralRuleOther
  95. }
  96. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'mgo'
  97. func (mgo *mgo) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  98. return locales.PluralRuleUnknown
  99. }
  100. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'mgo'
  101. func (mgo *mgo) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  102. return locales.PluralRuleUnknown
  103. }
  104. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  105. func (mgo *mgo) MonthAbbreviated(month time.Month) string {
  106. return mgo.monthsAbbreviated[month]
  107. }
  108. // MonthsAbbreviated returns the locales abbreviated months
  109. func (mgo *mgo) MonthsAbbreviated() []string {
  110. return mgo.monthsAbbreviated[1:]
  111. }
  112. // MonthNarrow returns the locales narrow month given the 'month' provided
  113. func (mgo *mgo) MonthNarrow(month time.Month) string {
  114. return mgo.monthsNarrow[month]
  115. }
  116. // MonthsNarrow returns the locales narrow months
  117. func (mgo *mgo) MonthsNarrow() []string {
  118. return mgo.monthsNarrow[1:]
  119. }
  120. // MonthWide returns the locales wide month given the 'month' provided
  121. func (mgo *mgo) MonthWide(month time.Month) string {
  122. return mgo.monthsWide[month]
  123. }
  124. // MonthsWide returns the locales wide months
  125. func (mgo *mgo) MonthsWide() []string {
  126. return mgo.monthsWide[1:]
  127. }
  128. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  129. func (mgo *mgo) WeekdayAbbreviated(weekday time.Weekday) string {
  130. return mgo.daysAbbreviated[weekday]
  131. }
  132. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  133. func (mgo *mgo) WeekdaysAbbreviated() []string {
  134. return mgo.daysAbbreviated
  135. }
  136. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  137. func (mgo *mgo) WeekdayNarrow(weekday time.Weekday) string {
  138. return mgo.daysNarrow[weekday]
  139. }
  140. // WeekdaysNarrow returns the locales narrow weekdays
  141. func (mgo *mgo) WeekdaysNarrow() []string {
  142. return mgo.daysNarrow
  143. }
  144. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  145. func (mgo *mgo) WeekdayShort(weekday time.Weekday) string {
  146. return mgo.daysShort[weekday]
  147. }
  148. // WeekdaysShort returns the locales short weekdays
  149. func (mgo *mgo) WeekdaysShort() []string {
  150. return mgo.daysShort
  151. }
  152. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  153. func (mgo *mgo) WeekdayWide(weekday time.Weekday) string {
  154. return mgo.daysWide[weekday]
  155. }
  156. // WeekdaysWide returns the locales wide weekdays
  157. func (mgo *mgo) WeekdaysWide() []string {
  158. return mgo.daysWide
  159. }
  160. // Decimal returns the decimal point of number
  161. func (mgo *mgo) Decimal() string {
  162. return mgo.decimal
  163. }
  164. // Group returns the group of number
  165. func (mgo *mgo) Group() string {
  166. return mgo.group
  167. }
  168. // Group returns the minus sign of number
  169. func (mgo *mgo) Minus() string {
  170. return mgo.minus
  171. }
  172. // FmtNumber returns 'num' with digits/precision of 'v' for 'mgo' and handles both Whole and Real numbers based on 'v'
  173. func (mgo *mgo) FmtNumber(num float64, v uint64) string {
  174. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  175. l := len(s) + 2 + 1*len(s[:len(s)-int(v)-1])/3
  176. count := 0
  177. inWhole := v == 0
  178. b := make([]byte, 0, l)
  179. for i := len(s) - 1; i >= 0; i-- {
  180. if s[i] == '.' {
  181. b = append(b, mgo.decimal[0])
  182. inWhole = true
  183. continue
  184. }
  185. if inWhole {
  186. if count == 3 {
  187. b = append(b, mgo.group[0])
  188. count = 1
  189. } else {
  190. count++
  191. }
  192. }
  193. b = append(b, s[i])
  194. }
  195. if num < 0 {
  196. b = append(b, mgo.minus[0])
  197. }
  198. // reverse
  199. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  200. b[i], b[j] = b[j], b[i]
  201. }
  202. return string(b)
  203. }
  204. // FmtPercent returns 'num' with digits/precision of 'v' for 'mgo' and handles both Whole and Real numbers based on 'v'
  205. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  206. func (mgo *mgo) FmtPercent(num float64, v uint64) string {
  207. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  208. l := len(s) + 3
  209. b := make([]byte, 0, l)
  210. for i := len(s) - 1; i >= 0; i-- {
  211. if s[i] == '.' {
  212. b = append(b, mgo.decimal[0])
  213. continue
  214. }
  215. b = append(b, s[i])
  216. }
  217. if num < 0 {
  218. b = append(b, mgo.minus[0])
  219. }
  220. // reverse
  221. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  222. b[i], b[j] = b[j], b[i]
  223. }
  224. b = append(b, mgo.percent...)
  225. return string(b)
  226. }
  227. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'mgo'
  228. func (mgo *mgo) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  229. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  230. symbol := mgo.currencies[currency]
  231. l := len(s) + len(symbol) + 4 + 1*len(s[:len(s)-int(v)-1])/3
  232. count := 0
  233. inWhole := v == 0
  234. b := make([]byte, 0, l)
  235. for i := len(s) - 1; i >= 0; i-- {
  236. if s[i] == '.' {
  237. b = append(b, mgo.decimal[0])
  238. inWhole = true
  239. continue
  240. }
  241. if inWhole {
  242. if count == 3 {
  243. b = append(b, mgo.group[0])
  244. count = 1
  245. } else {
  246. count++
  247. }
  248. }
  249. b = append(b, s[i])
  250. }
  251. for j := len(symbol) - 1; j >= 0; j-- {
  252. b = append(b, symbol[j])
  253. }
  254. for j := len(mgo.currencyPositivePrefix) - 1; j >= 0; j-- {
  255. b = append(b, mgo.currencyPositivePrefix[j])
  256. }
  257. if num < 0 {
  258. b = append(b, mgo.minus[0])
  259. }
  260. // reverse
  261. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  262. b[i], b[j] = b[j], b[i]
  263. }
  264. if int(v) < 2 {
  265. if v == 0 {
  266. b = append(b, mgo.decimal...)
  267. }
  268. for i := 0; i < 2-int(v); i++ {
  269. b = append(b, '0')
  270. }
  271. }
  272. return string(b)
  273. }
  274. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'mgo'
  275. // in accounting notation.
  276. func (mgo *mgo) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  277. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  278. symbol := mgo.currencies[currency]
  279. l := len(s) + len(symbol) + 4 + 1*len(s[:len(s)-int(v)-1])/3
  280. count := 0
  281. inWhole := v == 0
  282. b := make([]byte, 0, l)
  283. for i := len(s) - 1; i >= 0; i-- {
  284. if s[i] == '.' {
  285. b = append(b, mgo.decimal[0])
  286. inWhole = true
  287. continue
  288. }
  289. if inWhole {
  290. if count == 3 {
  291. b = append(b, mgo.group[0])
  292. count = 1
  293. } else {
  294. count++
  295. }
  296. }
  297. b = append(b, s[i])
  298. }
  299. if num < 0 {
  300. for j := len(symbol) - 1; j >= 0; j-- {
  301. b = append(b, symbol[j])
  302. }
  303. for j := len(mgo.currencyNegativePrefix) - 1; j >= 0; j-- {
  304. b = append(b, mgo.currencyNegativePrefix[j])
  305. }
  306. b = append(b, mgo.minus[0])
  307. } else {
  308. for j := len(symbol) - 1; j >= 0; j-- {
  309. b = append(b, symbol[j])
  310. }
  311. for j := len(mgo.currencyPositivePrefix) - 1; j >= 0; j-- {
  312. b = append(b, mgo.currencyPositivePrefix[j])
  313. }
  314. }
  315. // reverse
  316. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  317. b[i], b[j] = b[j], b[i]
  318. }
  319. if int(v) < 2 {
  320. if v == 0 {
  321. b = append(b, mgo.decimal...)
  322. }
  323. for i := 0; i < 2-int(v); i++ {
  324. b = append(b, '0')
  325. }
  326. }
  327. return string(b)
  328. }
  329. // FmtDateShort returns the short date representation of 't' for 'mgo'
  330. func (mgo *mgo) FmtDateShort(t time.Time) string {
  331. b := make([]byte, 0, 32)
  332. if t.Year() > 0 {
  333. b = strconv.AppendInt(b, int64(t.Year()), 10)
  334. } else {
  335. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  336. }
  337. b = append(b, []byte{0x2d}...)
  338. if t.Month() < 10 {
  339. b = append(b, '0')
  340. }
  341. b = strconv.AppendInt(b, int64(t.Month()), 10)
  342. b = append(b, []byte{0x2d}...)
  343. if t.Day() < 10 {
  344. b = append(b, '0')
  345. }
  346. b = strconv.AppendInt(b, int64(t.Day()), 10)
  347. return string(b)
  348. }
  349. // FmtDateMedium returns the medium date representation of 't' for 'mgo'
  350. func (mgo *mgo) FmtDateMedium(t time.Time) string {
  351. b := make([]byte, 0, 32)
  352. if t.Year() > 0 {
  353. b = strconv.AppendInt(b, int64(t.Year()), 10)
  354. } else {
  355. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  356. }
  357. b = append(b, []byte{0x20}...)
  358. b = append(b, mgo.monthsAbbreviated[t.Month()]...)
  359. b = append(b, []byte{0x20}...)
  360. b = strconv.AppendInt(b, int64(t.Day()), 10)
  361. return string(b)
  362. }
  363. // FmtDateLong returns the long date representation of 't' for 'mgo'
  364. func (mgo *mgo) FmtDateLong(t time.Time) string {
  365. b := make([]byte, 0, 32)
  366. if t.Year() > 0 {
  367. b = strconv.AppendInt(b, int64(t.Year()), 10)
  368. } else {
  369. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  370. }
  371. b = append(b, []byte{0x20}...)
  372. b = append(b, mgo.monthsWide[t.Month()]...)
  373. b = append(b, []byte{0x20}...)
  374. b = strconv.AppendInt(b, int64(t.Day()), 10)
  375. return string(b)
  376. }
  377. // FmtDateFull returns the full date representation of 't' for 'mgo'
  378. func (mgo *mgo) FmtDateFull(t time.Time) string {
  379. b := make([]byte, 0, 32)
  380. b = append(b, mgo.daysWide[t.Weekday()]...)
  381. b = append(b, []byte{0x2c, 0x20}...)
  382. if t.Year() > 0 {
  383. b = strconv.AppendInt(b, int64(t.Year()), 10)
  384. } else {
  385. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  386. }
  387. b = append(b, []byte{0x20}...)
  388. b = append(b, mgo.monthsWide[t.Month()]...)
  389. b = append(b, []byte{0x20}...)
  390. if t.Day() < 10 {
  391. b = append(b, '0')
  392. }
  393. b = strconv.AppendInt(b, int64(t.Day()), 10)
  394. return string(b)
  395. }
  396. // FmtTimeShort returns the short time representation of 't' for 'mgo'
  397. func (mgo *mgo) FmtTimeShort(t time.Time) string {
  398. b := make([]byte, 0, 32)
  399. if t.Hour() < 10 {
  400. b = append(b, '0')
  401. }
  402. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  403. b = append(b, mgo.timeSeparator...)
  404. if t.Minute() < 10 {
  405. b = append(b, '0')
  406. }
  407. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  408. return string(b)
  409. }
  410. // FmtTimeMedium returns the medium time representation of 't' for 'mgo'
  411. func (mgo *mgo) FmtTimeMedium(t time.Time) string {
  412. b := make([]byte, 0, 32)
  413. if t.Hour() < 10 {
  414. b = append(b, '0')
  415. }
  416. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  417. b = append(b, mgo.timeSeparator...)
  418. if t.Minute() < 10 {
  419. b = append(b, '0')
  420. }
  421. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  422. b = append(b, mgo.timeSeparator...)
  423. if t.Second() < 10 {
  424. b = append(b, '0')
  425. }
  426. b = strconv.AppendInt(b, int64(t.Second()), 10)
  427. return string(b)
  428. }
  429. // FmtTimeLong returns the long time representation of 't' for 'mgo'
  430. func (mgo *mgo) FmtTimeLong(t time.Time) string {
  431. b := make([]byte, 0, 32)
  432. if t.Hour() < 10 {
  433. b = append(b, '0')
  434. }
  435. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  436. b = append(b, mgo.timeSeparator...)
  437. if t.Minute() < 10 {
  438. b = append(b, '0')
  439. }
  440. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  441. b = append(b, mgo.timeSeparator...)
  442. if t.Second() < 10 {
  443. b = append(b, '0')
  444. }
  445. b = strconv.AppendInt(b, int64(t.Second()), 10)
  446. b = append(b, []byte{0x20}...)
  447. tz, _ := t.Zone()
  448. b = append(b, tz...)
  449. return string(b)
  450. }
  451. // FmtTimeFull returns the full time representation of 't' for 'mgo'
  452. func (mgo *mgo) FmtTimeFull(t time.Time) string {
  453. b := make([]byte, 0, 32)
  454. if t.Hour() < 10 {
  455. b = append(b, '0')
  456. }
  457. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  458. b = append(b, mgo.timeSeparator...)
  459. if t.Minute() < 10 {
  460. b = append(b, '0')
  461. }
  462. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  463. b = append(b, mgo.timeSeparator...)
  464. if t.Second() < 10 {
  465. b = append(b, '0')
  466. }
  467. b = strconv.AppendInt(b, int64(t.Second()), 10)
  468. b = append(b, []byte{0x20}...)
  469. tz, _ := t.Zone()
  470. if btz, ok := mgo.timezones[tz]; ok {
  471. b = append(b, btz...)
  472. } else {
  473. b = append(b, tz...)
  474. }
  475. return string(b)
  476. }