Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse dates with -0400 timezone string in Python?

I have a date string of the form '2009/05/13 19:19:30 -0400'. It seems that previous versions of Python may have supported a %z format tag in strptime for the trailing timezone specification, but 2.6.x seems to have removed that.

What's the right way to parse this string into a datetime object?

like image 640
fields Avatar asked Jul 09 '09 02:07

fields


People also ask

How do you parse a date in Python?

Python has a built-in method to parse dates, strptime . This example takes the string “2020–01–01 14:00” and parses it to a datetime object. The documentation for strptime provides a great overview of all format-string options.

How do you convert date and time to string in Python?

To convert Python datetime to string, use the strftime() function. The strftime() method is a built-in Python method that returns the string representing date and time using date, time, or datetime object.

How do you parse a string in Python?

Parse String to List With the str.split() function to split the string on the basis of each , . The str. split() function takes a delimiter/separator as an input parameter, splits the calling string based on the delimiter, and returns a list of substrings.

Does Python datetime include timezone?

One of the modules that helps you work with date and time in Python is datetime . With the datetime module, you can get the current date and time, or the current date and time in a particular time zone.


2 Answers

You can use the parse function from dateutil:

>>> from dateutil.parser import parse >>> d = parse('2009/05/13 19:19:30 -0400') >>> d datetime.datetime(2009, 5, 13, 19, 19, 30, tzinfo=tzoffset(None, -14400)) 

This way you obtain a datetime object you can then use.

As answered, dateutil2.0 is written for Python 3.0 and does not work with Python 2.x. For Python 2.x dateutil1.5 needs to be used.

like image 63
txwikinger Avatar answered Oct 07 '22 02:10

txwikinger


%z is supported in Python 3.2+:

>>> from datetime import datetime >>> datetime.strptime('2009/05/13 19:19:30 -0400', '%Y/%m/%d %H:%M:%S %z') datetime.datetime(2009, 5, 13, 19, 19, 30,                   tzinfo=datetime.timezone(datetime.timedelta(-1, 72000))) 

On earlier versions:

from datetime import datetime  date_str = '2009/05/13 19:19:30 -0400' naive_date_str, _, offset_str = date_str.rpartition(' ') naive_dt = datetime.strptime(naive_date_str, '%Y/%m/%d %H:%M:%S') offset = int(offset_str[-4:-2])*60 + int(offset_str[-2:]) if offset_str[0] == "-":    offset = -offset dt = naive_dt.replace(tzinfo=FixedOffset(offset)) print(repr(dt)) # -> datetime.datetime(2009, 5, 13, 19, 19, 30, tzinfo=FixedOffset(-240)) print(dt) # -> 2009-05-13 19:19:30-04:00 

where FixedOffset is a class based on the code example from the docs:

from datetime import timedelta, tzinfo  class FixedOffset(tzinfo):     """Fixed offset in minutes: `time = utc_time + utc_offset`."""     def __init__(self, offset):         self.__offset = timedelta(minutes=offset)         hours, minutes = divmod(offset, 60)         #NOTE: the last part is to remind about deprecated POSIX GMT+h timezones         #  that have the opposite sign in the name;         #  the corresponding numeric value is not used e.g., no minutes         self.__name = '<%+03d%02d>%+d' % (hours, minutes, -hours)     def utcoffset(self, dt=None):         return self.__offset     def tzname(self, dt=None):         return self.__name     def dst(self, dt=None):         return timedelta(0)     def __repr__(self):         return 'FixedOffset(%d)' % (self.utcoffset().total_seconds() / 60) 
like image 34
jfs Avatar answered Oct 07 '22 01:10

jfs