简体   繁体   English

将sqlite UTC时间转换为csv当地时间

[英]convert time from sqlite UTC to csv local time

Database is UTC, my python code: 数据库是UTC,我的python代码:

import sqlite3
import csv
import sys

conn = sqlite3.connect('/home/talo.db')
cur = conn.cursor()

data = cur.execute('select time, value from talo_data where position_id=1 AND DATETIME([time]) >= DATETIME("now","-1 day");')

with open('/home/log/my.csv', 'wb') as f:
  writer = csv.writer(f)
  writer.writerow(['date', 'value'])
  writer.writerows(data)
for row in data:
 print >> f, row

my csv file UTC, must be local time: 我的csv文件UTC,必须是本地时间:

date,value
2013-08-11 06:10:00,17.9375
2013-08-11 06:20:00,17.625
2013-08-11 06:30:00,18.0625
2013-08-11 06:40:00,19.4375
2013-08-11 06:50:00,19.4375
2013-08-11 07:00:00,18.6875
2013-08-11 07:10:00,19.8125
2013-08-11 07:20:00,19.5
2013-08-11 07:30:00,19.75

how i convert/do to csv file time +3 hours to my local time ? 我如何将csv文件时间转换/转换为本地时间+3小时?

In short use datetime.datetime.strptime(str, fmt) 简称为datetime.datetime.strptime(str,fmt)

>>> (datetime.datetime.strptime('2013-08-11 06:10:00,179375', "%Y-%m-%d %I:%M:%S,%f")+datetime.timedelta(hours=3)).strftime('%Y-%m-%d %I:%M:%S,%f')

In long this will be a three step process:- 总的来说,这将是一个三步过程:

  1. Convert the seconds in your date to a non decimal point format so that datetime.datetime.strptime understands it 将日期中的秒转换为非小数点格式,以便datetime.datetime.strptime可以理解
  2. Add/Subtract 3 hours using datetime.timedelta 使用datetime.timedelta加减3小时
  3. Output using strftime 使用strftime输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM