2

I'm trying to list out tweets with their time-stamps. I have this...

#!/usr/bin/python
import twitter
api = twitter.Api()
statuses = api.GetUserTimeline('ttytter')
for s in statuses:
  print s.created_at + " " + s.text

Which prints out...

Sat Oct 20 04:56:47 +0000 2012 @uriel1998 W/r/t DMs, they appear just fine in 2.0.4 and 2.1 beta here, near as I can tell.

Which is pretty much what I'm after, but for the time, which seems to be in the wrong timezone. https://twitter.com/ttytter/status/259518502069760000

Is there a way I can change this within the python-twitter library? I was looking at GetTimeZone() and SetTimeZone(), but I haven't been able to figure out how they work.

Also looking at how to shift a datetime object by 12 hours in python but not sure if I need to go there.

Thanks for any help!

Community
  • 1
  • 1
  • 1
    do you want to convert utc time to tweet's author timezone or your local timezone? – jfs Oct 28 '12 at 18:36
  • In this case, they are the same, and I'm ok with setting it manually. Probably, it would be a better script if it used the author's timezone, though. – Sean Camden Oct 28 '12 at 22:24
  • If it is not part of the question then you could post the code as an answer to allow independent voting, commenting, small fixes that are not appropriate if it is a part of the question e.g., `st_tz` stands for Stockholm timezone in my code, it could replaced by `la_tz`, `user_tz`, `local_tz` whatever more appropriate in your case (same for `st_dt`) – jfs Oct 28 '12 at 23:23
  • Ok, put it as its own answer. Thanks for pointing out the variable convention you're using. I will edit my code accordingly. – Sean Camden Oct 29 '12 at 02:23

2 Answers2

2

python-twitter returns the status timestamps as a string and as the number of seconds since the epoch. The latter is the simplest to convert to a timezone-aware datetime instance (see this answer).

Unfortunately the user's time_zone attribute is not in the standard tz database format used by pytz, so it is necessary to use the utc_offset user attribute instead (we still use the time_zone attribute to name the tzinfo created with with the UTC offset). The python-dateutil package provides a convenience type tzoffset that allows the creation of tzinfo instances from UTC offsets, which we can then use to convert the datetime from UTC to the local time zone:

import pytz
import twitter

from datetime import  datetime
from dateutil.tz import tzoffset

USERNAME = 'ttytter'

api = twitter.Api()

# get a 'tzinfo' instance with the UTC offset for the user's local time
user = api.GetUser(USERNAME)
localtime_tz = tzoffset(user.time_zone, user.utc_offset)

statuses = api.GetUserTimeline(USERNAME)
for s in statuses[:1]:
    # get UTC timestamp from seconds since epoch
    utc_dt = datetime.utcfromtimestamp(s.created_at_in_seconds).replace(tzinfo=pytz.utc)
    print('utc: {}'.format(utc_dt))
    # convert to local time in the user's timezone
    localtime_dt = utc_dt.astimezone(localtime_tz)
    print('localtime [{}]: {}'.format(localtime_dt.tzname(), localtime_dt))

which gives the output for the first status:

utc: 2012-10-20 04:56:47+00:00
localtime [Pacific Time (US & Canada)]: 2012-10-19 20:56:47-08:00
Community
  • 1
  • 1
Pedro Romano
  • 10,973
  • 4
  • 46
  • 50
  • you don't need `dateutil`. python-twitter provides `created_at_in_seconds` that you could convert to a time in given timezone using [the pytz example I've linked above](http://stackoverflow.com/a/12692910/4279). – jfs Oct 28 '12 at 19:15
  • You're absolutely right @J.F.Sebastian. Thanks. Revising the answer. – Pedro Romano Oct 28 '12 at 19:22
  • How does dateutil.tzoffset-based code account for DST transitions? – jfs Oct 28 '12 at 20:30
  • @J.F.Sebastian: it doesn't. I just assume that Twitter calculates the correct UTC offset for the local timezone and timestamp taking into account any DST transitions. I would prefer to use just `pytz`, but how can we get a `pytz` timezone from just the UTC offset? The `time_zone` attribute is useless because it doesn't contain proper Olson timezone names. – Pedro Romano Oct 28 '12 at 21:52
  • Thanks Pedro and J.F.! By combining your answers I've got exactly what I was after. – Sean Camden Oct 28 '12 at 22:57
1

Combining suggestions from Pedro Romano and J.F. Sebastian, I have this...

import pytz
import twitter

from datetime import  datetime

USERNAME = 'ttytter'

api = twitter.Api()

user = api.GetUser(USERNAME)
pst_tz = pytz.timezone('America/Los_Angeles')

statuses = api.GetUserTimeline(USERNAME)
for s in statuses[:1]:
    # get UTC timestamp from seconds since epoch
    utc_dt = datetime.utcfromtimestamp(s.created_at_in_seconds).replace(tzinfo=pytz.utc)
    # convert to given timezone
    pst_dt = pst_tz.normalize(utc_dt.astimezone(st_tz))
    print(pst_dt.strftime('%Y-%m-%d %H:%M:%S ')) + s.text

Output: 2012-10-19 21:56:47 @uriel1998 W/r/t DMs, they appear just fine in 2.0.4 and 2.1 beta here, near as I can tell. which is the correct time zone and also accounts for DST.

Thank you!