Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common: Return UTC TZ time.Time from convert functions #1551

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
6 changes: 3 additions & 3 deletions backtester/engine/backtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (bt *BackTest) ExecuteStrategy(waitForOfflineCompletion bool) error {
return fmt.Errorf("%w cannot wait for a live task to finish", errCannotHandleRequest)
}

bt.MetaData.DateStarted = time.Now()
bt.MetaData.DateStarted = time.Now().UTC()
liveTesting := bt.MetaData.LiveTesting
bt.m.Unlock()

Expand Down Expand Up @@ -615,7 +615,7 @@ func (bt *BackTest) Stop() error {
}
close(bt.shutdown)
bt.MetaData.Closed = true
bt.MetaData.DateEnded = time.Now()
bt.MetaData.DateEnded = time.Now().UTC()
if bt.MetaData.ClosePositionsOnStop {
err := bt.CloseAllPositions()
if err != nil {
Expand Down Expand Up @@ -791,7 +791,7 @@ func (bt *BackTest) SetupMetaData() error {
return err
}
bt.MetaData.ID = id
bt.MetaData.DateLoaded = time.Now()
bt.MetaData.DateLoaded = time.Now().UTC()
return nil
}

Expand Down
5 changes: 2 additions & 3 deletions backtester/engine/live.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,8 @@ func (d *dataChecker) FetchLatestData() (bool, error) {
var err error

results := make([]bool, len(d.sourcesToCheck))
// timeToRetrieve ensures consistent data retrieval
// in the event of a candle rollover mid-loop
timeToRetrieve := time.Now()
// timeToRetrieve ensures consistent data retrieval in the event of a candle rollover mid-loop
timeToRetrieve := time.Now().UTC()
for i := range d.sourcesToCheck {
if d.verboseDataCheck {
log.Infof(common.LiveStrategy, "%v %v %v checking for new data", d.sourcesToCheck[i].exchangeName, d.sourcesToCheck[i].asset, d.sourcesToCheck[i].pair)
Expand Down
2 changes: 1 addition & 1 deletion backtester/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (d *Data) GenerateReport() error {
fn += "-"
}
fn += d.Statistics.StrategyName + "-"
fn += time.Now().Format("2006-01-02-15-04-05")
fn += time.Now().UTC().Format("2006-01-02T15-04-05")

fileName, err := common.GenerateFileName(fn, "html")
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions cmd/gctcli/trades.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,16 +577,16 @@ func getHistoricTrades(c *cli.Context) error {
Quote: p.Quote.String(),
},
AssetType: assetType,
Start: s.Format(common.SimpleTimeFormatWithTimezone),
End: e.Format(common.SimpleTimeFormatWithTimezone),
Start: s.UTC().Format(common.SimpleTimeFormatWithTimezone),
End: e.UTC().Format(common.SimpleTimeFormatWithTimezone),
})
if err != nil {
return err
}
fmt.Printf("%v\t| Beginning stream retrieving trades in 1 hour batches from %v to %v\n",
time.Now().Format(time.Kitchen),
s.UTC().Format(common.SimpleTimeFormatWithTimezone),
e.UTC().Format(common.SimpleTimeFormatWithTimezone))
s.Format(common.SimpleTimeFormatWithTimezone),
e.Format(common.SimpleTimeFormatWithTimezone))
fmt.Printf("%v\t| If you have provided a large time range, please be patient\n\n",
time.Now().Format(time.Kitchen))
for {
Expand Down
17 changes: 8 additions & 9 deletions common/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,18 @@ func TimeFromUnixTimestampFloat(raw interface{}) (time.Time, error) {
if !ok {
return time.Time{}, fmt.Errorf("unable to parse, value not float64: %T", raw)
}
return time.UnixMilli(int64(ts)), nil
return time.UnixMilli(int64(ts)).UTC(), nil
}

// TimeFromUnixTimestampDecimal converts a unix timestamp in decimal form to
// a time.Time
// TimeFromUnixTimestampDecimal converts a unix timestamp in decimal form to a time.Time
func TimeFromUnixTimestampDecimal(input float64) time.Time {
i, f := math.Modf(input)
return time.Unix(int64(i), int64(f*(1e9)))
return time.Unix(int64(i), int64(f*(1e9))).UTC()
}

// UnixTimestampToTime returns time.time
func UnixTimestampToTime(timeint64 int64) time.Time {
return time.Unix(timeint64, 0)
return time.Unix(timeint64, 0).UTC()
}

// UnixTimestampStrToTime returns a time.time and an error
Expand All @@ -77,7 +76,7 @@ func UnixTimestampStrToTime(timeStr string) (time.Time, error) {
if err != nil {
return time.Time{}, err
}
return time.Unix(i, 0), nil
return time.Unix(i, 0).UTC(), nil
}

// BoolPtr takes in boolean condition and returns pointer version of it
Expand Down Expand Up @@ -227,11 +226,11 @@ func (k *ExchangeTime) UnmarshalJSON(data []byte) error {
case standard == 0:
*k = ExchangeTime(time.Time{})
case standard >= 1e13:
*k = ExchangeTime(time.Unix(standard/1e9, standard%1e9))
*k = ExchangeTime(time.Unix(standard/1e9, standard%1e9).UTC())
case standard > 9999999999:
*k = ExchangeTime(time.UnixMilli(standard))
*k = ExchangeTime(time.UnixMilli(standard).UTC())
default:
*k = ExchangeTime(time.Unix(standard, 0))
*k = ExchangeTime(time.Unix(standard, 0).UTC())
}
return nil
}
Expand Down
33 changes: 13 additions & 20 deletions common/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
)

