Time zone trouble

Aug 15, 2007

I get headaches just thinking about adjusting for different time zones. Actually, if time zones we’re pizzas I’d never order one. That’s how much trouble they give me. The concept behind time zones is very simple and makes perfect sense, but there are some inconsistencies that drive me nuts.

Confusing

First of all, time zones do not follow geography but are politically decided in the various countries. England has time zone GMT while France has GMT + 1. If you look at a map you’ll see that England is just north of France which would indicate that they used the same time zone, but of course they don’t. You’ll also notice that Spain is actually located west of the Greenwich median which would indicate that they used GMT just like England. Again, that is not the case. I could go on about this, but the fact is that it is really confusing.

Then there is the whole issue about daylight savings time (DST), which some countries have and some don’t. What it means is that the time zone offset between a country using DST and one that doesn’t varies from summer to winter. It just makes me even more confused.

Server time vs. local time

When you are developing websites that must take the users time zones into account then you can get into big trouble. If you only have one installation of the website, then you can control the server’s time zone and let the users pick theirs and everything is good. But if you build a web application that people can upload to their hosting provider of choice, then you cannot control the time zone anymore. You have no idea what time zone the server is running and if you do, you don’t know if it is the correct one and if it’s using DST.

So, what good does it do to let the users pick their time zone if you don’t even know how to convert it to the server’s? No good at all.

WordPress realized this and did something else which is quite brilliant. They knew that letting the users pick their time zone didn’t help to determine their local time against the server’s, so instead they made the user pick the time offset between the server and them selves. If the server time is 4:15pm and your local time is 8:15pm, then you just pick the offset +4.

It’s brilliant and at the same time sad that we in 2007 can’t figure this out in a consistent way without running into trouble. Another possibility is that I’m just slow and haven’ figured out how to do it properly. I’ll let you decide.

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (20) -

Jeff
Jeff United States
8/15/2007 8:49:44 PM #

Then throw in DST - which is not practiced here in Arizona Frown

Haacked
Haacked United States
8/15/2007 8:54:44 PM #

In Subtext we store both the server timezone and the local timezone. We then use a custom TimeZoneHelper class (borrowed from DasBlog) to convert back and forth.

Unfortunately, with .NET <= 2.0, there's no way to convert from one time zone to an arbitrary timezone. You can convert from UTC to the server timezone and from the server time to UTC, but you can't convert to PST if your server is in EST. That's why we used the DasBlog class.

Mads Kristensen
Mads Kristensen Denmark
8/15/2007 9:15:07 PM #

@Phil, wouldn't the WordPress solution be much simpler and still solve the same problem just as well?

Ole H&#248;jriis Kristensen
Ole Højriis Kristensen
8/15/2007 9:21:37 PM #

Timezones and DST will be the first thing I discontinue once I gain world domination.
Who says 18:00 has to be evening everywhere?

Fredrik
Fredrik Norway
8/16/2007 5:21:53 AM #

I agree, this is a real hassle to deal with. However, in .NET 3.5 they've tried to make this easier to cope with by introducing the TimeZoneInfo and DateTimeOffset classes. Have a look at blogs.msdn.com/.../...-datetime-anthony-moore.aspx Smile

Anders
Anders Denmark
8/16/2007 6:42:13 AM #

Perhaps this new article on code project could offer some pointers:

www.codeproject.com/.../Universal_Time__UTC_.asp

Matt Ellis
Matt Ellis
8/16/2007 7:13:24 AM #

Am I missing something? Can't you just use UTC on the server? Then you're always right, right?

Phil, why does Subtext use server local time?

Surely the only thing to think about with UTC is rendering it in a friendly manner to the user (author or reader). That can be set by a server side variable (optimising to the single author) or converted client side with a bit of javascript (optimising to the multiple readers).

Can anyone think of a reason why I wouldn't want to use UTC?

Mads Kristensen
Mads Kristensen Denmark
8/16/2007 7:38:59 AM #

