-
Notifications
You must be signed in to change notification settings - Fork 0
/
rates.go
57 lines (47 loc) · 913 Bytes
/
rates.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
package main
import (
"time"
)
var (
ratesUsed = []Rate{}
ratesPast = []Rate{}
)
// Rates parent node of base XML structure
type Rates struct {
Rates []Rate `xml:"Valute"`
}
// Rate child node of Rates
type Rate struct {
NumCode int16 `xml:"NumCode"`
CharCode string `xml:"CharCode"`
Name string `xml:"Name"`
Value float64 `xml:"Value"`
}
func getRates(past bool) error {
xml, err := getXML()
if err != nil {
return err
}
rates, err := parseXML(xml)
if err != nil {
return err
}
for _, r := range rates.Rates {
if currencies.Contains(r.CharCode) {
if past {
ratesPast = append(ratesPast, r)
} else {
ratesUsed = append(ratesUsed, r)
}
}
}
return nil
}
func getPastRates() error {
dateTime, _ := time.Parse(dateFormat, date)
dateBak := date
date = dateTime.AddDate(0, 0, -1).Format(dateFormat)
err := getRates(true)
date = dateBak
return err
}