Email Date Conversion in Python - python

I am capturing the header of an Email to Email Date in the format below:
Fri, 27 Mar 2020 12:05:17 +0000 (UTC)
I need to transform to the format: YYYY-MM-DD HH: MM
I tried to use the datetime.strptime function but to no avail.
Can anyone help me with this? As I'm starting now in Python, I'm taking a beating!

I would use python-dateutil to convert your string to a datetime object, then use strftime to output a string of the desired format.
import datetime
from dateutil import parser
x = parser.parse("Fri, 27 Mar 2020 12:05:17 +0000 (UTC)")
print(x.strftime("%Y-%m-%d %H:%M"))
the output should be
2020-03-27 12:05

A custom implementation without using the datetime library if you don't want that dependency.
date_given = "Fri, 27 Mar 2020 12:05:17 +0000 (UTC)"
month ={'Jan':'01','Feb':'02','Mar':'03','Apr':'04','May':'05','Jun':'06','Jul':'07','Aug':'08','Sep':'09','Oct':'10','Nov':'11','Dec':'12'}
_,d,m,y,t,_,_ = date_given.split(' ')
print(str(y)+"-"+str(month[m])+'-'+str(d)+' '+str(t[:-3]))
Would give you 2020-03-27 12:05

The standard library's email package provides tools to parse RFC5322 format datetime strings.
from email.headerregistry import DateHeader
kwds = {} # This dict is modified in-place
DateHeader.parse('Fri, 27 Mar 2020 12:05:17 +0000 (UTC)', kwds)
kwds['datetime']
datetime.datetime(2020, 3, 27, 12, 5, 17, tzinfo=datetime.timezone.utc)
While DateHeader is the modern tool for parsing date headers, the legacy* function email.utils.parsedate_to_datetime is easier to use
from email.utils import parsedate_to_datetime
parsedate_to_datetime('Fri, 27 Mar 2020 12:05:17 +0000 (UTC)')
datetime.datetime(2020, 3, 27, 12, 5, 17, tzinfo=datetime.timezone.utc)
*While to the docs list the utils module under the legacy API heading, parsedate_to_datetime is used internally by DateHeader to parse datetime strings, so it probably isn't going away any time soon.

Related

Change '1 Apr 2022, noon' to datetime format python

I want to change '1 Apr 2022, noon' to YYYY-MM-DD HH:MM:SS format.
I know the datetime thing in python has the ability to parse but I am unsure how
You can use the dateparser module's parse() function:
>>> from dateparser import parse
>>> parse("1 Apr 2022, noon")
datetime.datetime(2022, 4, 1, 12, 0)
This gets us a datetime.datetime object. We can now call strftime() to format it properly:
>>> date_time = parse("1 Apr 2022, noon")
>>> print(date_time.strftime("%Y-%m-%d %H:%M:%S")
'2022-04-01 12:00:00'

How to preserve timezone when converting a date time string to a datetime object [duplicate]

This question already has answers here:
Parsing date/time string with timezone abbreviated name in Python?
(6 answers)
Closed 5 years ago.
I have a date in this format = "Tue, 28 Feb 2017 18:30:32 GMT"
I can convert it to a datetime object using time.strptime("Tue, 28 Feb 2017 18:30:32 GMT", "%a, %d %b %Y %H:%M:%S %Z") but the datetime object does not keep track of timezone.
I want to be able to to know the timezone. How can I achieve that? Any help is much appreciated.
from dateutil import parser
parser.parse("Tue, 28 Feb 2017 18:30:32 GMT")
datetime.datetime(2017, 2, 28, 18, 30, 32, tzinfo=tzutc())
This problem is actually more involved than it might first appear. I understand that timezone names are not unique and that there are throngs of the things. However, if the number of them that you need to work with is manageable, and if your inputs are limited to that format, then this approach might be good for you.
>>> from dateutil.parser import *
>>> tzinfos = {'GMT': 0, 'PST': -50, 'DST': 22 }
>>> aDate = parse("Tue, 28 Feb 2017 18:30:32 GMT", tzinfos=tzinfos)
>>> aDate.tzinfo.tzname(0)
'GMT'
>>> aDate = parse("Tue, 28 Feb 2017 18:30:32 PST", tzinfos=tzinfos)
>>> aDate.tzinfo.tzname(0)
'PST'
>>> aDate = parse("Tue, 28 Feb 2017 18:30:32 DST", tzinfos=tzinfos)
>>> aDate.tzinfo.tzname(0)
'DST'
Load the alternative timezone abbreviations into a dictionary, in this code called tzinfos then parse away. The timezone parsed from the date expression becomes available in the construct shown.
Other date items are available, as you would expect.
>>> aDate.day
28
>>> aDate.month
2
>>> aDate.year
2017

How to specify TimeZone in date formatting to get local time

I'm trying to compare two date values:
1) Jun 23, 2016 10:36:31 EET
2) Thu, 23 Jun 2016 07:36:31 GMT
To do this I need to convert second date to same date format as first one, so I use following code:
import datetime
date = datetime.datetime.strptime("Thu, 23 Jun 2016 07:36:31 GMT", "%a, %d %b %Y %H:%M:%S %Z")
.strftime('%b %d, %Y %H:%M:%S %Z')
and get following output:
Jun 23, 2016 07:36:31
This is still GMT time (also time zone value not specified)
How should I update my strftime argument to get Jun 23, 2016 10:36:31 EET as output?
P.S. EET is my local time zone
Here is the basic approach using the pytz module:
import datetime
import pytz
fmt = "%a, %d %b %Y %H:%M:%S %Z"
date = datetime.datetime.strptime("Thu, 23 Jun 2016 07:36:31 GMT", fmt)
gmt_date = pytz.timezone('GMT').localize(date)
print("Time in GMT:", gmt_date.strftime(fmt), sep='\n')
# Now to convert! Notice it took into account "summer time"
print("Time in EET",
gmt_date.astimezone(pytz.timezone('EET')).strftime(fmt), sep='\n')
My output:
Time in GMT:
Thu, 23 Jun 2016 07:36:31 GMT
Time in EET
Thu, 23 Jun 2016 10:36:31 EEST
Please read the docs, as working with timezones is tricky, and there are many caveats:
http://pytz.sourceforge.net/
AFAIK AM isn't a timezone. Did you try to get datetime.astimezone(tz)to work? https://docs.python.org/2/library/datetime.html
If you want to compare date-time values, it is much better to convert them both to datetime objects, e.g. call datetime.strptime on both of the input strings, each with the appropriate format string, and then compare the resulting datetime objects.

