将历元时间转换为日期时间

时间:2021-10-16 11:24:48

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.

我想用MySQL格式的时间来转换纪元秒,这样我就可以在MySQL数据库中存储差异。

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

另外,为什么要输入TypeError:需要一个浮点数

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

7 个解决方案

#1


221  

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

若要将时间值(浮点数或整数)转换为格式化字符串,请使用:

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

#2


145  

You can also use datetime:

您还可以使用datetime:

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

#3


9  

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.

请输入一个浮点数而不是int,其他类型错误应该消失。

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

#4


7  

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:

在MySQL中,您还可以FROM_UNIXTIME:

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)

对于第二个问题,可能是因为getbbb_class。end_time是一个字符串。可以将其转换为如下数字:float(getbbb_class.end_time)

#5


7  

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

To get UTC,

UTC,

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

#6


5  

#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.

这有点啰嗦,但它来自unix中的date命令。

#7


2  

First a bit of info in epoch from man gmtime

首先从人类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.

只需确保传递给time.gmtime()的arg是整数。

#1


221  

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

若要将时间值(浮点数或整数)转换为格式化字符串,请使用:

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

#2


145  

You can also use datetime:

您还可以使用datetime:

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

#3


9  

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.

请输入一个浮点数而不是int,其他类型错误应该消失。

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

#4


7  

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:

在MySQL中,您还可以FROM_UNIXTIME:

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)

对于第二个问题,可能是因为getbbb_class。end_time是一个字符串。可以将其转换为如下数字:float(getbbb_class.end_time)

#5


7  

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

To get UTC,

UTC,

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

#6


5  

#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.

这有点啰嗦,但它来自unix中的date命令。

#7


2  

First a bit of info in epoch from man gmtime

首先从人类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.

只需确保传递给time.gmtime()的arg是整数。