python脚本不断将日期转换为utc

编程入门 行业动态 更新时间:2024-10-22 02:57:19
本文介绍了python脚本不断将日期转换为utc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下内容:

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)

这是可行的,但是在夏令时更改之后,一切都中断了...查询返回了db上的正确数据,但是当我从python脚本运行它并创建文件时,它仍然处于UTC时间.我不知道我在做什么,这将我的日期转换回UTC.有人可以帮助我吗?我尝试将顶部的时区设置为中央时区,这样它就可以将UTC转换为中央时区,而没有运气

This was working but after the daylight savings time change everything broke... The query returns the correct data on the db but when I run it from the python script and the file is created it is still in UTC time. I don't know what I am doing that is converting my dates back to UTC... Can anybody help me? I have tried setting the timezones at the top to be central so it converts the UTC to central with no luck

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

我还尝试将我的时区UTC更改为CST,但没有运气...我在网络上尝试了多种解决方案,但似乎没有任何效果.任何帮助将不胜感激!

I also tried changing my AT TIME ZONE UTC TO CST and no luck... I have tried multiple solutions on the web but nothing appears to be working. Any help will be greatly appreciated!!

推荐答案

以防万一将来有人遇到类似问题……如果您将以下内容添加到查询中,则会发现问题

just in case anybody runs into something like this in the future... I found the problem if you add the following to the query

`at time zone 'America/Chicago'

它将解决问题.以某种方式 UTC时区还不够,您仍然需要指定输出时区

it will resolve the problem. Somehow the at time zone UTC is not enough you still need to specify the output timezone

更多推荐

python脚本不断将日期转换为utc

本文发布于:2023-11-06 14:22:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563928.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   脚本   日期   python   utc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!