ecbratex

package module
v0.0.0-...-5b04e24 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2024 License: MIT Imports: 5 Imported by: 0

README

ecbratex Go Report Card

Go package for fetching and manipulating exchange rates from the European Central Bank.

ecbratex is inspired by openprovider/ecbrates, but has some additional features.

Features:

  • Different data structures for storing time series rate records: choose the most suitable for your purpose or use a sane default!
  • Rates approximation if there is no rates records for the desired date. (see example)
  • Simple interface to convert amounts from one currency to another in both minor and major units! (see example)

Usage examples

More examples can be found here.

Fetch latest rates
package main

import (
    "fmt"
    "github.com/jieggii/ecbratex"
)

func main() {
    record, _ := ecbratex.FetchLatest()
  
    // get latest USD exchange rate:
    usdRate, _ := record.Rate("USD")
    fmt.Printf("Latest USD rate (%s): %f\n", record.Date.String(), usdRate)
  
    // convert 500 USD to EUR:
    amount, _ := record.Convert(500, "USD", "EUR")
    fmt.Printf("500 USD = %f EUR\n", amount)
}
Fetch time series rates
package main

import (
    "fmt"
    "github.com/jieggii/ecbratex"
    "github.com/jieggii/ecbratex/pkg/record"
)

func main() {
    // fetch all exchange rates records:
    records, _ := ecbratex.FetchTimeSeries(ecbratex.PeriodWhole)
  
    // get USD rate on 2000-02-14:
    date := record.NewDate(2000, 2, 14)
    //date := time.Date(2000, 2, 14, 0, 0, 0, 0, time.UTC) // you can also use time module instead
  
    usdRate, _ := records.Rate(date, "USD")
    fmt.Printf("USD rate on %s: %f\n", date.String(), usdRate)
  
    // convert 999 USD to EUR on 2000-02-14:
    result, _ := records.Convert(date, 999, "USD", "EUR")
    fmt.Printf("999 USD = %f EUR on %s\n", result, date.String())
}

Supported currencies

Note: rates of some of these currencies are only present in historical data and not present in the latest rates.


List of supported currencies AUD, BGN, BRL, CAD, CHF, CNY, CYP, CZK, DKK, EEK, EUR, GBP, HKD, HRK, HUF, IDR, ILS, INR, ISK, JPY, KRW, LTL, LVL, MTL, MXN, MYR, NOK, NZD, PHP, PLN, ROL, RON, RUB, SEK, SGD, SIT, SKK, THB, TRL, TRY, USD, ZAR.

Documentation

Overview

Package ecbratex provides a convenient interface and useful data structures for fetching and manipulating currency exchange rate records from the European Central Bank (https://ecb.europa.eu).

Index

Constants

This section is empty.

Variables

View Source
var ErrUnexpectedPeriod = errors.New("unexpected period")
View Source
var Provider provider.Provider = provider.NewHTTPProvider(
	"https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml",
	"https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml",
	"https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml",
)

Provider is provider.Provider which will be used to fetch rates data across the ecbratex. provider.HTTPProvider with URLs to the ECB website is used by default.

Functions

func FetchLatest

func FetchLatest() (*record.WithDate, error)

FetchLatest fetches latest available exchange rates using Provider.

func FetchOrderedTimeSeries

func FetchOrderedTimeSeries(period Period) (timeseries.OrderedRecords, error)

FetchOrderedTimeSeries fetches rate records within the given period using Provider. Returns records represented as timeseries.RecordsSlice.

func FetchOrderedUnorderedTimeSeries

func FetchOrderedUnorderedTimeSeries(period Period) (*timeseries.OrderedUnorderedRecords, error)

FetchOrderedUnorderedTimeSeries fetches rate records within the given period using Provider. Returns records represented as timeseries.RecordsSliceMap.

func FetchTimeSeries

func FetchTimeSeries(period Period) (timeseries.UnorderedRecords, error)

FetchTimeSeries fetches rate records within the given period using Provider. Returns records represented as timeseries.RecordsMap.

func SetProvider

func SetProvider(p provider.Provider)

SetProvider sets data provider which will be used across the ecbratex to fetch exchange rates records.

Types

type Period

type Period uint8
const (
	PeriodWhole Period = iota
	PeriodLast90Days
)

func (Period) DataKind

func (p Period) DataKind() (provider.DataKind, error)

DataKind returns matching provider.DataKind for this period.

Directories

Path Synopsis
examples
latest-rates/convert-minors
This simple example illustrates usage of ConvertMinors function.
This simple example illustrates usage of ConvertMinors function.
pkg
xml

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL