OSDN Git Service

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