pa.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. package pa
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type pa 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 'pa' locale
  39. func New() locales.Translator {
  40. return &pa{
  41. locale: "pa",
  42. pluralsCardinal: []locales.PluralRule{2, 6},
  43. pluralsOrdinal: []locales.PluralRule{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 ", "BYR", "BZD", "CA$", "CDF", "CHE ", "CHF", "CHW ", "CLE ", "CLF ", "CLP", "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", "р.", "RWF", "SAR", "SBD", "SCR", "SDD ", "SDG", "SDP ", "SEK", "SGD", "SHP", "SIT ", "SKK ", "SLL", "SOS", "SRD", "SRG ", "SSP", "STD", "SUR", "SVC ", "SYP", "SZL", "฿", "TJR ", "TJS", "TMM ", "TMT", "TND", "TOP", "TPE ", "TRL ", "TRY", "TTD", "NT$", "TZS", "UAH", "UAK ", "UGS ", "UGX", "US$", "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{"AKST": "ਅਲਾਸਕਾ ਮਿਆਰੀ ਸਮਾਂ", "EDT": "ਉੱਤਰੀ ਅਮਰੀਕੀ ਪੂਰਬੀ ਪ੍ਰਕਾਸ਼ ਸਮਾਂ", "WIT": "ਪੂਰਬੀ ਇੰਡੋਨੇਸ਼ੀਆ ਸਮਾਂ", "ACST": "ਆਸਟ੍ਰੇਲੀਆਈ ਕੇਂਦਰੀ ਮਿਆਰੀ ਸਮਾਂ", "ACDT": "ਆਸਟ੍ਰੇਲੀਆਈ ਕੇਂਦਰੀ ਪ੍ਰਕਾਸ਼ ਸਮਾਂ", "PST": "ਉੱਤਰੀ ਅਮਰੀਕੀ ਪੈਸਿਫਿਕ ਮਿਆਰੀ ਸਮਾਂ", "EST": "ਉੱਤਰੀ ਅਮਰੀਕੀ ਪੂਰਬੀ ਮਿਆਰੀ ਸਮਾਂ", "HAST": "ਹਵਾਈ-ਅਲੇਯੂਸ਼ਿਅਨ ਮਿਆਰੀ ਸਮਾਂ", "OESZ": "ਪੂਰਬੀ ਯੂਰਪੀ ਗਰਮੀ-ਰੁੱਤ ਸਮਾਂ", "EAT": "ਪੂਰਬੀ ਅਫਰੀਕਾ ਸਮਾਂ", "TMT": "ਤੁਰਕਮੇਨਿਸਤਾਨ ਮਿਆਰੀ ਸਮਾਂ", "SRT": "ਸੂਰੀਨਾਮ ਸਮਾਂ", "JDT": "ਜਪਾਨ ਪ੍ਰਕਾਸ਼ ਸਮਾਂ", "MEZ": "ਮੱਧ ਯੂਰਪੀ ਮਿਆਰੀ ਸਮਾਂ", "COT": "ਕੋਲੰਬੀਆ ਮਿਆਰੀ ਸਮਾਂ", "HKST": "ਹਾਂਗ ਕਾਂਗ ਗਰਮੀ-ਰੁੱਤ ਸਮਾਂ", "TMST": "ਤੁਰਕਮੇਨਿਸਤਾਨ ਗਰਮੀ-ਰੁੱਤ ਸਮਾਂ", "ACWDT": "ਆਸਟ੍ਰੇਲੀਆਈ ਕੇਂਦਰੀ ਪੱਛਮੀ ਪ੍ਰਕਾਸ਼ ਸਮਾਂ", "CHADT": "ਚੈਥਮ ਪ੍ਰਕਾਸ਼ ਸਮਾਂ", "AST": "ਅਟਲਾਂਟਿਕ ਮਿਆਰੀ ਸਮਾਂ", "ADT": "ਅਟਲਾਂਟਿਕ ਪ੍ਰਕਾਸ਼ ਸਮਾਂ", "HKT": "ਹਾਂਗ ਕਾਂਗ ਮਿਆਰੀ ਸਮਾਂ", "HNT": "ਨਿਊਫਾਉਂਡਲੈਂਡ ਮਿਆਰੀ ਸਮਾਂ", "WAT": "ਪੱਛਮੀ ਅਫਰੀਕਾ ਮਿਆਰੀ ਸਮਾਂ", "WAST": "ਪੱਛਮੀ ਅਫਰੀਕਾ ਗਰਮੀ-ਰੁੱਤ ਸਮਾਂ", "HAT": "ਨਿਊਫਾਉਂਡਲੈਂਡ ਪ੍ਰਕਾਸ਼ ਸਮਾਂ", "LHST": "ਲੌਰਡ ਹੋਵੇ ਮਿਆਰੀ ਸਮਾਂ", "GYT": "ਗੁਯਾਨਾ ਸਮਾਂ", "JST": "ਜਪਾਨ ਮਿਆਰੀ ਸਮਾਂ", "CST": "ਉੱਤਰੀ ਅਮਰੀਕੀ ਕੇਂਦਰੀ ਮਿਆਰੀ ਸਮਾਂ", "MESZ": "ਮੱਧ ਯੂਰਪੀ ਗਰਮੀ-ਰੁੱਤ ਸਮਾਂ", "CAT": "ਕੇਂਦਰੀ ਅਫਰੀਕਾ ਸਮਾਂ", "OEZ": "ਪੂਰਬੀ ਯੂਰਪੀ ਮਿਆਰੀ ਸਮਾਂ", "NZST": "ਨਿਊਜ਼ੀਲੈਂਡ ਮਿਆਰੀ ਸਮਾਂ", "SAST": "ਦੱਖਣੀ ਅਫ਼ਰੀਕਾ ਮਿਆਰੀ ਸਮਾਂ", "PDT": "ਉੱਤਰੀ ਅਮਰੀਕੀ ਪੈਸਿਫਿਕ ਪ੍ਰਕਾਸ਼ ਸਮਾਂ", "LHDT": "ਲੌਰਡ ਹੋਵੇ ਪ੍ਰਕਾਸ਼ ਸਮਾਂ", "BT": "ਭੂਟਾਨ ਸਮਾਂ", "WIB": "ਪੱਛਮੀ ਇੰਡੋਨੇਸ਼ੀਆ ਸਮਾਂ", "MST": "ਮਕਾਉ ਮਿਆਰੀ ਸਮਾਂ", "∅∅∅": "ਅਮੇਜ਼ਨ ਗਰਮੀ-ਰੁੱਤ ਸਮਾਂ", "WESZ": "ਪੱਛਮੀ ਯੂਰਪੀ ਗਰਮੀ-ਰੁੱਤ ਸਮਾਂ", "CHAST": "ਚੈਥਮ ਮਿਆਰੀ ਸਮਾਂ", "CLST": "ਚਿਲੀ ਗਰਮੀ-ਰੁੱਤ ਸਮਾਂ", "COST": "ਕੋਲੰਬੀਆ ਗਰਮੀ-ਰੁੱਤ ਸਮਾਂ", "WART": "ਪੱਛਮੀ ਅਰਜਨਟੀਨਾ ਮਿਆਰੀ ਸਮਾਂ", "ECT": "ਇਕਵੇਡੋਰ ਸਮਾਂ", "AKDT": "ਅਲਾਸਕਾ ਪ੍ਰਕਾਸ਼ ਸਮਾਂ", "MYT": "ਮਲੇਸ਼ੀਆ ਸਮਾਂ", "NZDT": "ਨਿਊਜ਼ੀਲੈਂਡ ਪ੍ਰਕਾਸ਼ ਸਮਾਂ", "UYT": "ਉਰੂਗਵੇ ਮਿਆਰੀ ਸਮਾਂ", "UYST": "ਉਰੂਗਵੇ ਗਰਮੀ-ਰੁੱਤ ਸਮਾਂ", "ChST": "ਚਾਮੋਰੋ ਮਿਆਰੀ ਸਮਾਂ", "CLT": "ਚਿਲੀ ਮਿਆਰੀ ਸਮਾਂ", "HADT": "ਹਵਾਈ-ਅਲੇਯੂਸ਼ਿਅਨ ਪ੍ਰਕਾਸ਼ ਸਮਾਂ", "VET": "ਵੈਨੇਜ਼ੂਏਲਾ ਸਮਾਂ", "GFT": "ਫ੍ਰੈਂਚ ਗੁਏਨਾ ਸਮਾਂ", "WEZ": "ਪੱਛਮੀ ਯੂਰਪੀ ਮਿਆਰੀ ਸਮਾਂ", "ACWST": "ਆਸਟ੍ਰੇਲੀਆਈ ਕੇਂਦਰੀ ਪੱਛਮੀ ਮਿਆਰੀ ਸਮਾਂ", "SGT": "ਸਿੰਗਾਪੁਰ ਮਿਆਰੀ ਸਮਾਂ", "WITA": "ਮੱਧ ਇੰਡੋਨੇਸ਼ੀਆਈ ਸਮਾਂ", "ART": "ਅਰਜਨਟੀਨਾ ਮਿਆਰੀ ਸਮਾਂ", "AWST": "ਆਸਟ੍ਰੇਲੀਆਈ ਪੱਛਮੀ ਮਿਆਰੀ ਸਮਾਂ", "ARST": "ਅਰਜਨਟੀਨਾ ਗਰਮੀ-ਰੁੱਤ ਸਮਾਂ", "AWDT": "ਆਸਟ੍ਰੇਲੀਆਈ ਪੱਛਮੀ ਪ੍ਰਕਾਸ਼ ਸਮਾਂ", "CDT": "ਉੱਤਰੀ ਅਮਰੀਕੀ ਕੇਂਦਰੀ ਪ੍ਰਕਾਸ਼ ਸਮਾਂ", "AEST": "ਆਸਟ੍ਰੇਲੀਆਈ ਪੂਰਬੀ ਮਿਆਰੀ ਸਮਾਂ", "AEDT": "ਆਸਟ੍ਰੇਲੀਆਈ ਪੂਰਬੀ ਪ੍ਰਕਾਸ਼ ਸਮਾਂ", "MDT": "ਮਕਾਉ ਗਰਮੀ-ਰੁੱਤ ਸਮਾਂ", "BOT": "ਬੋਲੀਵੀਆ ਸਮਾਂ", "WARST": "ਪੱਛਮੀ ਅਰਜਨਟੀਨਾ ਗਰਮੀ-ਰੁੱਤ ਸਮਾਂ", "IST": "ਭਾਰਤੀ ਮਿਆਰੀ ਸਮਾਂ", "GMT": "ਗ੍ਰੀਨਵਿਚ ਮੀਨ ਟਾਈਮ"},
  67. }
  68. }
  69. // Locale returns the current translators string locale
  70. func (pa *pa) Locale() string {
  71. return pa.locale
  72. }
  73. // PluralsCardinal returns the list of cardinal plural rules associated with 'pa'
  74. func (pa *pa) PluralsCardinal() []locales.PluralRule {
  75. return pa.pluralsCardinal
  76. }
  77. // PluralsOrdinal returns the list of ordinal plural rules associated with 'pa'
  78. func (pa *pa) PluralsOrdinal() []locales.PluralRule {
  79. return pa.pluralsOrdinal
  80. }
  81. // PluralsRange returns the list of range plural rules associated with 'pa'
  82. func (pa *pa) PluralsRange() []locales.PluralRule {
  83. return pa.pluralsRange
  84. }
  85. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'pa'
  86. func (pa *pa) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  87. n := math.Abs(num)
  88. if n >= 0 && n <= 1 {
  89. return locales.PluralRuleOne
  90. }
  91. return locales.PluralRuleOther
  92. }
  93. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'pa'
  94. func (pa *pa) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  95. return locales.PluralRuleOther
  96. }
  97. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'pa'
  98. func (pa *pa) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  99. start := pa.CardinalPluralRule(num1, v1)
  100. end := pa.CardinalPluralRule(num2, v2)
  101. if start == locales.PluralRuleOne && end == locales.PluralRuleOne {
  102. return locales.PluralRuleOne
  103. } else if start == locales.PluralRuleOne && end == locales.PluralRuleOther {
  104. return locales.PluralRuleOther
  105. }
  106. return locales.PluralRuleOther
  107. }
  108. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  109. func (pa *pa) MonthAbbreviated(month time.Month) string {
  110. return pa.monthsAbbreviated[month]
  111. }
  112. // MonthsAbbreviated returns the locales abbreviated months
  113. func (pa *pa) MonthsAbbreviated() []string {
  114. return pa.monthsAbbreviated[1:]
  115. }
  116. // MonthNarrow returns the locales narrow month given the 'month' provided
  117. func (pa *pa) MonthNarrow(month time.Month) string {
  118. return pa.monthsNarrow[month]
  119. }
  120. // MonthsNarrow returns the locales narrow months
  121. func (pa *pa) MonthsNarrow() []string {
  122. return pa.monthsNarrow[1:]
  123. }
  124. // MonthWide returns the locales wide month given the 'month' provided
  125. func (pa *pa) MonthWide(month time.Month) string {
  126. return pa.monthsWide[month]
  127. }
  128. // MonthsWide returns the locales wide months
  129. func (pa *pa) MonthsWide() []string {
  130. return pa.monthsWide[1:]
  131. }
  132. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  133. func (pa *pa) WeekdayAbbreviated(weekday time.Weekday) string {
  134. return pa.daysAbbreviated[weekday]
  135. }
  136. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  137. func (pa *pa) WeekdaysAbbreviated() []string {
  138. return pa.daysAbbreviated
  139. }
  140. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  141. func (pa *pa) WeekdayNarrow(weekday time.Weekday) string {
  142. return pa.daysNarrow[weekday]
  143. }
  144. // WeekdaysNarrow returns the locales narrow weekdays
  145. func (pa *pa) WeekdaysNarrow() []string {
  146. return pa.daysNarrow
  147. }
  148. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  149. func (pa *pa) WeekdayShort(weekday time.Weekday) string {
  150. return pa.daysShort[weekday]
  151. }
  152. // WeekdaysShort returns the locales short weekdays
  153. func (pa *pa) WeekdaysShort() []string {
  154. return pa.daysShort
  155. }
  156. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  157. func (pa *pa) WeekdayWide(weekday time.Weekday) string {
  158. return pa.daysWide[weekday]
  159. }
  160. // WeekdaysWide returns the locales wide weekdays
  161. func (pa *pa) WeekdaysWide() []string {
  162. return pa.daysWide
  163. }
  164. // FmtNumber returns 'num' with digits/precision of 'v' for 'pa' and handles both Whole and Real numbers based on 'v'
  165. func (pa *pa) FmtNumber(num float64, v uint64) string {
  166. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  167. l := len(s) + 3 + 1*len(s[:len(s)-int(v)-1])/3
  168. count := 0
  169. inWhole := v == 0
  170. inSecondary := false
  171. groupThreshold := 3
  172. b := make([]byte, 0, l)
  173. for i := len(s) - 1; i >= 0; i-- {
  174. if s[i] == '.' {
  175. for j := len(pa.decimal) - 1; j >= 0; j-- {
  176. b = append(b, pa.decimal[j])
  177. }
  178. inWhole = true
  179. continue
  180. }
  181. if inWhole {
  182. if count == groupThreshold {
  183. b = append(b, pa.group[0])
  184. count = 1
  185. if !inSecondary {
  186. inSecondary = true
  187. groupThreshold = 2
  188. }
  189. } else {
  190. count++
  191. }
  192. }
  193. b = append(b, s[i])
  194. }
  195. if num < 0 {
  196. b = append(b, pa.minus[0])
  197. }
  198. // reverse
  199. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  200. b[i], b[j] = b[j], b[i]
  201. }
  202. return string(b)
  203. }
  204. // FmtPercent returns 'num' with digits/precision of 'v' for 'pa' and handles both Whole and Real numbers based on 'v'
  205. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  206. func (pa *pa) FmtPercent(num float64, v uint64) string {
  207. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  208. l := len(s) + 5
  209. b := make([]byte, 0, l)
  210. for i := len(s) - 1; i >= 0; i-- {
  211. if s[i] == '.' {
  212. for j := len(pa.decimal) - 1; j >= 0; j-- {
  213. b = append(b, pa.decimal[j])
  214. }
  215. continue
  216. }
  217. b = append(b, s[i])
  218. }
  219. if num < 0 {
  220. b = append(b, pa.minus[0])
  221. }
  222. // reverse
  223. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  224. b[i], b[j] = b[j], b[i]
  225. }
  226. b = append(b, pa.percent...)
  227. return string(b)
  228. }
  229. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'pa'
  230. func (pa *pa) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  231. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  232. symbol := pa.currencies[currency]
  233. l := len(s) + len(symbol) + 3 + 1*len(s[:len(s)-int(v)-1])/3
  234. count := 0
  235. inWhole := v == 0
  236. inSecondary := false
  237. groupThreshold := 3
  238. b := make([]byte, 0, l)
  239. for i := len(s) - 1; i >= 0; i-- {
  240. if s[i] == '.' {
  241. for j := len(pa.decimal) - 1; j >= 0; j-- {
  242. b = append(b, pa.decimal[j])
  243. }
  244. inWhole = true
  245. continue
  246. }
  247. if inWhole {
  248. if count == groupThreshold {
  249. b = append(b, pa.group[0])
  250. count = 1
  251. if !inSecondary {
  252. inSecondary = true
  253. groupThreshold = 2
  254. }
  255. } else {
  256. count++
  257. }
  258. }
  259. b = append(b, s[i])
  260. }
  261. for j := len(symbol) - 1; j >= 0; j-- {
  262. b = append(b, symbol[j])
  263. }
  264. if num < 0 {
  265. b = append(b, pa.minus[0])
  266. }
  267. // reverse
  268. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  269. b[i], b[j] = b[j], b[i]
  270. }
  271. if int(v) < 2 {
  272. if v == 0 {
  273. b = append(b, pa.decimal...)
  274. }
  275. for i := 0; i < 2-int(v); i++ {
  276. b = append(b, '0')
  277. }
  278. }
  279. return string(b)
  280. }
  281. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'pa'
  282. // in accounting notation.
  283. func (pa *pa) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  284. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  285. symbol := pa.currencies[currency]
  286. l := len(s) + len(symbol) + 3 + 1*len(s[:len(s)-int(v)-1])/3
  287. count := 0
  288. inWhole := v == 0
  289. inSecondary := false
  290. groupThreshold := 3
  291. b := make([]byte, 0, l)
  292. for i := len(s) - 1; i >= 0; i-- {
  293. if s[i] == '.' {
  294. for j := len(pa.decimal) - 1; j >= 0; j-- {
  295. b = append(b, pa.decimal[j])
  296. }
  297. inWhole = true
  298. continue
  299. }
  300. if inWhole {
  301. if count == groupThreshold {
  302. b = append(b, pa.group[0])
  303. count = 1
  304. if !inSecondary {
  305. inSecondary = true
  306. groupThreshold = 2
  307. }
  308. } else {
  309. count++
  310. }
  311. }
  312. b = append(b, s[i])
  313. }
  314. if num < 0 {
  315. for j := len(symbol) - 1; j >= 0; j-- {
  316. b = append(b, symbol[j])
  317. }
  318. b = append(b, pa.minus[0])
  319. } else {
  320. for j := len(symbol) - 1; j >= 0; j-- {
  321. b = append(b, symbol[j])
  322. }
  323. }
  324. // reverse
  325. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  326. b[i], b[j] = b[j], b[i]
  327. }
  328. if int(v) < 2 {
  329. if v == 0 {
  330. b = append(b, pa.decimal...)
  331. }
  332. for i := 0; i < 2-int(v); i++ {
  333. b = append(b, '0')
  334. }
  335. }
  336. return string(b)
  337. }
  338. // FmtDateShort returns the short date representation of 't' for 'pa'
  339. func (pa *pa) FmtDateShort(t time.Time) string {
  340. b := make([]byte, 0, 32)
  341. b = strconv.AppendInt(b, int64(t.Day()), 10)
  342. b = append(b, []byte{0x2f}...)
  343. b = strconv.AppendInt(b, int64(t.Month()), 10)
  344. b = append(b, []byte{0x2f}...)
  345. if t.Year() > 9 {
  346. b = append(b, strconv.Itoa(t.Year())[2:]...)
  347. } else {
  348. b = append(b, strconv.Itoa(t.Year())[1:]...)
  349. }
  350. return string(b)
  351. }
  352. // FmtDateMedium returns the medium date representation of 't' for 'pa'
  353. func (pa *pa) FmtDateMedium(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, pa.monthsAbbreviated[t.Month()]...)
  358. b = append(b, []byte{0x20}...)
  359. b = strconv.AppendInt(b, int64(t.Year()), 10)
  360. return string(b)
  361. }
  362. // FmtDateLong returns the long date representation of 't' for 'pa'
  363. func (pa *pa) FmtDateLong(t time.Time) string {
  364. b := make([]byte, 0, 32)
  365. b = strconv.AppendInt(b, int64(t.Day()), 10)
  366. b = append(b, []byte{0x20}...)
  367. b = append(b, pa.monthsWide[t.Month()]...)
  368. b = append(b, []byte{0x20}...)
  369. b = strconv.AppendInt(b, int64(t.Year()), 10)
  370. return string(b)
  371. }
  372. // FmtDateFull returns the full date representation of 't' for 'pa'
  373. func (pa *pa) FmtDateFull(t time.Time) string {
  374. b := make([]byte, 0, 32)
  375. b = append(b, pa.daysWide[t.Weekday()]...)
  376. b = append(b, []byte{0x2c, 0x20}...)
  377. b = strconv.AppendInt(b, int64(t.Day()), 10)
  378. b = append(b, []byte{0x20}...)
  379. b = append(b, pa.monthsWide[t.Month()]...)
  380. b = append(b, []byte{0x20}...)
  381. b = strconv.AppendInt(b, int64(t.Year()), 10)
  382. return string(b)
  383. }
  384. // FmtTimeShort returns the short time representation of 't' for 'pa'
  385. func (pa *pa) FmtTimeShort(t time.Time) string {
  386. b := make([]byte, 0, 32)
  387. h := t.Hour()
  388. if h > 12 {
  389. h -= 12
  390. }
  391. b = strconv.AppendInt(b, int64(h), 10)
  392. b = append(b, pa.timeSeparator...)
  393. if t.Minute() < 10 {
  394. b = append(b, '0')
  395. }
  396. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  397. b = append(b, []byte{0x20}...)
  398. if t.Hour() < 12 {
  399. b = append(b, pa.periodsAbbreviated[0]...)
  400. } else {
  401. b = append(b, pa.periodsAbbreviated[1]...)
  402. }
  403. return string(b)
  404. }
  405. // FmtTimeMedium returns the medium time representation of 't' for 'pa'
  406. func (pa *pa) FmtTimeMedium(t time.Time) string {
  407. b := make([]byte, 0, 32)
  408. h := t.Hour()
  409. if h > 12 {
  410. h -= 12
  411. }
  412. b = strconv.AppendInt(b, int64(h), 10)
  413. b = append(b, pa.timeSeparator...)
  414. if t.Minute() < 10 {
  415. b = append(b, '0')
  416. }
  417. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  418. b = append(b, pa.timeSeparator...)
  419. if t.Second() < 10 {
  420. b = append(b, '0')
  421. }
  422. b = strconv.AppendInt(b, int64(t.Second()), 10)
  423. b = append(b, []byte{0x20}...)
  424. if t.Hour() < 12 {
  425. b = append(b, pa.periodsAbbreviated[0]...)
  426. } else {
  427. b = append(b, pa.periodsAbbreviated[1]...)
  428. }
  429. return string(b)
  430. }
  431. // FmtTimeLong returns the long time representation of 't' for 'pa'
  432. func (pa *pa) FmtTimeLong(t time.Time) string {
  433. b := make([]byte, 0, 32)
  434. h := t.Hour()
  435. if h > 12 {
  436. h -= 12
  437. }
  438. b = strconv.AppendInt(b, int64(h), 10)
  439. b = append(b, pa.timeSeparator...)
  440. if t.Minute() < 10 {
  441. b = append(b, '0')
  442. }
  443. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  444. b = append(b, pa.timeSeparator...)
  445. if t.Second() < 10 {
  446. b = append(b, '0')
  447. }
  448. b = strconv.AppendInt(b, int64(t.Second()), 10)
  449. b = append(b, []byte{0x20}...)
  450. if t.Hour() < 12 {
  451. b = append(b, pa.periodsAbbreviated[0]...)
  452. } else {
  453. b = append(b, pa.periodsAbbreviated[1]...)
  454. }
  455. b = append(b, []byte{0x20}...)
  456. tz, _ := t.Zone()
  457. b = append(b, tz...)
  458. return string(b)
  459. }
  460. // FmtTimeFull returns the full time representation of 't' for 'pa'
  461. func (pa *pa) FmtTimeFull(t time.Time) string {
  462. b := make([]byte, 0, 32)
  463. h := t.Hour()
  464. if h > 12 {
  465. h -= 12
  466. }
  467. b = strconv.AppendInt(b, int64(h), 10)
  468. b = append(b, pa.timeSeparator...)
  469. if t.Minute() < 10 {
  470. b = append(b, '0')
  471. }
  472. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  473. b = append(b, pa.timeSeparator...)
  474. if t.Second() < 10 {
  475. b = append(b, '0')
  476. }
  477. b = strconv.AppendInt(b, int64(t.Second()), 10)
  478. b = append(b, []byte{0x20}...)
  479. if t.Hour() < 12 {
  480. b = append(b, pa.periodsAbbreviated[0]...)
  481. } else {
  482. b = append(b, pa.periodsAbbreviated[1]...)
  483. }
  484. b = append(b, []byte{0x20}...)
  485. tz, _ := t.Zone()
  486. if btz, ok := pa.timezones[tz]; ok {
  487. b = append(b, btz...)
  488. } else {
  489. b = append(b, tz...)
  490. }
  491. return string(b)
  492. }