Monday, August 9, 2010

What's New of SmartTools for SharePoint Time Display Bug and Fix

If you have not heard of SmartTools for SharePoint, then you need to check it out. It has several very useful SharePoint 2007 extensions to "make your life as a SharePoint user, developer or administrator a little bit easier!", to quote from the site.

However, the WhatsNew web part displays the modified date and time coloumns incorrectly, specifically, it converts the time from SharePoint to local time one time more than necessary. For example, it shows "6:56 AM" for a document last modified at "11:56 AM". The SharePoint regioinal setting is US Central Time. You can see the difference is 5 housrs, which is the difference between US Central Daylight Time and GMT.

The fix is very easy. In the source code which you can download from the site, comment line 406 and 411, and uncomment line 407 and 410. See correct code below.

                    if (modifiedDateTime.Date == DateTime.Today)
                        row["ModifiedDate"] = "Today";
                    else
                    {
                        //row["ModifiedDate"] = SPUtility.FormatDate(SPContext.Current.Web, modifiedDateTime.Date, SPDateFormat.DateOnly);
                        row["ModifiedDate"] = modifiedDateTime.Date.ToShortDateString();
                    }

                    row["ModifiedTime"] = modifiedDateTime.ToShortTimeString();
                    //row["ModifiedTime"] = SPUtility.FormatDate(SPContext.Current.Web, modifiedDateTime, SPDateFormat.TimeOnly) ;


SPUtility.FormatDate assumes the given time is UTC time and converts to local time based on regional settings (See what is good to know about SPUtility.FormatDate). When the code gets the modifiedDateTime, it is local time already. Another conversion to local time therefore is not necessary.

In SharePoint, Datetime value is stored as UTC time. When setting datetime, SharePoint converts the input to UTC and stores the UTC value; when displaying datetime, SharePoint converts the UTC time to local time.

According to this blog sharepoint web services and utc time fun and games from Andy Burns, there is some kind of problem with web services.