python script keeps converting dates to utc

2024/10/15 13:15:27

I have the following:

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 mimetypesimport 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_completedFROM ams.unit_repairleft join ams.uniton ams.unit_repair.unit_id=ams.unit.id and LOWER(ams.unit_repair.line) =  LOWER(ams.unit.line)right join ams.unit_repair_detailon ams.unit_repair.sid = ams.unit_repair_detail.unit_repair_sidWHERE 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)

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';") 

I have also tried

>>> import time
>>> offset = time.timezone if (time.localtime().tm_isdst == 0) else time.altzone
>>> offset / 60 / 60 * -1

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!!

Answer

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'

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

https://en.xdnf.cn/q/117825.html

Related Q&A

sklearn tsne with sparse matrix

Im trying to display tsne on a very sparse matrix with precomputed distances values but Im having trouble with it.It boils down to this:row = np.array([0, 2, 2, 0, 1, 2]) col = np.array([0, 0, 1, 2, 2,…

Removing a sublist from a list

I have a list e.g. l1 = [1,2,3,4] and another list: l2 = [1,2,3,4,5,6,7,1,2,3,4]. I would like to check if l1 is a subset in l2 and if it is, then I want to delete these elements from l2 such that l2 …

Python double FOR loops without threading

Basically, I want to make a grid with school subjects and all the test results I got from it, and I want to display about 10 results for every subject.Like this:... ------------------------------------…

Challenging way of counting entries of a file dynamically

I am facing a strange question, which despite of trying many times, i am not able to find the logic and proper code to the problem. I have a file in the format below: aa:bb:cc dd:ee:ff 100 ---------…

NSException on import of matplotlib, kivy in OSX

Im working on some kivy code thats working fine on windows 10, but crashes on osx sierra, Ive isolated that the crash happens when I import kivy.core.window along side matplotlib: import matplotlib mat…

strange behavior with lamba: getattr(obj, x) inside a list [duplicate]

This question already has answers here:Creating functions (or lambdas) in a loop (or comprehension)(9 answers)Closed 11 years ago.In the following example:class A(object):passprop1 = 1prop2 = 2prop3 = …

Detect or Generate Regular Expression from String

I was wondering if there were any Python packages out there that detects a regular expression from a string. Conceptually this is easy enough to do but I wanted to see if there was anyone else who has …

Date regex python

I am trying to match dates in a string where the date is formatted as (month dd, yyyy). I am confused by what I see when I use my regex pattern below. It only matches strings that begin with a date. Wh…

Not able to install the python-module numpy

After hours of trying Im still not able to install numpy. I READ LOTS OF HINTS, ANSWERS USW. BUT IT DOESNT HELP. Furthermore I have windows 7, 32 bit, Python 27. What I did:download numpy-1.10.2.zi…

Windows Error: 32 when trying to rename file in python

Im trying to rename some PDF files using pyPdf and my code it seems to work fine until it reaches the rename sentence. The While/if block of code looks for the page number where string "This stri…