Andrew Ducker (andrewducker) wrote,
Andrew Ducker
andrewducker

My final solution on my Date/Time/TimeZone issues.

Some of you may remember the fun and games I was going through trying to work out how to deal with dates, times, and timezones, between two disparate systems. If you don't, you can see the discussion thread here.

I ended up actually writing out my use cases, and then working through a case by hand to see what steps were necessary where. This is pretty much the first time I've ever had to do that for stuff I play with in my own time, which probably tells you something about what a mess dates/times are.

In any case, for those that are interested, the use cases are:

  1. Show posts on Friends Page in correct order.
    Therefore we need the time-zone of the poster.

  2. Account for DST when scheduling posts.
    This also means we need the time-zone of the poster.

  3. Display the history with the correct times against them.
    Therefore we need to convert to the specified time-zone of the poster when returning the history.


The major issue is that the JS DateTime object doesn't easily allow you to convert between different timezones specified in standard formats (like "Europe/London"), and none of the libraries available are accurate for this either. Therefore the only answer is to pass everything to the UI with the specificied time zone already set. Except that there's no way of telling JS that the time zone has already been applied, and also that the local time zone the user is in may be different from the time zone they specify. This leads to me following the advice various people gave me (particularly momentsmusicaux) and passing everything in UTC format. And then having to do an additional bit of conversion on the UI too.

Here's the final workflow for updating the time of a future post from "7am tomorrow morning" to "8am tomorrow morning":
Initial State
I am in Germany (UTC +1) <- This should have no effect on the time displayed, which means I need to do a bit of fiddling on the client.
My Timezone preference is New York/EST (UTC - 5)
My post is currently set up to occur at 7am tomorrow (in my specified timezone) - which means that the database contains 12:00:00 (UTC) for it.

Client requests post details
Server converts postingTime to be in EST, but strips timezone - passing it back as if it's UTC:
07:00:00 (UTC)

Browser then converts 07:00:00(UTC) into 07:00:00(Germany) by using:
new Date(postingTime.GetUTCYear, etc.) and displays it

UI Update occurs
I change the postingTime to be 8am.

Browser creates a new UTC Date based on the date/time selected by the user (by using postingTime.setUTCFullYear(localPostingTime.getFullYear()...)
This converts 08:00:00(Germany) to be 08:00:00(UTC)
It then sends that date back to the server.

Server processes the update
Server then creates a New York/EST DateTime object (ignoring the timezone passed in).
08:00:00 (EST)

It then converts that back to UTC:
13:00:00 (UTC)
And saves it.

Final State:
Database contains 13:00:00 (UTC)

And if anyone can think of a simpler way of doing that then I will be forever indebted (and kick myself for not having spotted it.)



Original post on Dreamwidth - there are comment count unavailable comments there.
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 7 comments