Timezones 1, Rails 0

drbrain | Tue, 04 Jul 2006 00:17:00 GMT

For Trackmap I need to take the time a picture was taken and a user supplied time zone and convert that to a correctly offset time. Since Flickr won't give me a time zone (cameras don't record them) I ask the user. If they say Mountain time I need to create a time that is correct for the server's clock (in my case Pacific Time).

Rails TimeZone

When I originally wrote the code back in November and December of 2005 I noticed that Rails provides the handy TimeZone class which worked great! Rails gave me handy offsets that I could use to adjust times correctly.

Then April rolled around, my clocks switched to daylight savings time, and Rails' TimeZone class broke. TimeZone thought the offset for Pacific time was -8 hours instead of -7 hours giving inaccurate conversions. I filed a ticket, but it was closed WONTFIX (the breakage remains undocumented). I was, however, pointed at the TZInfo Timezone plugin. Since April I haven't had much time to work on Trackmap, so I left it broken.

TZInfo Timezone Plugin

On Friday I switched over to the plugin and found it provides incorrect UTC offsets as a misfeature:

>> tzt = TimeZone['Pacific Time (US & Canada)']
=> #
>> tzt.utc_offset
=> -28800
>> Time.now.gmtoff
=> -25200

While the TZinfo Timezone plugin will convert times between known time zones, it doesn't provide a way of interpreting a user-input time so you get a correctly offset time, making it only half of a fix for Rails' TimeZone.

Interpreting Times

To handle user-input times you must use TZInfo::Timezone#period_for_utc or TZInfo::Timezone#period_for_local to retrieve the correct UTC offset. You can retrieve the TZInfo::Timezone object from TzinfoTimezone#tzinfo (which is all the utility the plugin provides).

Correct conversions require some work. I chose to do it this way:

  1. Create a Time using the user-supplied time
  2. Get the local TZInfo::TimezonePeriod for the timestamp
    • If a TimezonePeriod doesn't exist for the given time, get the utc period
    • If the TimezonePeriod is ambiguous, choose standard time
  3. Use TimezonePeriod#to_utc to adjust the Time to a UTC Time

A TimezonePeriod won't exist during the standard time to daylight savings time switch, the user probably chose the wrong time zone. The TimezonePeriod is ambiguous during the daylight savings time to standard time switch since the user's clock traverses the same time period twice. I chose to have strange behavior over failing. If the user notices and emails me I can say they probably fat-fingered something.

Rails Strikes Back

Having time interpretation finished I thought I was done. My database has timestamp with time zone columns so the database and Rails should just figure things out. Turns out Rails still can't handle timestamp with time zone despite having a patch in the bug tracking database for three months.

Instead of applying the patch (which I'd have to do twice, once on my laptop and once online, and for every Rails upgrade until it gets checked in), I adapted it into a 75 line extension to the PostgreSQL connection adapter. Maybe I'll make a plugin out of it while I'm waiting.

I'd like to think I was unlucky and the patch just got lost in the shuffle, but a look at the bug database makes me think that lost tickets happen frequently. One quarter of Rails' defect tickets are open (1098 out of 4034) so I don't see how anybody can figure out what tickets are invalid, valid, duplicate, fixed but not closed, or outright bogus. Furthermore, the amount of bugmail generated must be overwhelming.

The Rails core team should do what Mozilla did back in its early days and organize a bug day. Get a bunch of people on IRC to crawl through the open tickets to validate, cross-reference, prioritize and categorize bugs. (Mozilla even made it a weekly event with prizes!) Having a clean bug database will prevent useful patches from falling on the floor and rotting to the point that their authors must re-write them.

Posted in , ,  | 4 comments

The road to Trackmap Editing

drbrain | Sun, 11 Dec 2005 07:35:00 GMT

Wow, the Google Maps API is severely undocumented, but you can do evil, evil things with it.

I’m ripping coordinates out from the points property of a GPolyline then forcing a redraw to do live deletion (along with a bit of XMLHttpRequest). Next up is moving points about the map and then inserting new points.

But, all this javascript is tiring.

Posted in

And yet more trackmap

drbrain | Sat, 26 Nov 2005 10:45:00 GMT

I added some nifty stuff like a map on the front page and a scroller for the photos so you can see them in context. That last one is the Mariposa Sequoia Grove at Yosemite. You can also link straight to a point like this deer I was 8 feet away from.

Now I’ve got the major features all ironed out, and just minor polish-type-features left. (Like not giving a 500 when flickr gets really slow.)

Posted in

Yet More Trackmap

drbrain | Thu, 24 Nov 2005 06:39:00 GMT

Trackmap

Changes in no particular order:

  • Photos can be refetched from flickr
  • Routes can be deleted
  • Time zone offset can be specified so your photostream matches the track correctly
  • Markers bubbles look much better, now with as many as 3 photos
  • Photos in marker bubbles have flickr title as tooltip
  • Map auto-sizes to browser window

TODO in a semi-accurate order:

  1. More text about what actions do
  2. Accounts

    • That save flickr username and TZ offset
    • And only let you delete your routes
  3. Route descriptions
  4. Bookmarkable points
  5. Move show page actions up to a top navbar thingy
  6. Google map on front page

    • With picture in popup
  7. Photo scroller on side

    • That centers a point when you click the photo and brings up the marker
    • And brings up the photo description
  8. Figure out why long routes cause browsers to lock up
  9. Geotag flickr photos
  10. More Markers

    • Cameras
    • Start/Stop

Posted in

Trackmap Improvements

drbrain | Tue, 22 Nov 2005 08:53:00 GMT

  • Attach photos by flickr username, email, or NSID
  • Attach photos from the track create
  • Multiple photo support “N photos total” if more than one photo per point
  • Using the 75×75 size photos for faster loading of bubbles
  • More/better error messages
  • Tracks of unknown format will automatically get emailed to me
  • Switched to the large slider for easy zooming

TODO:

  • Text blurbs
  • Photo titles
  • Track deletion
  • Photo deletion
  • Time zone support
  • Accounts so you don’t have to set your flickr info/time zone offset every time
  • Better-looking markers
  • Marker images

Posted in

More Trackmap

drbrain | Mon, 21 Nov 2005 21:59:00 GMT

I’ve got the current revision of Trackmap running. Routes of interest are:

Posted in

Trackmap

drbrain | Mon, 21 Nov 2005 10:28:00 GMT


Trackmap, originally uploaded by drbrain.

OMG OMG OMG!

This is cooler than I thought it would be!

But maybe that’s just because its 2:30 and I just made it work!

From the Flickr description:


Trackmap combines GPS tracks + Google Maps + Flickr to give something totally sweet.

I have some issues to work out with threading, but those may be related only to WEBrick mode.

Pay no attention to the typo in the title bar.

Posted in