我有一个测试我的潮流的脚本:
from influxdb import InfluxDBClient
import time
now_time = "2016-03-21 15:03:46"
#now_time = time.strftime("%Y-%m-%dT%XZ",time.localtime())
json_body = [
{
"measurement": "info_cpu_load3",
"tags":{
"host": "1",
"cpu": "cpu0"
},
"time": "%s"%time.strftime("%Y-%m-%d %X",time.localtime()),
"fields": {
"user":12,
"iowait":15
}
}
]
client = InfluxDBClient('localhost', 8086, 'root', 'root', 'monitor')
client.create_database('monitor')
client.write_points(json_body)
当我评论该行" now_time = time.strftime("%Y-%m-%dT%XZ",time.localtime())"它会工作正常,但取消注释这一行,我无法从数据库中找到任何东西,为什么?
答案 0 :(得分:1)
我找到了时区的原因,在搜索了Influxdb文档之后,我得到了大量的flowxdb支持utc时间,所以我通过编写一个函数来解决这个问题,将本地时间转换为utc时间。
import pytz
def utctime(self,times):
'''
transform local time to utc time
:param times:
:return:
'''
try:
utc = pytz.utc
cmd = "cat /etc/sysconfig/clock|cut -d = -f 2"
out,err,stat = exec_shell(cmd)
now_utc = out.read().strip()
fmt = '%Y-%m-%d %H:%M:%S'
amsterdam = pytz.timezone(now_utc)
dt = datetime.datetime.strptime(times, fmt)
am_dt = amsterdam.localize(dt)
utc_time = am_dt.astimezone(utc).strftime(fmt)
except Exception,err:
utc_time = 1
dlog("utctime err:%s"%err)
return utc_time