Yoz Grahame's Unresolvable Discrepancy

I came here to apologise and eat biscuits, and I'm all out of biscuits

Daylight Saving and MovableType

Posted: March 28th, 2003 | 7 Comments »

This weekend, Europe switches to Daylight Saving Time, and all the clocks move forward an hour. Phil wondered if one should alter the timezone setting in one’s MovableType blog accordingly.

The short answer is that you probably don’t have to. That is, as long as your MT server is in the same country as you. And you’re not in Australia. Or Indiana.

Beyond that, unfortunately, things get a little complicated. Let’s briefly skim over this particularly-hideous corner of the arcana that is computational chronometry…

Doing anything with dates and times in programming has pitfalls. Most programmers either don’t think about them or think for a few minutes before sticking their fingers in their ears and shouting LALALALA CAN’T HEAR YOU. The most obvious case of this is/was the Y2K bug. Then there’s the slightly-lesser-known Y2038 bug. Both of these are obvious, stupid problems – very easy to explain and thoroughly spankable. If you want an example of code that’s built to last, Mark Crispin‘s extensive and fascinating research into how calendars work for the UW IMAP server deserves some kind of award, if only for the paragraph

There is code in c-client to support the modified Gregorian
calendar, although it is currently disabled. Sometime in the next
2000 years, someone will need to enable this code so that c-client is
Y4K compiliant. Then, 18,000 years from now, someone will have to
tear into c-client’s code to fix the Y20K bug.

… by which time, hopefully, UW IMAP will have finished opening my INBOX.

Thing is, that isn’t even the particularly scary stuff. If you want hardcore terror, try coding anything involving timezones and Daylight Saving Time without decent libraries. You will know pain.

I didn’t fully realise the extent of this until today, when I was trying to answer Phil’s question about MovableType. My first reaction was that surely MT should just obtain the current time and date from Perl’s localtime(), which will usually give you the right current time for the server, taking DST into account. The problem is that since MT is a server application, an MT user might not be in the same timezone as the server. This is quite common: She, for example, lives in Jerusalem but her Unbroken Glass is hosted by us, a.k.a. J-Colo, on our server in London.

MT acknowledges this by allowing timezone configuration on a per-blog basis. Here’s the drop-down in question:

One of the neat things there is that it doesn’t make the usual mistaken assumption that timezones are uniform longitudinal slices that are always a whole number of hours apart. Yes, you really do get half-hour differences.

The trouble with basing timezone-handling entirely on UTC zones is that it doesn’t take Daylight Saving Time into consideration. Despite UTC+1 being described as Central European Time, during the summer Central Europe is on UTC+2. After a quick shufti through MT’s excellent module documentation, I discovered that it has a stab at handling DST:

offset_time_list($unix_ts, $blog [, $direction ])

Given $unix_ts, a timestamp in Unix epoch format (seconds
since 1970), applies the timezone offset specified in the
blog $blog (either an MT::Blog object or a numeric blog
ID). If daylight saving time is in effect in the local
time zone (determined using the return value from local-
time()), the offset is automatically adjusted.

Returns the return value of gmtime() given the adjusted
Unix timestamp.

In other words, MT checks whether it’s DST on the server and makes that adjustment to the user’s timezone. With all due respect to Ben Trott (and much respect is due) this is wrong for these reasons:

  1. Firstly, there’s a usability issue: Suppose a reasonably savvy user is creating a blog during DST. Because the user isn’t told that MT does DST offsetting, they may make the mental calculation and choose an offset timezone in the dropdown, intending to set it back manually when DST ends.
  2. If you’re in the other hemisphere to the server, then MT will spend most of the year giving you the wrong time, since the server will be on DST when you’re not and vice-versa. (I can’t tell from the source whether the optional $direction would fix this, but given that it doesn’t seem to be used anywhere in MT, it’s irrelevant)
  3. Even if you’re in the same hemisphere, your server’s location may still start DST on a different date to you, providing a couple of weeks out of sync.

It’s at this point that the brainhammers move in, because if you’re going to do a decent job of calculating DST, you need to know where you are, and I mean really know where you are. While all of Europe starts and ends DST at the same time, other countries vary wildly from each other, and some aren’t even able to keep it consistent internally. Australia is a prime example: each state sets its own DST dates. Israel decides its DST start and end dates every year to ensure they don’t clash with the High Holy Days. However, that doesn’t include the Occupied Territories because the Palestinian Authority, clearly sick of all the mucking about, moved to solid dates at the first chance it had.

(Also of note is that, this year, Europe moves to DST two days before Iraq does, which is five days before the US does, guaranteeing a week of fascinating confusion for international cooperation and media efforts)

If you’re interested in DST and its history, easily the best site I’ve found is this one at WebExhibits, which not only has plenty on the history and reasoning behind DST but also a useful reference page of what different countries do, when and why. (Plus an explanation of Indiana’s weirdnesses and the fact that after the US extended DST by three weeks they saved 300,000 barrels of oil each year).

But back to the central problem, which is: how the hell do you code for this mess? It turns out that help is at hand; what’s more, it’s at hand on every modern *NIX OS in the form of the tz database (also known as Olson or zoneinfo). It features a large set of timezones divided according to continent and subarea, such as “Europe/London”. Originally created by Arthur Olson in 1994, this is now maintained as a group effort, and the GNU C library (glibc) provides date and time functions that use it, such as the aforementioned localtime().

Date and time manipulation in Perl has, until now, been a confusing (if colourful) mess – see this perl.com summary of the many modules available. The author of that piece, Dave Rolsky, is working on a new set of modules, intended to become authoritative. He’s already released initial versions of DateTime::TimeZone which features the entire tz database converted to Perl modules and weighs in at a scary 400k gzipped. (Hey, that’s big for a Perl module)

