I am trying to work with an XML data that has different kinds of (string) date values, like:
'Sun, 04 Apr 2021 13:32:26 +0200'
'Sun, 04 Apr 2021 11:52:29 GMT'
I want to save these in a Django object that has a datetime field.
The script that I have written to convert a str datetime is as below:
def normalise(val):
val = datetime.strptime(val, '%a, %d %b %Y %H:%M:%S %z')
return val
Although, this does not work for every datetime value I scrape. For example for above 2 examples, the script works for the first one but crashes for the second.
What would be an ideal way of normalising all the datetime values ?
dateutil module parses many different types of formats. You can find the doc here
This is a simple example:
if __name__ == '__main__':
from dateutil.parser import parse
date_strs = ['Sun, 04 Apr 2021 13:32:26 +0200','Sun, 04 Apr 2021 11:52:29 GMT']
for d in date_strs:
print(parse(d))
output:
2021-04-04 13:32:26+02:00
2021-04-04 11:52:29+00:00
If there are other date formats that this doesn't cover you can to store specific python format strings keyed by the xml element name.
Related
In a script, I am converting the date string into DateTime format, so that I can modify the date, but this timezone part is showing an error.
from datetime import datetime
date_str = 'Wed, 1 Jun 2022 16:44:40 +0200 (CEST)'
temp_date = datetime.strptime(date_str, '%a, %d %b %Y %H:%M:%S %z (%Z)')
print(temp_date)
When I run this I am getting ValueEror.
ValueError: time data 'Wed, 01 Jun 2022 16:44:40 +0200 (CEST)' does not match format '%a, %d %b %Y %H:%M:%S %z (%Z)'
An extract from the datetime documentation:
strptime() only accepts certain values for %Z:
any value in time.tzname for your machine’s locale
the hard-coded values UTC and GMT
So someone living in Japan may have JST, UTC, and GMT as valid values,
but probably not EST. It will raise ValueError for invalid values
Try running the below to see if CEST is in your machine's locale:
import time
print(time.tzname)
Seeing the complexity of problem, I suggest using third party library like dateutil which can parse datetime with ease.
from dateutil.parser import parse
date_str = 'Wed, 1 Jun 2022 16:44:40 +0200 (CEST)'
temp_date = parse(date_str)
print(temp_date)
temp_date is of type datetime.datetime
https://dateutil.readthedocs.io/en/stable/
In my python code I get start and end time some thing like:
end = int(time.time())
start = end - 1800
Now start and end variables holds values like 1460420758 and 1460422558.
I am trying to convert it in a meaningful format like :
Mon Apr 11 17:50:25 PDT 2016
But am unable to do so, I tried:
time.strftime("%a %b %d %H:%M:%S %Y", time.gmtime(start))
Gives me
Tue Apr 12 00:25:58 2016
But not only the timezone but the H:M:S are wrong
As date returns me the below information:
$ date
Mon Apr 11 18:06:27 PDT 2016
How to correct it?
This one involves utilizing datetime to great the format you wish with the strftime module.
What's important is that the time information you get 'MUST' be UTC in order to do this. Otherwise, you're doomed D:
I'm using timedelta to 'add' hours to the time. It will also increments the date, too. I still would recommend using the module I shared above to handle time zones.
import time
# import datetime so you could play with time
import datetime
print int(time.time())
date = time.gmtime(1460420758)
# Transform time into datetime
new_date = datetime.datetime(*date[:6])
new_date = new_date + datetime.timedelta(hours=8)
# Utilize datetime's strftime and manipulate it to what you want
print new_date.strftime('%a %b %d %X PDT %Y')
I am parsing a log file and one element contains the date as a String:
Tue Mar 31 20:24:23 BST 2015
The date is in element[i][0] of a 2DList
What I am a little stumped on (without going about this in some awful compare and replace manner) is how to turn this date into something comparable in Python.
I get a few entries in a log file which are within a few minutes of each other, so I would like to group these as one.
Tue Mar 31 20:24:23 BST 2015
Tue Mar 31 20:25:45 BST 2015
Tue Mar 31 20:26:02 BST 2015
What options can be suggested?
I am aware that I can input logic to replace 'Mar' with 3, remove Day Tue/Wed etc strings, but everything else is somewhat needed.
Would it be acceptable to replace a : with / I can then split the date into a list by its ' ' delimiter, then compare the 20/26/02 element, but before I go and do all that, is there a built in way? I have searched and found python datetime 1, which I would use after a lot of replacing values.
Really, I'm looking for a built in method!
You can use the datetime.datetime.strptime.
Here are format specifiers.
Something like datetime.strptime(your_string, "%a %b %d %H:%M:%S %Z %Y") should do the work.
I have a file with dates in a few different formats and am trying to get them all into YYYYMMDD format in Python. Most of the dates are in the below format:
Mon, 01 Jul 2013 16:33:59 GMT
and I have no idea how to get them into
20130701
I apologize if this is a pretty simple question---I am sort of new to python
EDIT: I am trying to do this for ANY given date. I used the 01 July as an example and in retrospect made it seem like I was asking a different question. So I guess I am looking for something that can both find dates and reformat them
Use the python-dateutil library:
from dateutil import parser
dtobject = parser.parse(datestring)
The datutil.parser.parse() method recognises a wide variety of date formats, and returns a datetime.datetime() object.
Use the datetime.strftime() method if you want to format the result as a (uniform) string again:
dtobject.strftime('%Y%m%d')
Demo:
>>> from dateutil import parser
>>> parser.parse('Mon, 01 Jul 2013 16:33:59 GMT')
datetime.datetime(2013, 7, 1, 16, 33, 59, tzinfo=tzlocal())
>>> parser.parse('Mon, 01 Jul 2013 16:33:59 GMT').strftime('%Y%m%d')
'20130701'
This can be achieved following way also:
import datetime
x = 'Mon, 01 Jul 2013 16:33:59 GMT'
''.join(str(datetime.datetime.strptime(x, '%a, %d %b %Y %H:%M:%S %Z').date()).split('-'))
if any other parameter is introduced in your date string, you can include the directive . for example %p is Locale’s equivalent of either AM or PM.
I'm using Python 3.3. I'm getting an email from an IMAP server, then converting it to an instance of an email from the standard email library.
I do this:
message.get("date")
Which gives me this for example:
Wed, 23 Jan 2011 12:03:11 -0700
I want to convert this to something I can put into time.strftime() so I can format it nicely. I want the result in local time, not UTC.
There are so many functions, deprecated approaches and side cases, not sure what is the modern route to take?
Something like this?
>>> import time
>>> s = "Wed, 23 Jan 2011 12:03:11 -0700"
>>> newtime = time.strptime(s, '%a, %d %b %Y %H:%M:%S -0700')
>>> print(time.strftime('Two years ago was %Y', newtime))
Two years ago was 2011 # Or whatever output you wish to receive.
I use python-dateutil for parsing datetime strings. Function parse from this library is very handy for this kind of task
Do this:
import email, email.utils, datetime, time
def dtFormat(s):
dt = email.utils.parsedate_tz(s)
dt = email.utils.mktime_tz(dt)
dt = datetime.datetime.fromtimestamp(dt)
dt = dt.timetuple()
return dt
then this:
s = message.get("date") # e.g. "Wed, 23 Jan 2011 12:03:11 -0700"
print(time.strftime("%Y-%m-%d-%H-%M-%S", dtFormat(s)))
gives this:
2011-01-23-21-03-11