or.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. package or
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type or 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 'or' locale
  41. func New() locales.Translator {
  42. return &or{
  43. locale: "or",
  44. pluralsCardinal: []locales.PluralRule{2, 6},
  45. pluralsOrdinal: nil,
  46. pluralsRange: nil,
  47. decimal: ".",
  48. group: ",",
  49. timeSeparator: ":",
  50. 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 "},
  51. currencyPositivePrefix: " ",
  52. currencyNegativePrefix: " ",
  53. monthsAbbreviated: []string{"", "ଜାନୁଆରୀ", "ଫେବୃଆରୀ", "ମାର୍ଚ୍ଚ", "ଅପ୍ରେଲ", "ମଇ", "ଜୁନ", "ଜୁଲାଇ", "ଅଗଷ୍ଟ", "ସେପ୍ଟେମ୍ବର", "ଅକ୍ଟୋବର", "ନଭେମ୍ବର", "ଡିସେମ୍ବର"},
  54. monthsNarrow: []string{"", "ଜା", "ଫେ", "ମା", "ଅ", "ମଇ", "ଜୁ", "ଜୁ", "ଅ", "ସେ", "ଅ", "ନ", "ଡି"},
  55. monthsWide: []string{"", "ଜାନୁଆରୀ", "ଫେବୃଆରୀ", "ମାର୍ଚ୍ଚ", "ଅପ୍ରେଲ", "ମଇ", "ଜୁନ", "ଜୁଲାଇ", "ଅଗଷ୍ଟ", "ସେପ୍ଟେମ୍ବର", "ଅକ୍ଟୋବର", "ନଭେମ୍ବର", "ଡିସେମ୍ବର"},
  56. daysAbbreviated: []string{"ରବି", "ସୋମ", "ମଙ୍ଗଳ", "ବୁଧ", "ଗୁରୁ", "ଶୁକ୍ର", "ଶନି"},
  57. daysNarrow: []string{"ର", "ସୋ", "ମ", "ବୁ", "ଗୁ", "ଶୁ", "ଶ"},
  58. daysWide: []string{"ରବିବାର", "ସୋମବାର", "ମଙ୍ଗଳବାର", "ବୁଧବାର", "ଗୁରୁବାର", "ଶୁକ୍ରବାର", "ଶନିବାର"},
  59. periodsAbbreviated: []string{"am", "pm"},
  60. periodsNarrow: []string{"am", "pm"},
  61. periodsWide: []string{"am", "pm"},
  62. timezones: map[string]string{"UYST": "UYST", "EDT": "EDT", "OEZ": "OEZ", "VET": "VET", "SGT": "SGT", "BT": "BT", "ADT": "ADT", "JST": "JST", "HKT": "HKT", "AWDT": "AWDT", "PDT": "PDT", "HAST": "HAST", "WIT": "WIT", "ARST": "ARST", "TMT": "TMT", "EAT": "EAT", "LHDT": "LHDT", "GYT": "GYT", "NZDT": "NZDT", "GFT": "GFT", "OESZ": "OESZ", "ACWST": "ACWST", "CDT": "CDT", "CLT": "CLT", "SRT": "SRT", "MESZ": "MESZ", "ChST": "ChST", "CLST": "CLST", "WEZ": "WEZ", "AST": "AST", "WAST": "WAST", "UYT": "UYT", "ECT": "ECT", "CAT": "CAT", "SAST": "SAST", "AKST": "AKST", "ACST": "ACST", "COST": "COST", "JDT": "JDT", "PST": "PST", "EST": "EST", "HADT": "HADT", "AWST": "AWST", "HNT": "HNT", "WITA": "WITA", "AKDT": "AKDT", "WESZ": "WESZ", "COT": "COT", "WAT": "WAT", "GMT": "GMT", "TMST": "TMST", "AEST": "AEST", "WARST": "WARST", "HAT": "HAT", "WIB": "WIB", "ART": "ART", "MDT": "MDT", "AEDT": "AEDT", "HKST": "HKST", "WART": "WART", "CHADT": "CHADT", "MST": "MST", "LHST": "LHST", "ACWDT": "ACWDT", "∅∅∅": "∅∅∅", "IST": "IST", "BOT": "BOT", "CST": "CST", "NZST": "NZST", "MEZ": "MEZ", "CHAST": "CHAST", "ACDT": "ACDT", "MYT": "MYT"},
  63. }
  64. }
  65. // Locale returns the current translators string locale
  66. func (or *or) Locale() string {
  67. return or.locale
  68. }
  69. // PluralsCardinal returns the list of cardinal plural rules associated with 'or'
  70. func (or *or) PluralsCardinal() []locales.PluralRule {
  71. return or.pluralsCardinal
  72. }
  73. // PluralsOrdinal returns the list of ordinal plural rules associated with 'or'
  74. func (or *or) PluralsOrdinal() []locales.PluralRule {
  75. return or.pluralsOrdinal
  76. }
  77. // PluralsRange returns the list of range plural rules associated with 'or'
  78. func (or *or) PluralsRange() []locales.PluralRule {
  79. return or.pluralsRange
  80. }
  81. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'or'
  82. func (or *or) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  83. n := math.Abs(num)
  84. if n == 1 {
  85. return locales.PluralRuleOne
  86. }
  87. return locales.PluralRuleOther
  88. }
  89. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'or'
  90. func (or *or) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  91. return locales.PluralRuleUnknown
  92. }
  93. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'or'
  94. func (or *or) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  95. return locales.PluralRuleUnknown
  96. }
  97. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  98. func (or *or) MonthAbbreviated(month time.Month) string {
  99. return or.monthsAbbreviated[month]
  100. }
  101. // MonthsAbbreviated returns the locales abbreviated months
  102. func (or *or) MonthsAbbreviated() []string {
  103. return or.monthsAbbreviated[1:]
  104. }
  105. // MonthNarrow returns the locales narrow month given the 'month' provided
  106. func (or *or) MonthNarrow(month time.Month) string {
  107. return or.monthsNarrow[month]
  108. }
  109. // MonthsNarrow returns the locales narrow months
  110. func (or *or) MonthsNarrow() []string {
  111. return or.monthsNarrow[1:]
  112. }
  113. // MonthWide returns the locales wide month given the 'month' provided
  114. func (or *or) MonthWide(month time.Month) string {
  115. return or.monthsWide[month]
  116. }
  117. // MonthsWide returns the locales wide months
  118. func (or *or) MonthsWide() []string {
  119. return or.monthsWide[1:]
  120. }
  121. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  122. func (or *or) WeekdayAbbreviated(weekday time.Weekday) string {
  123. return or.daysAbbreviated[weekday]
  124. }
  125. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  126. func (or *or) WeekdaysAbbreviated() []string {
  127. return or.daysAbbreviated
  128. }
  129. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  130. func (or *or) WeekdayNarrow(weekday time.Weekday) string {
  131. return or.daysNarrow[weekday]
  132. }
  133. // WeekdaysNarrow returns the locales narrow weekdays
  134. func (or *or) WeekdaysNarrow() []string {
  135. return or.daysNarrow
  136. }
  137. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  138. func (or *or) WeekdayShort(weekday time.Weekday) string {
  139. return or.daysShort[weekday]
  140. }
  141. // WeekdaysShort returns the locales short weekdays
  142. func (or *or) WeekdaysShort() []string {
  143. return or.daysShort
  144. }
  145. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  146. func (or *or) WeekdayWide(weekday time.Weekday) string {
  147. return or.daysWide[weekday]
  148. }
  149. // WeekdaysWide returns the locales wide weekdays
  150. func (or *or) WeekdaysWide() []string {
  151. return or.daysWide
  152. }
  153. // FmtNumber returns 'num' with digits/precision of 'v' for 'or' and handles both Whole and Real numbers based on 'v'
  154. func (or *or) FmtNumber(num float64, v uint64) string {
  155. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  156. l := len(s) + 1 + 1*len(s[:len(s)-int(v)-1])/3
  157. count := 0
  158. inWhole := v == 0
  159. inSecondary := false
  160. groupThreshold := 3
  161. b := make([]byte, 0, l)
  162. for i := len(s) - 1; i >= 0; i-- {
  163. if s[i] == '.' {
  164. b = append(b, or.decimal[0])
  165. inWhole = true
  166. continue
  167. }
  168. if inWhole {
  169. if count == groupThreshold {
  170. b = append(b, or.group[0])
  171. count = 1
  172. if !inSecondary {
  173. inSecondary = true
  174. groupThreshold = 2
  175. }
  176. } else {
  177. count++
  178. }
  179. }
  180. b = append(b, s[i])
  181. }
  182. if num < 0 {
  183. b = append(b, or.minus[0])
  184. }
  185. // reverse
  186. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  187. b[i], b[j] = b[j], b[i]
  188. }
  189. return string(b)
  190. }
  191. // FmtPercent returns 'num' with digits/precision of 'v' for 'or' and handles both Whole and Real numbers based on 'v'
  192. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  193. func (or *or) FmtPercent(num float64, v uint64) string {
  194. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  195. l := len(s) + 1
  196. b := make([]byte, 0, l)
  197. for i := len(s) - 1; i >= 0; i-- {
  198. if s[i] == '.' {
  199. b = append(b, or.decimal[0])
  200. continue
  201. }
  202. b = append(b, s[i])
  203. }
  204. if num < 0 {
  205. b = append(b, or.minus[0])
  206. }
  207. // reverse
  208. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  209. b[i], b[j] = b[j], b[i]
  210. }
  211. b = append(b, or.percent...)
  212. return string(b)
  213. }
  214. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'or'
  215. func (or *or) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  216. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  217. symbol := or.currencies[currency]
  218. l := len(s) + len(symbol) + 3 + 1*len(s[:len(s)-int(v)-1])/3
  219. count := 0
  220. inWhole := v == 0
  221. inSecondary := false
  222. groupThreshold := 3
  223. b := make([]byte, 0, l)
  224. for i := len(s) - 1; i >= 0; i-- {
  225. if s[i] == '.' {
  226. b = append(b, or.decimal[0])
  227. inWhole = true
  228. continue
  229. }
  230. if inWhole {
  231. if count == groupThreshold {
  232. b = append(b, or.group[0])
  233. count = 1
  234. if !inSecondary {
  235. inSecondary = true
  236. groupThreshold = 2
  237. }
  238. } else {
  239. count++
  240. }
  241. }
  242. b = append(b, s[i])
  243. }
  244. for j := len(symbol) - 1; j >= 0; j-- {
  245. b = append(b, symbol[j])
  246. }
  247. for j := len(or.currencyPositivePrefix) - 1; j >= 0; j-- {
  248. b = append(b, or.currencyPositivePrefix[j])
  249. }
  250. if num < 0 {
  251. b = append(b, or.minus[0])
  252. }
  253. // reverse
  254. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  255. b[i], b[j] = b[j], b[i]
  256. }
  257. if int(v) < 2 {
  258. if v == 0 {
  259. b = append(b, or.decimal...)
  260. }
  261. for i := 0; i < 2-int(v); i++ {
  262. b = append(b, '0')
  263. }
  264. }
  265. return string(b)
  266. }
  267. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'or'
  268. // in accounting notation.
  269. func (or *or) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  270. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  271. symbol := or.currencies[currency]
  272. l := len(s) + len(symbol) + 3 + 1*len(s[:len(s)-int(v)-1])/3
  273. count := 0
  274. inWhole := v == 0
  275. inSecondary := false
  276. groupThreshold := 3
  277. b := make([]byte, 0, l)
  278. for i := len(s) - 1; i >= 0; i-- {
  279. if s[i] == '.' {
  280. b = append(b, or.decimal[0])
  281. inWhole = true
  282. continue
  283. }
  284. if inWhole {
  285. if count == groupThreshold {
  286. b = append(b, or.group[0])
  287. count = 1
  288. if !inSecondary {
  289. inSecondary = true
  290. groupThreshold = 2
  291. }
  292. } else {
  293. count++
  294. }
  295. }
  296. b = append(b, s[i])
  297. }
  298. if num < 0 {
  299. for j := len(symbol) - 1; j >= 0; j-- {
  300. b = append(b, symbol[j])
  301. }
  302. for j := len(or.currencyNegativePrefix) - 1; j >= 0; j-- {
  303. b = append(b, or.currencyNegativePrefix[j])
  304. }
  305. b = append(b, or.minus[0])
  306. } else {
  307. for j := len(symbol) - 1; j >= 0; j-- {
  308. b = append(b, symbol[j])
  309. }
  310. for j := len(or.currencyPositivePrefix) - 1; j >= 0; j-- {
  311. b = append(b, or.currencyPositivePrefix[j])
  312. }
  313. }
  314. // reverse
  315. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  316. b[i], b[j] = b[j], b[i]
  317. }
  318. if int(v) < 2 {
  319. if v == 0 {
  320. b = append(b, or.decimal...)
  321. }
  322. for i := 0; i < 2-int(v); i++ {
  323. b = append(b, '0')
  324. }
  325. }
  326. return string(b)
  327. }
  328. // FmtDateShort returns the short date representation of 't' for 'or'
  329. func (or *or) FmtDateShort(t time.Time) string {
  330. b := make([]byte, 0, 32)
  331. b = strconv.AppendInt(b, int64(t.Day()), 10)
  332. b = append(b, []byte{0x2d}...)
  333. b = strconv.AppendInt(b, int64(t.Month()), 10)
  334. b = append(b, []byte{0x2d}...)
  335. if t.Year() > 9 {
  336. b = append(b, strconv.Itoa(t.Year())[2:]...)
  337. } else {
  338. b = append(b, strconv.Itoa(t.Year())[1:]...)
  339. }
  340. return string(b)
  341. }
  342. // FmtDateMedium returns the medium date representation of 't' for 'or'
  343. func (or *or) FmtDateMedium(t time.Time) string {
  344. b := make([]byte, 0, 32)
  345. b = strconv.AppendInt(b, int64(t.Day()), 10)
  346. b = append(b, []byte{0x20}...)
  347. b = append(b, or.monthsAbbreviated[t.Month()]...)
  348. b = append(b, []byte{0x20}...)
  349. b = strconv.AppendInt(b, int64(t.Year()), 10)
  350. return string(b)
  351. }
  352. // FmtDateLong returns the long date representation of 't' for 'or'
  353. func (or *or) FmtDateLong(t time.Time) string {
  354. b := make([]byte, 0, 32)
  355. b = strconv.AppendInt(b, int64(t.Day()), 10)
  356. b = append(b, []byte{0x20}...)
  357. b = append(b, or.monthsWide[t.Month()]...)
  358. b = append(b, []byte{0x20}...)
  359. b = strconv.AppendInt(b, int64(t.Year()), 10)
  360. return string(b)
  361. }
  362. // FmtDateFull returns the full date representation of 't' for 'or'
  363. func (or *or) FmtDateFull(t time.Time) string {
  364. b := make([]byte, 0, 32)
  365. b = append(b, or.daysWide[t.Weekday()]...)
  366. b = append(b, []byte{0x2c, 0x20}...)
  367. b = strconv.AppendInt(b, int64(t.Day()), 10)
  368. b = append(b, []byte{0x20}...)
  369. b = append(b, or.monthsWide[t.Month()]...)
  370. b = append(b, []byte{0x20}...)
  371. b = strconv.AppendInt(b, int64(t.Year()), 10)
  372. return string(b)
  373. }
  374. // FmtTimeShort returns the short time representation of 't' for 'or'
  375. func (or *or) FmtTimeShort(t time.Time) string {
  376. b := make([]byte, 0, 32)
  377. h := t.Hour()
  378. if h > 12 {
  379. h -= 12
  380. }
  381. b = strconv.AppendInt(b, int64(h), 10)
  382. b = append(b, or.timeSeparator...)
  383. if t.Minute() < 10 {
  384. b = append(b, '0')
  385. }
  386. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  387. b = append(b, []byte{0x20}...)
  388. if t.Hour() < 12 {
  389. b = append(b, or.periodsAbbreviated[0]...)
  390. } else {
  391. b = append(b, or.periodsAbbreviated[1]...)
  392. }
  393. return string(b)
  394. }
  395. // FmtTimeMedium returns the medium time representation of 't' for 'or'
  396. func (or *or) FmtTimeMedium(t time.Time) string {
  397. b := make([]byte, 0, 32)
  398. h := t.Hour()
  399. if h > 12 {
  400. h -= 12
  401. }
  402. b = strconv.AppendInt(b, int64(h), 10)
  403. b = append(b, or.timeSeparator...)
  404. if t.Minute() < 10 {
  405. b = append(b, '0')
  406. }
  407. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  408. b = append(b, or.timeSeparator...)
  409. if t.Second() < 10 {
  410. b = append(b, '0')
  411. }
  412. b = strconv.AppendInt(b, int64(t.Second()), 10)
  413. b = append(b, []byte{0x20}...)
  414. if t.Hour() < 12 {
  415. b = append(b, or.periodsAbbreviated[0]...)
  416. } else {
  417. b = append(b, or.periodsAbbreviated[1]...)
  418. }
  419. return string(b)
  420. }
  421. // FmtTimeLong returns the long time representation of 't' for 'or'
  422. func (or *or) FmtTimeLong(t time.Time) string {
  423. b := make([]byte, 0, 32)
  424. h := t.Hour()
  425. if h > 12 {
  426. h -= 12
  427. }
  428. b = strconv.AppendInt(b, int64(h), 10)
  429. b = append(b, or.timeSeparator...)
  430. if t.Minute() < 10 {
  431. b = append(b, '0')
  432. }
  433. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  434. b = append(b, or.timeSeparator...)
  435. if t.Second() < 10 {
  436. b = append(b, '0')
  437. }
  438. b = strconv.AppendInt(b, int64(t.Second()), 10)
  439. b = append(b, []byte{0x20}...)
  440. if t.Hour() < 12 {
  441. b = append(b, or.periodsAbbreviated[0]...)
  442. } else {
  443. b = append(b, or.periodsAbbreviated[1]...)
  444. }
  445. b = append(b, []byte{0x20}...)
  446. tz, _ := t.Zone()
  447. b = append(b, tz...)
  448. return string(b)
  449. }
  450. // FmtTimeFull returns the full time representation of 't' for 'or'
  451. func (or *or) FmtTimeFull(t time.Time) string {
  452. b := make([]byte, 0, 32)
  453. h := t.Hour()
  454. if h > 12 {
  455. h -= 12
  456. }
  457. b = strconv.AppendInt(b, int64(h), 10)
  458. b = append(b, or.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, or.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. if t.Hour() < 12 {
  470. b = append(b, or.periodsAbbreviated[0]...)
  471. } else {
  472. b = append(b, or.periodsAbbreviated[1]...)
  473. }
  474. b = append(b, []byte{0x20}...)
  475. tz, _ := t.Zone()
  476. if btz, ok := or.timezones[tz]; ok {
  477. b = append(b, btz...)
  478. } else {
  479. b = append(b, tz...)
  480. }
  481. return string(b)
  482. }