Every Site has a Guest User with its own Time Zone

A lesson learned during Dreamforce - be sure to keep the local time zone of a Site in mind when time is of the essence.

During Dreamforce, we ran a little locally-produced signage application that displayed upcoming announcements, as well as the next set of sessions in the Force.com Zone. Except, it had a bug – it seemed to be showing the sessions for the following day. After checking cache settings, and our queries, we eventually tracked it down to the Guest User account on a Force.com Site.

If you are hitting a Force.com Site and you aren’t authenticated, you get associated with the Guest User and its security profile. This is how, for example, you can control what data may be viewed by an anonymous user. However, this user also has a time zone setting, which means that code executed by the user will use this time zone information. That was the source of our bug. Take for example the following controller:

public class thedate {
public String getSoso() {
return datetime.now().format();
}
}

and this Visualforce page that uses it:

<apex:page controller="thedate">
{!soso}
</apex:page>

I’m formatting the data in the controller, but you can imagine processing it to return sessions that start after “now”. Right now, this outputs 23-11-2009 13:05 when I hit my Site that contains this Visualforce page. That’s because the user associated with the site is in the GMT timezone. If I change his locale settings and set the Time Zone to Pacific Standard Time, and hit refresh on the page, then I get 23-11-2009 5:05.

Tricky! Bear this in mind when you build a site that uses time!

PS. Here’s how to set/determine the time zone. Select your Site under Setup | Develop | Sites. Click Public Settings, then View Users:
viewusers.png

You’ll then be presented with the user associated with the guest profile for anonymous access to your website. The Time Zone field on the user determines the time zone information.

Published
November 23, 2009
Topics:

Leave your comments...

Every Site has a Guest User with its own Time Zone