[su_note note_color=”#fafafa”]Sitecore 8 added timezone support to all dates. Make sure you read this section of the documentation if you are not familiar.[/su_note]
Intro
We were mostly careful with our dates and put our Sitecore into GMT Standard Time from the very beginning to avoid converting things back and forth:
<setting name="ServerTimeZone" set:value="GMT Standard Time"/>
Turns out – and it’s equally important, silly, and frustrating – GMT Standard Time is not Greenwich Standard Time. The former observes daylight saving, the latter does not and both are referred to as GMT. Here’s a good thread on stackoverflow.
Sitecore documentation mentions GMT Standard Time as an example for your ServerTimeZone
and we just went with it. We should have used UTC instead.
Off By One Hour
Here’s how the problem manifests itself. We have an item representing an Event. It has a date:
A razor view that does an equivalent of:
@Html.Sitecore().Field("Effective Date")
Will show:
All good, right? Both dates are in server time right now. Both – the control in content editor and the renderField
pipeline will send the date through DateUtil.ToServerTime()
.
In the database, however, the date is stored like this:
20151023T230000Z
It’s stored in UTC and right now GMT Standard Time is on daylight saving time:
Does It Matter?
It wouldn’t matter if we weren’t sending the date field to another channel via a JSON feed or if our dates weren’t at 00:00:00. Right now some users experience 10/24 and the others see 10/23 – off by one hour became off by one day.
The JSON feed was given a date using an equivalent of:
((DateField) item.Fields["Effective Date"]).DateTime
Lessons Learned
- A date field sent through
renderField
pipeline will be in server time - A date displayed by the Content Editor date control will be in server time
- The
DateTime
field value will be in UTC - Date fields in the search index are also in UTC unless you computed them differently
And our biggest oversight was the assumption about GMT Standard Time. Never. Assume. Anything.
I don’t know if it’s relevant or of any use. In the UK, GMT (from a purely technical stand point) we consider static and also have BST (British Summer Time) during what you guys call DST (which is often a week out from US DST and is GMT +1h). We do however normally speak of ‘GMT’ as our timezone all year round including when we move to BST. Time zones are an ass, since they seem to be described differently depending on culture.
I am not sure how Portugal (who are also on GMT) treat their time zones, and whether CET automatically moves for DST or is termed differently as above, I believe its always CET and DST is respected.
Nat, I will only say this:
http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result
🙂