0

I have a large set of Tweets and I need to retrieve tweets from one week period, one day period etc. So, I want to specify a start and end date and retrieve all the data that matches the query. Now, I have this code:

pac=timezone('US/Pacific')
utc=timezone('UTC')
start_created_at = datetime.strptime(tweet['created_at'], '%a %b %d %H:%M:%S +0000 %Y')
start_utc_created_at = utc.localize(start_created_at)
start_pac_created=start_utc_created_at.astimezone(pac)

which gives me an output like this

2014-01-11 10:51:11-08:00

I am still trying to learn how to manipulate date/time so I would appreciate any hints on how to perform queries and select tweets from a specific time period that does not require manually inputing the whole string but instead of that, something like 01/01/2014-01/10/2014 or something easily managable.

Anastasia
  • 864
  • 5
  • 13
  • 26
  • If you're asking how to query your database, a bit of info on the database and table schema might help – mhlester Jan 15 '14 at 06:03
  • sorry! edited! I don't want to query the database at this moment, just wanted to learn the basics. Sorry about that! – Anastasia Jan 15 '14 at 06:32
  • Perfect. That's what I assumed, but I wanted to make sure. I've updated my answer below to reflect that – mhlester Jan 15 '14 at 06:38

1 Answers1

0

You're already converting it to a datetime object, which is 100% correct.

We need to compare that against two other datetime objects, representing the start and end of our date range. Create the start timestamp by passing year, month, day into the datetime class:

start_timestamp = datetime.datetime(2014, 1, 14)

Now create the end timestamp by adding a timedelta object equal to one week, (7 days)

end_timestamp = start_timestamp + datetime.timedelta(7)

Last, ensure the tweet timestamp is between start and end:

if start_timestamp < start_created_at < end_timestamp:
    print 'yay!'
mhlester
  • 22,781
  • 10
  • 52
  • 75
  • I am getting an error: TypeError: can't compare offset-naive and offset-aware datetimes – Anastasia Jan 15 '14 at 06:49
  • Check out this thread: http://stackoverflow.com/a/796019/3130539. You can either add timezone info, or remove timezone info to make the "naivete" match – mhlester Jan 15 '14 at 06:57