ml.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. package ml
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type ml 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. currencyNegativePrefix 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 'ml' locale
  41. func New() locales.Translator {
  42. return &ml{
  43. locale: "ml",
  44. pluralsCardinal: []locales.PluralRule{2, 6},
  45. pluralsOrdinal: []locales.PluralRule{6},
  46. pluralsRange: []locales.PluralRule{2, 6},
  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", "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", "¥", "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"},
  55. currencyNegativePrefix: "(",
  56. currencyNegativeSuffix: ")",
  57. monthsAbbreviated: []string{"", "ജനു", "ഫെബ്രു", "മാർ", "ഏപ്രി", "മേയ്", "ജൂൺ", "ജൂലൈ", "ഓഗ", "സെപ്റ്റം", "ഒക്ടോ", "നവം", "ഡിസം"},
  58. monthsNarrow: []string{"", "ജ", "ഫെ", "മാ", "ഏ", "മെ", "ജൂൺ", "ജൂ", "ഓ", "സെ", "ഒ", "ന", "ഡി"},
  59. monthsWide: []string{"", "ജനുവരി", "ഫെബ്രുവരി", "മാർച്ച്", "ഏപ്രിൽ", "മേയ്", "ജൂൺ", "ജൂലൈ", "ഓഗസ്റ്റ്", "സെപ്റ്റംബർ", "ഒക്\u200cടോബർ", "നവംബർ", "ഡിസംബർ"},
  60. daysAbbreviated: []string{"ഞായർ", "തിങ്കൾ", "ചൊവ്വ", "ബുധൻ", "വ്യാഴം", "വെള്ളി", "ശനി"},
  61. daysNarrow: []string{"ഞ", "തി", "ചൊ", "ബു", "വ്യാ", "വെ", "ശ"},
  62. daysShort: []string{"ഞാ", "തി", "ചൊ", "ബു", "വ്യാ", "വെ", "ശ"},
  63. daysWide: []string{"ഞായറാഴ്\u200cച", "തിങ്കളാഴ്\u200cച", "ചൊവ്വാഴ്ച", "ബുധനാഴ്\u200cച", "വ്യാഴാഴ്\u200cച", "വെള്ളിയാഴ്\u200cച", "ശനിയാഴ്\u200cച"},
  64. periodsAbbreviated: []string{"AM", "PM"},
  65. periodsNarrow: []string{"AM", "PM"},
  66. periodsWide: []string{"AM", "PM"},
  67. erasAbbreviated: []string{"ക്രി.മു.", "എഡി"},
  68. erasNarrow: []string{"ക്രി.മു.", "എഡി"},
  69. erasWide: []string{"ക്രിസ്\u200cതുവിന് മുമ്പ്", "ആന്നോ ഡൊമിനി"},
  70. timezones: map[string]string{"HEEG": "കിഴക്കൻ ഗ്രീൻലാൻഡ് ഗ്രീഷ്\u200cമകാല സമയം", "GYT": "ഗയാന സമയം", "AWDT": "ഓസ്\u200cട്രേലിയൻ പടിഞ്ഞാറൻ ഡേലൈറ്റ് സമയം", "AKDT": "അലാസ്\u200cക ഡേലൈറ്റ് സമയം", "ACDT": "ഓസ്ട്രേലിയൻ സെൻട്രൽ ഡേലൈറ്റ് സമയം", "CST": "വടക്കെ അമേരിക്കൻ സെൻട്രൽ സ്റ്റാൻഡേർഡ് സമയം", "WAST": "പടിഞ്ഞാറൻ ആഫ്രിക്ക ഗ്രീഷ്\u200cമകാല സമയം", "WART": "പടിഞ്ഞാറൻ അർജന്റീന സ്റ്റാൻഡേർഡ് സമയം", "HEPM": "സെന്റ് പിയറി ആൻഡ് മിക്വലൻ ഡേലൈറ്റ് സമയം", "CLST": "ചിലി ഗ്രീഷ്\u200cമകാല സമയം", "OESZ": "കിഴക്കൻ യൂറോപ്യൻ ഗ്രീഷ്മകാല സമയം", "MESZ": "സെൻട്രൽ യൂറോപ്യൻ ഗ്രീഷ്മകാല സമയം", "HKST": "ഹോങ്കോങ്ങ് ഗ്രീഷ്\u200cമകാല സമയം", "HENOMX": "വടക്കുപടിഞ്ഞാറൻ മെക്സിക്കൻ ഡേലൈറ്റ് സമയം", "CDT": "വടക്കെ അമേരിക്കൻ സെൻട്രൽ ഡേലൈറ്റ് സമയം", "EST": "വടക്കെ അമേരിക്കൻ കിഴക്കൻ സ്റ്റാൻഡേർഡ് സമയം", "MEZ": "സെൻട്രൽ യൂറോപ്യൻ സ്റ്റാൻഡേർഡ് സമയം", "LHDT": "ലോർഡ് ഹോവ് ഡേലൈറ്റ് സമയം", "MDT": "മകൌ വേനൽക്കാല സമയം", "COT": "കൊളംബിയ സ്റ്റാൻഡേർഡ് സമയം", "MYT": "മലേഷ്യ സമയം", "HAST": "ഹവായ്-അലൂഷ്യൻ സ്റ്റാൻഡേർഡ് സമയം", "HADT": "ഹവായ്-അലൂഷ്യൻ ഡേലൈറ്റ് സമയം", "ART": "അർജന്റീന സ്റ്റാൻഡേർഡ് സമയം", "AST": "അറ്റ്\u200cലാന്റിക് സ്റ്റാൻഡേർഡ് സമയം", "∅∅∅": "പെറു ഗ്രീഷ്\u200cമകാല സമയം", "CLT": "ചിലി സ്റ്റാൻഡേർഡ് സമയം", "TMT": "തുർക്ക്\u200cമെനിസ്ഥാൻ സ്റ്റാൻഡേർഡ് സമയം", "TMST": "തുർക്ക്\u200cമെനിസ്ഥാൻ ഗ്രീഷ്\u200cമകാല സമയം", "JDT": "ജപ്പാൻ ഡേലൈറ്റ് സമയം", "HKT": "ഹോങ്കോങ്ങ് സ്റ്റാൻഡേർഡ് സമയം", "ACWDT": "ഓസ്ട്രേലിയൻ സെൻട്രൽ പടിഞ്ഞാറൻ ഡേലൈറ്റ് സമയം", "HNEG": "കിഴക്കൻ ഗ്രീൻലാൻഡ് സ്റ്റാൻഡേർഡ് സമയം", "WARST": "പടിഞ്ഞാറൻ അർജന്റീന ഗ്രീഷ്\u200cമകാല സമയം", "JST": "ജപ്പാൻ സ്റ്റാൻഡേർഡ് സമയം", "NZDT": "ന്യൂസിലാൻഡ് ഡേലൈറ്റ് സമയം", "ACWST": "ഓസ്ട്രേലിയൻ സെൻട്രൽ പടിഞ്ഞാറൻ സ്റ്റാൻഡേർഡ് സമയം", "GFT": "ഫ്രഞ്ച് ഗയാന സമയം", "ECT": "ഇക്വഡോർ സമയം", "HNNOMX": "വടക്കുപടിഞ്ഞാറൻ മെക്\u200cസിക്കൻ സ്റ്റാൻഡേർഡ് സമയം", "SRT": "സുരിനെയിം സമയം", "ADT": "അറ്റ്\u200cലാന്റിക് ഡേലൈറ്റ് സമയം", "WEZ": "പടിഞ്ഞാറൻ യൂറോപ്യൻ സ്റ്റാൻഡേർഡ് സമയം", "IST": "ഇന്ത്യൻ സ്റ്റാൻഡേർഡ് സമയം", "OEZ": "കിഴക്കൻ യൂറോപ്യൻ സ്റ്റാൻഡേർഡ് സമയം", "CHADT": "ചാത്തം ഗ്രീഷ്\u200cമകാല സമയം", "AEST": "ഓസ്\u200cട്രേലിയൻ കിഴക്കൻ സ്റ്റാൻഡേർഡ് സമയം", "MST": "മകൌ സ്റ്റാൻഡേർഡ് സമയം", "GMT": "ഗ്രീൻവിച്ച് മീൻ സമയം", "SAST": "ദക്ഷിണാഫ്രിക്ക സ്റ്റാൻഡേർഡ് സമയം", "PDT": "വടക്കെ അമേരിക്കൻ പസഫിക് ഡേലൈറ്റ് സമയം", "HEPMX": "മെക്സിക്കൻ പസഫിക് ഡേലൈറ്റ് സമയം", "LHST": "ലോർഡ് ഹോവ് സ്റ്റാൻഡേർഡ് സമയം", "WIT": "കിഴക്കൻ ഇന്തോനേഷ്യ സമയം", "COST": "കൊളംബിയ ഗ്രീഷ്\u200cമകാല സമയം", "UYST": "ഉറുഗ്വേ ഗ്രീഷ്\u200cമകാല സമയം", "HECU": "ക്യൂബ ഡേലൈറ്റ് സമയം", "HNPMX": "മെക്\u200cസിക്കൻ പസഫിക് സ്റ്റാൻഡേർഡ് സമയം", "AEDT": "ഓസ്\u200cട്രേലിയൻ കിഴക്കൻ ഡേലൈറ്റ് സമയം", "WAT": "പടിഞ്ഞാറൻ ആഫ്രിക്ക സ്റ്റാൻഡേർഡ് സമയം", "HNT": "ന്യൂഫൗണ്ട്\u200cലാന്റ് സ്റ്റാൻഡേർഡ് സമയം", "VET": "വെനിസ്വേല സമയം", "ARST": "അർജന്റീന ഗ്രീഷ്\u200cമകാല സമയം", "HNCU": "ക്യൂബ സ്റ്റാൻഡേർഡ് സമയം", "HNOG": "പടിഞ്ഞാറൻ ഗ്രീൻലാൻഡ് സ്റ്റാൻഡേർഡ് സമയം", "ACST": "ഓസ്ട്രേലിയൻ സെൻട്രൽ സ്റ്റാൻഡേർഡ് സമയം", "WITA": "മധ്യ ഇന്തോനേഷ്യ സമയം", "CAT": "മധ്യ ആഫ്രിക്ക സമയം", "EAT": "കിഴക്കൻ ആഫ്രിക്ക സമയം", "AKST": "അലാസ്ക സ്റ്റാൻഡേർഡ് സമയം", "AWST": "ഓസ്\u200cട്രേലിയൻ പടിഞ്ഞാറൻ സ്റ്റാൻഡേർഡ് സമയം", "WIB": "പടിഞ്ഞാറൻ ഇന്തോനേഷ്യ സമയം", "EDT": "വടക്കെ അമേരിക്കൻ കിഴക്കൻ ഡേലൈറ്റ് സമയം", "HEOG": "പടിഞ്ഞാറൻ ഗ്രീൻലാൻഡ് ഗ്രീഷ്\u200cമകാല സമയം", "HAT": "ന്യൂഫൗണ്ട്\u200cലാന്റ് ഡേലൈറ്റ് സമയം", "PST": "വടക്കെ അമേരിക്കൻ പസഫിക് സ്റ്റാൻഡേർഡ് സമയം", "BT": "ഭൂട്ടാൻ സമയം", "BOT": "ബൊളീവിയ സമയം", "NZST": "ന്യൂസിലാൻഡ് സ്റ്റാൻഡേർഡ് സമയം", "SGT": "സിംഗപ്പൂർ സ്റ്റാൻഡേർഡ് സമയം", "HNPM": "സെന്റ് പിയറി ആൻഡ് മിക്വലൻ സ്റ്റാൻഡേർഡ് സമയം", "UYT": "ഉറുഗ്വേ സ്റ്റാൻഡേർഡ് സമയം", "CHAST": "ചാത്തം സ്റ്റാൻഡേർഡ് സമയം", "WESZ": "പടിഞ്ഞാറൻ യൂറോപ്യൻ ഗ്രീഷ്\u200cമകാല സമയം", "ChST": "ചമോറോ സ്റ്റാൻഡേർഡ് സമയം"},
  71. }
  72. }
  73. // Locale returns the current translators string locale
  74. func (ml *ml) Locale() string {
  75. return ml.locale
  76. }
  77. // PluralsCardinal returns the list of cardinal plural rules associated with 'ml'
  78. func (ml *ml) PluralsCardinal() []locales.PluralRule {
  79. return ml.pluralsCardinal
  80. }
  81. // PluralsOrdinal returns the list of ordinal plural rules associated with 'ml'
  82. func (ml *ml) PluralsOrdinal() []locales.PluralRule {
  83. return ml.pluralsOrdinal
  84. }
  85. // PluralsRange returns the list of range plural rules associated with 'ml'
  86. func (ml *ml) PluralsRange() []locales.PluralRule {
  87. return ml.pluralsRange
  88. }
  89. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'ml'
  90. func (ml *ml) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  91. n := math.Abs(num)
  92. if n == 1 {
  93. return locales.PluralRuleOne
  94. }
  95. return locales.PluralRuleOther
  96. }
  97. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'ml'
  98. func (ml *ml) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  99. return locales.PluralRuleOther
  100. }
  101. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'ml'
  102. func (ml *ml) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  103. start := ml.CardinalPluralRule(num1, v1)
  104. end := ml.CardinalPluralRule(num2, v2)
  105. if start == locales.PluralRuleOne && end == locales.PluralRuleOther {
  106. return locales.PluralRuleOther
  107. } else if start == locales.PluralRuleOther && end == locales.PluralRuleOne {
  108. return locales.PluralRuleOne
  109. }
  110. return locales.PluralRuleOther
  111. }
  112. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  113. func (ml *ml) MonthAbbreviated(month time.Month) string {
  114. return ml.monthsAbbreviated[month]
  115. }
  116. // MonthsAbbreviated returns the locales abbreviated months
  117. func (ml *ml) MonthsAbbreviated() []string {
  118. return ml.monthsAbbreviated[1:]
  119. }
  120. // MonthNarrow returns the locales narrow month given the 'month' provided
  121. func (ml *ml) MonthNarrow(month time.Month) string {
  122. return ml.monthsNarrow[month]
  123. }
  124. // MonthsNarrow returns the locales narrow months
  125. func (ml *ml) MonthsNarrow() []string {
  126. return ml.monthsNarrow[1:]
  127. }
  128. // MonthWide returns the locales wide month given the 'month' provided
  129. func (ml *ml) MonthWide(month time.Month) string {
  130. return ml.monthsWide[month]
  131. }
  132. // MonthsWide returns the locales wide months
  133. func (ml *ml) MonthsWide() []string {
  134. return ml.monthsWide[1:]
  135. }
  136. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  137. func (ml *ml) WeekdayAbbreviated(weekday time.Weekday) string {
  138. return ml.daysAbbreviated[weekday]
  139. }
  140. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  141. func (ml *ml) WeekdaysAbbreviated() []string {
  142. return ml.daysAbbreviated
  143. }
  144. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  145. func (ml *ml) WeekdayNarrow(weekday time.Weekday) string {
  146. return ml.daysNarrow[weekday]
  147. }
  148. // WeekdaysNarrow returns the locales narrow weekdays
  149. func (ml *ml) WeekdaysNarrow() []string {
  150. return ml.daysNarrow
  151. }
  152. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  153. func (ml *ml) WeekdayShort(weekday time.Weekday) string {
  154. return ml.daysShort[weekday]
  155. }
  156. // WeekdaysShort returns the locales short weekdays
  157. func (ml *ml) WeekdaysShort() []string {
  158. return ml.daysShort
  159. }
  160. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  161. func (ml *ml) WeekdayWide(weekday time.Weekday) string {
  162. return ml.daysWide[weekday]
  163. }
  164. // WeekdaysWide returns the locales wide weekdays
  165. func (ml *ml) WeekdaysWide() []string {
  166. return ml.daysWide
  167. }
  168. // Decimal returns the decimal point of number
  169. func (ml *ml) Decimal() string {
  170. return ml.decimal
  171. }
  172. // Group returns the group of number
  173. func (ml *ml) Group() string {
  174. return ml.group
  175. }
  176. // Group returns the minus sign of number
  177. func (ml *ml) Minus() string {
  178. return ml.minus
  179. }
  180. // FmtNumber returns 'num' with digits/precision of 'v' for 'ml' and handles both Whole and Real numbers based on 'v'
  181. func (ml *ml) FmtNumber(num float64, v uint64) string {
  182. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  183. l := len(s) + 2 + 1*len(s[:len(s)-int(v)-1])/3
  184. count := 0
  185. inWhole := v == 0
  186. inSecondary := false
  187. groupThreshold := 3
  188. b := make([]byte, 0, l)
  189. for i := len(s) - 1; i >= 0; i-- {
  190. if s[i] == '.' {
  191. b = append(b, ml.decimal[0])
  192. inWhole = true
  193. continue
  194. }
  195. if inWhole {
  196. if count == groupThreshold {
  197. b = append(b, ml.group[0])
  198. count = 1
  199. if !inSecondary {
  200. inSecondary = true
  201. groupThreshold = 2
  202. }
  203. } else {
  204. count++
  205. }
  206. }
  207. b = append(b, s[i])
  208. }
  209. if num < 0 {
  210. b = append(b, ml.minus[0])
  211. }
  212. // reverse
  213. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  214. b[i], b[j] = b[j], b[i]
  215. }
  216. return string(b)
  217. }
  218. // FmtPercent returns 'num' with digits/precision of 'v' for 'ml' and handles both Whole and Real numbers based on 'v'
  219. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  220. func (ml *ml) FmtPercent(num float64, v uint64) string {
  221. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  222. l := len(s) + 3
  223. b := make([]byte, 0, l)
  224. for i := len(s) - 1; i >= 0; i-- {
  225. if s[i] == '.' {
  226. b = append(b, ml.decimal[0])
  227. continue
  228. }
  229. b = append(b, s[i])
  230. }
  231. if num < 0 {
  232. b = append(b, ml.minus[0])
  233. }
  234. // reverse
  235. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  236. b[i], b[j] = b[j], b[i]
  237. }
  238. b = append(b, ml.percent...)
  239. return string(b)
  240. }
  241. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'ml'
  242. func (ml *ml) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  243. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  244. symbol := ml.currencies[currency]
  245. l := len(s) + len(symbol) + 2 + 1*len(s[:len(s)-int(v)-1])/3
  246. count := 0
  247. inWhole := v == 0
  248. b := make([]byte, 0, l)
  249. for i := len(s) - 1; i >= 0; i-- {
  250. if s[i] == '.' {
  251. b = append(b, ml.decimal[0])
  252. inWhole = true
  253. continue
  254. }
  255. if inWhole {
  256. if count == 3 {
  257. b = append(b, ml.group[0])
  258. count = 1
  259. } else {
  260. count++
  261. }
  262. }
  263. b = append(b, s[i])
  264. }
  265. for j := len(symbol) - 1; j >= 0; j-- {
  266. b = append(b, symbol[j])
  267. }
  268. if num < 0 {
  269. b = append(b, ml.minus[0])
  270. }
  271. // reverse
  272. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  273. b[i], b[j] = b[j], b[i]
  274. }
  275. if int(v) < 2 {
  276. if v == 0 {
  277. b = append(b, ml.decimal...)
  278. }
  279. for i := 0; i < 2-int(v); i++ {
  280. b = append(b, '0')
  281. }
  282. }
  283. return string(b)
  284. }
  285. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'ml'
  286. // in accounting notation.
  287. func (ml *ml) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  288. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  289. symbol := ml.currencies[currency]
  290. l := len(s) + len(symbol) + 4 + 1*len(s[:len(s)-int(v)-1])/3
  291. count := 0
  292. inWhole := v == 0
  293. b := make([]byte, 0, l)
  294. for i := len(s) - 1; i >= 0; i-- {
  295. if s[i] == '.' {
  296. b = append(b, ml.decimal[0])
  297. inWhole = true
  298. continue
  299. }
  300. if inWhole {
  301. if count == 3 {
  302. b = append(b, ml.group[0])
  303. count = 1
  304. } else {
  305. count++
  306. }
  307. }
  308. b = append(b, s[i])
  309. }
  310. if num < 0 {
  311. for j := len(symbol) - 1; j >= 0; j-- {
  312. b = append(b, symbol[j])
  313. }
  314. b = append(b, ml.currencyNegativePrefix[0])
  315. } else {
  316. for j := len(symbol) - 1; j >= 0; j-- {
  317. b = append(b, symbol[j])
  318. }
  319. }
  320. // reverse
  321. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  322. b[i], b[j] = b[j], b[i]
  323. }
  324. if int(v) < 2 {
  325. if v == 0 {
  326. b = append(b, ml.decimal...)
  327. }
  328. for i := 0; i < 2-int(v); i++ {
  329. b = append(b, '0')
  330. }
  331. }
  332. if num < 0 {
  333. b = append(b, ml.currencyNegativeSuffix...)
  334. }
  335. return string(b)
  336. }
  337. // FmtDateShort returns the short date representation of 't' for 'ml'
  338. func (ml *ml) FmtDateShort(t time.Time) string {
  339. b := make([]byte, 0, 32)
  340. b = strconv.AppendInt(b, int64(t.Day()), 10)
  341. b = append(b, []byte{0x2f}...)
  342. b = strconv.AppendInt(b, int64(t.Month()), 10)
  343. b = append(b, []byte{0x2f}...)
  344. if t.Year() > 9 {
  345. b = append(b, strconv.Itoa(t.Year())[2:]...)
  346. } else {
  347. b = append(b, strconv.Itoa(t.Year())[1:]...)
  348. }
  349. return string(b)
  350. }
  351. // FmtDateMedium returns the medium date representation of 't' for 'ml'
  352. func (ml *ml) FmtDateMedium(t time.Time) string {
  353. b := make([]byte, 0, 32)
  354. if t.Year() > 0 {
  355. b = strconv.AppendInt(b, int64(t.Year()), 10)
  356. } else {
  357. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  358. }
  359. b = append(b, []byte{0x2c, 0x20}...)
  360. b = append(b, ml.monthsAbbreviated[t.Month()]...)
  361. b = append(b, []byte{0x20}...)
  362. b = strconv.AppendInt(b, int64(t.Day()), 10)
  363. return string(b)
  364. }
  365. // FmtDateLong returns the long date representation of 't' for 'ml'
  366. func (ml *ml) FmtDateLong(t time.Time) string {
  367. b := make([]byte, 0, 32)
  368. if t.Year() > 0 {
  369. b = strconv.AppendInt(b, int64(t.Year()), 10)
  370. } else {
  371. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  372. }
  373. b = append(b, []byte{0x2c, 0x20}...)
  374. b = append(b, ml.monthsWide[t.Month()]...)
  375. b = append(b, []byte{0x20}...)
  376. b = strconv.AppendInt(b, int64(t.Day()), 10)
  377. return string(b)
  378. }
  379. // FmtDateFull returns the full date representation of 't' for 'ml'
  380. func (ml *ml) FmtDateFull(t time.Time) string {
  381. b := make([]byte, 0, 32)
  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{0x2c, 0x20}...)
  388. b = append(b, ml.monthsWide[t.Month()]...)
  389. b = append(b, []byte{0x20}...)
  390. b = strconv.AppendInt(b, int64(t.Day()), 10)
  391. b = append(b, []byte{0x2c, 0x20}...)
  392. b = append(b, ml.daysWide[t.Weekday()]...)
  393. return string(b)
  394. }
  395. // FmtTimeShort returns the short time representation of 't' for 'ml'
  396. func (ml *ml) FmtTimeShort(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, ml.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, []byte{0x20}...)
  409. if t.Hour() < 12 {
  410. b = append(b, ml.periodsAbbreviated[0]...)
  411. } else {
  412. b = append(b, ml.periodsAbbreviated[1]...)
  413. }
  414. return string(b)
  415. }
  416. // FmtTimeMedium returns the medium time representation of 't' for 'ml'
  417. func (ml *ml) FmtTimeMedium(t time.Time) string {
  418. b := make([]byte, 0, 32)
  419. h := t.Hour()
  420. if h > 12 {
  421. h -= 12
  422. }
  423. b = strconv.AppendInt(b, int64(h), 10)
  424. b = append(b, ml.timeSeparator...)
  425. if t.Minute() < 10 {
  426. b = append(b, '0')
  427. }
  428. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  429. b = append(b, ml.timeSeparator...)
  430. if t.Second() < 10 {
  431. b = append(b, '0')
  432. }
  433. b = strconv.AppendInt(b, int64(t.Second()), 10)
  434. b = append(b, []byte{0x20}...)
  435. if t.Hour() < 12 {
  436. b = append(b, ml.periodsAbbreviated[0]...)
  437. } else {
  438. b = append(b, ml.periodsAbbreviated[1]...)
  439. }
  440. return string(b)
  441. }
  442. // FmtTimeLong returns the long time representation of 't' for 'ml'
  443. func (ml *ml) FmtTimeLong(t time.Time) string {
  444. b := make([]byte, 0, 32)
  445. h := t.Hour()
  446. if h > 12 {
  447. h -= 12
  448. }
  449. b = strconv.AppendInt(b, int64(h), 10)
  450. b = append(b, ml.timeSeparator...)
  451. if t.Minute() < 10 {
  452. b = append(b, '0')
  453. }
  454. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  455. b = append(b, ml.timeSeparator...)
  456. if t.Second() < 10 {
  457. b = append(b, '0')
  458. }
  459. b = strconv.AppendInt(b, int64(t.Second()), 10)
  460. b = append(b, []byte{0x20}...)
  461. if t.Hour() < 12 {
  462. b = append(b, ml.periodsAbbreviated[0]...)
  463. } else {
  464. b = append(b, ml.periodsAbbreviated[1]...)
  465. }
  466. b = append(b, []byte{0x20}...)
  467. tz, _ := t.Zone()
  468. b = append(b, tz...)
  469. return string(b)
  470. }
  471. // FmtTimeFull returns the full time representation of 't' for 'ml'
  472. func (ml *ml) FmtTimeFull(t time.Time) string {
  473. b := make([]byte, 0, 32)
  474. h := t.Hour()
  475. if h > 12 {
  476. h -= 12
  477. }
  478. b = strconv.AppendInt(b, int64(h), 10)
  479. b = append(b, ml.timeSeparator...)
  480. if t.Minute() < 10 {
  481. b = append(b, '0')
  482. }
  483. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  484. b = append(b, ml.timeSeparator...)
  485. if t.Second() < 10 {
  486. b = append(b, '0')
  487. }
  488. b = strconv.AppendInt(b, int64(t.Second()), 10)
  489. b = append(b, []byte{0x20}...)
  490. if t.Hour() < 12 {
  491. b = append(b, ml.periodsAbbreviated[0]...)
  492. } else {
  493. b = append(b, ml.periodsAbbreviated[1]...)
  494. }
  495. b = append(b, []byte{0x20}...)
  496. tz, _ := t.Zone()
  497. if btz, ok := ml.timezones[tz]; ok {
  498. b = append(b, btz...)
  499. } else {
  500. b = append(b, tz...)
  501. }
  502. return string(b)
  503. }