Python : string with timezone to datetime object conversion - python

I have a string like this
dateStr = "Wed Mar 15 12:50:52 GMT+05:30 2017"
which is IST time.
Is there any way to read the dateStr as per the specified timezone within the dateStr
i.e. GMT+05:30.
So that I can make datetime object directly.
I have tried to parse it using format
format = "%a %b %d %H:%M:%S %Z%z %Y"
But it gives me error of format does not match.

Can you try this?
>>> dateStr = "Wed Mar 15 12:50:52 GMT+05:30 2017"
>>> from dateutil.parser import parse
>>> parse(dateStr)
datetime.datetime(2017, 3, 15, 12, 50, 52, tzinfo=tzoffset(None, -19800))

Related

Python get valid date format from date string

Is there a way to guess datetime format of a given string in python?
e.g. desired usage would be:
> guess_format('2020-02-24 07:22')
'%Y-%m-%d %H:%M'
There's dateutil project which automates datetime string conversion to valid Datetime objects:
> from dateutil.parser import parse
> parse('2020-02-24 07:22')
datetime.datetime(2020, 2, 24, 7, 22)
but can it produce valid formatting strings?
The pydateinfer package provides the possibility to infer the datetime format string of a given date string.
Example:
>>> import dateinfer
>>> dateinfer.infer(['Mon Jan 13 09:52:52 MST 2014', 'Tue Jan 21 15:30:00 EST 2014'])
'%a %b %d %H:%M:%S %Z %Y'

How do I round off date to the nearest hour in a JSON file using Python?

