我正在处理Python中的日期,我需要将它们转换为UTC时间戳,以便在Javascript中使用.以下代码不起作用:
>>> d = datetime.date(2011,01,01)
>>> datetime.datetime.utcfromtimestamp(time.mktime(d.timetuple()))
datetime.datetime(2010, 12, 31, 23, 0)
Run Code Online (Sandbox Code Playgroud)
将日期对象首先转换为datetime也无济于事.我试过这个链接的例子,但是:
from pytz import utc, timezone
from datetime import datetime
from time import mktime
input_date = datetime(year=2011, month=1, day=15)
Run Code Online (Sandbox Code Playgroud)
现在要么:
mktime(utc.localize(input_date).utctimetuple())
Run Code Online (Sandbox Code Playgroud)
要么
mktime(timezone('US/Eastern').localize(input_date).utctimetuple())
Run Code Online (Sandbox Code Playgroud)
确实有效.
所以一般的问题:如何根据UTC获得自纪元以来转换为秒的日期?
我有一段时间在UTC,从那里我想要自纪元以来的秒数.
我使用strftime将其转换为秒数.以2012年4月1日为例.
>>>datetime.datetime(2012,04,01,0,0).strftime('%s')
'1333234800'
Run Code Online (Sandbox Code Playgroud)
2012年4月1日UTC的纪元是1333238400但是这个上面返回1333234800,相差1小时.
所以看起来strftime正在考虑我的系统时间并在某个地方应用时区转换.我以为datetime纯粹是天真的?
我怎么能绕过那个?如果可能的话,避免导入其他库,除非标准.(我有可移植性问题).
如何将格式的人类可读时间转换20.12.2016 09:38:42,76为Unix时间戳(以毫秒为单位)?我发现了很多类似的问题,但没有找到这个具体的问题/答案.
请帮我将datetime对象(例如: 2011-12-17 11:31:00-05:00)(包括时区)更改为Unix时间戳(如Python中的函数time.time()).
我需要一个几秒钟的日期时间列,到处(包括文档)说我应该使用Series.dt.total_seconds()但它找不到该功能.我假设我有错误的版本,但我不...
pip freeze | grep pandas
pandas==0.20.3
python --version
Python 3.5.3
Run Code Online (Sandbox Code Playgroud)
这一切都在一个长期无故障运行的虚拟环境中,其他Series.dt功能都有效.这是代码:
from pandas import Series
from datetime import datetime
s = Series([datetime.now() for _ in range(10)])
0 2017-08-25 15:55:25.079495
1 2017-08-25 15:55:25.079504
2 2017-08-25 15:55:25.079506
3 2017-08-25 15:55:25.079508
4 2017-08-25 15:55:25.079509
5 2017-08-25 15:55:25.079510
6 2017-08-25 15:55:25.079512
7 2017-08-25 15:55:25.079513
8 2017-08-25 15:55:25.079514
9 2017-08-25 15:55:25.079516
dtype: datetime64[ns]
s.dt
<pandas.core.indexes.accessors.DatetimeProperties object at 0x7f5a686507b8>
s.dt.minute
0 55
1 55
2 55
3 55
4 …Run Code Online (Sandbox Code Playgroud) 重要事项 如果您今天正在处理此问题,请使用datastax中的新cassandra-driver(即import cassandra),因为它解决了大多数常见问题,并且不再使用旧的cql驱动程序,它已经过时了!这个问题从新驱动程序甚至开发之前就已经过时了,我们不得不使用一个名为cql的不完整的旧库(import cql < - 不再使用它,转移到新的驱动程序).
介绍 我正在使用python库cql来访问Cassandra 1.2数据库.在数据库中,我有一个带有timestamp列的表,在我的Python代码中,我有一个要插入列的日期时间.示例如下:
表
CREATE TABLE test (
id text PRIMARY KEY,
last_sent timestamp
);
Run Code Online (Sandbox Code Playgroud)
代码
import cql
import datetime
...
cql_statement = "update test set last_sent = :last_sent where id =:id"
rename_dict = {}
rename_dict['id'] = 'someid'
rename_dict['last_sent'] = datetime.datetime.now()
cursor.execute (cql_statement, rename_dict)
Run Code Online (Sandbox Code Playgroud)
问题
当我执行代码时,执行的实际cql语句是这样的:
update test set last_sent =2013-05-13 15:12:51 where id = 'someid'
Run Code Online (Sandbox Code Playgroud)
然后它失败并出现错误
Bad Request: line 1:XX missing EOF at '-05'
Run Code Online (Sandbox Code Playgroud)
问题似乎是cql库没有转义('')或在运行查询之前转换日期时间.
问题 如果没有手动转义日期并且能够将更精确的完整时间戳存储到cassandra时间戳列中,这样做的正确方法是什么?
提前致谢!
I am trying to convert '2015-09-15T17:13:29.380Z' to milliseconds.
At first I used:
time.mktime(
datetime.datetime.strptime(
"2015-09-15T17:13:29.380Z",
"%Y-%m-%dT%H:%M:%S.%fZ"
).timetuple()
)
Run Code Online (Sandbox Code Playgroud)
I got 1442330009.0 - with no microseconds. I think time.mktime rounds the number to the nearest second.
In the end I did:
origTime = '2015-09-15T17:13:29.380Z'
tupleTime = datetime.datetime.strptime(origTime, "%Y-%m-%dT%H:%M:%S.%fZ")
microsecond = tupleTime.microsecond
updated = float(time.mktime(tupleTime.timetuple())) + (microsecond * 0.000001)
Run Code Online (Sandbox Code Playgroud)
Is there a better way to do this and how do I work with the timezone?
在我的Django项目中,我正在使用DateTimeField模型.这基本上有python datetime.datetime实例.
从纪元(以秒为单位)转换到时间的最快方法是什么?
例如:
>>> print dt
2012-12-04 19:00:00-05:00
如您所见,我有这个日期时间对象如何将此日期时间对象转换为GMT -5中的纪元秒.
我该怎么做呢?
我用tweepy来发推文.我试图通过Slack发送这条推文.Tweepy以奇怪的格式给出时间,Slack需要时间在epoch中.
for i in tweets:
created=(i.created_at)
print(created)
print(created.strftime('%s'))
Run Code Online (Sandbox Code Playgroud)
这回来了
2017-01-17 14:36:26
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\slackbot3\run.py", line 287, in main
print(created.strftime('%s'))
ValueError: Invalid format string
Run Code Online (Sandbox Code Playgroud)
怎么能把2017-01-17 14:36:26送到纪元时间?
我想将当前日期时间转换为 Unix 时间戳
我的代码
import time
import datetime
d = datetime.datetime.now()
unixtime = time.mktime(d.timetuple())
print(unixtime)
Run Code Online (Sandbox Code Playgroud)
我的输出:
1577098747.0
Run Code Online (Sandbox Code Playgroud)
预期输出:
1577098747123.0
Run Code Online (Sandbox Code Playgroud)
上面的代码给了我最多 10 位的时间戳,但我希望它准确到 13 位。
注意:我不想手动转换它乘以10**3我想捕获准确的毫秒数。
我想将 UTC TimeDate 标记字符串转换为毫秒的整数值(可能需要是 64 位数量),以便在存储在 mySQL 数据库列中时占用更少的空间。这个 UTC 字符串是从另一个库生成的,我将它存储为一种每用户 GUID。
datetime 或 dateutil 能否将其转换为单个整数值(例如“自纪元以来的毫秒数”)?还是我需要自己做?
使用这种方法解析:
myDateTime = dateutil.parser.parse("2015-06-27T02:10:05.653000Z")
print("Parsed datetime String is {0}, ordinal value is {1}".format(myDateTime, myDateTime.toordinal()))
Run Code Online (Sandbox Code Playgroud)
给出输出:
Parsed datetime String is 2015-06-27 02:10:05.652999+00:00, ordinal value is 735776
Run Code Online (Sandbox Code Playgroud)
...它只给出日期的序数值。此外,如果我有一个整数 653 毫秒的时间,那么我希望解析的对象知道它有 653 毫秒,而不是 652999。
我如何将此时间戳值转换为表示 python 中自纪元以来的秒数的整数
2016-08-06T06:07:36.349Z
Run Code Online (Sandbox Code Playgroud)
这是我在弹性搜索查询中收到的时间戳值。