-1
import praw,time
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

username=""
password=""
r = praw.Reddit(user_agent='')
r.login(username,password,disable_warning=True)
posts=r.search('china disaster', subreddit=None, sort=None, syntax=None, period=None,limit=7)
title=[];created=[]
for index,post in enumerate(posts): 
    date=time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(post.created))  
    title.append(post.title);created.append(post.created)
    print date,title[index]
    break #added so it prints one post as a example

Error: I get incorrect times.

<time title="Fri Jan 23 01:22:20 2015 UTC" datetime="2015-01-22T17:22:20-08:00" class="">5 months ago</time>

I don't understand the issue, I think I making a mistake in time-zone conversion. But reddit posts mention UTC, thus I don't get the error.

Abhishek Bhatia
  • 9,404
  • 26
  • 87
  • 142
  • the output does not correspond to the code. What value do you expect to get? – jfs Jun 26 '15 at 13:34
  • Added `break` statement so prints only one post as an example. Sorry for not explaining correctly before. If you mentioning about the post content, if won't as the order of top posts keep changing on reddit. – Abhishek Bhatia Jun 26 '15 at 13:40
  • `print date` where `date` has `"%Y-%m-%d.."` format won't produce ` – jfs Jun 26 '15 at 13:42
  • Just remove all the code unrelated to the epoch time to string conversion i.e., your input should be a single number `post.created`? ("seconds since epoch") and specify what output *exactly* do you expect and what you get instead. – jfs Jun 26 '15 at 13:45

1 Answers1

1

I didn't get your exact problem about how is the "Incorrect". There are two attributes about the created time "created" and "created_utc". Maybe you want to try the second one instead.

Glaive
  • 48
  • 3
  • Great! Thanks so much. Really dumb of me to miss that. – Abhishek Bhatia Jun 26 '15 at 13:44
  • `gmtime()` accepts "seconds since the Epoch" that should be the same number whatever timezone is i.e., if `created` is a POSIX timestamp then `created_utc` should be the same number and therefore the result won't change. – jfs Jun 26 '15 at 13:51
  • @J.F. Sebastian Amazing! Thanks for the explantation, makes sense now. – Abhishek Bhatia Jun 26 '15 at 14:24