DateTime::TimeZone is probably already good enough to be able to be dropped into the next version of MT, which would mostly solve the problem. I say “mostly” because it probably won’t be able to handle the Israeli government’s indecisiveness, and so poor She will still have to deal with wrong timings on her entries. (Not that one hour’s difference is that big a deal, but still.) Personally, I’d like to see DT::TZ as the default, but with the option to turn it off and just do like Phil suggested: change the clock yourself.


7 Comments on “Daylight Saving and MovableType”

  1. 1 Phil said at 11:32 am on March 28th, 2003:

    Good point. On some weblogs I’d prefer to keep all times as UTC, as it means readers don’t need to know what country I’m in. But if MT always adjusts things automatically there’s no way of me doing that reliably.

  2. 2 Daniel said at 12:23 pm on March 28th, 2003:

    “MT acknowledges this by allowing timezone configuration on a per-blog basis. Here’s the drop-down in question:
    [DROP-DOWN]
    One of the neat things there is that it doesn’t make the usual mistaken assumption that timezones are uniform longitudinal slices that are always a whole number of hours apart. Yes, you really do get half-hour differences.”
    They seem though to have forgotten about Nepal (UTC + 5h45m) – don’t ask me why …

  3. 3 Jim said at 2:59 pm on March 28th, 2003:

    I’m surprised more people don’t use the technique we use on h2g2 (Tim’s idea, IIRC) and show all the recent dates as relative (posted 4 hours ago). Saves all that tedious mucking about in UTC.

  4. 4 She said at 10:05 pm on March 29th, 2003:

    Yes, my entries are definitely two hours behind, so I have to change the clock manually before I post.
    Which is rather annoying, actually.
    (Didn’t we already say that you should have an horizontal rule or something in between your comments? *Where* is it?)

  5. 5 Tim said at 9:14 pm on March 31st, 2003:

    Jim: well, in a way it was my idea, but I got the idea while looking at a Finder window on a Mac – MacOS shows file dates as relative (Today, Yesterday, etc) which is dead handy.
    She: damn straight!
    Yoz: Your email field doesn’t accept ‘armoured’ email addresses – sort it out!

  6. 6 Jim said at 12:03 pm on April 1st, 2003:

    Sorry Yoz – for some reason I remembered that as Tim’s idea.

  7. 7 stepan said at 4:22 pm on May 9th, 2003:

    MovableType stores timestamps in local time (adjusted by the user specified TZ offset and the server’s DST offset, if in effect), but it does not store this (cumulative) offset in the database. Figuring out what the current “correct DST” is, will finally always show the correct local time (from the blog author’s point of view). However this will not, by itself, solve the problem of the current GMT time of the post (which, for example, is used in RSS feeds).
    Right now, the RSS feed uses the BlogTimeZone, which is wrong half of the year. I could write a plugin that shows an entry’s TimeZone – adjusted for DST if it was in effect when the entry was posted – and have a solution of this, right?
    Not neccessarily. If I move to another place and adjust the timezone on my blog accordingly I run into another problem. When I rebuild my blog, the time offset info for all my old entries now becomes incorrect.
    A complete solution would require to store the time offset currently in effect with each (local) time stamp in the database.

Archive

The complete list of posts lives here.

yoz's bookmarks

  • How we use Redis at Bump - Bump Dev Blog 2011/07/16
    How Redis became Bump's Swiss Army Knife to solve all kinds of data-related problems
  • Heroku | The New Heroku (Part 4 of 4): Erosion-resistance & Explicit Contracts 2011/06/29
    Fascinating description of how Heroku's recent changes are aimed at killing software erosion (or what I think of as "bitrot").
  • What are the most interesting HTML/JS/DOM/CSS hacks that most web developers don't know about? - Quora 2011/06/17
    Marvellous collection of JS, CSS & HTML hacks. Did you know you can get the browser to parse a URL or escape HTML for you, with existing JS functions? (via gnat)
  • Avatars In Motion 2011/05/21
    "This blog is to show all the beauty you can find in Second Life." Gorgeous photography of great SL locations. (via Hamlet)
  • Gabe Newell on Valve | Game development | Features by Develop 2011/05/14
    Great, inspirational interview on how they hire and organise.
  • Design @ Quora (Web2.0 Expo Presentat... by Rebekah Cox - Quora 2011/05/03
    "Great design is all the work you don't ask the people who use your products to do."
  • David Kelley on Designing Curious Employees | Fast Company 2011/04/20
    "In this interview, he explains why leaders should seek understanding rather than blind obedience, why it’s better to be a coach and a taskmaster and why you can’t teach leadership with a PowerPoint presentation."
  • GIRP 2011/03/29
    Ingenious, tricky rock-climbing game which ends up being Twister for your keyboard (via infovore)
  • Goodnight Room - Skogkatt - Goodnight Moon - Margaret Wise Brown [Archive of Our Own] 2011/03/15
    "Goodnight Moon" fanfic, in which the room is actually travelling through space, the old lady is actually a stuck hologram, and this is wonderfully creepy yet still heartwarming.
  • Travel Hacks: What are the best travel hacks? - Quora 2011/01/24
    One of those super-compilation questions that shows off Quora at its best. Most tips are for heavy travellers, but some good ones for backpackers and adventurers too.

yoz on twitter

    follow me on Twitter

    Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org

    Content licensed under the Creative Commons (Attribution - Share Alike) | Theme based on Clean Room by Columbia, MO Web Design