Converting Epoch time into the datetime
Asked Answered
R

10

427

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
Rickart answered 13/9, 2012 at 6:0 Comment(0)
C
496

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

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

preceded by this import:

from time import strftime, localtime
Charlottetown answered 13/9, 2012 at 6:27 Comment(1)
why do i get OSError: [Errno 22] Invalid argument? strftime('%Y-%m-%d %H:%M:%S', localtime(1713505340511))Lidda
E
307

You can also use datetime:

>>> import datetime
>>> datetime.datetime.fromtimestamp(1347517370).strftime('%c')
  '2012-09-13 02:22:50'
Enzymolysis answered 11/4, 2013 at 16:9 Comment(0)
N
107
>>> 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'
Ninepins answered 24/10, 2017 at 15:19 Comment(1)
For UTC, why not use time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(1347517370)) ?Pfister
L
21

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
Lapham answered 13/9, 2012 at 6:34 Comment(0)
K
14

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)

Kidwell answered 13/9, 2012 at 6:22 Comment(0)
P
12

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

Preen answered 3/9, 2020 at 16:38 Comment(0)
A
8
#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.

Arrowroot answered 20/10, 2013 at 4:2 Comment(0)
M
6

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.

Mho answered 13/9, 2012 at 6:25 Comment(0)
C
5

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')
Clipclop answered 4/12, 2021 at 17:2 Comment(0)
D
0

Just for a quick example,

if you want to import only time:

import time

current timestamp:

>>> time.time() # current timestamp
1702244283.169546

formatted string:

>>> time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()) # current UTC
'2023-12-10 21:38:28'
>>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) # current local time
'2023-12-11 06:38:37'
>>> time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(1702244283.169546)) # timestamp->UTC
'2023-12-10 21:38:03'
>>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1702244283.169546)) # timestamp->local time
'2023-12-11 06:38:03'
Drier answered 10/12, 2023 at 21:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.