@Matt. Because you cannot control the server's time zone, you can't be sure that the UTC is the correct one.

Florian
Florian Germany
8/16/2007 8:15:26 AM #

I think mads approach is the simplest way to handle the problem.
All you need is the difference between the and server time, don't you?.

Florian
Florian Germany
8/16/2007 9:58:15 AM #

oopps I mean "...between the client and server time"..

Carl
Carl United Kingdom
8/16/2007 10:08:57 AM #

@Florian - I don't think that it is that simple, is it? If, when I set the timezone, the difference is 4hrs - what about when I come back later and DST has kicked in (Server side or client side)? The time difference could now be 3 or 5 hrs, how does the server know that?

Carl

Sean
Sean Canada
8/16/2007 10:12:34 AM #

@Mads: Doesn't Date.UTCNow (or GETUTCDATE in T-SQL)take care of that for you?

In my ASP.NET apps I exclusively use UTC on the server side and then capture the user's offset via JavaScript.  From there it's a pretty easy conversion to always show dates/times in their local timezone.  

There really isn't a need to ask them to pick a timezone, but since the calculated one will change whenever they login from a different timezone I also give them the option in their profile to "force" a specific timezone.

Eric D. Burdo
Eric D. Burdo United States
8/16/2007 10:52:45 AM #

DST is a whole ball of wax in itself.  In the United States, most states use DST, but some (such as Arizona already mentioned) do not.  And some states only use DST for certain areas!  It gets really confusing then.

Josh Stodola
Josh Stodola United States
8/16/2007 12:26:33 PM #

The culprit is DST!!  Who is the moron that came up with this anyways (i just looked on Wikipedia and his name is Willy Willet)?  What's so great about daylight that we have to start saving it twice a year?!  I guess I am a night owl...

Carl
Carl United Kingdom
8/16/2007 2:49:09 PM #

lol, they'll stop saving daylight eventually anyway - as the ozone hole gets bigger and we end up with more daylight than we know what to do with...

Clarence
Clarence United States
8/21/2007 8:08:14 PM #

So when will this be implemented in the BlogEngine.net?

- Clarence

Mads Kristensen
Mads Kristensen Denmark
8/21/2007 8:13:31 PM #

This week

Claus Thomsen
Claus Thomsen Denmark
8/23/2007 11:01:42 AM #

It seems that this problem has been around for a long time and that is why we have timeservers to overcome sync issues on time.

Get Utc time from server and compare it to Utc retrieved from a SNTP server then you know if servers time is wrong and is able to adjust for it in code if needed.

Or use SNTP server to get UTC time instead of System.DateTime....

see www.codeproject.com/cs/internet/ntpclient.asp




ClubLantic
ClubLantic Netherlands
9/2/2007 8:30:35 PM #

Although the above trackback was written with a wink at the Swatch Internet Time buzz a few years ago, I'm actually starting to believe such a time format might work well for server-to-server time conversion. What do you think?

Mike van Zandwijk
Mike van Zandwijk Netherlands
3/16/2008 9:25:54 AM #

Hi Mads,

Here's where my earlier suggestion (1) about Swatch Universal Internet Time (2) belonged.
Never intended as time notation to the human eye, but as alternative for
"server time offset" in BlogEngine.NET (stored in the .xml).

E.g. <pubDate>2008-03-16 @513</pubDate>

Where @123, @513, @etc is the same 'time' all over the world, just different context.
The basic advantage is there's no time conversion anymore, just server interpretation (3)
Based on that, you can show whatever meaning to your reader, e.g.

- Today around 10 am (your local time)
- Six hours ago (or weeks, days, minutes, etc)
- Almost 22:00 (or 10pm or "ten in the evening")

Perhaps I should write a clear, structured article about it to drive my point better.

References:
(1) blog.madskristensen.dk/.../...know-you-needed.aspx
(2) http://en.wikipedia.org/wiki/Swatch_Internet_Time
(3) http://www.csgnetwork.com/csgbmtcvt.html

Pingbacks and trackbacks (1)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.