Posts

Showing posts with the label Timestamp

Compute Date Out Of Timestamp From Binance-API (Python)

Answer : 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 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

Android Get Current Timestamp?

Answer : The solution is : Long tsLong = System.currentTimeMillis()/1000; String ts = tsLong.toString(); From developers blog: System.currentTimeMillis() is the standard "wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the phone network (see setCurrentTimeMillis(long)), so the time may jump backwards or forwards unpredictably. This clock should only be used when correspondence with real-world dates and times is important, such as in a calendar or alarm clock application. Interval or elapsed time measurements should use a different clock. If you are using System.currentTimeMillis() , consider listening to the ACTION_TIME_TICK , ACTION_TIME_CHANGED and ACTION_TIMEZONE_CHANGED Intent broadcasts to find out when the time changes. 1320917972 is Unix timestamp using number of seconds since 00:00:00 UTC on January 1, 1970. You can use TimeUnit class for unit conversion - from System.currentTimeMillis() to s...

Convert Datetime To Unix Timestamp And Convert It Back In Python

Answer : solution is import time import datetime d = datetime.date(2015,1,5) unixtime = time.mktime(d.timetuple()) What you missed here is timezones. Presumably you've five hours off UTC, so 2013-09-01T11:00:00 local and 2013-09-01T06:00:00Z are the same time. You need to read the top of the datetime docs, which explain about timezones and "naive" and "aware" objects. If your original naive datetime was UTC, the way to recover it is to use utcfromtimestamp instead of fromtimestamp . On the other hand, if your original naive datetime was local, you shouldn't have subtracted a UTC timestamp from it in the first place; use datetime.fromtimestamp(0) instead. Or, if you had an aware datetime object, you need to either use a local (aware) epoch on both sides, or explicitly convert to and from UTC. If you have, or can upgrade to, Python 3.3 or later, you can avoid all of these problems by just using the timestamp method instead of trying to figure out how to...