15

I am designing a scheduling web app. I expect events to be added by users in several different timezones and locales. The challenge is correct presentation of these events.

So, as an example:
if a user is in EST timezone and is looking at a webinar event that was added by another user in PST, I want to convert the actual PST time of event into a local time for viewer. So, if an event is scheduled for 2 PM PST, then it should shown as 5 PM EST.

I also want to be careful that performance does not take a hit if there are thousands of events which may require a conversion from actual event time to the viewer's local time.

All thoughts and comments are appreciated.
TIA

1 Answer 1

49

In general, scheduling future events is a complex subject. You have to make a distinction in the context of what is going to be scheduled:

  • Does the event occur at a specific universal instant in time? If so, you should record the event time in terms of UTC.

    For example, a task that runs every 24 hours would be scheduled by UTC time and not by the local time. It might start off at some local midnight, but as daylight saving time changes take effect it could be running at 23:00 or 01:00 by the local clock.

  • However, if the event is scheduled by human beings, it is likely to be in terms of a local time, so you should record it that way.

    For example, a meeting that occurs at 08:00 Eastern Time will always occur at that local time. In the winter, that would be 13:00 UTC, and in the summer it would be at 12:00 UTC.

    So in this context, you cannot record the scheduled start time in terms of UTC. This is a very common mistake, as there is a ton of advice on the Internet that says "always store using UTC", which would be wrong in this scenario.

    Instead, you should store two values - the local time such as 08:00 and its IANA time zone identifier, such as America/New_York. You may also need to store a recurrence pattern or specific date depending on how the event is scheduled.

  • Consider using Joda Time instead of Java's Calendar or Date classess. It will save you from many headaches. Make sure you read the Joda Time documentation and understand how it works.

    Joda Time has all of the functions you will need for converting between one time zone and another - which I believe was the primary concern of your question.

  • Be sure to have a procedure in place for updating the time zone data regularly. Updates are pushed out multiple times a year, as the governments of the world make changes to the legal definitions of their time zones. You cannot just deploy it once and forget about it.

  • Also be sure you understand that conversion from local time to a specific UTC moment is not a perfect function due to daylight saving time. If an event is scheduled during an invalid or ambiguous local time, you should have a strategy for detecting and dealing with that in your application. You might just apply some assumptions, or you might want to go out of your way to ask the user what to do.

    For example, if I schedule an event at 2:00 AM Eastern Time every day, then on March 10th 2013, that time does not exist. Should the event occur at 3:00 AM? Or should it not occur at all?

    Another example, if I schedule an event at 1:00 AM Eastern Time every day, then on November 3rd, 2013, that time occurs twice. Should the event happen at the first (daylight time) instance? Or at the second (standard time) instance? Or both? Should I assume one or the other, or should I ask the user which they mean?

    Only you can decide what to do, as it is your application. But ignoring the problem will likely lead to errors.

  • Once an event has passed, you can record it in UTC if you wish, or record it with the full local date time and offset. Either are acceptable. This works just fine for singular past events, just not for recurring future ones.

14
  • Thanks for detailed response Matt. I expect that almost all events will be created by humans. My thought is to save the timezone and milliseconds, so I know the actual local time of the event and would be able to convert it into the viewer's timezone. You are correct, this not as simple as it seems to be ... Oct 5, 2013 at 3:41
  • 1
    If you store milliseconds, such as from Date.getTime(), that will be in UTC, not local time. If the time zone rules get changed before your time occurs, then it could be the wrong value. In several parts of the world, these time zone change happen frequently, so its very difficult to be sure that a future local time will align with a future UTC instant. (I did say this was complicated. :) ) Oct 5, 2013 at 17:37
  • 1
    You should probably use a LocalDateTime from JodaTime (and a time zone). What DB are you storing these in? Oct 5, 2013 at 17:38
  • Dang! You are correct about UTC Matt, but my thought is that I would convert it using the local TZ and millis which should give me the local time. I am using MySQL. I haven't used JopdaTime before but will certainly look at that solution. Would I run into a problem converting to the viewer's date time if I store event creator's local DateTime and TZ? Oct 9, 2013 at 0:53
  • That should be ok, just watch out for DST issues as I described. Oct 10, 2013 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.