hi.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. package hi
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type hi 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. monthsAbbreviated []string
  23. monthsNarrow []string
  24. monthsWide []string
  25. daysAbbreviated []string
  26. daysNarrow []string
  27. daysShort []string
  28. daysWide []string
  29. periodsAbbreviated []string
  30. periodsNarrow []string
  31. periodsShort []string
  32. periodsWide []string
  33. erasAbbreviated []string
  34. erasNarrow []string
  35. erasWide []string
  36. timezones map[string]string
  37. }
  38. // New returns a new instance of translator for the 'hi' locale
  39. func New() locales.Translator {
  40. return &hi{
  41. locale: "hi",
  42. pluralsCardinal: []locales.PluralRule{2, 6},
  43. pluralsOrdinal: []locales.PluralRule{2, 3, 4, 5, 6},
  44. pluralsRange: []locales.PluralRule{2, 6},
  45. decimal: ".",
  46. group: ",",
  47. minus: "-",
  48. percent: "%",
  49. perMille: "‰",
  50. timeSeparator: ":",
  51. inifinity: "∞",
  52. 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", "BYN", "BYR", "BZD", "CA$", "CDF", "CHE", "CHF", "CHW", "CLE", "CLF", "CLP", "CNH", "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", "JP¥", "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", "STN", "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"},
  53. monthsAbbreviated: []string{"", "जन॰", "फ़र॰", "मार्च", "अप्रैल", "मई", "जून", "जुल॰", "अग॰", "सित॰", "अक्तू॰", "नव॰", "दिस॰"},
  54. monthsNarrow: []string{"", "ज", "फ़", "मा", "अ", "म", "जू", "जु", "अ", "सि", "अ", "न", "दि"},
  55. monthsWide: []string{"", "जनवरी", "फ़रवरी", "मार्च", "अप्रैल", "मई", "जून", "जुलाई", "अगस्त", "सितंबर", "अक्तूबर", "नवंबर", "दिसंबर"},
  56. daysAbbreviated: []string{"रवि", "सोम", "मंगल", "बुध", "गुरु", "शुक्र", "शनि"},
  57. daysNarrow: []string{"र", "सो", "मं", "बु", "गु", "शु", "श"},
  58. daysShort: []string{"र", "सो", "मं", "बु", "गु", "शु", "श"},
  59. daysWide: []string{"रविवार", "सोमवार", "मंगलवार", "बुधवार", "गुरुवार", "शुक्रवार", "शनिवार"},
  60. periodsAbbreviated: []string{"पूर्वाह्न", "अपराह्न"},
  61. periodsNarrow: []string{"पू", "अ"},
  62. periodsWide: []string{"पूर्वाह्न", "अपराह्न"},
  63. erasAbbreviated: []string{"ईसा-पूर्व", "ईस्वी"},
  64. erasNarrow: []string{"", ""},
  65. erasWide: []string{"ईसा-पूर्व", "ईसवी सन"},
  66. timezones: map[string]string{"SGT": "सिंगापुर समय", "IST": "भारतीय मानक समय", "HNT": "न्यूफ़ाउंडलैंड मानक समय", "ChST": "चामोरो मानक समय", "HECU": "क्यूबा डेलाइट समय", "MST": "उत्तरी अमेरिकी माउंटेन मानक समय", "BOT": "बोलीविया समय", "AKST": "अलास्\u200dका मानक समय", "CST": "उत्तरी अमेरिकी केंद्रीय मानक समय", "PDT": "उत्तरी अमेरिकी प्रशांत डेलाइट समय", "HKT": "हाँग काँग मानक समय", "UYT": "उरुग्वे मानक समय", "ART": "अर्जेंटीना मानक समय", "COST": "कोलंबिया ग्रीष्मकालीन समय", "WIB": "पश्चिमी इंडोनेशिया समय", "ACWST": "ऑस्\u200dट्रेलियाई केंद्रीय पश्चिमी मानक समय", "HAST": "हवाई–आल्यूशन मानक समय", "ECT": "इक्वाडोर समय", "EST": "उत्तरी अमेरिकी पूर्वी मानक समय", "WITA": "मध्य इंडोनेशिया समय", "CDT": "उत्तरी अमेरिकी केंद्रीय डेलाइट समय", "WESZ": "पश्चिमी यूरोपीय ग्रीष्\u200dमकालीन समय", "NZST": "न्यूज़ीलैंड मानक समय", "NZDT": "न्यूज़ीलैंड डेलाइट समय", "JDT": "जापान डेलाइट समय", "HNOG": "पश्चिमी ग्रीनलैंड मानक समय", "WART": "पश्चिमी अर्जेंटीना मानक समय", "VET": "वेनेज़ुएला समय", "CLST": "चिली ग्रीष्मकालीन समय", "ADT": "अटलांटिक डेलाइट समय", "WAST": "पश्चिम अफ़्रीका ग्रीष्मकालीन समय", "WEZ": "पश्चिमी यूरोपीय मानक समय", "OESZ": "पूर्वी यूरोपीय ग्रीष्मकालीन समय", "MDT": "उत्तरी अमेरिकी माउंटेन डेलाइट समय", "TMT": "तुर्कमेनिस्तान मानक समय", "TMST": "तुर्कमेनिस्तान ग्रीष्मकालीन समय", "PST": "उत्तरी अमेरिकी प्रशांत मानक समय", "HKST": "हाँग काँग ग्रीष्मकालीन समय", "OEZ": "पूर्वी यूरोपीय मानक समय", "HADT": "हवाई–आल्यूशन डेलाइट समय", "MESZ": "मध्\u200dय यूरोपीय ग्रीष्\u200dमकालीन समय", "HEPM": "सेंट पिएरे और मिक्वेलान डेलाइट समय", "HNCU": "क्यूबा मानक समय", "AWDT": "ऑस्ट्रेलियाई पश्चिमी डेलाइट समय", "WAT": "पश्चिम अफ़्रीका मानक समय", "∅∅∅": "∅∅∅", "AKDT": "अलास्\u200dका डेलाइट समय", "CAT": "मध्य अफ़्रीका समय", "CHAST": "चैथम मानक समय", "GFT": "फ़्रेंच गुयाना समय", "ACDT": "ऑस्\u200dट्रेलियाई केंद्रीय डेलाइट समय", "HNPM": "सेंट पिएरे और मिक्वेलान मानक समय", "JST": "जापान मानक समय", "ARST": "अर्जेंटीना ग्रीष्मकालीन समय", "HEEG": "पूर्वी ग्रीनलैंड ग्रीष्मकालीन समय", "WARST": "पश्चिमी अर्जेंटीना ग्रीष्मकालीन समय", "EAT": "पूर्वी अफ़्रीका समय", "GMT": "ग्रीनविच मीन टाइम", "AEDT": "ऑस्\u200dट्रेलियाई पूर्वी डेलाइट समय", "SAST": "दक्षिण अफ़्रीका मानक समय", "EDT": "उत्तरी अमेरिकी पूर्वी डेलाइट समय", "GYT": "गुयाना समय", "UYST": "उरुग्वे ग्रीष्मकालीन समय", "HNPMX": "मेक्सिकन प्रशांत मानक समय", "BT": "भूटान समय", "HEOG": "पश्चिमी ग्रीनलैंड ग्रीष्मकालीन समय", "WIT": "पूर्वी इंडोनेशिया समय", "SRT": "सूरीनाम समय", "COT": "कोलंबिया मानक समय", "CHADT": "चैथम डेलाइट समय", "HEPMX": "मेक्सिकन प्रशांत डेलाइट समय", "MYT": "मलेशिया समय", "LHST": "लॉर्ड होवे मानक समय", "HNNOMX": "उत्तर पश्चिमी मेक्सिको मानक समय", "LHDT": "लॉर्ड होवे डेलाइट समय", "AST": "अटलांटिक मानक समय", "ACST": "ऑस्\u200dट्रेलियाई केंद्रीय मानक समय", "MEZ": "मध्य यूरोपीय मानक समय", "HENOMX": "उत्तर पश्चिमी मेक्सिको डेलाइट समय", "CLT": "चिली मानक समय", "AEST": "ऑस्\u200dट्रेलियाई पूर्वी मानक समय", "ACWDT": "ऑस्\u200dट्रेलियाई केंद्रीय पश्चिमी डेलाइट समय", "HNEG": "पूर्वी ग्रीनलैंड मानक समय", "HAT": "न्यूफ़ाउंडलैंड डेलाइट समय", "AWST": "ऑस्ट्रेलियाई पश्चिमी मानक समय"},
  67. }
  68. }
  69. // Locale returns the current translators string locale
  70. func (hi *hi) Locale() string {
  71. return hi.locale
  72. }
  73. // PluralsCardinal returns the list of cardinal plural rules associated with 'hi'
  74. func (hi *hi) PluralsCardinal() []locales.PluralRule {
  75. return hi.pluralsCardinal
  76. }
  77. // PluralsOrdinal returns the list of ordinal plural rules associated with 'hi'
  78. func (hi *hi) PluralsOrdinal() []locales.PluralRule {
  79. return hi.pluralsOrdinal
  80. }
  81. // PluralsRange returns the list of range plural rules associated with 'hi'
  82. func (hi *hi) PluralsRange() []locales.PluralRule {
  83. return hi.pluralsRange
  84. }
  85. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'hi'
  86. func (hi *hi) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  87. n := math.Abs(num)
  88. i := int64(n)
  89. if (i == 0) || (n == 1) {
  90. return locales.PluralRuleOne
  91. }
  92. return locales.PluralRuleOther
  93. }
  94. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'hi'
  95. func (hi *hi) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  96. n := math.Abs(num)
  97. if n == 1 {
  98. return locales.PluralRuleOne
  99. } else if n == 2 || n == 3 {
  100. return locales.PluralRuleTwo
  101. } else if n == 4 {
  102. return locales.PluralRuleFew
  103. } else if n == 6 {
  104. return locales.PluralRuleMany
  105. }
  106. return locales.PluralRuleOther
  107. }
  108. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'hi'
  109. func (hi *hi) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  110. start := hi.CardinalPluralRule(num1, v1)
  111. end := hi.CardinalPluralRule(num2, v2)
  112. if start == locales.PluralRuleOne && end == locales.PluralRuleOne {
  113. return locales.PluralRuleOne
  114. } else if start == locales.PluralRuleOne && end == locales.PluralRuleOther {
  115. return locales.PluralRuleOther
  116. }
  117. return locales.PluralRuleOther
  118. }
  119. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  120. func (hi *hi) MonthAbbreviated(month time.Month) string {
  121. return hi.monthsAbbreviated[month]
  122. }
  123. // MonthsAbbreviated returns the locales abbreviated months
  124. func (hi *hi) MonthsAbbreviated() []string {
  125. return hi.monthsAbbreviated[1:]
  126. }
  127. // MonthNarrow returns the locales narrow month given the 'month' provided
  128. func (hi *hi) MonthNarrow(month time.Month) string {
  129. return hi.monthsNarrow[month]
  130. }
  131. // MonthsNarrow returns the locales narrow months
  132. func (hi *hi) MonthsNarrow() []string {
  133. return hi.monthsNarrow[1:]
  134. }
  135. // MonthWide returns the locales wide month given the 'month' provided
  136. func (hi *hi) MonthWide(month time.Month) string {
  137. return hi.monthsWide[month]
  138. }
  139. // MonthsWide returns the locales wide months
  140. func (hi *hi) MonthsWide() []string {
  141. return hi.monthsWide[1:]
  142. }
  143. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  144. func (hi *hi) WeekdayAbbreviated(weekday time.Weekday) string {
  145. return hi.daysAbbreviated[weekday]
  146. }
  147. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  148. func (hi *hi) WeekdaysAbbreviated() []string {
  149. return hi.daysAbbreviated
  150. }
  151. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  152. func (hi *hi) WeekdayNarrow(weekday time.Weekday) string {
  153. return hi.daysNarrow[weekday]
  154. }
  155. // WeekdaysNarrow returns the locales narrow weekdays
  156. func (hi *hi) WeekdaysNarrow() []string {
  157. return hi.daysNarrow
  158. }
  159. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  160. func (hi *hi) WeekdayShort(weekday time.Weekday) string {
  161. return hi.daysShort[weekday]
  162. }
  163. // WeekdaysShort returns the locales short weekdays
  164. func (hi *hi) WeekdaysShort() []string {
  165. return hi.daysShort
  166. }
  167. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  168. func (hi *hi) WeekdayWide(weekday time.Weekday) string {
  169. return hi.daysWide[weekday]
  170. }
  171. // WeekdaysWide returns the locales wide weekdays
  172. func (hi *hi) WeekdaysWide() []string {
  173. return hi.daysWide
  174. }
  175. // Decimal returns the decimal point of number
  176. func (hi *hi) Decimal() string {
  177. return hi.decimal
  178. }
  179. // Group returns the group of number
  180. func (hi *hi) Group() string {
  181. return hi.group
  182. }
  183. // Group returns the minus sign of number
  184. func (hi *hi) Minus() string {
  185. return hi.minus
  186. }
  187. // FmtNumber returns 'num' with digits/precision of 'v' for 'hi' and handles both Whole and Real numbers based on 'v'
  188. func (hi *hi) FmtNumber(num float64, v uint64) string {
  189. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  190. l := len(s) + 2 + 1*len(s[:len(s)-int(v)-1])/3
  191. count := 0
  192. inWhole := v == 0
  193. inSecondary := false
  194. groupThreshold := 3
  195. b := make([]byte, 0, l)
  196. for i := len(s) - 1; i >= 0; i-- {
  197. if s[i] == '.' {
  198. b = append(b, hi.decimal[0])
  199. inWhole = true
  200. continue
  201. }
  202. if inWhole {
  203. if count == groupThreshold {
  204. b = append(b, hi.group[0])
  205. count = 1
  206. if !inSecondary {
  207. inSecondary = true
  208. groupThreshold = 2
  209. }
  210. } else {
  211. count++
  212. }
  213. }
  214. b = append(b, s[i])
  215. }
  216. if num < 0 {
  217. b = append(b, hi.minus[0])
  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. return string(b)
  224. }
  225. // FmtPercent returns 'num' with digits/precision of 'v' for 'hi' and handles both Whole and Real numbers based on 'v'
  226. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  227. func (hi *hi) FmtPercent(num float64, v uint64) string {
  228. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  229. l := len(s) + 3
  230. b := make([]byte, 0, l)
  231. for i := len(s) - 1; i >= 0; i-- {
  232. if s[i] == '.' {
  233. b = append(b, hi.decimal[0])
  234. continue
  235. }
  236. b = append(b, s[i])
  237. }
  238. if num < 0 {
  239. b = append(b, hi.minus[0])
  240. }
  241. // reverse
  242. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  243. b[i], b[j] = b[j], b[i]
  244. }
  245. b = append(b, hi.percent...)
  246. return string(b)
  247. }
  248. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'hi'
  249. func (hi *hi) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  250. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  251. symbol := hi.currencies[currency]
  252. l := len(s) + len(symbol) + 2 + 1*len(s[:len(s)-int(v)-1])/3
  253. count := 0
  254. inWhole := v == 0
  255. inSecondary := false
  256. groupThreshold := 3
  257. b := make([]byte, 0, l)
  258. for i := len(s) - 1; i >= 0; i-- {
  259. if s[i] == '.' {
  260. b = append(b, hi.decimal[0])
  261. inWhole = true
  262. continue
  263. }
  264. if inWhole {
  265. if count == groupThreshold {
  266. b = append(b, hi.group[0])
  267. count = 1
  268. if !inSecondary {
  269. inSecondary = true
  270. groupThreshold = 2
  271. }
  272. } else {
  273. count++
  274. }
  275. }
  276. b = append(b, s[i])
  277. }
  278. for j := len(symbol) - 1; j >= 0; j-- {
  279. b = append(b, symbol[j])
  280. }
  281. if num < 0 {
  282. b = append(b, hi.minus[0])
  283. }
  284. // reverse
  285. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  286. b[i], b[j] = b[j], b[i]
  287. }
  288. if int(v) < 2 {
  289. if v == 0 {
  290. b = append(b, hi.decimal...)
  291. }
  292. for i := 0; i < 2-int(v); i++ {
  293. b = append(b, '0')
  294. }
  295. }
  296. return string(b)
  297. }
  298. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'hi'
  299. // in accounting notation.
  300. func (hi *hi) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  301. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  302. symbol := hi.currencies[currency]
  303. l := len(s) + len(symbol) + 2 + 1*len(s[:len(s)-int(v)-1])/3
  304. count := 0
  305. inWhole := v == 0
  306. inSecondary := false
  307. groupThreshold := 3
  308. b := make([]byte, 0, l)
  309. for i := len(s) - 1; i >= 0; i-- {
  310. if s[i] == '.' {
  311. b = append(b, hi.decimal[0])
  312. inWhole = true
  313. continue
  314. }
  315. if inWhole {
  316. if count == groupThreshold {
  317. b = append(b, hi.group[0])
  318. count = 1
  319. if !inSecondary {
  320. inSecondary = true
  321. groupThreshold = 2
  322. }
  323. } else {
  324. count++
  325. }
  326. }
  327. b = append(b, s[i])
  328. }
  329. if num < 0 {
  330. for j := len(symbol) - 1; j >= 0; j-- {
  331. b = append(b, symbol[j])
  332. }
  333. b = append(b, hi.minus[0])
  334. } else {
  335. for j := len(symbol) - 1; j >= 0; j-- {
  336. b = append(b, symbol[j])
  337. }
  338. }
  339. // reverse
  340. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  341. b[i], b[j] = b[j], b[i]
  342. }
  343. if int(v) < 2 {
  344. if v == 0 {
  345. b = append(b, hi.decimal...)
  346. }
  347. for i := 0; i < 2-int(v); i++ {
  348. b = append(b, '0')
  349. }
  350. }
  351. return string(b)
  352. }
  353. // FmtDateShort returns the short date representation of 't' for 'hi'
  354. func (hi *hi) FmtDateShort(t time.Time) string {
  355. b := make([]byte, 0, 32)
  356. b = strconv.AppendInt(b, int64(t.Day()), 10)
  357. b = append(b, []byte{0x2f}...)
  358. b = strconv.AppendInt(b, int64(t.Month()), 10)
  359. b = append(b, []byte{0x2f}...)
  360. if t.Year() > 9 {
  361. b = append(b, strconv.Itoa(t.Year())[2:]...)
  362. } else {
  363. b = append(b, strconv.Itoa(t.Year())[1:]...)
  364. }
  365. return string(b)
  366. }
  367. // FmtDateMedium returns the medium date representation of 't' for 'hi'
  368. func (hi *hi) FmtDateMedium(t time.Time) string {
  369. b := make([]byte, 0, 32)
  370. b = strconv.AppendInt(b, int64(t.Day()), 10)
  371. b = append(b, []byte{0x20}...)
  372. b = append(b, hi.monthsAbbreviated[t.Month()]...)
  373. b = append(b, []byte{0x20}...)
  374. if t.Year() > 0 {
  375. b = strconv.AppendInt(b, int64(t.Year()), 10)
  376. } else {
  377. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  378. }
  379. return string(b)
  380. }
  381. // FmtDateLong returns the long date representation of 't' for 'hi'
  382. func (hi *hi) FmtDateLong(t time.Time) string {
  383. b := make([]byte, 0, 32)
  384. b = strconv.AppendInt(b, int64(t.Day()), 10)
  385. b = append(b, []byte{0x20}...)
  386. b = append(b, hi.monthsWide[t.Month()]...)
  387. b = append(b, []byte{0x20}...)
  388. if t.Year() > 0 {
  389. b = strconv.AppendInt(b, int64(t.Year()), 10)
  390. } else {
  391. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  392. }
  393. return string(b)
  394. }
  395. // FmtDateFull returns the full date representation of 't' for 'hi'
  396. func (hi *hi) FmtDateFull(t time.Time) string {
  397. b := make([]byte, 0, 32)
  398. b = append(b, hi.daysWide[t.Weekday()]...)
  399. b = append(b, []byte{0x2c, 0x20}...)
  400. b = strconv.AppendInt(b, int64(t.Day()), 10)
  401. b = append(b, []byte{0x20}...)
  402. b = append(b, hi.monthsWide[t.Month()]...)
  403. b = append(b, []byte{0x20}...)
  404. if t.Year() > 0 {
  405. b = strconv.AppendInt(b, int64(t.Year()), 10)
  406. } else {
  407. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  408. }
  409. return string(b)
  410. }
  411. // FmtTimeShort returns the short time representation of 't' for 'hi'
  412. func (hi *hi) FmtTimeShort(t time.Time) string {
  413. b := make([]byte, 0, 32)
  414. h := t.Hour()
  415. if h > 12 {
  416. h -= 12
  417. }
  418. b = strconv.AppendInt(b, int64(h), 10)
  419. b = append(b, hi.timeSeparator...)
  420. if t.Minute() < 10 {
  421. b = append(b, '0')
  422. }
  423. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  424. b = append(b, []byte{0x20}...)
  425. if t.Hour() < 12 {
  426. b = append(b, hi.periodsAbbreviated[0]...)
  427. } else {
  428. b = append(b, hi.periodsAbbreviated[1]...)
  429. }
  430. return string(b)
  431. }
  432. // FmtTimeMedium returns the medium time representation of 't' for 'hi'
  433. func (hi *hi) FmtTimeMedium(t time.Time) string {
  434. b := make([]byte, 0, 32)
  435. h := t.Hour()
  436. if h > 12 {
  437. h -= 12
  438. }
  439. b = strconv.AppendInt(b, int64(h), 10)
  440. b = append(b, hi.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, hi.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. if t.Hour() < 12 {
  452. b = append(b, hi.periodsAbbreviated[0]...)
  453. } else {
  454. b = append(b, hi.periodsAbbreviated[1]...)
  455. }
  456. return string(b)
  457. }
  458. // FmtTimeLong returns the long time representation of 't' for 'hi'
  459. func (hi *hi) FmtTimeLong(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, hi.timeSeparator...)
  467. if t.Minute() < 10 {
  468. b = append(b, '0')
  469. }
  470. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  471. b = append(b, hi.timeSeparator...)
  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, hi.periodsAbbreviated[0]...)
  479. } else {
  480. b = append(b, hi.periodsAbbreviated[1]...)
  481. }
  482. b = append(b, []byte{0x20}...)
  483. tz, _ := t.Zone()
  484. b = append(b, tz...)
  485. return string(b)
  486. }
  487. // FmtTimeFull returns the full time representation of 't' for 'hi'
  488. func (hi *hi) FmtTimeFull(t time.Time) string {
  489. b := make([]byte, 0, 32)
  490. h := t.Hour()
  491. if h > 12 {
  492. h -= 12
  493. }
  494. b = strconv.AppendInt(b, int64(h), 10)
  495. b = append(b, hi.timeSeparator...)
  496. if t.Minute() < 10 {
  497. b = append(b, '0')
  498. }
  499. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  500. b = append(b, hi.timeSeparator...)
  501. if t.Second() < 10 {
  502. b = append(b, '0')
  503. }
  504. b = strconv.AppendInt(b, int64(t.Second()), 10)
  505. b = append(b, []byte{0x20}...)
  506. if t.Hour() < 12 {
  507. b = append(b, hi.periodsAbbreviated[0]...)
  508. } else {
  509. b = append(b, hi.periodsAbbreviated[1]...)
  510. }
  511. b = append(b, []byte{0x20}...)
  512. tz, _ := t.Zone()
  513. if btz, ok := hi.timezones[tz]; ok {
  514. b = append(b, btz...)
  515. } else {
  516. b = append(b, tz...)
  517. }
  518. return string(b)
  519. }