I am trying the following code to log using the timezone 'America/New_York'
the time is shown correct, but the %z and %Z are not showing the right time Zone
from datetime import datetime
from pytz import timezone
import logging
def timetz(*args):
tz = timezone('America/New_York')
print(tz)
dt = datetime.now(tz)
print(dt)
return dt.timetuple()
logger = logging.getLogger()
formatter = logging.Formatter(fmt='%(asctime)s::%(levelname)-8s\n%(message)s',datefmt='%Y-%m-%d %H:%M:%S %p %Z %z')
formatter.converter = timetz
formatter.default_msec_format = '%s.%03d'
handler= logging.StreamHandler()
handler.setFormatter(formatter)
handler.setLevel(logging.INFO)
logger.addHandler(handler)
logger.error('test')
I get
2021-03-05 14:53:04 PM UTC +0000::ERROR
test
But i should get
2021-03-05 14:53:04 PM EST -0500::ERROR
test
The %z and %Z are not working
Here is my error I do not manage to solve
ValueError: time data '1/31/2021 22:59' does not match format '%d/%m/%Y %H:%M:%S'
Here is my code
90% of the time my String date I need to convert goes in my try part and It works, I have a problem with my second part.
def StringToDateTime(DateString):
from datetime import datetime
try:
return datetime.strptime(DateString, '%Y-%m-%d %H:%M:%S')
except:
DateString = str(DateString)+':00'
return datetime.strptime(DateString, '%d/%m/%Y %H:%M:%S')
The error you're seeing is due to the str not having a seconds value -- the %S of the datetime format string.
Change the format string so it doesn't have the seconds placeholder, and it should work as expected:
try:
# Remove the %S from the format string here
return datetime.strptime(DateString, '%Y-%m-%d %H:%M')
except:
DateString = str(DateString)+':00'
return datetime.strptime(DateString, '%d/%m/%Y %H:%M:%S')
Or, if you want to alter the DateString as you do in your except clause:
# Add the seconds to the date string
DateString = f"{DateString}:00"
try:
return datetime.strptime(DateString, '%Y-%m-%d %H:%M:%S')
except:
return datetime.strptime(DateString, '%d/%m/%Y %H:%M:%S')
I have a string "Mai 2, 2018 6:35:52 AM".
I set the local to german (only one i can get to work is deu_deu)
when trying to then format I get the data remains error.
import pandas as pd
import locale
locale.setlocale(locale.LC_ALL, 'deu_deu')
input_file_path = 'C:\\Users\\xxx'
customer_file_path = pd.read_csv(input_file_path,sep='\t', warn_bad_lines=True, error_bad_lines=False)
customer_file_path['RECEIVED_DATE'] = customer_file_path['RECEIVED_DATE'].str.replace(',', '')
customer_file_path['RECEIVED_DATE'] = customer_file_path['RECEIVED_DATE'].str.replace('AM', 'am')
customer_file_path['RECEIVED_DATE'] = customer_file_path['RECEIVED_DATE'].str.replace('PM', 'pm')
customer_file_path['RECEIVED_DATE'] = pd.to_datetime(customer_file_path['RECEIVED_DATE'] ,format='%b %d %Y %I:%M:%S %p')
customer_file_path['RECEIVED_DATE'] = pd.to_datetime(customer_file_path['RECEIVED_DATE'] ,format='%b %d %Y %I:%M:%S %p').dt.strftime('%Y-%m-%d %H:%M:%S.%f')
Either one fails with the same message:
getlocale returns
locale.setlocale(locale.LC_ALL, 'deu_deu')
Out[167]: 'German_Germany.1252'
I am trying to find the average time taken. The value 'x' will allow me to get the time taken for every row there is, but how am I able to find the average time taken for all the rows. I will think it is something like x divided by count, but I am not able to find a solution to this... any pros out there can help me??
import datetime,time,csv
from itertools import islice
from Tkinter import Tk #python GUI programming
from tkFileDialog import askopenfilename
from collections import Counter
from datetime import datetime
import pandas
Tk().withdraw()
category_list=[]
description_list=[]
reported_date=[]
acknowledged_date=[]
count = 0
# hard code all possible date formats
date_formats = ['%m/%d/%Y %H:%M', '%-d-%b-%y', '%d/%m/%Y %h:%M %p', '%d/%m/%Y %H:%M', '%A, %d %B %Y %H:%M','%A, %d %B %Y %H:%M','%A %d %B %Y %H%M',"%d/%m/%Y %H:%M %p"," %d/%m/%Y %H:%M %p", '%d-%b-%y' ,
'%d.%m.%Y', '%d %b %Y %H%M hrs', '%d %b %Y %H%M', '%d-%m-%y', '%d-%b-%y', '%b-%d-%y', '%d-%a-%y','%e-%a-%y','%b %d %Y %H%M hrs','%d/%b/%Y %m:%M %p','%A, %e %B %Y %H:%M',' %d/%m/%Y %h:%M','%d-%b-%y','%m/%d/%Y %H:%M:%S %p']
#file = askopenfilename() #ask user which file to open
#f = open(file,'r')
with open('Feedback and Complaints_Sample Dataset.csv', 'rb') as f:
reader = csv.reader(f)
header = next(reader) #read 2nd line onwards
data= [] #make a list called data
for row in reader: #assign data in every column and name them respectively
for format in date_formats:
try:
reported_on = datetime.strptime(row[0], format) #try and get the dates
acknowledged_on = datetime.strptime(row[12], format) #try and get the dates
x= acknowledged_on-reported_on #time taken to acknowledge
#acknowledged_date.append(acknowledged_on)
#reported_date.append(reported_on)
count += 1
break # if correct format, dont test any other formats
except ValueError:
pass # if incorrect format, try other formats`enter code here`
Subtracting two datetime objects creates a timedelta object. You need to keep a total time, so create a timedelta object, and for each x add it to your total.
At the end, you can then divide your total_time by your count:
import csv
from itertools import islice
from datetime import datetime, timedelta
count = 0
total_time = timedelta()
# hard code all possible date formats
date_formats = ['%m/%d/%Y %H:%M', '%-d-%b-%y', '%d/%m/%Y %h:%M %p', '%d/%m/%Y %H:%M', '%A, %d %B %Y %H:%M','%A, %d %B %Y %H:%M','%A %d %B %Y %H%M',"%d/%m/%Y %H:%M %p"," %d/%m/%Y %H:%M %p", '%d-%b-%y' ,
'%d.%m.%Y', '%d %b %Y %H%M hrs', '%d %b %Y %H%M', '%d-%m-%y', '%d-%b-%y', '%b-%d-%y', '%d-%a-%y','%e-%a-%y','%b %d %Y %H%M hrs','%d/%b/%Y %m:%M %p','%A, %e %B %Y %H:%M',' %d/%m/%Y %h:%M','%d-%b-%y','%m/%d/%Y %H:%M:%S %p']
with open('Feedback and Complaints_Sample Dataset.csv', 'rb') as f:
reader = csv.reader(f)
header = next(reader) #read 2nd line onwards
for row in reader:
for format in date_formats:
try:
reported_on = datetime.strptime(row[0], format) #try and get the dates
acknowledged_on = datetime.strptime(row[12], format) #try and get the dates
x = acknowledged_on - reported_on #time taken to acknowledge
total_time += x
count += 1
break # if correct format, don't test any other formats
except ValueError:
pass # if incorrect format, try other formats`enter code here`
print "Total time taken:", total_time
print "Average time taken:", total_time / count
Note: your logic for the date_formats implies that both dates in a single row will always share the same date format.
I am having some issues getting timezone.localize() to work correctly. My goal is to grab today's date and convert it from CST to EST. Then finally format the datetime before spitting it out. I am able to format the date correctly, but the datetime is not changing from CST to EST. Additionally when I format the date I don't see the text representation of the timezone included.
Below I have listed out a simple program I created to test this out:
#! /usr/bin/python
#Test script
import threading
import datetime
import pexpect
import pxssh
import threading
from pytz import timezone
import pytz
est = timezone('US/Eastern')
curtime = est.localize(datetime.datetime.now().strftime("%a %b %d %H:%M:%S %Z %Y"))
#test time change
#curtime = datetime.datetime.now().strftime("%a %b %d %H:%M:%S %Z %Y")
class ThreadClass(threading.Thread):
def run(self):
#now = (datetime.datetime.now() + datetime.timedelta(0, 3600))
now = (datetime.datetime.now())
print "%s says Hello World at time: %s" % (self.getName(), curtime)
for i in range(3):
t = ThreadClass()
t.start()
.localize() takes a naive datetime object and interprets it as if it is in that timezone. It does not move the time to another timezone. A naive datetime object has no timezone information to be able to make that move possible.
You want to interpret now() in your local timezone instead, then use .astimezone() to interpret the datetime in another timezone:
est = timezone('US/Eastern')
cst = timezone('US/Central')
curtime = cst.localize(datetime.datetime.now())
est_curtime = curtime.astimezone(est).strftime("%a %b %d %H:%M:%S %Z %Y")
def run(self):
print("%s says Hello World at time: %s" % (self.getName(), est_curtime))
Use cst.localize to make a naive datetime into a timezone-aware datetime.
Then use astimezone to convert a timezone-aware datetime to another timezone.
import pytz
import datetime
est = pytz.timezone('US/Eastern')
cst = pytz.timezone('US/Central')
curtime = cst.localize(datetime.datetime.now())
curtime = curtime.astimezone(est)