mgo.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 ", "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 ", "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{"CLST": "CLST", "WESZ": "WESZ", "HKST": "HKST", "WARST": "WARST", "GFT": "GFT", "WAST": "WAST", "ACST": "ACST", "ART": "ART", "PST": "PST", "MESZ": "MESZ", "UYT": "UYT", "ACWST": "ACWST", "∅∅∅": "∅∅∅", "OESZ": "OESZ", "GMT": "GMT", "EDT": "EDT", "SAST": "SAST", "JST": "JST", "UYST": "UYST", "CAT": "CAT", "ARST": "ARST", "MDT": "MDT", "BT": "BT", "IST": "IST", "SRT": "SRT", "MYT": "MYT", "EAT": "EAT", "AWST": "AWST", "MST": "MST", "EST": "EST", "WIT": "WIT", "WIB": "WIB", "HNT": "HNT", "WITA": "WITA", "ACWDT": "ACWDT", "TMT": "TMT", "AWDT": "AWDT", "VET": "VET", "AEST": "AEST", "ADT": "ADT", "AKDT": "AKDT", "GYT": "GYT", "LHDT": "LHDT", "COST": "COST", "ECT": "ECT", "HADT": "HADT", "SGT": "SGT", "HAT": "HAT", "JDT": "JDT", "ChST": "ChST", "HAST": "HAST", "PDT": "PDT", "LHST": "LHST", "MEZ": "MEZ", "NZDT": "NZDT", "AEDT": "AEDT", "WART": "WART", "TMST": "TMST", "AKST": "AKST", "CST": "CST", "NZST": "NZST", "WAT": "WAT", "BOT": "BOT", "CLT": "CLT", "WEZ": "WEZ", "CHAST": "CHAST", "CHADT": "CHADT", "AST": "AST", "CDT": "CDT", "COT": "COT", "HKT": "HKT", "OEZ": "OEZ", "ACDT": "ACDT"},
  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. // FmtNumber returns 'num' with digits/precision of 'v' for 'mgo' and handles both Whole and Real numbers based on 'v'
  161. func (mgo *mgo) FmtNumber(num float64, v uint64) string {
  162. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  163. l := len(s) + 2 + 1*len(s[:len(s)-int(v)-1])/3
  164. count := 0
  165. inWhole := v == 0
  166. b := make([]byte, 0, l)
  167. for i := len(s) - 1; i >= 0; i-- {
  168. if s[i] == '.' {
  169. b = append(b, mgo.decimal[0])
  170. inWhole = true
  171. continue
  172. }
  173. if inWhole {
  174. if count == 3 {
  175. b = append(b, mgo.group[0])
  176. count = 1
  177. } else {
  178. count++
  179. }
  180. }
  181. b = append(b, s[i])
  182. }
  183. if num < 0 {
  184. b = append(b, mgo.minus[0])
  185. }
  186. // reverse
  187. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  188. b[i], b[j] = b[j], b[i]
  189. }
  190. return string(b)
  191. }
  192. // FmtPercent returns 'num' with digits/precision of 'v' for 'mgo' and handles both Whole and Real numbers based on 'v'
  193. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  194. func (mgo *mgo) FmtPercent(num float64, v uint64) string {
  195. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  196. l := len(s) + 3
  197. b := make([]byte, 0, l)
  198. for i := len(s) - 1; i >= 0; i-- {
  199. if s[i] == '.' {
  200. b = append(b, mgo.decimal[0])
  201. continue
  202. }
  203. b = append(b, s[i])
  204. }
  205. if num < 0 {
  206. b = append(b, mgo.minus[0])
  207. }
  208. // reverse
  209. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  210. b[i], b[j] = b[j], b[i]
  211. }
  212. b = append(b, mgo.percent...)
  213. return string(b)
  214. }
  215. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'mgo'
  216. func (mgo *mgo) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  217. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  218. symbol := mgo.currencies[currency]
  219. l := len(s) + len(symbol) + 4 + 1*len(s[:len(s)-int(v)-1])/3
  220. count := 0
  221. inWhole := v == 0
  222. b := make([]byte, 0, l)
  223. for i := len(s) - 1; i >= 0; i-- {
  224. if s[i] == '.' {
  225. b = append(b, mgo.decimal[0])
  226. inWhole = true
  227. continue
  228. }
  229. if inWhole {
  230. if count == 3 {
  231. b = append(b, mgo.group[0])
  232. count = 1
  233. } else {
  234. count++
  235. }
  236. }
  237. b = append(b, s[i])
  238. }
  239. for j := len(symbol) - 1; j >= 0; j-- {
  240. b = append(b, symbol[j])
  241. }
  242. for j := len(mgo.currencyPositivePrefix) - 1; j >= 0; j-- {
  243. b = append(b, mgo.currencyPositivePrefix[j])
  244. }
  245. if num < 0 {
  246. b = append(b, mgo.minus[0])
  247. }
  248. // reverse
  249. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  250. b[i], b[j] = b[j], b[i]
  251. }
  252. if int(v) < 2 {
  253. if v == 0 {
  254. b = append(b, mgo.decimal...)
  255. }
  256. for i := 0; i < 2-int(v); i++ {
  257. b = append(b, '0')
  258. }
  259. }
  260. return string(b)
  261. }
  262. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'mgo'
  263. // in accounting notation.
  264. func (mgo *mgo) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  265. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  266. symbol := mgo.currencies[currency]
  267. l := len(s) + len(symbol) + 4 + 1*len(s[:len(s)-int(v)-1])/3
  268. count := 0
  269. inWhole := v == 0
  270. b := make([]byte, 0, l)
  271. for i := len(s) - 1; i >= 0; i-- {
  272. if s[i] == '.' {
  273. b = append(b, mgo.decimal[0])
  274. inWhole = true
  275. continue
  276. }
  277. if inWhole {
  278. if count == 3 {
  279. b = append(b, mgo.group[0])
  280. count = 1
  281. } else {
  282. count++
  283. }
  284. }
  285. b = append(b, s[i])
  286. }
  287. if num < 0 {
  288. for j := len(symbol) - 1; j >= 0; j-- {
  289. b = append(b, symbol[j])
  290. }
  291. for j := len(mgo.currencyNegativePrefix) - 1; j >= 0; j-- {
  292. b = append(b, mgo.currencyNegativePrefix[j])
  293. }
  294. b = append(b, mgo.minus[0])
  295. } else {
  296. for j := len(symbol) - 1; j >= 0; j-- {
  297. b = append(b, symbol[j])
  298. }
  299. for j := len(mgo.currencyPositivePrefix) - 1; j >= 0; j-- {
  300. b = append(b, mgo.currencyPositivePrefix[j])
  301. }
  302. }
  303. // reverse
  304. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  305. b[i], b[j] = b[j], b[i]
  306. }
  307. if int(v) < 2 {
  308. if v == 0 {
  309. b = append(b, mgo.decimal...)
  310. }
  311. for i := 0; i < 2-int(v); i++ {
  312. b = append(b, '0')
  313. }
  314. }
  315. return string(b)
  316. }
  317. // FmtDateShort returns the short date representation of 't' for 'mgo'
  318. func (mgo *mgo) FmtDateShort(t time.Time) string {
  319. b := make([]byte, 0, 32)
  320. b = strconv.AppendInt(b, int64(t.Year()), 10)
  321. b = append(b, []byte{0x2d}...)
  322. if t.Month() < 10 {
  323. b = append(b, '0')
  324. }
  325. b = strconv.AppendInt(b, int64(t.Month()), 10)
  326. b = append(b, []byte{0x2d}...)
  327. if t.Day() < 10 {
  328. b = append(b, '0')
  329. }
  330. b = strconv.AppendInt(b, int64(t.Day()), 10)
  331. return string(b)
  332. }
  333. // FmtDateMedium returns the medium date representation of 't' for 'mgo'
  334. func (mgo *mgo) FmtDateMedium(t time.Time) string {
  335. b := make([]byte, 0, 32)
  336. b = strconv.AppendInt(b, int64(t.Year()), 10)
  337. b = append(b, []byte{0x20}...)
  338. b = append(b, mgo.monthsAbbreviated[t.Month()]...)
  339. b = append(b, []byte{0x20}...)
  340. b = strconv.AppendInt(b, int64(t.Day()), 10)
  341. return string(b)
  342. }
  343. // FmtDateLong returns the long date representation of 't' for 'mgo'
  344. func (mgo *mgo) FmtDateLong(t time.Time) string {
  345. b := make([]byte, 0, 32)
  346. b = strconv.AppendInt(b, int64(t.Year()), 10)
  347. b = append(b, []byte{0x20}...)
  348. b = append(b, mgo.monthsWide[t.Month()]...)
  349. b = append(b, []byte{0x20}...)
  350. b = strconv.AppendInt(b, int64(t.Day()), 10)
  351. return string(b)
  352. }
  353. // FmtDateFull returns the full date representation of 't' for 'mgo'
  354. func (mgo *mgo) FmtDateFull(t time.Time) string {
  355. b := make([]byte, 0, 32)
  356. b = append(b, mgo.daysWide[t.Weekday()]...)
  357. b = append(b, []byte{0x2c, 0x20}...)
  358. b = strconv.AppendInt(b, int64(t.Year()), 10)
  359. b = append(b, []byte{0x20}...)
  360. b = append(b, mgo.monthsWide[t.Month()]...)
  361. b = append(b, []byte{0x20}...)
  362. if t.Day() < 10 {
  363. b = append(b, '0')
  364. }
  365. b = strconv.AppendInt(b, int64(t.Day()), 10)
  366. return string(b)
  367. }
  368. // FmtTimeShort returns the short time representation of 't' for 'mgo'
  369. func (mgo *mgo) FmtTimeShort(t time.Time) string {
  370. b := make([]byte, 0, 32)
  371. if t.Hour() < 10 {
  372. b = append(b, '0')
  373. }
  374. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  375. b = append(b, mgo.timeSeparator...)
  376. if t.Minute() < 10 {
  377. b = append(b, '0')
  378. }
  379. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  380. return string(b)
  381. }
  382. // FmtTimeMedium returns the medium time representation of 't' for 'mgo'
  383. func (mgo *mgo) FmtTimeMedium(t time.Time) string {
  384. b := make([]byte, 0, 32)
  385. if t.Hour() < 10 {
  386. b = append(b, '0')
  387. }
  388. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  389. b = append(b, mgo.timeSeparator...)
  390. if t.Minute() < 10 {
  391. b = append(b, '0')
  392. }
  393. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  394. b = append(b, mgo.timeSeparator...)
  395. if t.Second() < 10 {
  396. b = append(b, '0')
  397. }
  398. b = strconv.AppendInt(b, int64(t.Second()), 10)
  399. return string(b)
  400. }
  401. // FmtTimeLong returns the long time representation of 't' for 'mgo'
  402. func (mgo *mgo) FmtTimeLong(t time.Time) string {
  403. b := make([]byte, 0, 32)
  404. if t.Hour() < 10 {
  405. b = append(b, '0')
  406. }
  407. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  408. b = append(b, mgo.timeSeparator...)
  409. if t.Minute() < 10 {
  410. b = append(b, '0')
  411. }
  412. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  413. b = append(b, mgo.timeSeparator...)
  414. if t.Second() < 10 {
  415. b = append(b, '0')
  416. }
  417. b = strconv.AppendInt(b, int64(t.Second()), 10)
  418. b = append(b, []byte{0x20}...)
  419. tz, _ := t.Zone()
  420. b = append(b, tz...)
  421. return string(b)
  422. }
  423. // FmtTimeFull returns the full time representation of 't' for 'mgo'
  424. func (mgo *mgo) FmtTimeFull(t time.Time) string {
  425. b := make([]byte, 0, 32)
  426. if t.Hour() < 10 {
  427. b = append(b, '0')
  428. }
  429. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  430. b = append(b, mgo.timeSeparator...)
  431. if t.Minute() < 10 {
  432. b = append(b, '0')
  433. }
  434. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  435. b = append(b, mgo.timeSeparator...)
  436. if t.Second() < 10 {
  437. b = append(b, '0')
  438. }
  439. b = strconv.AppendInt(b, int64(t.Second()), 10)
  440. b = append(b, []byte{0x20}...)
  441. tz, _ := t.Zone()
  442. if btz, ok := mgo.timezones[tz]; ok {
  443. b = append(b, btz...)
  444. } else {
  445. b = append(b, tz...)
  446. }
  447. return string(b)
  448. }