-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrency.go
402 lines (355 loc) · 13.5 KB
/
currency.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright 2018 Simon Zimmermann. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// package currency implements a currency converter
package currency
import (
"database/sql/driver"
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/shopspring/decimal"
)
// Currency is a ISO 4217 three-letter alphabetic code.
type Currency string
// Scan implements the Scanner interface.
// The value type must be string / []byte otherwise Scan fails.
func (c *Currency) Scan(value interface{}) error {
if value == nil {
return fmt.Errorf("nil err")
}
var s string
switch v := value.(type) {
case []byte:
s = string(v)
case string:
s = v
default:
return fmt.Errorf("Can't convert %T to currency.Currency", value)
}
cc, err := ParseCurrency(s)
if err != nil {
return err
}
*c = cc
return nil
}
// Value implements the driver Valuer interface.
func (c Currency) Value() (driver.Value, error) {
return string(c), nil
}
const (
AED Currency = "AED" // United Arab Emirates Dirham
AFN = "AFN" // Afghanistan Afghani
ALL = "ALL" // Albania Lek
AMD = "AMD" // Armenia Dram
ANG = "ANG" // Netherlands Antilles Guilder
AOA = "AOA" // Angola Kwanza
ARS = "ARS" // Argentina Peso
AUD = "AUD" // Australia Dollar
AWG = "AWG" // Aruba Guilder
AZN = "AZN" // Azerbaijan New Manat
BAM = "BAM" // Bosnia and Herzegovina Convertible Marka
BBD = "BBD" // Barbados Dollar
BDT = "BDT" // Bangladesh Taka
BGN = "BGN" // Bulgaria Lev
BHD = "BHD" // Bahrain Dinar
BIF = "BIF" // Burundi Franc
BMD = "BMD" // Bermuda Dollar
BND = "BND" // Brunei Darussalam Dollar
BOB = "BOB" // Bolivia Boliviano
BRL = "BRL" // Brazil Real
BSD = "BSD" // Bahamas Dollar
BTN = "BTN" // Bhutan Ngultrum
BWP = "BWP" // Botswana Pula
BYR = "BYR" // Belarus Ruble
BZD = "BZD" // Belize Dollar
CAD = "CAD" // Canada Dollar
CDF = "CDF" // Congo/Kinshasa Franc
CHF = "CHF" // Switzerland Franc
CLP = "CLP" // Chile Peso
CNY = "CNY" // China Yuan Renminbi
COP = "COP" // Colombia Peso
CRC = "CRC" // Costa Rica Colon
CUC = "CUC" // Cuba Convertible Peso
CUP = "CUP" // Cuba Peso
CVE = "CVE" // Cape Verde Escudo
CZK = "CZK" // Czech Republic Koruna
DJF = "DJF" // Djibouti Franc
DKK = "DKK" // Denmark Krone
DOP = "DOP" // Dominican Republic Peso
DZD = "DZD" // Algeria Dinar
EGP = "EGP" // Egypt Pound
ERN = "ERN" // Eritrea Nakfa
ETB = "ETB" // Ethiopia Birr
EUR = "EUR" // Euro Member Countries
FJD = "FJD" // Fiji Dollar
FKP = "FKP" // Falkland Islands (Malvinas) Pound
GBP = "GBP" // United Kingdom Pound
GEL = "GEL" // Georgia Lari
GGP = "GGP" // Guernsey Pound
GHS = "GHS" // Ghana Cedi
GIP = "GIP" // Gibraltar Pound
GMD = "GMD" // Gambia Dalasi
GNF = "GNF" // Guinea Franc
GTQ = "GTQ" // Guatemala Quetzal
GYD = "GYD" // Guyana Dollar
HKD = "HKD" // Hong Kong Dollar
HNL = "HNL" // Honduras Lempira
HRK = "HRK" // Croatia Kuna
HTG = "HTG" // Haiti Gourde
HUF = "HUF" // Hungary Forint
IDR = "IDR" // Indonesia Rupiah
ILS = "ILS" // Israel Shekel
IMP = "IMP" // Isle of Man Pound
INR = "INR" // India Rupee
IQD = "IQD" // Iraq Dinar
IRR = "IRR" // Iran Rial
ISK = "ISK" // Iceland Krona
JEP = "JEP" // Jersey Pound
JMD = "JMD" // Jamaica Dollar
JOD = "JOD" // Jordan Dinar
JPY = "JPY" // Japan Yen
KES = "KES" // Kenya Shilling
KGS = "KGS" // Kyrgyzstan Som
KHR = "KHR" // Cambodia Riel
KMF = "KMF" // Comoros Franc
KPW = "KPW" // Korea (North) Won
KRW = "KRW" // Korea (South) Won
KWD = "KWD" // Kuwait Dinar
KYD = "KYD" // Cayman Islands Dollar
KZT = "KZT" // Kazakhstan Tenge
LAK = "LAK" // Laos Kip
LBP = "LBP" // Lebanon Pound
LKR = "LKR" // Sri Lanka Rupee
LRD = "LRD" // Liberia Dollar
LSL = "LSL" // Lesotho Loti
LYD = "LYD" // Libya Dinar
MAD = "MAD" // Morocco Dirham
MDL = "MDL" // Moldova Leu
MGA = "MGA" // Madagascar Ariary
MKD = "MKD" // Macedonia Denar
MMK = "MMK" // Myanmar (Burma) Kyat
MNT = "MNT" // Mongolia Tughrik
MOP = "MOP" // Macau Pataca
MRO = "MRO" // Mauritania Ouguiya
MUR = "MUR" // Mauritius Rupee
MVR = "MVR" // Maldives (Maldive Islands) Rufiyaa
MWK = "MWK" // Malawi Kwacha
MXN = "MXN" // Mexico Peso
MXV = "MXV" // Mexican Unidad de Inversion (UDI) (funds code)
MYR = "MYR" // Malaysia Ringgit
MZN = "MZN" // Mozambique Metical
NAD = "NAD" // Namibia Dollar
NGN = "NGN" // Nigeria Naira
NIO = "NIO" // Nicaragua Cordoba
NOK = "NOK" // Norway Krone
NPR = "NPR" // Nepal Rupee
NZD = "NZD" // New Zealand Dollar
OMR = "OMR" // Oman Rial
PAB = "PAB" // Panama Balboa
PEN = "PEN" // Peru Nuevo Sol
PGK = "PGK" // Papua New Guinea Kina
PHP = "PHP" // Philippines Peso
PKR = "PKR" // Pakistan Rupee
PLN = "PLN" // Poland Zloty
PYG = "PYG" // Paraguay Guarani
QAR = "QAR" // Qatar Riyal
RON = "RON" // Romania New Leu
RSD = "RSD" // Serbia Dinar
RUB = "RUB" // Russia Ruble
RWF = "RWF" // Rwanda Franc
SAR = "SAR" // Saudi Arabia Riyal
SBD = "SBD" // Solomon Islands Dollar
SCR = "SCR" // Seychelles Rupee
SDG = "SDG" // Sudan Pound
SEK = "SEK" // Sweden Krona
SGD = "SGD" // Singapore Dollar
SHP = "SHP" // Saint Helena Pound
SLL = "SLL" // Sierra Leone Leone
SOS = "SOS" // Somalia Shilling
SPL = "SPL" // Seborga Luigino
SRD = "SRD" // Suriname Dollar
STD = "STD" // São Tomé and Príncipe Dobra
SVC = "SVC" // El Salvador Colon
SYP = "SYP" // Syria Pound
SZL = "SZL" // Swaziland Lilangeni
THB = "THB" // Thailand Baht
TJS = "TJS" // Tajikistan Somoni
TMT = "TMT" // Turkmenistan Manat
TND = "TND" // Tunisia Dinar
TOP = "TOP" // Tonga Pa'anga
TRY = "TRY" // Turkey Lira
TTD = "TTD" // Trinidad and Tobago Dollar
TVD = "TVD" // Tuvalu Dollar
TWD = "TWD" // Taiwan New Dollar
TZS = "TZS" // Tanzania Shilling
UAH = "UAH" // Ukraine Hryvnia
UGX = "UGX" // Uganda Shilling
USD = "USD" // United States Dollar
UYU = "UYU" // Uruguay Peso
UZS = "UZS" // Uzbekistan Som
VEF = "VEF" // Venezuela Bolivar
VND = "VND" // Viet Nam Dong
VUV = "VUV" // Vanuatu Vatu
WST = "WST" // Samoa Tala
XAF = "XAF" // Communauté Financière Africaine (BEAC) CFA Franc BEAC
XCD = "XCD" // East Caribbean Dollar
XDR = "XDR" // International Monetary Fund (IMF) Special Drawing Rights
XOF = "XOF" // Communauté Financière Africaine (BCEAO) Franc
XPF = "XPF" // Comptoirs Français du Pacifique (CFP) Franc
YER = "YER" // Yemen Rial
ZAR = "ZAR" // South Africa Rand
ZMW = "ZMW" // Zambia Kwacha
ZWD = "ZWD" // Zimbabwe Dollar
// Metals
XAG Currency = "XAU" // Gold
XAU = "XAG" // Silver
XCP = "XCP" // Copper
XPD = "XPD" // Palladium
XPT = "XPT" // Platinum
// Historic currencies
CYP Currency = "CYP" // Cypriot pound
DEM = "DEM" // German mark
ECS = "ECS" // Ecuadorian sucre
FRF = "FRF" // French franc
IEP = "IEP" // Irish pound (punt in Irish language)
ITL = "ITL" // Italian lira
LTL = "LTL" // Lithuanian litas
LVL = "LVL" // Latvian lats
SIT = "SIT" // Slovenian tolar
ZWL = "ZWL" // Zimbabwean dollar A/10
// Unofficial currency codes
CNH Currency = "CNH" // Chinese yuan (when traded offshore)
// Other
CLF Currency = "CLF" // Unidad de Fomento (funds code)
)
var currencies = [...]Currency{
AED, AFN, ALL, AMD, ANG, AOA, ARS, AUD, AWG, AZN, BAM, BBD, BDT, BGN, BHD,
BIF, BMD, BND, BOB, BRL, BSD, BTN, BWP, BYR, BZD, CAD, CDF, CHF, CLP, CNY,
COP, CRC, CUC, CUP, CVE, CZK, DJF, DKK, DOP, DZD, EGP, ERN, ETB, EUR, FJD,
FKP, GBP, GEL, GGP, GHS, GIP, GMD, GNF, GTQ, GYD, HKD, HNL, HRK, HTG, HUF,
IDR, ILS, IMP, INR, IQD, IRR, ISK, JEP, JMD, JOD, JPY, KES, KGS, KHR, KMF,
KPW, KRW, KWD, KYD, KZT, LAK, LBP, LKR, LRD, LSL, LYD, MAD, MDL, MGA, MKD,
MMK, MNT, MOP, MRO, MUR, MVR, MWK, MXN, MXV, MYR, MZN, NAD, NGN, NIO, NOK,
NPR, NZD, OMR, PAB, PEN, PGK, PHP, PKR, PLN, PYG, QAR, RON, RSD, RUB, RWF,
SAR, SBD, SCR, SDG, SEK, SGD, SHP, SLL, SOS, SPL, SRD, STD, SVC, SYP, SZL,
THB, TJS, TMT, TND, TOP, TRY, TTD, TVD, TWD, TZS, UAH, UGX, USD, UYU, UZS,
VEF, VND, VUV, WST, XAF, XCD, XDR, XOF, XPF, YER, ZAR, ZMW, ZWD, XAU, XAG,
XCP, XPD, XPT, CYP, DEM, ECS, FRF, IEP, ITL, LTL, LVL, SIT, ZWL, CNH, CLF}
var ErrCurrencyLength = errors.New("Currency should be 3 char long")
var ErrCurrencyUnknown = errors.New("Currency is unknown")
var ErrFetchingData = errors.New("Unable to fetch data for date")
type ErrNotExist struct {
Time time.Time
Currency Currency
}
func (err ErrNotExist) Error() string {
return fmt.Sprintf("Exchange rate for %s @ %s does not exist", err.Currency, toDate(err.Time))
}
// ParseCurrency returns the Currency value represented by the string.
func ParseCurrency(v string) (Currency, error) {
if len(v) != 3 {
return "", ErrCurrencyLength
}
cur := Currency(strings.ToUpper(v))
for _, curb := range currencies {
if curb == cur {
return cur, nil
}
}
return "", ErrCurrencyUnknown
}
// Converter holds an Exchange and implements some methods for currency
// conversion using the exchange.
type Converter struct {
ex *Exchange
}
// New initializes an Converter
func New(apiToken string) *Converter {
if apiToken == "" {
apiToken = os.Getenv("FIXER_API_TOKEN")
}
return &Converter{
ex: NewExchange(apiToken),
}
}
// Convert converts the decimal value to the given currency.
func (c *Converter) Convert(value decimal.Decimal, from, to Currency) (decimal.Decimal, error) {
return c.genConvert(value, from, to, nil)
}
// ConvertAt converts the decimal value to the given currency using the
// exchange rate from the date specificied.
func (c *Converter) ConvertAt(value decimal.Decimal, from, to Currency, at time.Time) (decimal.Decimal, error) {
return c.genConvert(value, from, to, &at)
}
// ConvertString converts the decimal value (represented as a string) to the
// given currency.
func (c *Converter) ConvertString(value string, from, to Currency) (decimal.Decimal, error) {
v, _ := decimal.NewFromString(value)
return c.genConvert(v, from, to, nil)
}
// ConvertStringAt converts the decimal value (represented as a string) to the
// given currency using the exchange rate from the date specificied.
func (c *Converter) ConvertStringAt(value string, from, to Currency, at time.Time) (decimal.Decimal, error) {
v, _ := decimal.NewFromString(value)
return c.genConvert(v, from, to, &at)
}
func (c *Converter) genConvert(value decimal.Decimal, from, to Currency, at *time.Time) (decimal.Decimal, error) {
var t time.Time
if at == nil {
t = time.Now().UTC()
} else {
t = *at
}
var eur decimal.Decimal
if from == EUR {
eur = value
} else {
fromRate, err := c.ex.Get(t, from)
if err != nil {
return decimal.Zero, err
}
eur = value.Mul(fromRate.ToEUR)
}
if to == EUR {
return eur, nil
}
toRate, err := c.ex.Get(t, to)
if err != nil {
return decimal.Zero, err
}
return eur.Mul(toRate.FromEUR), nil
}
// DefaultConverter is the default Converter and is used by Convert, ConvertAt,
// ConvertString and ConvertStringAt.
var DefaultConverter = New("")
// Convert converts the decimal value to the given currency.
//
// Convert is a wrapper for DefaultConverter.Convert.
func Convert(value decimal.Decimal, from, to Currency) (decimal.Decimal, error) {
return DefaultConverter.Convert(value, from, to)
}
// ConvertAt converts the decimal value to the given currency using the
// exchange rate from the date specificied.
//
// ConvertAt is a wrapper for DefaultConverter.ConvertAt.
func ConvertAt(value decimal.Decimal, from, to Currency, at time.Time) (decimal.Decimal, error) {
return DefaultConverter.ConvertAt(value, from, to, at)
}
// ConvertString converts the decimal value (represented as a string) to the
// given currency.
//
// ConvertString is a wrapper for DefaultConverter.ConvertString.
func ConvertString(value string, from, to Currency) (decimal.Decimal, error) {
return DefaultConverter.ConvertString(value, from, to)
}
// ConvertStringAt converts the decimal value (represented as a string) to the
// given currency using the exchange rate from the date specificied.
//
// ConvertStringAt is a wrapper for DefaultConverter.ConvertStringAt.
func ConvertStringAt(value string, from, to Currency, at time.Time) (decimal.Decimal, error) {
return DefaultConverter.ConvertStringAt(value, from, to, at)
}