Python: datetime format

I have the following datetime string:
Mon Oct 27 23:00:03 +0000 2014
I would like to convert this string to a form where I could compare the datetimes. So, the first thing I tried is converting this to a datetime in Python.
I am having trouble with the correct formatting. I have followed the documentation, but it does not work.
I have tried the following:
str = 'Mon Oct 27 23:00:03 +0000 2014'
datetime.strptime(str, '%a %b %d %X %Z %Y')
How can I get this to work?
If you want to convert it to the datetime object you can use library python-dateutil.
For example:
In [6]: dateutil.parser.parse('Mon Oct 27 23:00:03 +0000 2014')
Out[6]: datetime.datetime(2014, 10, 27, 23, 0, 3, tzinfo=tzutc())
In Python 3.2+:
>>> from datetime import datetime
>>> timestr = 'Mon Oct 27 23:00:03 +0000 2014'
>>> datetime.strptime(timestr, '%a %b %d %X %z %Y')
datetime.datetime(2014, 10, 27, 23, 0, 3, tzinfo=datetime.timezone.utc)
Note the lower case %z.
Here's a stdlib-only version that works on Python 2 and 3:
#!/usr/bin/env python
from datetime import datetime
from email.utils import parsedate_tz, mktime_tz
timestamp = mktime_tz(parsedate_tz('Mon Oct 27 23:00:03 +0000 2014'))
utc_dt = datetime.utcfromtimestamp(timestamp)
# -> datetime.datetime(2014, 10, 27, 23, 0, 3)
where utc_dt is a datetime object that represents time in UTC timezone (regardless of the input timezone).
Note: it doesn't support the time that represents a leap second (though datetime object can't represent it anyway):
>>> datetime.utcfromtimestamp(mktime_tz(parsedate_tz('Tue June 30 23:59:60 +0000 2015')))
datetime.datetime(2015, 7, 1, 0, 0)
Your problem lies with your %z UTC offset value (you should've used a lowercase z). However,
%z is only supported in Python 3.2+
If you are stuck with an older version of Python, you could possibly take out the UTC offset from the string and try converting it after you convert the rest

Parsing the string to Dates in python [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parse date and format it using python?
I'm very new to Python. I have the following two strings :
Mon, 29 Oct 2012 13:07:07 GMT
2012-10-29 12:57:08
I wish there could be any date parsing lib available in python which does parse the above two strings to something like this:
2012-10-29 12:57:08
So that I can compare them. Note that the comparison should be able to produce the result like integer comparison. Like 1 is less than 2, so the same way 2012-10-29 12:57:08 is less than 2012-10-29 13:57:08
Are there any easy to do so in python?
Thanks in advance.
Use the dateutil module for general date parsing:
>>> from dateutil.parser import parse
>>> parse('Mon, 29 Oct 2012 13:07:07 GMT')
datetime.datetime(2012, 10, 29, 13, 7, 7, tzinfo=tzutc())
>>> parse('2012-10-29 12:57:08')
datetime.datetime(2012, 10, 29, 12, 57, 8)
datetime.datetime objects can be compared in various ways.
If you know the exact format of each date string to be parsed, you can also do the parsing more explicitly using the datetime.datetime.strptime() method:
>>> import datetime
>>> datetime.datetime.strptime('Mon, 29 Oct 2012 13:07:07 GMT', '%a, %d %b %Y %H:%M:%S %Z')
datetime.datetime(2012, 10, 29, 13, 7, 7)
Note however that that method ignores timezones!
Yes, time.strptime can convert the text to date representations. From there you can use strftime to print it how you like.
>>> a = '2012-10-29 12:57:08'
>>> time.strptime(a, '%Y-%m-%d %H:%M:%S')
time.struct_time(tm_year=2012, tm_mon=10, tm_mday=29, tm_hour=12, tm_min=57, tm_sec=8, tm_wday=0, tm_yday=303, tm_isdst=-1)
>>> b = 'Mon, 29 Oct 2012 13:07:07 GMT'
>>> time.strptime(b, '%a, %d %b %Y %H:%M:%S %Z')
time.struct_time(tm_year=2012, tm_mon=10, tm_mday=29, tm_hour=13, tm_min=7, tm_sec=7, tm_wday=0, tm_yday=303, tm_isdst=0)
The datetime module in python, with its strptime function (string parse), can do that.
For example, you can use the function like this:
somestring = '2004-03-13T03:00:00Z'
result = datetime.datetime.strptime(somestring, '%Y-%m-%dT%H:%M:%SZ')
Docs here.

Categories