gen_common.go 1.6 KB

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