Correctly Parsing Graph API Event Times

One of things that really really annoys me about the Facebook Graph API is the way that they handle Events times. If you read the docs you’ll see the description:

Start Time: A JSON string containing an ISO-8601 formatted date/time or a UNIX timestamp; if it contains a time zone (not recommended), it will be converted to Pacific time before being stored and displayed

Nice and cryptic, and an absolute pain to work with if you have a device which isn’t in the Pacific time zone. The time that is returned also doesn’t have a sense of Time Zone, when you create an Event Facebook takes the date and time, and then converts that exact time into Pacific time. For example, if you create a Event starting at 9pm on 1st July 2011 GMT, Facebook will store 9pm 1st July 2011 Pacific Time. They do not do any conversion for time zones.

Therefore if you have a global app, you’ll need to convert it to the user’s TimeZone:

Receiving Time’s in Epoch Format

While this isn’t necessary, it does make parsing quicker and easier. When requesting objects through the Graph, all times will be returned in ISO-8601 format (e.g. 2011-07-02T05:00:00+0000) which is a string, and therefore slow to parse. Facebook recently added a feature where date/times will be returned in Unix timestamp format, simply add the date_format=U query parameter to end of your Graph call. For example:

https://graph.facebook.com/me/events?date_format=U&access_token=.....

This will return time/dates in Unix timestamp format (such as 1309572000).

Converting Time to User’s Time Zone

Now that we have the time in a format we can actually use, it’s time to convert it to the User’s current TimeZone. There are a couple of ways to do this, but the simplest is this:

public static long formatTimeForEvent(long pacificTime) {
	return (pacificTime + TimeZone.getTimeZone("America/Los_Angeles").getOffset(pacificTime))
		- TimeZone.getDefault().getOffset(pacificTime);
}

What this does is, convert the Pacific Time (America/Los_Angeles) time to GMT, and then into the User’s Time Zone via getDefault().

Easy when you know how eh? This took me about a week to work out.