Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing datetime in Python..?

I have a system (developed in Python) that accepts datetime as string in VARIOUS formats and i have to parse them..Currently datetime string formats are :

Fri Sep 25 18:09:49 -0500 2009  2008-06-29T00:42:18.000Z  2011-07-16T21:46:39Z  1294989360 

Now i want a generic parser that can convert any of these datetime formats in appropriate datetime object...

Otherwise, i have to go with parsing them individually. So please also provide method for parsing them individually (if there is no generic parser)..!!

like image 803
Ramandeep Singh Avatar asked Mar 01 '12 12:03

Ramandeep Singh


People also ask

How do you parse a date format 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 string to time in Python?

We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.

What is parsing a date?

parse() The Date. parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31). Only the ISO 8601 format ( YYYY-MM-DDTHH:mm:ss.

How do you parse a string to a date 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.


1 Answers

As @TimPietzcker suggested, the dateutil package is the way to go, it handles the first 3 formats correctly and automatically:

>>> from dateutil.parser import parse >>> parse("Fri Sep 25 18:09:49 -0500 2009") datetime.datetime(2009, 9, 25, 18, 9, 49, tzinfo=tzoffset(None, -18000)) >>> parse("2008-06-29T00:42:18.000Z") datetime.datetime(2008, 6, 29, 0, 42, 18, tzinfo=tzutc()) >>> parse("2011-07-16T21:46:39Z") datetime.datetime(2011, 7, 16, 21, 46, 39, tzinfo=tzutc()) 

The unixtime format it seems to hiccough on, but luckily the standard datetime.datetime is up for the task:

>>> from datetime import datetime >>> datetime.utcfromtimestamp(float("1294989360")) datetime.datetime(2011, 1, 14, 7, 16) 

It is rather easy to make a function out of this that handles all 4 formats:

from dateutil.parser import parse from datetime import datetime  def parse_time(s):     try:         ret = parse(s)     except ValueError:         ret = datetime.utcfromtimestamp(s)     return ret 
like image 165
Kimvais Avatar answered Oct 01 '22 23:10

Kimvais