Quantcast
Channel: Sitecore 8 – JocksToTheCore
Viewing all articles
Browse latest Browse all 24

Off By One Hour

$
0
0
Sitecore 8 added timezone support to all dates. Make sure you read this section of the documentation if you are not familiar.

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:

date

A razor view that does an equivalent of:

@Html.Sitecore().Field("Effective Date")

Will show:

date 2

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:

gmt

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.

The post Off By One Hour appeared first on JocksToTheCore.


Viewing all articles
Browse latest Browse all 24

Trending Articles