...

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

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

     1  package am
     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 am 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 'am' locale
    45  func New() locales.Translator {
    46  	return &am{
    47  		locale:                 "am",
    48  		pluralsCardinal:        []locales.PluralRule{2, 6},
    49  		pluralsOrdinal:         []locales.PluralRule{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", "AU$", "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", "የቻይና ዩዋን", "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", "ብር", "€", "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", "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", "฿", "TJR", "TJS", "TMM", "TMT", "TND", "TOP", "TPE", "TRL", "TRY", "TTD", "NT$", "TZS", "UAH", "UAK", "UGS", "UGX", "US$", "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{"ጥዋት", "ከሰዓት"},
    69  		periodsNarrow:          []string{"ጠ", "ከ"},
    70  		periodsWide:            []string{"ጥዋት", "ከሰዓት"},
    71  		erasAbbreviated:        []string{"", ""},
    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": "የቻታም የቀን ብርሃን ሰዓት", "CHAST": "የቻታም መደበኛ ሰዓት", "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": "የሲንጋፒር መደበኛ ሰዓት", "SRT": "የሱሪናም ሰዓት", "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 (am *am) Locale() string {
    80  	return am.locale
    81  }
    82  
    83  // PluralsCardinal returns the list of cardinal plural rules associated with 'am'
    84  func (am *am) PluralsCardinal() []locales.PluralRule {
    85  	return am.pluralsCardinal
    86  }
    87  
    88  // PluralsOrdinal returns the list of ordinal plural rules associated with 'am'
    89  func (am *am) PluralsOrdinal() []locales.PluralRule {
    90  	return am.pluralsOrdinal
    91  }
    92  
    93  // PluralsRange returns the list of range plural rules associated with 'am'
    94  func (am *am) PluralsRange() []locales.PluralRule {
    95  	return am.pluralsRange
    96  }
    97  
    98  // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'am'
    99  func (am *am) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
   100  
   101  	n := math.Abs(num)
   102  	i := int64(n)
   103  
   104  	if (i == 0) || (n == 1) {
   105  		return locales.PluralRuleOne
   106  	}
   107  
   108  	return locales.PluralRuleOther
   109  }
   110  
   111  // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'am'
   112  func (am *am) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
   113  	return locales.PluralRuleOther
   114  }
   115  
   116  // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'am'
   117  func (am *am) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
   118  
   119  	start := am.CardinalPluralRule(num1, v1)
   120  	end := am.CardinalPluralRule(num2, v2)
   121  
   122  	if start == locales.PluralRuleOne && end == locales.PluralRuleOne {
   123  		return locales.PluralRuleOne
   124  	} else if start == locales.PluralRuleOne && end == locales.PluralRuleOther {
   125  		return locales.PluralRuleOther
   126  	}
   127  
   128  	return locales.PluralRuleOther
   129  
   130  }
   131  
   132  // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
   133  func (am *am) MonthAbbreviated(month time.Month) string {
   134  	return am.monthsAbbreviated[month]
   135  }
   136  
   137  // MonthsAbbreviated returns the locales abbreviated months
   138  func (am *am) MonthsAbbreviated() []string {
   139  	return am.monthsAbbreviated[1:]
   140  }
   141  
   142  // MonthNarrow returns the locales narrow month given the 'month' provided
   143  func (am *am) MonthNarrow(month time.Month) string {
   144  	return am.monthsNarrow[month]
   145  }
   146  
   147  // MonthsNarrow returns the locales narrow months
   148  func (am *am) MonthsNarrow() []string {
   149  	return am.monthsNarrow[1:]
   150  }
   151  
   152  // MonthWide returns the locales wide month given the 'month' provided
   153  func (am *am) MonthWide(month time.Month) string {
   154  	return am.monthsWide[month]
   155  }
   156  
   157  // MonthsWide returns the locales wide months
   158  func (am *am) MonthsWide() []string {
   159  	return am.monthsWide[1:]
   160  }
   161  
   162  // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
   163  func (am *am) WeekdayAbbreviated(weekday time.Weekday) string {
   164  	return am.daysAbbreviated[weekday]
   165  }
   166  
   167  // WeekdaysAbbreviated returns the locales abbreviated weekdays
   168  func (am *am) WeekdaysAbbreviated() []string {
   169  	return am.daysAbbreviated
   170  }
   171  
   172  // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
   173  func (am *am) WeekdayNarrow(weekday time.Weekday) string {
   174  	return am.daysNarrow[weekday]
   175  }
   176  
   177  // WeekdaysNarrow returns the locales narrow weekdays
   178  func (am *am) WeekdaysNarrow() []string {
   179  	return am.daysNarrow
   180  }
   181  
   182  // WeekdayShort returns the locales short weekday given the 'weekday' provided
   183  func (am *am) WeekdayShort(weekday time.Weekday) string {
   184  	return am.daysShort[weekday]
   185  }
   186  
   187  // WeekdaysShort returns the locales short weekdays
   188  func (am *am) WeekdaysShort() []string {
   189  	return am.daysShort
   190  }
   191  
   192  // WeekdayWide returns the locales wide weekday given the 'weekday' provided
   193  func (am *am) WeekdayWide(weekday time.Weekday) string {
   194  	return am.daysWide[weekday]
   195  }
   196  
   197  // WeekdaysWide returns the locales wide weekdays
   198  func (am *am) WeekdaysWide() []string {
   199  	return am.daysWide
   200  }
   201  
   202  // Decimal returns the decimal point of number
   203  func (am *am) Decimal() string {
   204  	return am.decimal
   205  }
   206  
   207  // Group returns the group of number
   208  func (am *am) Group() string {
   209  	return am.group
   210  }
   211  
   212  // Group returns the minus sign of number
   213  func (am *am) Minus() string {
   214  	return am.minus
   215  }
   216  
   217  // FmtNumber returns 'num' with digits/precision of 'v' for 'am' and handles both Whole and Real numbers based on 'v'
   218  func (am *am) FmtNumber(num float64, v uint64) string {
   219  
   220  	s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   221  	l := len(s) + 2 + 1*len(s[:len(s)-int(v)-1])/3
   222  	count := 0
   223  	inWhole := v == 0
   224  	b := make([]byte, 0, l)
   225  
   226  	for i := len(s) - 1; i >= 0; i-- {
   227  
   228  		if s[i] == '.' {
   229  			b = append(b, am.decimal[0])
   230  			inWhole = true
   231  			continue
   232  		}
   233  
   234  		if inWhole {
   235  			if count == 3 {
   236  				b = append(b, am.group[0])
   237  				count = 1
   238  			} else {
   239  				count++
   240  			}
   241  		}
   242  
   243  		b = append(b, s[i])
   244  	}
   245  
   246  	if num < 0 {
   247  		b = append(b, am.minus[0])
   248  	}
   249  
   250  	// reverse
   251  	for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
   252  		b[i], b[j] = b[j], b[i]
   253  	}
   254  
   255  	return string(b)
   256  }
   257  
   258  // FmtPercent returns 'num' with digits/precision of 'v' for 'am' and handles both Whole and Real numbers based on 'v'
   259  // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
   260  func (am *am) FmtPercent(num float64, v uint64) string {
   261  	s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   262  	l := len(s) + 3
   263  	b := make([]byte, 0, l)
   264  
   265  	for i := len(s) - 1; i >= 0; i-- {
   266  
   267  		if s[i] == '.' {
   268  			b = append(b, am.decimal[0])
   269  			continue
   270  		}
   271  
   272  		b = append(b, s[i])
   273  	}
   274  
   275  	if num < 0 {
   276  		b = append(b, am.minus[0])
   277  	}
   278  
   279  	// reverse
   280  	for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
   281  		b[i], b[j] = b[j], b[i]
   282  	}
   283  
   284  	b = append(b, am.percent...)
   285  
   286  	return string(b)
   287  }
   288  
   289  // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'am'
   290  func (am *am) FmtCurrency(num float64, v uint64, currency currency.Type) string {
   291  
   292  	s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   293  	symbol := am.currencies[currency]
   294  	l := len(s) + len(symbol) + 2 + 1*len(s[:len(s)-int(v)-1])/3
   295  	count := 0
   296  	inWhole := v == 0
   297  	b := make([]byte, 0, l)
   298  
   299  	for i := len(s) - 1; i >= 0; i-- {
   300  
   301  		if s[i] == '.' {
   302  			b = append(b, am.decimal[0])
   303  			inWhole = true
   304  			continue
   305  		}
   306  
   307  		if inWhole {
   308  			if count == 3 {
   309  				b = append(b, am.group[0])
   310  				count = 1
   311  			} else {
   312  				count++
   313  			}
   314  		}
   315  
   316  		b = append(b, s[i])
   317  	}
   318  
   319  	for j := len(symbol) - 1; j >= 0; j-- {
   320  		b = append(b, symbol[j])
   321  	}
   322  
   323  	if num < 0 {
   324  		b = append(b, am.minus[0])
   325  	}
   326  
   327  	// reverse
   328  	for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
   329  		b[i], b[j] = b[j], b[i]
   330  	}
   331  
   332  	if int(v) < 2 {
   333  
   334  		if v == 0 {
   335  			b = append(b, am.decimal...)
   336  		}
   337  
   338  		for i := 0; i < 2-int(v); i++ {
   339  			b = append(b, '0')
   340  		}
   341  	}
   342  
   343  	return string(b)
   344  }
   345  
   346  // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'am'
   347  // in accounting notation.
   348  func (am *am) FmtAccounting(num float64, v uint64, currency currency.Type) string {
   349  
   350  	s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   351  	symbol := am.currencies[currency]
   352  	l := len(s) + len(symbol) + 4 + 1*len(s[:len(s)-int(v)-1])/3
   353  	count := 0
   354  	inWhole := v == 0
   355  	b := make([]byte, 0, l)
   356  
   357  	for i := len(s) - 1; i >= 0; i-- {
   358  
   359  		if s[i] == '.' {
   360  			b = append(b, am.decimal[0])
   361  			inWhole = true
   362  			continue
   363  		}
   364  
   365  		if inWhole {
   366  			if count == 3 {
   367  				b = append(b, am.group[0])
   368  				count = 1
   369  			} else {
   370  				count++
   371  			}
   372  		}
   373  
   374  		b = append(b, s[i])
   375  	}
   376  
   377  	if num < 0 {
   378  
   379  		for j := len(symbol) - 1; j >= 0; j-- {
   380  			b = append(b, symbol[j])
   381  		}
   382  
   383  		b = append(b, am.currencyNegativePrefix[0])
   384  
   385  	} else {
   386  
   387  		for j := len(symbol) - 1; j >= 0; j-- {
   388  			b = append(b, symbol[j])
   389  		}
   390  
   391  	}
   392  
   393  	// reverse
   394  	for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
   395  		b[i], b[j] = b[j], b[i]
   396  	}
   397  
   398  	if int(v) < 2 {
   399  
   400  		if v == 0 {
   401  			b = append(b, am.decimal...)
   402  		}
   403  
   404  		for i := 0; i < 2-int(v); i++ {
   405  			b = append(b, '0')
   406  		}
   407  	}
   408  
   409  	if num < 0 {
   410  		b = append(b, am.currencyNegativeSuffix...)
   411  	}
   412  
   413  	return string(b)
   414  }
   415  
   416  // FmtDateShort returns the short date representation of 't' for 'am'
   417  func (am *am) FmtDateShort(t time.Time) string {
   418  
   419  	b := make([]byte, 0, 32)
   420  
   421  	if t.Day() < 10 {
   422  		b = append(b, '0')
   423  	}
   424  
   425  	b = strconv.AppendInt(b, int64(t.Day()), 10)
   426  	b = append(b, []byte{0x2f}...)
   427  
   428  	if t.Month() < 10 {
   429  		b = append(b, '0')
   430  	}
   431  
   432  	b = strconv.AppendInt(b, int64(t.Month()), 10)
   433  
   434  	b = append(b, []byte{0x2f}...)
   435  
   436  	if t.Year() > 0 {
   437  		b = strconv.AppendInt(b, int64(t.Year()), 10)
   438  	} else {
   439  		b = strconv.AppendInt(b, int64(-t.Year()), 10)
   440  	}
   441  
   442  	return string(b)
   443  }
   444  
   445  // FmtDateMedium returns the medium date representation of 't' for 'am'
   446  func (am *am) FmtDateMedium(t time.Time) string {
   447  
   448  	b := make([]byte, 0, 32)
   449  
   450  	b = strconv.AppendInt(b, int64(t.Day()), 10)
   451  	b = append(b, []byte{0x20}...)
   452  	b = append(b, am.monthsAbbreviated[t.Month()]...)
   453  	b = append(b, []byte{0x20}...)
   454  
   455  	if t.Year() > 0 {
   456  		b = strconv.AppendInt(b, int64(t.Year()), 10)
   457  	} else {
   458  		b = strconv.AppendInt(b, int64(-t.Year()), 10)
   459  	}
   460  
   461  	return string(b)
   462  }
   463  
   464  // FmtDateLong returns the long date representation of 't' for 'am'
   465  func (am *am) FmtDateLong(t time.Time) string {
   466  
   467  	b := make([]byte, 0, 32)
   468  
   469  	b = strconv.AppendInt(b, int64(t.Day()), 10)
   470  	b = append(b, []byte{0x20}...)
   471  	b = append(b, am.monthsWide[t.Month()]...)
   472  	b = append(b, []byte{0x20}...)
   473  
   474  	if t.Year() > 0 {
   475  		b = strconv.AppendInt(b, int64(t.Year()), 10)
   476  	} else {
   477  		b = strconv.AppendInt(b, int64(-t.Year()), 10)
   478  	}
   479  
   480  	return string(b)
   481  }
   482  
   483  // FmtDateFull returns the full date representation of 't' for 'am'
   484  func (am *am) FmtDateFull(t time.Time) string {
   485  
   486  	b := make([]byte, 0, 32)
   487  
   488  	if t.Year() > 0 {
   489  		b = strconv.AppendInt(b, int64(t.Year()), 10)
   490  	} else {
   491  		b = strconv.AppendInt(b, int64(-t.Year()), 10)
   492  	}
   493  
   494  	b = append(b, []byte{0x20}...)
   495  	b = append(b, am.monthsWide[t.Month()]...)
   496  	b = append(b, []byte{0x20}...)
   497  	b = strconv.AppendInt(b, int64(t.Day()), 10)
   498  	b = append(b, []byte{0x2c, 0x20}...)
   499  	b = append(b, am.daysWide[t.Weekday()]...)
   500  
   501  	return string(b)
   502  }
   503  
   504  // FmtTimeShort returns the short time representation of 't' for 'am'
   505  func (am *am) FmtTimeShort(t time.Time) string {
   506  
   507  	b := make([]byte, 0, 32)
   508  
   509  	h := t.Hour()
   510  
   511  	if h > 12 {
   512  		h -= 12
   513  	}
   514  
   515  	b = strconv.AppendInt(b, int64(h), 10)
   516  	b = append(b, am.timeSeparator...)
   517  
   518  	if t.Minute() < 10 {
   519  		b = append(b, '0')
   520  	}
   521  
   522  	b = strconv.AppendInt(b, int64(t.Minute()), 10)
   523  	b = append(b, []byte{0x20}...)
   524  
   525  	if t.Hour() < 12 {
   526  		b = append(b, am.periodsAbbreviated[0]...)
   527  	} else {
   528  		b = append(b, am.periodsAbbreviated[1]...)
   529  	}
   530  
   531  	return string(b)
   532  }
   533  
   534  // FmtTimeMedium returns the medium time representation of 't' for 'am'
   535  func (am *am) FmtTimeMedium(t time.Time) string {
   536  
   537  	b := make([]byte, 0, 32)
   538  
   539  	h := t.Hour()
   540  
   541  	if h > 12 {
   542  		h -= 12
   543  	}
   544  
   545  	b = strconv.AppendInt(b, int64(h), 10)
   546  	b = append(b, am.timeSeparator...)
   547  
   548  	if t.Minute() < 10 {
   549  		b = append(b, '0')
   550  	}
   551  
   552  	b = strconv.AppendInt(b, int64(t.Minute()), 10)
   553  	b = append(b, am.timeSeparator...)
   554  
   555  	if t.Second() < 10 {
   556  		b = append(b, '0')
   557  	}
   558  
   559  	b = strconv.AppendInt(b, int64(t.Second()), 10)
   560  	b = append(b, []byte{0x20}...)
   561  
   562  	if t.Hour() < 12 {
   563  		b = append(b, am.periodsAbbreviated[0]...)
   564  	} else {
   565  		b = append(b, am.periodsAbbreviated[1]...)
   566  	}
   567  
   568  	return string(b)
   569  }
   570  
   571  // FmtTimeLong returns the long time representation of 't' for 'am'
   572  func (am *am) FmtTimeLong(t time.Time) string {
   573  
   574  	b := make([]byte, 0, 32)
   575  
   576  	h := t.Hour()
   577  
   578  	if h > 12 {
   579  		h -= 12
   580  	}
   581  
   582  	b = strconv.AppendInt(b, int64(h), 10)
   583  	b = append(b, am.timeSeparator...)
   584  
   585  	if t.Minute() < 10 {
   586  		b = append(b, '0')
   587  	}
   588  
   589  	b = strconv.AppendInt(b, int64(t.Minute()), 10)
   590  	b = append(b, am.timeSeparator...)
   591  
   592  	if t.Second() < 10 {
   593  		b = append(b, '0')
   594  	}
   595  
   596  	b = strconv.AppendInt(b, int64(t.Second()), 10)
   597  	b = append(b, []byte{0x20}...)
   598  
   599  	if t.Hour() < 12 {
   600  		b = append(b, am.periodsAbbreviated[0]...)
   601  	} else {
   602  		b = append(b, am.periodsAbbreviated[1]...)
   603  	}
   604  
   605  	b = append(b, []byte{0x20}...)
   606  
   607  	tz, _ := t.Zone()
   608  	b = append(b, tz...)
   609  
   610  	return string(b)
   611  }
   612  
   613  // FmtTimeFull returns the full time representation of 't' for 'am'
   614  func (am *am) FmtTimeFull(t time.Time) string {
   615  
   616  	b := make([]byte, 0, 32)
   617  
   618  	h := t.Hour()
   619  
   620  	if h > 12 {
   621  		h -= 12
   622  	}
   623  
   624  	b = strconv.AppendInt(b, int64(h), 10)
   625  	b = append(b, am.timeSeparator...)
   626  
   627  	if t.Minute() < 10 {
   628  		b = append(b, '0')
   629  	}
   630  
   631  	b = strconv.AppendInt(b, int64(t.Minute()), 10)
   632  	b = append(b, am.timeSeparator...)
   633  
   634  	if t.Second() < 10 {
   635  		b = append(b, '0')
   636  	}
   637  
   638  	b = strconv.AppendInt(b, int64(t.Second()), 10)
   639  	b = append(b, []byte{0x20}...)
   640  
   641  	if t.Hour() < 12 {
   642  		b = append(b, am.periodsAbbreviated[0]...)
   643  	} else {
   644  		b = append(b, am.periodsAbbreviated[1]...)
   645  	}
   646  
   647  	b = append(b, []byte{0x20}...)
   648  
   649  	tz, _ := t.Zone()
   650  
   651  	if btz, ok := am.timezones[tz]; ok {
   652  		b = append(b, btz...)
   653  	} else {
   654  		b = append(b, tz...)
   655  	}
   656  
   657  	return string(b)
   658  }
   659  

View as plain text