func TestFloatFromString(t *testing.T) {
Expand Down Expand Up @@ -98,31 +99,23 @@ func TestTimeFromUnixTimestampFloat(t *testing.T) {
}

func TestTimeFromUnixTimestampDecimal(t *testing.T) {
r := TimeFromUnixTimestampDecimal(1590633982.5714)
if r.Year() != 2020 ||
r.Month().String() != "May" ||
r.Day() != 28 {
t.Error("unexpected result")
}

r = TimeFromUnixTimestampDecimal(1560516023.070651)
if r.Year() != 2019 ||
r.Month().String() != "June" ||
r.Day() != 14 {
t.Error("unexpected result")
for in, exp := range map[float64]time.Time{
1590633982.5714: time.Date(2020, 5, 28, 2, 46, 22, 571400000, time.UTC),
1560516023.070651: time.Date(2019, 6, 14, 12, 40, 23, 70651000, time.UTC),
// Examples from Kraken
1373750306.9819: time.Date(2013, 7, 13, 21, 18, 26, 981900000, time.UTC),
1534614098.345543: time.Date(2018, 8, 18, 17, 41, 38, 345543000, time.UTC),
} {
got := TimeFromUnixTimestampDecimal(in)
z, _ := got.Zone()
assert.Equal(t, "UTC", z, "TimeFromUnixTimestampDecimal should return a UTC time")
assert.WithinRangef(t, got, exp.Add(-time.Microsecond), exp.Add(time.Microsecond), "TimeFromUnixTimestampDecimal(%f) should parse a unix timestamp correctly", in)
}
}

func TestUnixTimestampToTime(t *testing.T) {
t.Parallel()
testTime := int64(1489439831)
tm := time.Unix(testTime, 0)
expectedOutput := "2017-03-13 21:17:11 +0000 UTC"
actualResult := UnixTimestampToTime(testTime)
if tm.String() != actualResult.String() {
t.Errorf(
"Expected '%s'. Actual '%s'.", expectedOutput, actualResult)
}
assert.Equal(t, int64(1489439831000000000), UnixTimestampToTime(int64(1489439831)).UnixNano())
}

func TestUnixTimestampStrToTime(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion communications/base/base_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c IComm) Setup() {
continue
}
log.Debugf(log.CommunicationMgr, "Communications: %v is enabled and online.", c[i].GetName())
c[i].SetServiceStarted(time.Now())
c[i].SetServiceStarted(time.Now().UTC())
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ const (

// vars related to the app version
var (
Copyright = fmt.Sprintf("Copyright (c) 2014-%d The GoCryptoTrader Developers.",
time.Now().Year())
Copyright = fmt.Sprintf("Copyright (c) 2014-%d The GoCryptoTrader Developers.", time.Now().UTC().Year())
)

// Version returns the version string
Expand Down
2 changes: 1 addition & 1 deletion currency/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func (s *Storage) WriteCurrencyDataToFile(path string, mainUpdate bool) error {
}

if mainUpdate {
t := time.Now()
t := time.Now().UTC()
data.LastMainUpdate = t.Unix()
s.currencyCodes.LastMainUpdate = t
}
Expand Down
14 changes: 7 additions & 7 deletions engine/datahistory_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ ranges:
IntervalStartDate: job.rangeHolder.Ranges[i].Start.Time,
IntervalEndDate: job.rangeHolder.Ranges[i].End.Time,
Status: dataHistoryStatusComplete,
Date: time.Now(),
Date: time.Now().UTC(),
},
}
}
Expand Down Expand Up @@ -512,7 +512,7 @@ func (m *DataHistoryManager) runValidationJob(job *DataHistoryJob, exch exchange
nextIntervalToProcess := job.StartDate
timesToFetch:
for t, results := range job.Results {
tt := time.Unix(t, 0)
tt := time.Unix(t, 0).UTC()
if len(results) < int(job.MaxRetryAttempts) {
for x := range results {
if results[x].Status == dataHistoryStatusComplete {
Expand Down Expand Up @@ -708,7 +708,7 @@ func (m *DataHistoryManager) processCandleData(job *DataHistoryJob, exch exchang
IntervalStartDate: startRange,
IntervalEndDate: endRange,
Status: dataHistoryStatusComplete,
Date: time.Now(),
Date: time.Now().UTC(),
}
candles, err := exch.GetHistoricCandlesExtended(context.TODO(),
job.Pair,
Expand Down Expand Up @@ -761,7 +761,7 @@ func (m *DataHistoryManager) processTradeData(job *DataHistoryJob, exch exchange
IntervalStartDate: startRange,
IntervalEndDate: endRange,
Status: dataHistoryStatusComplete,
Date: time.Now(),
Date: time.Now().UTC(),
}
trades, err := exch.GetHistoricTrades(context.TODO(),
job.Pair,
Expand Down Expand Up @@ -835,7 +835,7 @@ func (m *DataHistoryManager) convertTradesToCandles(job *DataHistoryJob, startRa
IntervalStartDate: startRange,
IntervalEndDate: endRange,
Status: dataHistoryStatusComplete,
Date: time.Now(),
Date: time.Now().UTC(),
}
trades, err := m.tradeLoader(job.Exchange, job.Asset.String(), job.Pair.Base.String(), job.Pair.Quote.String(), startRange, endRange)
if err != nil {
Expand Down Expand Up @@ -874,7 +874,7 @@ func (m *DataHistoryManager) convertCandleData(job *DataHistoryJob, startRange,
IntervalStartDate: startRange,
IntervalEndDate: endRange,
Status: dataHistoryStatusComplete,
Date: time.Now(),
Date: time.Now().UTC(),
}
candles, err := m.candleLoader(job.Exchange, job.Pair, job.Asset, job.Interval, startRange, endRange)
if err != nil {
Expand Down Expand Up @@ -916,7 +916,7 @@ func (m *DataHistoryManager) validateCandles(job *DataHistoryJob, exch exchange.
IntervalStartDate: startRange,
IntervalEndDate: endRange,
Status: dataHistoryStatusComplete,
Date: time.Now(),
Date: time.Now().UTC(),
}

apiCandles, err := exch.GetHistoricCandlesExtended(context.TODO(),
Expand Down
2 changes: 1 addition & 1 deletion engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (bot *Engine) Start() error {
}
}

bot.uptime = time.Now()
bot.uptime = time.Now().UTC()
gctlog.Debugf(gctlog.Global, "Bot '%s' started.\n", bot.Config.Name)
gctlog.Debugf(gctlog.Global, "Using data dir: %s\n", bot.Settings.DataDir)
if *bot.Config.Logging.Enabled && strings.Contains(bot.Config.Logging.Output, "file") {
Expand Down
11 changes: 6 additions & 5 deletions engine/ntp_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ func (m *ntpManager) processTime() error {
if err != nil {
return err
}
currentTime := time.Now()
currentTime := time.Now().UTC()
diff := NTPTime.Sub(currentTime)
configNTPTime := m.allowedDifference
negDiff := m.allowedNegativeDifference
configNTPNegativeTime := -negDiff
if diff > configNTPTime || diff < configNTPNegativeTime {
log.Warnf(log.TimeMgr, "NTP manager: Time out of sync (NTP): %v | (time.Now()): %v | (Difference): %v | (Allowed): +%v / %v\n",
log.Warnf(log.TimeMgr, "NTP manager: Time out of sync (NTP): %v | (Local): %v | (Difference): %v | (Allowed): +%v / %v\n",
NTPTime,
currentTime,
diff,
Expand All @@ -152,8 +152,8 @@ func (m *ntpManager) processTime() error {
return nil
}

// checkTimeInPools returns local based on ntp servers provided timestamp
// if no server can be reached will return local time in UTC()
// checkTimeInPools returns the first available time from a pool of ntp servers
// If no server can be reached will return local server time
func (m *ntpManager) checkTimeInPools() time.Time {
for i := range m.pools {
con, err := net.DialTimeout("udp", m.pools[i], 5*time.Second)
Expand Down Expand Up @@ -191,14 +191,15 @@ func (m *ntpManager) checkTimeInPools() time.Time {
continue
}

// Convert NTP epoch (1900) to Unix epoch (1970); 220898880 seconds between them
secs := float64(rsp.TxTimeSec) - 2208988800
nanos := (int64(rsp.TxTimeFrac) * 1e9) >> 32

err = con.Close()
if err != nil {
log.Errorln(log.TimeMgr, err)
}
return time.Unix(int64(secs), nanos)
return time.Unix(int64(secs), nanos).UTC()
}
log.Warnln(log.TimeMgr, "No valid NTP servers found, using current system time")
return time.Now().UTC()
Expand Down
6 changes: 3 additions & 3 deletions engine/order_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ func (m *OrderManager) processOrders() {
return
}
if sd.IsZero() {
sd = time.Now().Add(m.futuresPositionSeekDuration)
sd = time.Now().UTC().Add(m.futuresPositionSeekDuration)
}
positions, err = exchanges[x].GetFuturesPositionOrders(context.TODO(), &futures.PositionsRequest{
Asset: enabledAssets[y],
Expand Down Expand Up @@ -811,7 +811,7 @@ func (m *OrderManager) processFuturesPositions(exch exchange.IBotExchange, posit
Asset: position.Asset,
Pair: position.Pair,
StartDate: position.Orders[0].Date,
EndDate: time.Now(),
EndDate: time.Now().UTC(),
IncludePayments: true,
IncludePredictedRate: true,
})
Expand Down Expand Up @@ -847,7 +847,7 @@ func (m *OrderManager) FetchAndUpdateExchangeOrder(exch exchange.IBotExchange, o
ord.Status = order.UnknownStatus
return err
}
fetchedOrder.LastUpdated = time.Now()
fetchedOrder.LastUpdated = time.Now().UTC()
_, err = m.UpsertOrder(fetchedOrder)
return err
}
Expand Down
4 changes: 2 additions & 2 deletions engine/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4686,8 +4686,8 @@ func (s *RPCServer) GetFundingRates(ctx context.Context, r *gctrpc.GetFundingRat
if !a.IsFutures() {
return nil, fmt.Errorf("%s %w", a, futures.ErrNotFuturesAsset)
}
start := time.Now().AddDate(0, -1, 0)
end := time.Now()
start := time.Now().AddDate(0, -1, 0).UTC()
end := time.Now().UTC()
if r.StartDate != "" {
start, err = time.Parse(common.SimpleTimeFormatWithTimezone, r.StartDate)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions engine/sync_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (m *SyncManager) Start() error {
"Exchange CurrencyPairSyncer initial sync started. %d items to process.",
createdCounter)
}
m.initSyncStartTime = time.Now()
m.initSyncStartTime = time.Now().UTC()
}

go func() {
Expand Down Expand Up @@ -261,7 +261,7 @@ func newCurrencyPairSyncAgent(k key.ExchangePairAsset) *currencyPairSyncAgent {
return &currencyPairSyncAgent{
Key: k,
Pair: currency.NewPair(k.Base.Currency(), k.Quote.Currency()),
Created: time.Now(),
Created: time.Now().UTC(),
locks: make([]sync.Mutex, SyncItemTrade+1),
trackers: make([]*syncBase, SyncItemTrade+1),
}
Expand Down Expand Up @@ -423,7 +423,7 @@ func (m *SyncManager) update(c *currencyPairSyncAgent, syncType syncItemType, er
s := c.trackers[syncType]

origHadData := s.HaveData
s.LastUpdated = time.Now()
s.LastUpdated = time.Now().UTC()
if err != nil {
s.NumErrors++
}
Expand Down Expand Up @@ -596,7 +596,7 @@ func (m *SyncManager) syncTicker(c *currencyPairSyncAgent, e exchange.IBotExchan
m.tickerBatchLastRequested[key.ExchangeAsset{
Exchange: c.Key.Exchange,
Asset: c.Key.Asset,
}] = time.Now()
}] = time.Now().UTC()
m.mux.Unlock()
} else {
if m.config.Verbose {
Expand Down
Loading
Loading