common.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
  2. package currency
  3. import (
  4. "time"
  5. "golang.org/x/text/language"
  6. )
  7. // This file contains code common to gen.go and the package code.
  8. const (
  9. cashShift = 3
  10. roundMask = 0x7
  11. nonTenderBit = 0x8000
  12. )
  13. // currencyInfo contains information about a currency.
  14. // bits 0..2: index into roundings for standard rounding
  15. // bits 3..5: index into roundings for cash rounding
  16. type currencyInfo byte
  17. // roundingType defines the scale (number of fractional decimals) and increments
  18. // in terms of units of size 10^-scale. For example, for scale == 2 and
  19. // increment == 1, the currency is rounded to units of 0.01.
  20. type roundingType struct {
  21. scale, increment uint8
  22. }
  23. // roundings contains rounding data for currencies. This struct is
  24. // created by hand as it is very unlikely to change much.
  25. var roundings = [...]roundingType{
  26. {2, 1}, // default
  27. {0, 1},
  28. {1, 1},
  29. {3, 1},
  30. {4, 1},
  31. {2, 5}, // cash rounding alternative
  32. {2, 50},
  33. }
  34. // regionToCode returns a 16-bit region code. Only two-letter codes are
  35. // supported. (Three-letter codes are not needed.)
  36. func regionToCode(r language.Region) uint16 {
  37. if s := r.String(); len(s) == 2 {
  38. return uint16(s[0])<<8 | uint16(s[1])
  39. }
  40. return 0
  41. }
  42. func toDate(t time.Time) uint32 {
  43. y := t.Year()
  44. if y == 1 {
  45. return 0
  46. }
  47. date := uint32(y) << 4
  48. date |= uint32(t.Month())
  49. date <<= 5
  50. date |= uint32(t.Day())
  51. return date
  52. }
  53. func fromDate(date uint32) time.Time {
  54. return time.Date(int(date>>9), time.Month((date>>5)&0xf), int(date&0x1f), 0, 0, 0, 0, time.UTC)
  55. }