I want to round off time to the nearest hour in a JSON file which has a collection of tweets with different timestamps.
Example:
{
"created_at": "Tue Sep 30 01:24:46 +0000 2018",
Here I want to round off the time to 01:00:00. Could anyone help with a script which can do that in python? I tried to use this but since the timestamp changes for every tweet I'm unable to do it.
line = re.sub('^{"created_at": "Tue Sep 30 01:24:46', '^{"created_at": "Tue Sep 30 01:24:46,', line)
Any help is appreciated! Thanks!
>>> timestamp = "Tue Sep 30 01:24:46 +0000 2018"
>>> from datetime import datetime
>>> original = datetime.strptime(timestamp, '%a %b %d %H:%M:%S %z %Y')
>>> original
datetime.datetime(2018, 9, 30, 1, 24, 46, tzinfo=datetime.timezone.utc)
>>> modified = original.replace(minute=0, second=0)
>>> modified
datetime.datetime(2018, 9, 30, 1, 0, tzinfo=datetime.timezone.utc)
>>> modified.isoformat()
'2018-09-30T01:00:00+00:00'
You can a python library like datetime to parse dates in general.

Turn a String into a Python Date object

I have a String, Sent: Fri Sep 18 00:30:12 2009 that I want to turn into a Python date object.
I know there's a strptime() function that can be used like so:
>>> dt_str = '9/24/2010 5:03:29 PM'
>>> dt_obj = datetime.strptime(dt_str, '%m/%d/%Y %I:%M:%S %p')
>>> dt_obj
datetime.datetime(2010, 9, 24, 17, 3, 29)
Can anybody think of an easier way to accomplish this than going through a bunch of conditionals to parse out if Sep, month = 9?
To parse rfc 822-like date-time string, you could use email stdlib package:
>>> from email.utils import parsedate_to_datetime
>>> parsedate_to_datetime('Fri Sep 18 00:30:12 2009')
datetime.datetime(2009, 9, 18, 0, 30, 12)
This is Python 3 code, see Python 2.6+ compatible code.
You could also provide the explicit format string:
>>> from datetime import datetime
>>> datetime.strptime('Fri Sep 18 00:30:12 2009', '%a %b %d %H:%M:%S %Y')
datetime.datetime(2009, 9, 18, 0, 30, 12)
See the table with the format codes.
Use the python-dateutil library!
First: pip install python-dateutil into your virtual-env if you have one then you can run the following code:
from dateutil import parser
s = u'Sent: Fri Sep 18 00:30:12 2009'
date = parser.parse(s.split(':', 1)[-1])

Python Error Handling concerning datetime and time

I have this variable called pubdate which is derived from rss feeds. Most of the time it's a time tuple which is what I want it to be, so there are no errors.
Sometimes it's a unicode string, that's where it gets annoying.
So far, I have this following code concerning pubdate when it is a unicode string:
if isinstance(pubdate, unicode):
try:
pubdate = time.mktime(datetime.strptime(pubdate, '%d/%m/%Y %H:%M:%S').timetuple()) # turn the string into a unix timestamp
except ValueError:
pubdate = re.sub(r'\w+,\s*', '', pubdate) # removes day words from string, i.e 'Mon', 'Tue', etc.
pubdate = time.mktime(datetime.strptime(pubdate, '%d %b %Y %H:%M:%S').timetuple()) # turn the string into a unix timestamp
But my problem is if the unicode string pubdate is in a different format from the one in the except ValueError clause it will raise another ValueError, what's the pythonic way to deal with multiple ValueError cases?
As you are parsing date string from a Rss. Maybe you need some guess when parsing the date string. I recommend you to use dateutil instead of the datetime module.
dateutil.parser offers a generic date/time string parser which is able to parse most known formats to represent a date and/or time.
The prototype of this function is: parse(timestr)(you don't have to specify the format yourself).
DEMO
>>> parse("2003-09-25T10:49:41")
datetime.datetime(2003, 9, 25, 10, 49, 41)
>>> parse("2003-09-25T10:49")
datetime.datetime(2003, 9, 25, 10, 49)
>>> parse("2003-09-25T10")
datetime.datetime(2003, 9, 25, 10, 0)
>>> parse("2003-09-25")
datetime.datetime(2003, 9, 25, 0, 0)
>>> parse("Sep 03", default=DEFAULT)
datetime.datetime(2003, 9, 3, 0, 0)
Fuzzy parsing:
>>> s = "Today is 25 of September of 2003, exactly " \
... "at 10:49:41 with timezone -03:00."
>>> parse(s, fuzzy=True)
datetime.datetime(2003, 9, 25, 10, 49, 41,
tzinfo=tzoffset(None, -10800))
You could take the following approach:
from datetime import datetime
import time
pub_dates = ['2/5/2013 12:23:34', 'Monday 2 Jan 2013 12:23:34', 'mon 2 Jan 2013 12:23:34', '10/14/2015 11:11', '10 2015']
for pub_date in pub_dates:
pubdate = 0 # value if all conversion attempts fail
for format in ['%d/%m/%Y %H:%M:%S', '%d %b %Y %H:%M:%S', '%a %d %b %Y %H:%M:%S', '%A %d %b %Y %H:%M:%S', '%m/%d/%Y %H:%M']:
try:
pubdate = time.mktime(datetime.strptime(pub_date, format).timetuple()) # turn the string into a unix timestamp
break
except ValueError as e:
pass
print '{:<12} {}'.format(pubdate, pub_date)
Giving output as:
1367493814.0 2/5/2013 12:23:34
1357129414.0 Monday 2 Jan 2013 12:23:34
1357129414.0 mon 2 Jan 2013 12:23:34
1444817460.0 10/14/2015 11:11
0 10 2015

How to convert date like "Apr 15 2014 16:21:16 UTC" to UTC time using python

I have dates in the following format that are used to name zip files:
Apr 15 2014 16:21:16 UTC
I would like to convert that to UTC numbers using Python. Does python recognize the 3-character month?
Use:
import datetime
datetime.datetime.strptime(yourstring, '%b %d %Y %H:%M:%S UTC')
%b is the abbreviated month name. By default, Python uses the C (English) locale, regardless of environment variables used.
Demo:
>>> import datetime
>>> yourstring = 'Apr 15 2014 16:21:16 UTC'
>>> datetime.datetime.strptime(yourstring, '%b %d %Y %H:%M:%S UTC')
datetime.datetime(2014, 4, 15, 16, 21, 16)
The value is timezone neutral, which for UTC timestamps is fine, provided you don't mix local objects into the mix (e.g. stick to datetime.datetime.utcnow() and similar methods).
An easier way is to use dateutil:
>>> from dateutil import parser
>>> parser.parse("Apr 15 2014 16:21:16 UTC")
datetime.datetime(2014, 4, 15, 16, 21, 16, tzinfo=tzutc())
Timezone is handled, and it supports other common datetime formats as well.

Categories

Resources