gen_common.go 1.7 KB

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