我有以下内容:
import psycopg2
from openpyxl import Workbook
wb = Workbook()
wb.active =0
ws = wb.active
ws.title = "Repair"
ws.sheet_properties.tabColor = "CCFFCC"
print(wb.sheetnames)
import datetime
import smtplib
import mimetypes
import logging
LOG_FILENAME = 'log-production.out'
logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG)
logging.debug('This message should go to the log file')
from datetime import date, timedelta
import os, sys
try:
conn = psycopg2.connect("connection string")
except:
print "I am unable to connect to the database"
cur = conn.cursor()
cur.execute("""SELECT ams.unit.line,ams.unit.work_order,ams.unit.model_num, ams.unit.revision ,ams.unit.serial_num,ams.unit.lpn, ams.unit_repair_detail.level_1_name as level_1,
ams.unit_repair_detail.level_2_name as level_2, ams.unit_repair_detail.level_3_name as level_3,ams.unit_repair_detail.level_4_name as level_4,ams.unit_repair.date_started AT TIME ZONE 'UTC' as date_started,ams.unit_repair.date_completed AT TIME ZONE 'UTC' as date_completed
FROM ams.unit_repair
left join
ams.unit
on ams.unit_repair.unit_id=ams.unit.id and
LOWER(ams.unit_repair.line) = LOWER(ams.unit.line)
right join
ams.unit_repair_detail
on ams.unit_repair.sid = ams.unit_repair_detail.unit_repair_sid
WHERE
LOWER(ams.unit.line) like ('%') and
ams.unit_repair_detail.date_created >= (CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - interval '24 hours')
AND ams.unit_repair_detail.date_created <= (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
and LOWER(ams.unit.model_num) like LOWER('%')
order by model_num asc""")
rows = cur.fetchall()
print "\nShow me the databases:\n"
col_names = ["Line","Work order","Model number","Revision","Serial number","Lpn","Level 1","Level 2","Level 3","Level 4","Date started","Date completed"]
ws.append(col_names)
for row in rows:
ws.append(row)
这是有效的但是在夏令时改变之后一切都崩溃了...查询返回数据库上的正确数据但是当我从python脚本运行它并且文件被创建时它仍然是UTC时间。我不知道我在做什么,将我的日期转换回UTC ......有人可以帮帮我吗?我已经尝试将顶部的时区设置为中心,因此它将UTC转换为中心而没有运气
cur.execute("SET TIME ZONE 'America/Chicago';")
我也试过
>>> import time
>>> offset = time.timezone if (time.localtime().tm_isdst == 0) else time.altzone
>>> offset / 60 / 60 * -1
我也试过将我的AT时区UTC转换为CST而没有运气......我在网上尝试了多种解决方案,但似乎没有任何效果。任何帮助将不胜感激!!
答案 0 :(得分:1)
以防万一将来遇到这样的事情......如果你将以下内容添加到查询中我发现了问题
`at time zone 'America/Chicago'
它将解决问题。不知怎的,at time zone UTC
还不够,你还需要指定输出时区