github.com/liquid-dev/text@v0.3.3-liquid/currency/gen_common.go (about) 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 "github.com/liquid-dev/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 {2, 50}, 46 } 47 48 // regionToCode returns a 16-bit region code. Only two-letter codes are 49 // supported. (Three-letter codes are not needed.) 50 func regionToCode(r language.Region) uint16 { 51 if s := r.String(); len(s) == 2 { 52 return uint16(s[0])<<8 | uint16(s[1]) 53 } 54 return 0 55 } 56 57 func toDate(t time.Time) uint32 { 58 y := t.Year() 59 if y == 1 { 60 return 0 61 } 62 date := uint32(y) << 4 63 date |= uint32(t.Month()) 64 date <<= 5 65 date |= uint32(t.Day()) 66 return date 67 } 68 69 func fromDate(date uint32) time.Time { 70 return time.Date(int(date>>9), time.Month((date>>5)&0xf), int(date&0x1f), 0, 0, 0, 0, time.UTC) 71 }