Saturday 13 July 2019

python - Converting Epoch time into the datetime




I am getting a response from the rest is an Epoch time format like



start_time = 1234566
end_time = 1234578


I want to convert that epoch seconds in MySQL format time so that I could store the differences in my MySQL database.



I tried:




>>> import time
>>> time.gmtime(123456)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=2, tm_hour=10, tm_min=17, tm_sec=36, tm_wday=4, tm_yday=2, tm_isdst=0)


The above result is not what I am expecting. I want it be like



2012-09-12 21:00:00



Please suggest how can I achieve this?



Also,
Why I am getting TypeError: a float is required for



>>> getbbb_class.end_time = 1347516459425
>>> mend = time.gmtime(getbbb_class.end_time).tm_hour
Traceback (most recent call last):
...
TypeError: a float is required


Answer



To convert your time value (float or int) to a formatted string, use:



time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370))

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...