Wednesday, February 29, 2012

How SharePoint Deals With Time And Time Zones

What’s Going On Here?

It’s a little weird how hard it is to find some detailed descriptions on how SharePoint handles time, and time zones. So here’s a dump on a lot of things time and time zone related to better help you troubleshoot and predict behavior.TimeWarp

How Time Is Stored

Fields like created/modified are stored will all other list item properties in the dbo.AllUserData table as tp_Modified and tp_Created. Like all list items, events are stored in this table as well.

Time Calculation With Respect to Time Zones

Time gets stored using the clock of the machine, this is not influenced by the time zone of the machine. If the clock on the server is off, the time stored is off by the amount of the clock skew. Changing the time zone of the server (OS) doesn’t affect how time is stored. This is true for most applications.

WHY?

If a server is at 6pm PST (Pacific) and you change the time zone to EST (Eastern) sure the time looks like it changes from 6pm PST to 9pm EST, but those times are equivalent. 9pm EST is just another way of writing 6pm PST. Events taking place in California are really happening at the exact same time in New York, their citizens just happen to format time differently. Time zones are just another way of expressing (formatting) the current time. To be clear. There is only one time, there are many time formats…and there is no spoon.


The Clock IS Important Though

If you change the time on the server (OS) clock you’ll change how time will get stored (for new edits/additions). In this case you’ve actually had an impact on time. Namely because going from 6pm PST to 7pm PST means someone actually lost an hour they’ll never get back (not that fake ones you tell yourself about when you travel through different time zones). By changing the clock from 6pm PST to 7pm PST, you’ve effectively moved the server through time. As a result, this will effect the times that items are recorded to be stored at.

How Time Zones Get Set

By default all your users see the time as it is formatted at the root site (SPWeb) level. These settings get set when the site collection is provisioned by the web application default time zone (Figure 1 shown below). These settings get copied down when new sub sites are provisioned under the root site. Finally, if any user doesn’t like the way dates and times are displayed, they can override how time is presented to them using their personal regional settings (Figure 2 also below). But most of the time its the root site settings time zone that users are complaining about.

Default Site (SPWeb) Time Zones

This setting dictates the formatting/time zone for all data in the site. By default these get copied down from the parent web at time of creation. You can view/change these settings (if you’re a Site Administrator) by going to:
  1. Site Actions->Site Settings
  2. Click on Regional Settings
Site (SPWeb) Regional Settings including Time Zone
Figure 1. Site (SPWeb) Regional Settings
This setting is dictating how time is displayed to all users whom haven’t expressed a preference using their personal regional settings. At the root of the site collection (root site), you can also tell all sub sites to inherit the settings of the root web, resetting them to a single time zone.
Changing the Regional Settings time zone for a web programmatically looks like the code below. Time zone IDs come from a list of known time zones.

using (SPSite siteCollection = new SPSite("http://localhost"))
{
    using(SPWeb websiteRoot = siteCollection.RootWeb)
    { 
        SPRegionalSettings regionalSettings = websiteRoot.RegionalSettings; 
        SPTimeZone timeZoneIndianaEast = regionalSettings.TimeZones[15]; 
        regionalSettings.TimeZone.ID = timeZoneIndianaEast.ID; 
        websiteRoot.Update();
    }
}

User Site Collection Time Zones

We say user site collection time zones settings because even though this is stored per user, these settings reset in across site collections. Just because you’ve set this in site collection A doesn’t have any impact on site collection B. It will follow you around across many sites (SPWeb) within a site collection though.

You can override the default site time zone by going to:

  1. Click on your username in the header and click on My Settings.Click on My Settings to access Regional Settings
  2. Click on My Regional Settings.
  3. Uncheck Always follow web settings and override the time zone with one of your preference for this site collection.
User Site Collection Time Zone settings overriding the default time zone.

Figure 2. User Site Collection Time Zone settings  
overriding the default time zone

After you click OK, time will be formatted for the time zone you dictate.
You can change this for a given user with the following code:

//Create a new regional settings based off of the current web.
SPRegionalSettings regionalSettings = new SPRegionalSettings(web, true);

//Set some settings
SPTimeZone timeZoneIndiaEast = regionalSettings.TimeZones[15];
regionalSettings.TimeZone.ID = timeZoneIndiaEast.ID;

//Assign to user and update.
user.RegionalSettings = regionalSettings;
user.Update();

Fallout

There are some interesting effects of the time zone settings. For instance, if you ship a site collection to another time zone, it will still behave as if it’s in your original time zone with no difference at all so long as the clock at the host is the correct time.

Different users can have very different time settings across different site collections. The good news is that they get the final say on how time is formatted.

Both these settings can be manipulated programmatically. When these settings are set appropriately they can help users from very different time zones collaborate on data with time formatted in a way that make sense to them. This especially applies to event/calendar items.

Hope that helps someone, regional settings can have a big impact on collaboration projects.

My Best,
Tyler

4 comments:

Albert Henry said...

In SharePoint 2010 you get the resources to turn time according to the customers time location. There are generally two types of setting to deal with this thing i.e. SPuser and SPweb.

Anonymous said...

Have you figured out a way to keep regional settings for a user synchronized across site collections in a web application so that if it changes it one site collection it will change in all others under the same web application?

Anonymous said...

Do you know which web service can I use to get the site collection time zone info?

digital signature Adobe Reader said...

Very informative post. I sometimes do presentations on SharePoint and was wondering if I could use your Print List example in my presentations and refer my audience to your website for further info.