Python Django Format Localization - python

I created custom format files in my django project.
see: https://docs.djangoproject.com/en/4.0/topics/i18n/formatting/#creating-custom-format-files
I created my format files for my project as in the link. And I have format file for all languages to be used in my project. According to the user's language, the relevant format file is automatically called by django.
Can I call the definitions here based on the user's language? If I can call my definitions, my automatic transform methods will be ready.
Your opinion is very valuable to me, thank you.
formats/tr/formats.py:
DATE_FORMAT = 'd F Y'
TIME_FORMAT = 'H:i:s'
STR_DATE_FORMAT = "%d.%m.%Y"
STR_DATE_SQL_FORMAT = "%Y-%m-%d"
STR_DATETIME_FORMAT = "%d.%m.%Y %H:%M:%S"
STR_TIME_FORMAT = "%H:%M:%S"
DATE_INPUT_FORMATS = (
'%d.%m.%Y', # '25.10.2022', '25.10.06'
'%d/%m/%Y', # '25/10/2022'
# '%d %B %Y', '%d %b. %Y', # '25 Ekim 2022', '25 Eki. 2022'
)
DATETIME_INPUT_FORMATS = (
'%d.%m.%Y %H:%M:%S', # '25.10.2022 14:30:59'
'%d.%m.%Y %H:%M:%S.%f', # '25.10.2022 14:30:59.000200'
'%d.%m.%Y %H:%M', # '25.10.2022 14:30'
'%d.%m.%Y', # '25.10.2022'
)
converts/datetime.py:
class DateTime:
from formats.tr import formats as ff
def __init__(self):
from formats.en import formats as ff
if a == "tr":
from formats.tr import formats as ff
else:
from formats.en import formats as ff
#classmethod
def to_string(cls, tarih, date_format=ff.STR_DATE_FORMAT, time_format=ff.STR_TIME_FORMAT):
"""
:param tarih:
:return:
"""
if isinstance(tarih, datetime.datetime):
return tarih.strftime("%s %s" % (date_format, time_format))
elif isinstance(tarih, datetime.date):
return tarih.strftime(date_format)
elif isinstance(tarih, datetime.time):
return tarih.strftime(time_format)
#classmethod
def to_datetime(cls, tarih, dtformat=ff.STR_DATETIME_FORMAT):
"""
:param tarih:
:return:
"""
try:
from dateutil.parser import parse
# print "%s == %s" % (tarih, parse(tarih))
# return parse(tarih)
return datetime.datetime.strptime(tarih, dtformat)
except ValueError:
zaman = "%s %s" % (tarih, "00:00:00")
return datetime.datetime.strptime(zaman, dtformat)
It would be ridiculous for me to call like in the datetime.py init method. There has to be a more logical way to do this. But, how?

Related

Date String in Datetime Format Python

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

Formating a german date cause unconverted data remains:am

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'

Python CSV Finding average time taken

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.

Proper way to use python's datetime's strftime method

I am using FUSE (a virtual file system) to try and implement a read call that will give me the current date/time as a string.
import os
import sys
import errno
import datetime
from fuse import FUSE, FuseOSError, Operations
class FileSys(Operations):
def __init__(self, root):
self.root = root
def _full_path(self, partial):
if partial.startswith("/"):
partial = partial[1:]
path = os.path.join(self.root, partial)
return path
# allows us to set attributes
def getattr(self, path, fh= None):
full_path = self._full_path(path)
st = os.lstat(full_path)
return dict((key, getattr(st, key)) for key in ('st_atime', 'st_ctime',
'st_gid', 'st_mode', 'st_mtime', 'st_nlink', 'st_size', 'st_uid'))
# allows us to see files
def readdir(self, path, fh):
#logging.info("Enter readdir")
full_path = self._full_path(path)
dirents = ['.', '..']
if(os.path.isdir(full_path)):
dirents.extend(os.listdir(full_path))
for r in dirents:
yield r
def read(self, path, length, offset, fh= None):
date = datetime.datetime.today()
date = date.strftime("%a %b %d %H:%M:%S %Z %Y")
return date
def main(root, mountpoint):
FUSE(FileSys(root), mountpoint, foreground= True)
if __name__ == '__main__':
main('/home/user/mydir', '/mnt/dummy')
However, my output is printing like this
Tue May 2
When I really want something like this
Tue May 27 14:43:06 CDT 2014
So, only getting up to the first digit of the day. Anyone see what I am doing wrong? I looked at the strftime documentation and I am sure all of my letters are corresponding to the correct pieces of the formatted string.

Python: timezone.localize() not working

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)

Categories

Resources