support set timezone

This commit is contained in:
MaysWind
2021-03-07 23:59:13 +08:00
parent 7c47b3253e
commit fc2023fba2
14 changed files with 127 additions and 27 deletions
@@ -2,6 +2,7 @@ package exchangerates
import (
"encoding/xml"
"time"
"github.com/mayswind/lab/pkg/core"
"github.com/mayswind/lab/pkg/errs"
@@ -14,6 +15,9 @@ const euroCentralBankExchangeRateReferenceUrl = "https://www.ecb.europa.eu/stats
const euroCentralBankDataSource = "European Central Bank"
const euroCentralBankBaseCurrency = "EUR"
const euroCentralBankDataUpdateDateFormat = "2006-01-02 15"
const euroCentralBankDataUpdateDateTimezone = "Etc/GMT-1" // UTC+01:00
// EuroCentralBankDataSource defines the structure of exchange rates data source of euro central bank
type EuroCentralBankDataSource struct {
ExchangeRatesDataSource
@@ -55,10 +59,23 @@ func (e *EuroCentralBankExchangeRateData) ToLatestExchangeRateResponse() *models
exchangeRates[i] = latestEuroCentralBankExchangeRate.ExchangeRates[i].ToLatestExchangeRate()
}
timezone, err := time.LoadLocation(euroCentralBankDataUpdateDateTimezone)
if err != nil {
return nil
}
updateDateTime := latestEuroCentralBankExchangeRate.Date + " 16" // The reference rates are usually updated around 16:00 CET on every working day
updateTime, err := time.ParseInLocation(euroCentralBankDataUpdateDateFormat, updateDateTime, timezone)
if err != nil {
return nil
}
latestExchangeRateResp := &models.LatestExchangeRateResponse{
DataSource: euroCentralBankDataSource,
ReferenceUrl: euroCentralBankExchangeRateReferenceUrl,
Date: latestEuroCentralBankExchangeRate.Date,
UpdateTime: updateTime.Unix(),
BaseCurrency: euroCentralBankBaseCurrency,
ExchangeRates: exchangeRates,
}
+1 -1
View File
@@ -4,7 +4,7 @@ package models
type LatestExchangeRateResponse struct {
DataSource string `json:"dataSource"`
ReferenceUrl string `json:"referenceUrl"`
Date string `json:"date"`
UpdateTime int64 `json:"updateTime"`
BaseCurrency string `json:"baseCurrency"`
ExchangeRates []*LatestExchangeRate `json:"exchangeRates"`
}