Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute Date out of Timestamp from Binance-API (Python)

I received the servertime from the Binance-API,I try to work with and it looks like this:

{
  "serverTime": 1518440400000
}

The question is, how can I compute the date out of this stamp?

I tried

import datetime

print(datetime.datetime.fromtimestamp(
       int("1518308894652")).strftime('%Y-%m-%d %H:%M:%S'))

But the date wasn´t valid.

Do you have ideas, or is it to specific? Thank you!

like image 830
cosmonaut Avatar asked Feb 13 '18 00:02

cosmonaut


People also ask

How does Python connect to Binance API?

Connect to Binance Client Using Python To connect to the client just define your API and secret key variable and execute the client function. To verify that your keys are correct and that you're connected to Binance, execute this function that will ping the server.

How do I sync my Binance server time?

Start -> search date and time. Make sure you have set Set time automatically to On. In the opened window you have an option Synchronize your clock, and press Sync now.

Does Binance use Python?

The Binance API allows algorithmic traders to automate their trading by plugging into the Binance servers using Python or a variety of other programming languages.


2 Answers

You could use this:

from datetime import datetime
datetime.fromtimestamp(int("1518308894652"))

But python says the year is out of range (understandably, considering it says it's 50087). So I suspect that serverTime is not a normal timestamp.

But assuming the response that you got was the timestamp, so you don't need to do any other conversions other than turning the string into an int.

Edit:

Turns out the docs say "All time and timestamp related fields are in milliseconds." So just divide the response by 1000 and you'll be fine: datetime.fromtimestamp(int("1518308894652")/1000). Source

like image 88
Addison Avatar answered Sep 18 '22 17:09

Addison


Your response is in milliseconds when datetime.fromtimestamp requires seconds.

import datetime

print(datetime.datetime.fromtimestamp(1518308894652/1000))

# 2018-02-10 19:28:14.652000
like image 45
AidanGawronski Avatar answered Sep 18 '22 17:09

AidanGawronski