Gre*_*een -3 python unix timestamp
对这个还是陌生的。我试图将 13 位时间戳转换为您可以阅读但没有运气的内容。我正在做的是从楼宇自动化系统中采样温度数据。这是我正在使用的代码。我错过了一些非常简单的东西。我可以朝正确的方向戳一下吗?
when_updated=driver.find_element_by_name("_lastUpdated")
timestamp=when_updated.get_attribute("value")
print("Timestamp:", timestamp)
nos=10 # Number of samples
freq=5 # Sampling frequency
print("\nTemperature & timestamp, sampled every", freq, "seconds:")
while True:
ts=driver.find_element_by_name("_lastUpdated").get_attribute("value")
print("\nTemperature: ", element.text, "read at:", ts)
time.sleep(freq)
driver.switch_to_default_content()
Run Code Online (Sandbox Code Playgroud)
输出
Temperature:
Timestamp: 0
Temperature & timestamp, sampled every 5 seconds:
Temperature: read at: 0
Temperature: 21.0 ºC read at: 1520912895599
Temperature: 21.0 ºC read at: 1520912901432
Temperature: 21.0 ºC read at: 1520912907070
Run Code Online (Sandbox Code Playgroud)
#!python2
# Epoch time needs to be converted to a human readable format.
# Also, epoch time uses 10 digits, yours has 13, the last 3 are milliseconds
import datetime, time
epoch_time = 1520912901432
# truncate last 3 digits because they're milliseconds
epoch_time = str(epoch_time)[0: 10]
# print timestamp without milliseconds
print datetime.datetime.fromtimestamp(float(epoch_time)).strftime('%m/%d/%Y -- %H:%M:%S')
'''
03/12/2018 -- 21:48:21
'''
# % % % % % % % % % % % % % % % % % % % % % %
# If you need milliseconds
epoch_time = 1520912901432
# get first 10 digits
leading = str(epoch_time)[0: 10]
# get last 3 digits
trailing = str(epoch_time)[-3:]
# print timestamp with milliseconds
print datetime.datetime.fromtimestamp(float(leading)).strftime('%m/%d/%Y -- %H:%M:%S.') + ('%s' % int(trailing))
'''
03/12/2018 -- 21:48:21.432
'''
Run Code Online (Sandbox Code Playgroud)
Python 2time模块:https : //docs.python.org/2/library/time.html
| 归档时间: |
|
| 查看次数: |
3819 次 |
| 最近记录: |