我得到的答案是其余的Epoch时间格式
start_time = 1234566
end_time = 1234578
Run Code Online (Sandbox Code Playgroud)
我想在MySQL格式时转换那个纪元秒,这样我就可以在MySQL数据库中存储差异.
我试过了:
>>> 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)
Run Code Online (Sandbox Code Playgroud)
以上结果不是我所期待的.我希望它像
2012-09-12 21:00:00
Run Code Online (Sandbox Code Playgroud)
请建议我如何实现这一目标?
另外,为什么我收到TypeError: a float is required了
>>> getbbb_class.end_time = 1347516459425
>>> mend = time.gmtime(getbbb_class.end_time).tm_hour
Traceback (most recent call last):
...
TypeError: a float is required
Run Code Online (Sandbox Code Playgroud)
ron*_*man 327
要将时间值(float或int)转换为格式化字符串,请使用:
time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370))
Run Code Online (Sandbox Code Playgroud)
png*_*png 202
您还可以使用datetime:
>>> import datetime
>>> datetime.datetime.fromtimestamp(1347517370).strftime('%c')
'2012-09-13 02:22:50'
Run Code Online (Sandbox Code Playgroud)
use*_*188 45
>>> import datetime
>>> datetime.datetime.fromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')
'2012-09-13 14:22:50' # Local time
Run Code Online (Sandbox Code Playgroud)
要获得UTC:
>>> datetime.datetime.utcfromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')
'2012-09-13 06:22:50'
Run Code Online (Sandbox Code Playgroud)
ron*_*nak 13
这就是你需要的
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'
Run Code Online (Sandbox Code Playgroud)
请输入一个float而不是一个int,其他TypeError应该消失.
mend = time.gmtime(float(getbbb_class.end_time)).tm_hour
Run Code Online (Sandbox Code Playgroud)
dka*_*ins 10
试试这个:
>>> import time
>>> time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(1347517119))
'2012-09-12 23:18:39'
Run Code Online (Sandbox Code Playgroud)
同样在MySQL中,你可以FROM_UNIXTIME喜欢:
INSERT INTO tblname VALUES (FROM_UNIXTIME(1347517119))
Run Code Online (Sandbox Code Playgroud)
对于你的第二个问题,可能是因为getbbb_class.end_time是一个字符串.您可以将其转换为数字,如:float(getbbb_class.end_time)
小智 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)
Run Code Online (Sandbox Code Playgroud)
这有点罗嗦,但它来自unix中的date命令.
小智 6
如果您有以毫秒为单位的纪元,则可能的解决方案将转换为秒:
import time
time.ctime(milliseconds/1000)
Run Code Online (Sandbox Code Playgroud)
更多time功能:https : //docs.python.org/3/library/time.html#functions
首先是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).
Run Code Online (Sandbox Code Playgroud)
了解时代应该是怎样的。
>>> time.time()
1347517171.6514659
>>> time.gmtime(time.time())
(2012, 9, 13, 6, 19, 34, 3, 257, 0)
Run Code Online (Sandbox Code Playgroud)
只需确保您传递给的 argtime.gmtime()是整数。
分享一个明确区分 UTC 和本地时间转换的答案。import datetime在使用下面的方法之前先在顶部使用。
转换为本地计算机时区的日期时间
datetime.datetime.fromtimestamp(1347517370)
Run Code Online (Sandbox Code Playgroud)
转换为 UTC 时区的日期时间
datetime.datetime.utcfromtimestamp(1347517370)
Run Code Online (Sandbox Code Playgroud)
对于上述两种方法,如果您希望返回格式化的日期字符串,请使用以下代码块
datetime.datetime.fromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')
datetime.datetime.utcfromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
381901 次 |
| 最近记录: |