Parsing a String date into a date object in Python - python

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.

Related

normalize different date formats

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.

unix timestamp's output using datetime.utcfromtimestamp() differs from its correct value

I have followed following answer in order to convert unix timestamp string into a readable date.
from datetime import datetime ts = int("1284101485")
print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))
As input when I provider 1607111882000; it prints 2020-12-04 19:58:02. But on the following site (https://www.unixtimestamp.com/index.php), the output is as follows:
1607111882000
Is equivalent to:
05/06/52897 # 11:13pm (UTC)
52897-05-06T23:13:20+00:00 in ISO 8601
Mon, 06 May 52897 23:13:20 +0000 in RFC 822, 1036, 1123, 2822
Monday, 06-May-97 23:13:20 UTC in RFC 2822
52897-05-06T23:13:20+00:00 in RFC 3339
Why is there this difference and which one is correct? What should I do to obtain the same result as in the unixtimestamp.com site?
That website is probably using time.ctime or an equivalent function:
>>> time.ctime(time.mktime(time.gmtime(1607111882000)))
'Mon May 6 23:13:20 52897'
As to whether or not it's correct to use that is debatable. The date might not be 100% accurate.
For your number, I think you put milliseconds instead of seconds as on my machine it gives an error (ValueError: year is out of range), but dividing by 1000 gives the correct date for both functions:
>>> ts = 1607111882
>>> print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))
2020-12-04 19:58:02
>>> time.ctime(time.mktime(time.gmtime(ts)))
'Fri Dec 4 19:58:02 2020'
>>>
You right. And unixtimestamp (UT) wrong. I think UT mistake between second and milliseconds.
Let access https://unixtime.org/ , this website Supports Unix timestamps in seconds, milliseconds, microseconds and nanoseconds.
Example: 1607111882000
Result:
Format Milliseconds (1/1,000 second)
GMT Fri Dec 04 2020 19:58:02 GMT+0000
Relative 3 months ago

Day of week constants with Sunday = 0 in Python

The calendar module has day of week constants beginning with
calendar.MONDAY
Out[60]: 0
However, sometimes one must interface with a system (perhaps written in JavaScript) that uses the convention Sunday = 0. Does Python provide such constants?
There are no such constants in the Python standard library. It is trivial to define your own however:
SUN, MON, TUE, WED, THU, FRI, SAT = range(7)
or, when using Python 3.4 or up, you could use the enum module functional API:
from enum import IntEnum
Weekdays = IntEnum('Weekdays', 'sun mon tue wed thu fri sat', start=0)
weekday == Weekdays.wed
and then you can then also map a weekday integer to an enumeration value by calling the enum.Enum object:
weekday_enum = Weekdays(weekday)
I used 3-letter abbreviations but you are free to use full names if you find that more readable.
I just use this with mydate.isoweekday(), does the job.
def weekday_sun_zero(self, isoweekday):
return 0 if isoweekday == 7 else isoweekday
hope this helps:
from datetime import datetime
weekdays_dic ={0:'Mon', 1:'Tue',2:'Wed',3:'Thu',4:'Fri',5:'Sat', 6:'SUN'}
print(weekdays_dic[datetime.today().weekday()])

How to convert a string based timestamp from Unix's date command to a Python date object?

A variety of programs output date formats according to the syntax of their Unix platforms date command. For example: Tue Nov 5 12:38:00 EST 2013.
How can I easily convert this into a Python date object?
The answer is actually pretty simple. You just need to use the datetime.strptime() method which converts a string representation of a date (1st parameter) into a date object based on a directive which specifies that format of the string representation (2nd parameter).
In this case, this is the code you would use:
import datetime
unix_date_format = '%a %b %d %H:%M:%S %Z %Y'
# Matches strings like Tue Nov 5 12:38:00 EST 2013
my_date = datetime.datetime.strptime(
date_in_string_format, unix_date_format)
Further Reading
datetime.strptime() method

Reformatting Dates Python

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.

Categories

Resources