...

Source file src/github.com/go-playground/locales/or/or.go

Documentation: github.com/go-playground/locales/or

     1  package or
     2  
     3  import (
     4  	"math"
     5  	"strconv"
     6  	"time"
     7  
     8  	"github.com/go-playground/locales"
     9  	"github.com/go-playground/locales/currency"
    10  )
    11  
    12  type or struct {
    13  	locale                 string
    14  	pluralsCardinal        []locales.PluralRule
    15  	pluralsOrdinal         []locales.PluralRule
    16  	pluralsRange           []locales.PluralRule
    17  	decimal                string
    18  	group                  string
    19  	minus                  string
    20  	percent                string
    21  	perMille               string
    22  	timeSeparator          string
    23  	inifinity              string
    24  	currencies             []string // idx = enum of currency code
    25  	currencyNegativePrefix string
    26  	currencyNegativeSuffix string
    27  	monthsAbbreviated      []string
    28  	monthsNarrow           []string
    29  	monthsWide             []string
    30  	daysAbbreviated        []string
    31  	daysNarrow             []string
    32  	daysShort              []string
    33  	daysWide               []string
    34  	periodsAbbreviated     []string
    35  	periodsNarrow          []string
    36  	periodsShort           []string
    37  	periodsWide            []string
    38  	erasAbbreviated        []string
    39  	erasNarrow             []string
    40  	erasWide               []string
    41  	timezones              map[string]string
    42  }
    43  
    44  // New returns a new instance of translator for the 'or' locale
    45  func New() locales.Translator {
    46  	return &or{
    47  		locale:                 "or",
    48  		pluralsCardinal:        []locales.PluralRule{2, 6},
    49  		pluralsOrdinal:         []locales.PluralRule{2, 3, 4, 5, 6},
    50  		pluralsRange:           []locales.PluralRule{2, 6},
    51  		decimal:                ".",
    52  		group:                  ",",
    53  		minus:                  "-",
    54  		percent:                "%",
    55  		perMille:               "‰",
    56  		timeSeparator:          ":",
    57  		inifinity:              "∞",
    58  		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", "MRU", "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", "THB", "TJR", "TJS", "TMM", "TMT", "TND", "TOP", "TPE", "TRL", "TRY", "TTD", "NT$", "TZS", "UAH", "UAK", "UGS", "UGX", "$", "USN", "USS", "UYI", "UYP", "UYU", "UYW", "UZS", "VEB", "VEF", "VES", "₫", "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"},
    59  		currencyNegativePrefix: "(",
    60  		currencyNegativeSuffix: ")",
    61  		monthsAbbreviated:      []string{"", "ଜାନୁଆରୀ", "ଫେବୃଆରୀ", "ମାର୍ଚ୍ଚ", "ଅପ୍ରେଲ", "ମଇ", "ଜୁନ", "ଜୁଲାଇ", "ଅଗଷ୍ଟ", "ସେପ୍ଟେମ୍ବର", "ଅକ୍ଟୋବର", "ନଭେମ୍ବର", "ଡିସେମ୍ବର"},
    62  		monthsNarrow:           []string{"", "ଜା", "ଫେ", "ମା", "ଅ", "ମଇ", "ଜୁ", "ଜୁ", "ଅ", "ସେ", "ଅ", "ନ", "ଡି"},
    63  		monthsWide:             []string{"", "ଜାନୁଆରୀ", "ଫେବୃଆରୀ", "ମାର୍ଚ୍ଚ", "ଅପ୍ରେଲ", "ମଇ", "ଜୁନ", "ଜୁଲାଇ", "ଅଗଷ୍ଟ", "ସେପ୍ଟେମ୍ବର", "ଅକ୍ଟୋବର", "ନଭେମ୍ବର", "ଡିସେମ୍ବର"},
    64  		daysAbbreviated:        []string{"ରବି", "ସୋମ", "ମଙ୍ଗଳ", "ବୁଧ", "ଗୁରୁ", "ଶୁକ୍ର", "ଶନି"},
    65  		daysNarrow:             []string{"ର", "ସୋ", "ମ", "ବୁ", "ଗୁ", "ଶୁ", "ଶ"},
    66  		daysShort:              []string{"ରବି", "ସୋମ", "ମଙ୍ଗଳ", "ବୁଧ", "ଗୁରୁ", "ଶୁକ୍ର", "ଶନି"},
    67  		daysWide:               []string{"ରବିବାର", "ସୋମବାର", "ମଙ୍ଗଳବାର", "ବୁଧବାର", "ଗୁରୁବାର", "ଶୁକ୍ରବାର", "ଶନିବାର"},
    68  		periodsAbbreviated:     []string{"AM", "PM"},
    69  		periodsNarrow:          []string{"ପୂ", "ଅ"},
    70  		periodsWide:            []string{"AM", "PM"},
    71  		erasAbbreviated:        []string{"BC", "AD"},
    72  		erasNarrow:             []string{"", ""},
    73  		erasWide:               []string{"ଖ୍ରୀଷ୍ଟପୂର୍ବ", "ଖ୍ରୀଷ୍ଟାବ୍ଦ"},
    74  		timezones:              map[string]string{"ACDT": "ଅଷ୍ଟ୍ରେଲିୟ ମଧ୍ୟ ଦିବାଲୋକ ସମୟ", "ACST": "ଅଷ୍ଟ୍ରେଲିୟ ମଧ୍ୟ ମାନାଙ୍କ ସମୟ", "ACWDT": "ଅଷ୍ଟ୍ରେଲିୟ ମଧ୍ୟ ପଶ୍ଚିମ ଦିବାଲୋକ ସମୟ", "ACWST": "ଅଷ୍ଟ୍ରେଲିୟ ମଧ୍ୟ ପଶ୍ଚିମ ମାନାଙ୍କ ସମୟ", "ADT": "ଆଟଲାଣ୍ଟିକ୍ ଦିବାଲୋକ ସମୟ", "AEDT": "ଅଷ୍ଟ୍ରେଲିୟ ପୂର୍ବ ଦିବାଲୋକ ସମୟ", "AEST": "ଅଷ୍ଟ୍ରେଲିୟ ପୂର୍ବ ମାନାଙ୍କ ସମୟ", "AKDT": "ଆଲାସ୍କା ଦିବାଲୋକ ସମୟ", "AKST": "ଆଲାସ୍କା ମାନାଙ୍କ ସମୟ", "ARST": "ଆର୍ଜେଣ୍ଟିନା ଗ୍ରୀଷ୍ମକାଳ ସମୟ", "ART": "ଆର୍ଜେଣ୍ଟିନା ମାନାଙ୍କ ସମୟ", "AST": "ଆଟଲାଣ୍ଟିକ୍ ମାନାଙ୍କ ସମୟ", "AWDT": "ଅଷ୍ଟ୍ରେଲିୟ ପଶ୍ଚିମ ଦିବାଲୋକ ସମୟ", "AWST": "ଅଷ୍ଟ୍ରେଲିୟ ପଶ୍ଚିମ ମାନାଙ୍କ ସମୟ", "BOT": "ବଲିଭିଆ ସମୟ", "BT": "ଭୁଟାନ ସମୟ", "CAT": "ମଧ୍ୟ ଆଫ୍ରିକା ସମୟ", "CDT": "କେନ୍ଦ୍ରୀୟ ଦିବାଲୋକ ସମୟ", "CHADT": "ଚାଥାମ୍\u200c ଦିବାଲୋକ ସମୟ", "CHAST": "ଚାଥାମ୍\u200c ମାନାଙ୍କ ସମୟ", "CLST": "ଚିଲି ଗ୍ରୀଷ୍ମକାଳ ସମୟ", "CLT": "ଚିଲି ମାନାଙ୍କ ସମୟ", "COST": "କଲମ୍ବିଆ ଗ୍ରୀଷ୍ମକାଳ ସମୟ", "COT": "କଲମ୍ବିଆ ମାନାଙ୍କ ସମୟ", "CST": "କେନ୍ଦ୍ରୀୟ ମାନାଙ୍କ ସମୟ", "ChST": "ଚାମୋରୋ ମାନାଙ୍କ ସମୟ", "EAT": "ପୂର୍ବ ଆଫ୍ରିକା ସମୟ", "ECT": "ଇକ୍ୱେଡର ସମୟ", "EDT": "ପୂର୍ବାଞ୍ଚଳ ଦିବାଲୋକ ସମୟ", "EST": "ପୂର୍ବାଞ୍ଚଳ ମାନାଙ୍କ ସମୟ", "GFT": "ଫ୍ରେଞ୍ଚ ଗୁଆନା ସମୟ", "GMT": "ଗ୍ରୀନୱିଚ୍ ମିନ୍ ସମୟ", "GST": "ଗଲ୍ଫ ମାନାଙ୍କ ସମୟ", "GYT": "ଗୁଏନା ସମୟ", "HADT": "ହୱାଇ-ଆଲେଉଟିୟ ଦିବାଲୋକ ସମୟ", "HAST": "ହୱାଇ-ଆଲେଉଟିୟ ମାନାଙ୍କ ସମୟ", "HAT": "ନ୍ୟୁଫାଉଣ୍ଡଲ୍ୟାଣ୍ଡ୍ ଦିବାଲୋକ ସମୟ", "HECU": "କ୍ୟୁବା ଦିବାଲୋକ ସମୟ", "HEEG": "ପୂର୍ବ ଗ୍ରୀନଲ୍ୟାଣ୍ଡ୍ ଗ୍ରୀଷ୍ମକାଳ ସମୟ", "HENOMX": "ଉତ୍ତରପଶ୍ଚିମ ମେକ୍ସିକୋ ଦିବାଲୋକ ସମୟ", "HEOG": "ପଶ୍ଚିମ ଗ୍ରୀନଲ୍ୟାଣ୍ଡ୍ ଗ୍ରୀଷ୍ମ ସମୟ", "HEPM": "ସେଣ୍ଟ. ପିଏରେ ଏବଂ ମିକ୍ୟୁଲୋନ୍ ଦିବାଲୋକ ସମୟ", "HEPMX": "ମେକ୍ସିକାନ୍ ପାସିଫିକ୍ ଦିବାଲୋକ ସମୟ", "HKST": "ହଂ କଂ ଗ୍ରୀଷ୍ମକାଳ ସମୟ", "HKT": "ହଂ କଂ ମାନାଙ୍କ ସମୟ", "HNCU": "କ୍ୟୁବା ମାନାଙ୍କ ସମୟ", "HNEG": "ପୂର୍ବ ଗ୍ରୀନଲ୍ୟାଣ୍ଡ୍ ମାନାଙ୍କ ସମୟ", "HNNOMX": "ଉତ୍ତରପଶ୍ଚିମ ମେକ୍ସିକୋ ମାନାଙ୍କ ସମୟ", "HNOG": "ପଶ୍ଚିମ ଗ୍ରୀନଲ୍ୟାଣ୍ଡ୍ ମାନାଙ୍କ ସମୟ", "HNPM": "ସେଣ୍ଟ. ପିଏରେ ଏବଂ ମିକ୍ୟୁଲୋନ୍ ମାନାଙ୍କ ସମୟ", "HNPMX": "ମେକ୍ସିକାନ୍ ପାସିଫିକ୍ ମାନାଙ୍କ ସମୟ", "HNT": "ନ୍ୟୁଫାଉଣ୍ଡଲ୍ୟାଣ୍ଡ୍ ମାନାଙ୍କ ସମୟ", "IST": "ଭାରତ ମାନାଙ୍କ ସମୟ", "JDT": "ଜାପାନ ଦିବାଲୋକ ସମୟ", "JST": "ଜାପାନ ମାନାଙ୍କ ସମୟ", "LHDT": "ଲର୍ଡ ହୋୱେ ଦିବାଲୋକ ସମୟ", "LHST": "ଲର୍ଡ ହୋୱେ ମାନାଙ୍କ ସମୟ", "MDT": "ପାର୍ବତ୍ୟ ଦିବାଲୋକ ସମୟ", "MESZ": "କେନ୍ଦ୍ରୀୟ ୟୁରୋପୀୟ ଗ୍ରୀଷ୍ମକାଳ ସମୟ", "MEZ": "କେନ୍ଦ୍ରୀୟ ୟୁରୋପୀୟ ମାନାଙ୍କ ସମୟ", "MST": "ପାର୍ବତ୍ୟ ମାନାଙ୍କ ସମୟ", "MYT": "ମାଲେସିଆ ସମୟ", "NZDT": "ନ୍ୟୁଜିଲାଣ୍ଡ ଦିବାଲୋକ ସମୟ", "NZST": "ନ୍ୟୁଜିଲାଣ୍ଡ ମାନାଙ୍କ ସମୟ", "OESZ": "ପୂର୍ବାଞ୍ଚଳ ୟୁରୋପୀୟ ଗ୍ରୀଷ୍ମକାଳ ସମୟ", "OEZ": "ପୂର୍ବାଞ୍ଚଳ ୟୁରୋପୀୟ ମାନାଙ୍କ ସମୟ", "PDT": "ପାସିଫିକ୍ ଦିବାଲୋକ ସମୟ", "PST": "ପାସିଫିକ୍ ମାନାଙ୍କ ସମୟ", "SAST": "ଦକ୍ଷିଣ ଆଫ୍ରିକା ମାନାଙ୍କ ସମୟ", "SGT": "ସିଙ୍ଗାପୁର୍\u200c ମାନାଙ୍କ ସମୟ", "SRT": "ସୁରିନେମ୍\u200c ସମୟ", "TMST": "ତୁର୍କମେନିସ୍ତାନ ଖରାଦିନ ସମୟ", "TMT": "ତୁର୍କମେନିସ୍ତାନ ମାନାଙ୍କ ସମୟ", "UYST": "ଉରୁଗୁଏ ଗ୍ରୀଷ୍ମକାଳ ସମୟ", "UYT": "ଉରୁଗୁଏ ମାନାଙ୍କ ସମୟ", "VET": "ଭେନିଜୁଏଲା ସମୟ", "WARST": "ପଶ୍ଚିମ ଆର୍ଜେଣ୍ଟିନା ଗ୍ରୀଷ୍ମକାଳ ସମୟ", "WART": "ପଶ୍ଚିମ ଆର୍ଜେଣ୍ଟିନା ମାନାଙ୍କ ସମୟ", "WAST": "ପଶ୍ଚିମ ଆଫ୍ରିକା ଖରାଦିନ ସମୟ", "WAT": "ପଶ୍ଚିମ ଆଫ୍ରିକା ମାନାଙ୍କ ସମୟ", "WESZ": "ପଶ୍ଚିମାଞ୍ଚଳ ୟୁରୋପୀୟ ଗ୍ରୀଷ୍ମକାଳ ସମୟ", "WEZ": "ପଶ୍ଚିମାଞ୍ଚଳ ୟୁରୋପୀୟ ମାନାଙ୍କ ସମୟ", "WIB": "ପଶ୍ଚିମ ଇଣ୍ଡୋନେସିଆ ସମୟ", "WIT": "ପୂର୍ବ ଇଣ୍ଡୋନେସିଆ ସମୟ", "WITA": "ମଧ୍ୟ ଇଣ୍ଡୋନେସିଆ ସମୟ", "∅∅∅": "ଆଜୋରେସ୍ ଗ୍ରୀଷ୍ମକାଳ ସମୟ"},
    75  	}
    76  }
    77  
    78  // Locale returns the current translators string locale
    79  func (or *or) Locale() string {
    80  	return or.locale
    81  }
    82  
    83  // PluralsCardinal returns the list of cardinal plural rules associated with 'or'
    84  func (or *or) PluralsCardinal() []locales.PluralRule {
    85  	return or.pluralsCardinal
    86  }
    87  
    88  // PluralsOrdinal returns the list of ordinal plural rules associated with 'or'
    89  func (or *or) PluralsOrdinal() []locales.PluralRule {
    90  	return or.pluralsOrdinal
    91  }
    92  
    93  // PluralsRange returns the list of range plural rules associated with 'or'
    94  func (or *or) PluralsRange() []locales.PluralRule {
    95  	return or.pluralsRange
    96  }
    97  
    98  // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'or'
    99  func (or *or) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
   100  
   101  	n := math.Abs(num)
   102  
   103  	if n == 1 {
   104  		return locales.PluralRuleOne
   105  	}
   106  
   107  	return locales.PluralRuleOther
   108  }
   109  
   110  // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'or'
   111  func (or *or) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
   112  
   113  	n := math.Abs(num)
   114  
   115  	if n == 1 || n == 5 || n >= 7 && n <= 9 {
   116  		return locales.PluralRuleOne
   117  	} else if n == 2 || n == 3 {
   118  		return locales.PluralRuleTwo
   119  	} else if n == 4 {
   120  		return locales.PluralRuleFew
   121  	} else if n == 6 {
   122  		return locales.PluralRuleMany
   123  	}
   124  
   125  	return locales.PluralRuleOther
   126  }
   127  
   128  // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'or'
   129  func (or *or) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
   130  
   131  	start := or.CardinalPluralRule(num1, v1)
   132  	end := or.CardinalPluralRule(num2, v2)
   133  
   134  	if start == locales.PluralRuleOne && end == locales.PluralRuleOne {
   135  		return locales.PluralRuleOther
   136  	} else if start == locales.PluralRuleOne && end == locales.PluralRuleOther {
   137  		return locales.PluralRuleOther
   138  	} else if start == locales.PluralRuleOther && end == locales.PluralRuleOne {
   139  		return locales.PluralRuleOne
   140  	}
   141  
   142  	return locales.PluralRuleOther
   143  
   144  }
   145  
   146  // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
   147  func (or *or) MonthAbbreviated(month time.Month) string {
   148  	return or.monthsAbbreviated[month]
   149  }
   150  
   151  // MonthsAbbreviated returns the locales abbreviated months
   152  func (or *or) MonthsAbbreviated() []string {
   153  	return or.monthsAbbreviated[1:]
   154  }
   155  
   156  // MonthNarrow returns the locales narrow month given the 'month' provided
   157  func (or *or) MonthNarrow(month time.Month) string {
   158  	return or.monthsNarrow[month]
   159  }
   160  
   161  // MonthsNarrow returns the locales narrow months
   162  func (or *or) MonthsNarrow() []string {
   163  	return or.monthsNarrow[1:]
   164  }
   165  
   166  // MonthWide returns the locales wide month given the 'month' provided
   167  func (or *or) MonthWide(month time.Month) string {
   168  	return or.monthsWide[month]
   169  }
   170  
   171  // MonthsWide returns the locales wide months
   172  func (or *or) MonthsWide() []string {
   173  	return or.monthsWide[1:]
   174  }
   175  
   176  // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
   177  func (or *or) WeekdayAbbreviated(weekday time.Weekday) string {
   178  	return or.daysAbbreviated[weekday]
   179  }
   180  
   181  // WeekdaysAbbreviated returns the locales abbreviated weekdays
   182  func (or *or) WeekdaysAbbreviated() []string {
   183  	return or.daysAbbreviated
   184  }
   185  
   186  // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
   187  func (or *or) WeekdayNarrow(weekday time.Weekday) string {
   188  	return or.daysNarrow[weekday]
   189  }
   190  
   191  // WeekdaysNarrow returns the locales narrow weekdays
   192  func (or *or) WeekdaysNarrow() []string {
   193  	return or.daysNarrow
   194  }
   195  
   196  // WeekdayShort returns the locales short weekday given the 'weekday' provided
   197  func (or *or) WeekdayShort(weekday time.Weekday) string {
   198  	return or.daysShort[weekday]
   199  }
   200  
   201  // WeekdaysShort returns the locales short weekdays
   202  func (or *or) WeekdaysShort() []string {
   203  	return or.daysShort
   204  }
   205  
   206  // WeekdayWide returns the locales wide weekday given the 'weekday' provided
   207  func (or *or) WeekdayWide(weekday time.Weekday) string {
   208  	return or.daysWide[weekday]
   209  }
   210  
   211  // WeekdaysWide returns the locales wide weekdays
   212  func (or *or) WeekdaysWide() []string {
   213  	return or.daysWide
   214  }
   215  
   216  // Decimal returns the decimal point of number
   217  func (or *or) Decimal() string {
   218  	return or.decimal
   219  }
   220  
   221  // Group returns the group of number
   222  func (or *or) Group() string {
   223  	return or.group
   224  }
   225  
   226  // Group returns the minus sign of number
   227  func (or *or) Minus() string {
   228  	return or.minus
   229  }
   230  
   231  // FmtNumber returns 'num' with digits/precision of 'v' for 'or' and handles both Whole and Real numbers based on 'v'
   232  func (or *or) FmtNumber(num float64, v uint64) string {
   233  
   234  	s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   235  	l := len(s) + 2 + 1*len(s[:len(s)-int(v)-1])/3
   236  	count := 0
   237  	inWhole := v == 0
   238  	inSecondary := false
   239  	groupThreshold := 3
   240  
   241  	b := make([]byte, 0, l)
   242  
   243  	for i := len(s) - 1; i >= 0; i-- {
   244  
   245  		if s[i] == '.' {
   246  			b = append(b, or.decimal[0])
   247  			inWhole = true
   248  			continue
   249  		}
   250  
   251  		if inWhole {
   252  
   253  			if count == groupThreshold {
   254  				b = append(b, or.group[0])
   255  				count = 1
   256  
   257  				if !inSecondary {
   258  					inSecondary = true
   259  					groupThreshold = 2
   260  				}
   261  			} else {
   262  				count++
   263  			}
   264  		}
   265  
   266  		b = append(b, s[i])
   267  	}
   268  
   269  	if num < 0 {
   270  		b = append(b, or.minus[0])
   271  	}
   272  
   273  	// reverse
   274  	for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
   275  		b[i], b[j] = b[j], b[i]
   276  	}
   277  
   278  	return string(b)
   279  }
   280  
   281  // FmtPercent returns 'num' with digits/precision of 'v' for 'or' and handles both Whole and Real numbers based on 'v'
   282  // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
   283  func (or *or) FmtPercent(num float64, v uint64) string {
   284  	s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   285  	l := len(s) + 3
   286  	b := make([]byte, 0, l)
   287  
   288  	for i := len(s) - 1; i >= 0; i-- {
   289  
   290  		if s[i] == '.' {
   291  			b = append(b, or.decimal[0])
   292  			continue
   293  		}
   294  
   295  		b = append(b, s[i])
   296  	}
   297  
   298  	if num < 0 {
   299  		b = append(b, or.minus[0])
   300  	}
   301  
   302  	// reverse
   303  	for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
   304  		b[i], b[j] = b[j], b[i]
   305  	}
   306  
   307  	b = append(b, or.percent...)
   308  
   309  	return string(b)
   310  }
   311  
   312  // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'or'
   313  func (or *or) FmtCurrency(num float64, v uint64, currency currency.Type) string {
   314  
   315  	s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   316  	symbol := or.currencies[currency]
   317  	l := len(s) + len(symbol) + 2 + 1*len(s[:len(s)-int(v)-1])/3
   318  	count := 0
   319  	inWhole := v == 0
   320  	b := make([]byte, 0, l)
   321  
   322  	for i := len(s) - 1; i >= 0; i-- {
   323  
   324  		if s[i] == '.' {
   325  			b = append(b, or.decimal[0])
   326  			inWhole = true
   327  			continue
   328  		}
   329  
   330  		if inWhole {
   331  			if count == 3 {
   332  				b = append(b, or.group[0])
   333  				count = 1
   334  			} else {
   335  				count++
   336  			}
   337  		}
   338  
   339  		b = append(b, s[i])
   340  	}
   341  
   342  	for j := len(symbol) - 1; j >= 0; j-- {
   343  		b = append(b, symbol[j])
   344  	}
   345  
   346  	if num < 0 {
   347  		b = append(b, or.minus[0])
   348  	}
   349  
   350  	// reverse
   351  	for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
   352  		b[i], b[j] = b[j], b[i]
   353  	}
   354  
   355  	if int(v) < 2 {
   356  
   357  		if v == 0 {
   358  			b = append(b, or.decimal...)
   359  		}
   360  
   361  		for i := 0; i < 2-int(v); i++ {
   362  			b = append(b, '0')
   363  		}
   364  	}
   365  
   366  	return string(b)
   367  }
   368  
   369  // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'or'
   370  // in accounting notation.
   371  func (or *or) FmtAccounting(num float64, v uint64, currency currency.Type) string {
   372  
   373  	s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   374  	symbol := or.currencies[currency]
   375  	l := len(s) + len(symbol) + 4 + 1*len(s[:len(s)-int(v)-1])/3
   376  	count := 0
   377  	inWhole := v == 0
   378  	b := make([]byte, 0, l)
   379  
   380  	for i := len(s) - 1; i >= 0; i-- {
   381  
   382  		if s[i] == '.' {
   383  			b = append(b, or.decimal[0])
   384  			inWhole = true
   385  			continue
   386  		}
   387  
   388  		if inWhole {
   389  			if count == 3 {
   390  				b = append(b, or.group[0])
   391  				count = 1
   392  			} else {
   393  				count++
   394  			}
   395  		}
   396  
   397  		b = append(b, s[i])
   398  	}
   399  
   400  	if num < 0 {
   401  
   402  		for j := len(symbol) - 1; j >= 0; j-- {
   403  			b = append(b, symbol[j])
   404  		}
   405  
   406  		b = append(b, or.currencyNegativePrefix[0])
   407  
   408  	} else {
   409  
   410  		for j := len(symbol) - 1; j >= 0; j-- {
   411  			b = append(b, symbol[j])
   412  		}
   413  
   414  	}
   415  
   416  	// reverse
   417  	for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
   418  		b[i], b[j] = b[j], b[i]
   419  	}
   420  
   421  	if int(v) < 2 {
   422  
   423  		if v == 0 {
   424  			b = append(b, or.decimal...)
   425  		}
   426  
   427  		for i := 0; i < 2-int(v); i++ {
   428  			b = append(b, '0')
   429  		}
   430  	}
   431  
   432  	if num < 0 {
   433  		b = append(b, or.currencyNegativeSuffix...)
   434  	}
   435  
   436  	return string(b)
   437  }
   438  
   439  // FmtDateShort returns the short date representation of 't' for 'or'
   440  func (or *or) FmtDateShort(t time.Time) string {
   441  
   442  	b := make([]byte, 0, 32)
   443  
   444  	b = strconv.AppendInt(b, int64(t.Month()), 10)
   445  	b = append(b, []byte{0x2f}...)
   446  	b = strconv.AppendInt(b, int64(t.Day()), 10)
   447  	b = append(b, []byte{0x2f}...)
   448  
   449  	if t.Year() > 9 {
   450  		b = append(b, strconv.Itoa(t.Year())[2:]...)
   451  	} else {
   452  		b = append(b, strconv.Itoa(t.Year())[1:]...)
   453  	}
   454  
   455  	return string(b)
   456  }
   457  
   458  // FmtDateMedium returns the medium date representation of 't' for 'or'
   459  func (or *or) FmtDateMedium(t time.Time) string {
   460  
   461  	b := make([]byte, 0, 32)
   462  
   463  	b = append(b, or.monthsAbbreviated[t.Month()]...)
   464  	b = append(b, []byte{0x20}...)
   465  	b = strconv.AppendInt(b, int64(t.Day()), 10)
   466  	b = append(b, []byte{0x2c, 0x20}...)
   467  
   468  	if t.Year() > 0 {
   469  		b = strconv.AppendInt(b, int64(t.Year()), 10)
   470  	} else {
   471  		b = strconv.AppendInt(b, int64(-t.Year()), 10)
   472  	}
   473  
   474  	return string(b)
   475  }
   476  
   477  // FmtDateLong returns the long date representation of 't' for 'or'
   478  func (or *or) FmtDateLong(t time.Time) string {
   479  
   480  	b := make([]byte, 0, 32)
   481  
   482  	b = append(b, or.monthsWide[t.Month()]...)
   483  	b = append(b, []byte{0x20}...)
   484  	b = strconv.AppendInt(b, int64(t.Day()), 10)
   485  	b = append(b, []byte{0x2c, 0x20}...)
   486  
   487  	if t.Year() > 0 {
   488  		b = strconv.AppendInt(b, int64(t.Year()), 10)
   489  	} else {
   490  		b = strconv.AppendInt(b, int64(-t.Year()), 10)
   491  	}
   492  
   493  	return string(b)
   494  }
   495  
   496  // FmtDateFull returns the full date representation of 't' for 'or'
   497  func (or *or) FmtDateFull(t time.Time) string {
   498  
   499  	b := make([]byte, 0, 32)
   500  
   501  	b = append(b, or.daysWide[t.Weekday()]...)
   502  	b = append(b, []byte{0x2c, 0x20}...)
   503  	b = append(b, or.monthsWide[t.Month()]...)
   504  	b = append(b, []byte{0x20}...)
   505  	b = strconv.AppendInt(b, int64(t.Day()), 10)
   506  	b = append(b, []byte{0x2c, 0x20}...)
   507  
   508  	if t.Year() > 0 {
   509  		b = strconv.AppendInt(b, int64(t.Year()), 10)
   510  	} else {
   511  		b = strconv.AppendInt(b, int64(-t.Year()), 10)
   512  	}
   513  
   514  	return string(b)
   515  }
   516  
   517  // FmtTimeShort returns the short time representation of 't' for 'or'
   518  func (or *or) FmtTimeShort(t time.Time) string {
   519  
   520  	b := make([]byte, 0, 32)
   521  
   522  	h := t.Hour()
   523  
   524  	if h > 12 {
   525  		h -= 12
   526  	}
   527  
   528  	b = strconv.AppendInt(b, int64(h), 10)
   529  	b = append(b, or.timeSeparator...)
   530  
   531  	if t.Minute() < 10 {
   532  		b = append(b, '0')
   533  	}
   534  
   535  	b = strconv.AppendInt(b, int64(t.Minute()), 10)
   536  	b = append(b, []byte{0x20}...)
   537  
   538  	if t.Hour() < 12 {
   539  		b = append(b, or.periodsAbbreviated[0]...)
   540  	} else {
   541  		b = append(b, or.periodsAbbreviated[1]...)
   542  	}
   543  
   544  	return string(b)
   545  }
   546  
   547  // FmtTimeMedium returns the medium time representation of 't' for 'or'
   548  func (or *or) FmtTimeMedium(t time.Time) string {
   549  
   550  	b := make([]byte, 0, 32)
   551  
   552  	h := t.Hour()
   553  
   554  	if h > 12 {
   555  		h -= 12
   556  	}
   557  
   558  	b = strconv.AppendInt(b, int64(h), 10)
   559  	b = append(b, or.timeSeparator...)
   560  
   561  	if t.Minute() < 10 {
   562  		b = append(b, '0')
   563  	}
   564  
   565  	b = strconv.AppendInt(b, int64(t.Minute()), 10)
   566  	b = append(b, or.timeSeparator...)
   567  
   568  	if t.Second() < 10 {
   569  		b = append(b, '0')
   570  	}
   571  
   572  	b = strconv.AppendInt(b, int64(t.Second()), 10)
   573  	b = append(b, []byte{0x20}...)
   574  
   575  	if t.Hour() < 12 {
   576  		b = append(b, or.periodsAbbreviated[0]...)
   577  	} else {
   578  		b = append(b, or.periodsAbbreviated[1]...)
   579  	}
   580  
   581  	return string(b)
   582  }
   583  
   584  // FmtTimeLong returns the long time representation of 't' for 'or'
   585  func (or *or) FmtTimeLong(t time.Time) string {
   586  
   587  	b := make([]byte, 0, 32)
   588  
   589  	h := t.Hour()
   590  
   591  	if h > 12 {
   592  		h -= 12
   593  	}
   594  
   595  	b = strconv.AppendInt(b, int64(h), 10)
   596  	b = append(b, or.timeSeparator...)
   597  
   598  	if t.Minute() < 10 {
   599  		b = append(b, '0')
   600  	}
   601  
   602  	b = strconv.AppendInt(b, int64(t.Minute()), 10)
   603  	b = append(b, or.timeSeparator...)
   604  
   605  	if t.Second() < 10 {
   606  		b = append(b, '0')
   607  	}
   608  
   609  	b = strconv.AppendInt(b, int64(t.Second()), 10)
   610  	b = append(b, []byte{0x20}...)
   611  
   612  	if t.Hour() < 12 {
   613  		b = append(b, or.periodsAbbreviated[0]...)
   614  	} else {
   615  		b = append(b, or.periodsAbbreviated[1]...)
   616  	}
   617  
   618  	b = append(b, []byte{0x20}...)
   619  
   620  	tz, _ := t.Zone()
   621  	b = append(b, tz...)
   622  
   623  	return string(b)
   624  }
   625  
   626  // FmtTimeFull returns the full time representation of 't' for 'or'
   627  func (or *or) FmtTimeFull(t time.Time) string {
   628  
   629  	b := make([]byte, 0, 32)
   630  
   631  	h := t.Hour()
   632  
   633  	if h > 12 {
   634  		h -= 12
   635  	}
   636  
   637  	b = strconv.AppendInt(b, int64(h), 10)
   638  	b = append(b, or.timeSeparator...)
   639  
   640  	if t.Minute() < 10 {
   641  		b = append(b, '0')
   642  	}
   643  
   644  	b = strconv.AppendInt(b, int64(t.Minute()), 10)
   645  	b = append(b, or.timeSeparator...)
   646  
   647  	if t.Second() < 10 {
   648  		b = append(b, '0')
   649  	}
   650  
   651  	b = strconv.AppendInt(b, int64(t.Second()), 10)
   652  	b = append(b, []byte{0x20}...)
   653  
   654  	if t.Hour() < 12 {
   655  		b = append(b, or.periodsAbbreviated[0]...)
   656  	} else {
   657  		b = append(b, or.periodsAbbreviated[1]...)
   658  	}
   659  
   660  	b = append(b, []byte{0x20}...)
   661  
   662  	tz, _ := t.Zone()
   663  
   664  	if btz, ok := or.timezones[tz]; ok {
   665  		b = append(b, btz...)
   666  	} else {
   667  		b = append(b, tz...)
   668  	}
   669  
   670  	return string(b)
   671  }
   672  

View as plain text