擅长:python、mysql、java
<p>abhi已经声明可以使用epoch之后的毫秒作为cqlsh中的一个长值,现在我们需要在Python代码中使用它。</p>
<p>当使用cql库时,此转换(从datetime到毫秒since epoch)不会发生,因此为了使更新工作并且仍然具有将datetime转换为毫秒since epoch所需的精度。</p>
<p><strong>来源</strong>
使用这个有用的问题:<a href="https://stackoverflow.com/questions/6999726/python-getting-millis-since-epoch-from-datetime">Getting millis since epoch from datetime</a>,特别是这个函数(注意我所做的小改动):</p>
<p><strong>解决方案</strong></p>
<pre><code>import datetime
def unix_time(dt):
epoch = datetime.datetime.utcfromtimestamp(0)
delta = dt - epoch
return delta.total_seconds()
def unix_time_millis(dt):
return long(unix_time(dt) * 1000.0)
</code></pre>
<p>对于本例,代码为:</p>
<pre><code>cql_statement = "update test set last_sent = :last_sent where id =:id"
rename_dict = {}
rename_dict['id'] = 'someid'
rename_dict['last_sent'] = unix_time_millis(datetime.datetime.now())
cursor.execute (cql_statement, rename_dict)
</code></pre>
<p>您可以将datetime转换为一个长值,该值包含自epoch以来的毫秒数,也就是说,更新将使用长值作为时间戳转换为等效的表单。</p>
<p>希望能帮助别人</p>