Converting Epoch time into the datetime

Python

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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

import time

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

For example:

import time

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

print(my_time)

Solution 2 - Python

You can also use datetime:

>>> import datetime
>>> datetime.datetime.fromtimestamp(1347517370).strftime('%c')
  '2012-09-13 02:22:50'

Solution 3 - Python

>>> import datetime
>>> datetime.datetime.fromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')
'2012-09-13 14:22:50' # Local time

To get UTC:

>>> datetime.datetime.utcfromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')
  '2012-09-13 06:22:50'

Solution 4 - Python

This is what you need

In [1]: time.time()
Out[1]: 1347517739.44904

In [2]: time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(time.time()))
Out[2]: '2012-09-13 06:31:43'

Please input a float instead of an int and that other TypeError should go away.

mend = time.gmtime(float(getbbb_class.end_time)).tm_hour

Solution 5 - Python

Try this:

>>> import time
>>> time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(1347517119))
'2012-09-12 23:18:39'

Also in MySQL, you can FROM_UNIXTIME like:

INSERT INTO tblname VALUES (FROM_UNIXTIME(1347517119))

For your 2nd question, it is probably because getbbb_class.end_time is a string. You can convert it to numeric like: float(getbbb_class.end_time)

Solution 6 - Python

If you have epoch in milliseconds a possible solution is convert to seconds:

import time
time.ctime(milliseconds/1000)

For more time functions: https://docs.python.org/3/library/time.html#functions

Solution 7 - Python

#This adds 10 seconds from now.
from datetime import datetime
import commands

date_string_command="date +%s"
utc = commands.getoutput(date_string_command)
a_date=datetime.fromtimestamp(float(int(utc))).strftime('%Y-%m-%d %H:%M:%S')
print('a_date:'+a_date)
utc = int(utc)+10
b_date=datetime.fromtimestamp(float(utc)).strftime('%Y-%m-%d %H:%M:%S')
print('b_date:'+b_date)

This is a little more wordy but it comes from date command in unix.

Solution 8 - Python

First a bit of info in epoch from man gmtime

The ctime(), gmtime() and localtime() functions all take an argument of data type time_t which represents calendar  time.   When  inter-
       preted  as  an absolute time value, it represents the number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal
       Time (UTC).

to understand how epoch should be.

>>> time.time()
1347517171.6514659
>>> time.gmtime(time.time())
(2012, 9, 13, 6, 19, 34, 3, 257, 0)

just ensure the arg you are passing to time.gmtime() is integer.

Solution 9 - Python

Sharing an answer to clearly distinguish UTC and local time conversions. Use import datetime at the top before using the below methods.

Convert to datetime of local machine's timezone

datetime.datetime.fromtimestamp(1347517370)

Convert to datetime of UTC timezone

datetime.datetime.utcfromtimestamp(1347517370)

For both the above methods, if you wish to return a formatted date string, use the following code block

datetime.datetime.fromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')
datetime.datetime.utcfromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1667633View Question on Stackoverflow
Solution 1 - Pythonron rothmanView Answer on Stackoverflow
Solution 2 - PythonpngView Answer on Stackoverflow
Solution 3 - Pythonuser799188View Answer on Stackoverflow
Solution 4 - PythonronakView Answer on Stackoverflow
Solution 5 - PythondkaminsView Answer on Stackoverflow
Solution 6 - PythonJhon Didier SottoView Answer on Stackoverflow
Solution 7 - Pythonuser2899300View Answer on Stackoverflow
Solution 8 - PythontuxudayView Answer on Stackoverflow
Solution 9 - PythonKeet SugathadasaView Answer on Stackoverflow