Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format pubDate with Python

I am newbie in Python and i m trying to do the following.

In PHP if we want to convert the date we use something like this:

$item_date  = date("Y-m-j G:i:s", strtotime($RSSitem->pubDate));

Now, i m trying to do the same using Python, but i cant understand the exact method for doing it.

Can anyone help me by writing this line to python?

like image 462
zuperakos Avatar asked Sep 04 '12 19:09

zuperakos


2 Answers

According to RSS specification pubDate must follow RFC822:

mytime.strftime("%a, %d %b %Y %H:%M:%S %z")
like image 162
Maksym Polshcha Avatar answered Nov 17 '22 08:11

Maksym Polshcha


There are two parts:

  • convert pubDate from string to a datetime/time object. If format for pubDate is fixed you could use datetime.strptime() function otherwise you could use dateutil.parser.parse() or feeparser._parse_date() (the later might be easier to install).
  • convert datetime/time object to string using .strftime() method/time.strftime() function.

See strftime() and strptime() behavior.

like image 5
jfs Avatar answered Nov 17 '22 08:11

jfs