.NET and Open Source: better together

RTur.net

  • Join Us on Facebook!
  • Follow Us on Twitter!
  • LinkedIn
  • Subcribe to Our RSS Feed

Crazy Fun Times

In application where you publish things, published time seems simple. I write post at 12 pm, hit “publish” and all I want is my post saved with that 12 pm time-stamp. How hard it can be, right? Wrong, which I found the hard way trying to fix issues with publish time in relatively simple application. So I made this cheat sheet, or rather note, to help straighten things up.  

First of all, if you deal with web application that can run on the server far far away, you have to bring in UTC times. Seems like adding complexity, but this is necessity. To understand how and why, here is a picture. Say I'm publishing post from cafe somewhere at the West Coast, and it is 12 pm sharp on the clock. But my server is running at Chicago where 2 pm right now. And for the next week I'm planning visit New York and will be posting from there, too. UTC helps here because, when I hit "publish" and save time as UTC - it is the same for any location.

And this dictates 2 simple rules you should follow to keep everything on schedule.

  • Always save date/time to the disk in universal UTC format. So in this example, time in the database/file storage should be saved as 20:00 pm
  • When reading time from storage, apply user time zone to convert to the local time, so wherever you are, your post time in UI will correspond time on the clock
And that is pretty much it, don't do it any more complex, or it quickly gets out of hands. Just allow user select current time zone and drop to generic UTC time for anonymous.