简体   繁体   中英

convert time from sqlite UTC to csv local time

Database is UTC, my python code:

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:

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 ?

In short use 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
  2. Add/Subtract 3 hours using datetime.timedelta
  3. Output using strftime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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