tzm.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. package tzm
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type tzm 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. currencyPositiveSuffix string
  23. currencyNegativeSuffix 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 'tzm' locale
  41. func New() locales.Translator {
  42. return &tzm{
  43. locale: "tzm",
  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", "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", "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. currencyPositiveSuffix: " ",
  52. currencyNegativeSuffix: " ",
  53. monthsAbbreviated: []string{"", "Yen", "Yeb", "Mar", "Ibr", "May", "Yun", "Yul", "Ɣuc", "Cut", "Kṭu", "Nwa", "Duj"},
  54. monthsNarrow: []string{"", "Y", "Y", "M", "I", "M", "Y", "Y", "Ɣ", "C", "K", "N", "D"},
  55. monthsWide: []string{"", "Yennayer", "Yebrayer", "Mars", "Ibrir", "Mayyu", "Yunyu", "Yulyuz", "Ɣuct", "Cutanbir", "Kṭuber", "Nwanbir", "Dujanbir"},
  56. daysAbbreviated: []string{"Asa", "Ayn", "Asn", "Akr", "Akw", "Asm", "Asḍ"},
  57. daysNarrow: []string{"A", "A", "A", "A", "A", "A", "A"},
  58. daysWide: []string{"Asamas", "Aynas", "Asinas", "Akras", "Akwas", "Asimwas", "Asiḍyas"},
  59. periodsAbbreviated: []string{"Zdat azal", "Ḍeffir aza"},
  60. periodsWide: []string{"Zdat azal", "Ḍeffir aza"},
  61. erasAbbreviated: []string{"ZƐ", "ḌƐ"},
  62. erasNarrow: []string{"", ""},
  63. erasWide: []string{"Zdat Ɛisa (TAƔ)", "Ḍeffir Ɛisa (TAƔ)"},
  64. timezones: map[string]string{"GMT": "GMT", "HEPMX": "HEPMX", "HNEG": "HNEG", "HEOG": "HEOG", "IST": "IST", "JST": "JST", "EDT": "EDT", "HNPM": "HNPM", "MST": "MST", "CAT": "CAT", "OESZ": "OESZ", "AWST": "AWST", "BOT": "BOT", "HNPMX": "HNPMX", "ECT": "ECT", "SRT": "SRT", "AEST": "AEST", "GFT": "GFT", "ACDT": "ACDT", "WIB": "WIB", "HEEG": "HEEG", "LHST": "LHST", "EAT": "EAT", "∅∅∅": "∅∅∅", "UYST": "UYST", "ADT": "ADT", "WEZ": "WEZ", "HKT": "HKT", "CLT": "CLT", "COST": "COST", "NZST": "NZST", "ACWST": "ACWST", "MEZ": "MEZ", "AEDT": "AEDT", "MESZ": "MESZ", "HNT": "HNT", "VET": "VET", "HADT": "HADT", "ChST": "ChST", "CHAST": "CHAST", "AWDT": "AWDT", "HAT": "HAT", "WIT": "WIT", "MYT": "MYT", "GYT": "GYT", "CDT": "CDT", "PST": "PST", "ACST": "ACST", "BT": "BT", "JDT": "JDT", "ACWDT": "ACWDT", "MDT": "MDT", "ART": "ART", "HECU": "HECU", "WAT": "WAT", "WESZ": "WESZ", "CST": "CST", "AST": "AST", "HKST": "HKST", "WARST": "WARST", "HEPM": "HEPM", "WITA": "WITA", "HNNOMX": "HNNOMX", "ARST": "ARST", "WAST": "WAST", "EST": "EST", "WART": "WART", "NZDT": "NZDT", "LHDT": "LHDT", "HENOMX": "HENOMX", "HAST": "HAST", "UYT": "UYT", "CHADT": "CHADT", "SAST": "SAST", "CLST": "CLST", "TMST": "TMST", "OEZ": "OEZ", "PDT": "PDT", "AKST": "AKST", "HNOG": "HNOG", "TMT": "TMT", "COT": "COT", "HNCU": "HNCU", "AKDT": "AKDT", "SGT": "SGT"},
  65. }
  66. }
  67. // Locale returns the current translators string locale
  68. func (tzm *tzm) Locale() string {
  69. return tzm.locale
  70. }
  71. // PluralsCardinal returns the list of cardinal plural rules associated with 'tzm'
  72. func (tzm *tzm) PluralsCardinal() []locales.PluralRule {
  73. return tzm.pluralsCardinal
  74. }
  75. // PluralsOrdinal returns the list of ordinal plural rules associated with 'tzm'
  76. func (tzm *tzm) PluralsOrdinal() []locales.PluralRule {
  77. return tzm.pluralsOrdinal
  78. }
  79. // PluralsRange returns the list of range plural rules associated with 'tzm'
  80. func (tzm *tzm) PluralsRange() []locales.PluralRule {
  81. return tzm.pluralsRange
  82. }
  83. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'tzm'
  84. func (tzm *tzm) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  85. n := math.Abs(num)
  86. if (n >= 0 && n <= 1) || (n >= 11 && n <= 99) {
  87. return locales.PluralRuleOne
  88. }
  89. return locales.PluralRuleOther
  90. }
  91. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'tzm'
  92. func (tzm *tzm) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  93. return locales.PluralRuleUnknown
  94. }
  95. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'tzm'
  96. func (tzm *tzm) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  97. return locales.PluralRuleUnknown
  98. }
  99. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  100. func (tzm *tzm) MonthAbbreviated(month time.Month) string {
  101. return tzm.monthsAbbreviated[month]
  102. }
  103. // MonthsAbbreviated returns the locales abbreviated months
  104. func (tzm *tzm) MonthsAbbreviated() []string {
  105. return tzm.monthsAbbreviated[1:]
  106. }
  107. // MonthNarrow returns the locales narrow month given the 'month' provided
  108. func (tzm *tzm) MonthNarrow(month time.Month) string {
  109. return tzm.monthsNarrow[month]
  110. }
  111. // MonthsNarrow returns the locales narrow months
  112. func (tzm *tzm) MonthsNarrow() []string {
  113. return tzm.monthsNarrow[1:]
  114. }
  115. // MonthWide returns the locales wide month given the 'month' provided
  116. func (tzm *tzm) MonthWide(month time.Month) string {
  117. return tzm.monthsWide[month]
  118. }
  119. // MonthsWide returns the locales wide months
  120. func (tzm *tzm) MonthsWide() []string {
  121. return tzm.monthsWide[1:]
  122. }
  123. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  124. func (tzm *tzm) WeekdayAbbreviated(weekday time.Weekday) string {
  125. return tzm.daysAbbreviated[weekday]
  126. }
  127. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  128. func (tzm *tzm) WeekdaysAbbreviated() []string {
  129. return tzm.daysAbbreviated
  130. }
  131. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  132. func (tzm *tzm) WeekdayNarrow(weekday time.Weekday) string {
  133. return tzm.daysNarrow[weekday]
  134. }
  135. // WeekdaysNarrow returns the locales narrow weekdays
  136. func (tzm *tzm) WeekdaysNarrow() []string {
  137. return tzm.daysNarrow
  138. }
  139. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  140. func (tzm *tzm) WeekdayShort(weekday time.Weekday) string {
  141. return tzm.daysShort[weekday]
  142. }
  143. // WeekdaysShort returns the locales short weekdays
  144. func (tzm *tzm) WeekdaysShort() []string {
  145. return tzm.daysShort
  146. }
  147. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  148. func (tzm *tzm) WeekdayWide(weekday time.Weekday) string {
  149. return tzm.daysWide[weekday]
  150. }
  151. // WeekdaysWide returns the locales wide weekdays
  152. func (tzm *tzm) WeekdaysWide() []string {
  153. return tzm.daysWide
  154. }
  155. // Decimal returns the decimal point of number
  156. func (tzm *tzm) Decimal() string {
  157. return tzm.decimal
  158. }
  159. // Group returns the group of number
  160. func (tzm *tzm) Group() string {
  161. return tzm.group
  162. }
  163. // Group returns the minus sign of number
  164. func (tzm *tzm) Minus() string {
  165. return tzm.minus
  166. }
  167. // FmtNumber returns 'num' with digits/precision of 'v' for 'tzm' and handles both Whole and Real numbers based on 'v'
  168. func (tzm *tzm) FmtNumber(num float64, v uint64) string {
  169. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  170. }
  171. // FmtPercent returns 'num' with digits/precision of 'v' for 'tzm' and handles both Whole and Real numbers based on 'v'
  172. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  173. func (tzm *tzm) FmtPercent(num float64, v uint64) string {
  174. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  175. }
  176. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'tzm'
  177. func (tzm *tzm) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  178. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  179. symbol := tzm.currencies[currency]
  180. l := len(s) + len(symbol) + 3 + 2*len(s[:len(s)-int(v)-1])/3
  181. count := 0
  182. inWhole := v == 0
  183. b := make([]byte, 0, l)
  184. for i := len(s) - 1; i >= 0; i-- {
  185. if s[i] == '.' {
  186. b = append(b, tzm.decimal[0])
  187. inWhole = true
  188. continue
  189. }
  190. if inWhole {
  191. if count == 3 {
  192. for j := len(tzm.group) - 1; j >= 0; j-- {
  193. b = append(b, tzm.group[j])
  194. }
  195. count = 1
  196. } else {
  197. count++
  198. }
  199. }
  200. b = append(b, s[i])
  201. }
  202. if num < 0 {
  203. b = append(b, tzm.minus[0])
  204. }
  205. // reverse
  206. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  207. b[i], b[j] = b[j], b[i]
  208. }
  209. if int(v) < 2 {
  210. if v == 0 {
  211. b = append(b, tzm.decimal...)
  212. }
  213. for i := 0; i < 2-int(v); i++ {
  214. b = append(b, '0')
  215. }
  216. }
  217. b = append(b, tzm.currencyPositiveSuffix...)
  218. b = append(b, symbol...)
  219. return string(b)
  220. }
  221. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'tzm'
  222. // in accounting notation.
  223. func (tzm *tzm) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  224. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  225. symbol := tzm.currencies[currency]
  226. l := len(s) + len(symbol) + 3 + 2*len(s[:len(s)-int(v)-1])/3
  227. count := 0
  228. inWhole := v == 0
  229. b := make([]byte, 0, l)
  230. for i := len(s) - 1; i >= 0; i-- {
  231. if s[i] == '.' {
  232. b = append(b, tzm.decimal[0])
  233. inWhole = true
  234. continue
  235. }
  236. if inWhole {
  237. if count == 3 {
  238. for j := len(tzm.group) - 1; j >= 0; j-- {
  239. b = append(b, tzm.group[j])
  240. }
  241. count = 1
  242. } else {
  243. count++
  244. }
  245. }
  246. b = append(b, s[i])
  247. }
  248. if num < 0 {
  249. b = append(b, tzm.minus[0])
  250. }
  251. // reverse
  252. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  253. b[i], b[j] = b[j], b[i]
  254. }
  255. if int(v) < 2 {
  256. if v == 0 {
  257. b = append(b, tzm.decimal...)
  258. }
  259. for i := 0; i < 2-int(v); i++ {
  260. b = append(b, '0')
  261. }
  262. }
  263. if num < 0 {
  264. b = append(b, tzm.currencyNegativeSuffix...)
  265. b = append(b, symbol...)
  266. } else {
  267. b = append(b, tzm.currencyPositiveSuffix...)
  268. b = append(b, symbol...)
  269. }
  270. return string(b)
  271. }
  272. // FmtDateShort returns the short date representation of 't' for 'tzm'
  273. func (tzm *tzm) FmtDateShort(t time.Time) string {
  274. b := make([]byte, 0, 32)
  275. if t.Day() < 10 {
  276. b = append(b, '0')
  277. }
  278. b = strconv.AppendInt(b, int64(t.Day()), 10)
  279. b = append(b, []byte{0x2f}...)
  280. if t.Month() < 10 {
  281. b = append(b, '0')
  282. }
  283. b = strconv.AppendInt(b, int64(t.Month()), 10)
  284. b = append(b, []byte{0x2f}...)
  285. if t.Year() > 0 {
  286. b = strconv.AppendInt(b, int64(t.Year()), 10)
  287. } else {
  288. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  289. }
  290. return string(b)
  291. }
  292. // FmtDateMedium returns the medium date representation of 't' for 'tzm'
  293. func (tzm *tzm) FmtDateMedium(t time.Time) string {
  294. b := make([]byte, 0, 32)
  295. b = strconv.AppendInt(b, int64(t.Day()), 10)
  296. b = append(b, []byte{0x20}...)
  297. b = append(b, tzm.monthsAbbreviated[t.Month()]...)
  298. b = append(b, []byte{0x20}...)
  299. if t.Year() > 0 {
  300. b = strconv.AppendInt(b, int64(t.Year()), 10)
  301. } else {
  302. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  303. }
  304. return string(b)
  305. }
  306. // FmtDateLong returns the long date representation of 't' for 'tzm'
  307. func (tzm *tzm) FmtDateLong(t time.Time) string {
  308. b := make([]byte, 0, 32)
  309. b = strconv.AppendInt(b, int64(t.Day()), 10)
  310. b = append(b, []byte{0x20}...)
  311. b = append(b, tzm.monthsWide[t.Month()]...)
  312. b = append(b, []byte{0x20}...)
  313. if t.Year() > 0 {
  314. b = strconv.AppendInt(b, int64(t.Year()), 10)
  315. } else {
  316. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  317. }
  318. return string(b)
  319. }
  320. // FmtDateFull returns the full date representation of 't' for 'tzm'
  321. func (tzm *tzm) FmtDateFull(t time.Time) string {
  322. b := make([]byte, 0, 32)
  323. b = append(b, tzm.daysWide[t.Weekday()]...)
  324. b = append(b, []byte{0x2c, 0x20}...)
  325. b = strconv.AppendInt(b, int64(t.Day()), 10)
  326. b = append(b, []byte{0x20}...)
  327. b = append(b, tzm.monthsWide[t.Month()]...)
  328. b = append(b, []byte{0x20}...)
  329. if t.Year() > 0 {
  330. b = strconv.AppendInt(b, int64(t.Year()), 10)
  331. } else {
  332. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  333. }
  334. return string(b)
  335. }
  336. // FmtTimeShort returns the short time representation of 't' for 'tzm'
  337. func (tzm *tzm) FmtTimeShort(t time.Time) string {
  338. b := make([]byte, 0, 32)
  339. return string(b)
  340. }
  341. // FmtTimeMedium returns the medium time representation of 't' for 'tzm'
  342. func (tzm *tzm) FmtTimeMedium(t time.Time) string {
  343. b := make([]byte, 0, 32)
  344. return string(b)
  345. }
  346. // FmtTimeLong returns the long time representation of 't' for 'tzm'
  347. func (tzm *tzm) FmtTimeLong(t time.Time) string {
  348. b := make([]byte, 0, 32)
  349. return string(b)
  350. }
  351. // FmtTimeFull returns the full time representation of 't' for 'tzm'
  352. func (tzm *tzm) FmtTimeFull(t time.Time) string {
  353. b := make([]byte, 0, 32)
  354. return string(b)
  355. }