Search code examples
pythonfacebookfacebook-graph-apifacebook-fqlcherrypy

How can I get the time of a facebook user?


I'm building a web application which monitors the up times of friends on facebook in CherryPy (Python Web Framework) and I was wondering how it would be possible to get the time of a facebook user at any given time.

If I need to say that "friend A was online" at a specific time, it would need to match their timezones and hence I've been left a bit stumped.

I played around with the pytz module in python, however had no luck.

At the moment, I am retrieving the "timezone" for a user via the FQL and then adding the timezone value to a database:

user_details = fbconsole.fql("""SELECT uid, timezone, name FROM user WHERE uid = me()""")

I am wondering about a few things:

  • Is it possible to get the time in which friends are online through Facebook API?
  • Is it possible to alternatively figure out the time of the client at any given time, through using the timezone offset? (if so, how? I haven't been able to do this yet)

Thank you in advance


Solution

  • I was able to solve this problem myself through the following code:

    from datetime import datetime
    from pytz import timezone
    #set user timezone (GMT +10)
    user_timezone = timezone('Etc/GMT+10')
    #convert local time to user time
    user_timezone_time = user_timezone.localize(datetime.now())
    #format in 12hr time (%I for 12 hour time and %p for AM PM clarification)
    formatted_user_timezone = user_timezone_time.strftime('%Y-%m-%d %I:%M:%S %p %Z%z')
    print formatted_user_timezone
    

    This allowed me to retrieve the timezone field from the "user" field for the Facebook API and then apply it with the code above, to find out the local time of a facebook user.

    Thanks! I hope this helps others.