I have a Rails app for bands. Bands can import their shows which all occur in different time zones. It seems like a ton of work to store these events in UTC. I would have to figure out the time zone for any show created and then convert back to the show's local time zone when displaying to the user. Is there a simple plugin to get a UTC offset based on geolocation? That would probably help, but does anyone see any major reasons why I should store in UTC here? I understand storing timestamps in UTC is probably a good idea...but band event times?
one way to do it is store them all in utc and then let the user decide what timezone they want to see them in.
http://mad.ly/2008/04/09/rails-21-time-zone-support-an-overview/
You don't need to worry about UTC here, since the event is tied to the time and location in which it's occurring. If someone is traveling from Hawaii to San Francisco for an 8pm show, that doesn't mean the show is going to start at 4pm for them. So, you can store plain time and location, don't worry about TZ (since presumably the bands just have a time and location form) and store it like that in the db.
As far as Rails is concerned, all events will be occurring in the TZ in which it exists, just with this funny "location" attribute that tells the reader that the event is really 1000mi away. This means it can do it's normal auto-TZ conversions for itself, but the reader won't be able to tell. It will just be converting to time-as-entered by the band in the first place. 8pm+"Los Angeles", 10pm+"Tulsa, OK."
The one wrinkle here is whether shows are broadcast over the internet or something like that, where the location is decoupled from the event, in which you would still do the above, but let the viewer/potential-audience-member choose their own TZ (or geolocate them) and have the app do the conversion upon display in the view.
I'm working on something similar - a site with a list of events. In my situation, it's important to have the times standardized (in UTC) because we have announce and on-sale times to worry about (i.e., when the events appear on the site and when the on-sale links show up), not just displaying the event start time (which itself doesn't care what time zone it's in).
Going from a UTC time in the database to the local time for the given venue (i.e., to display the local time in the event listing) is pretty simple using something along the lines of e.start.in_time_zone("#{e.venue.time_zone}")
. What I couldn't figure out was getting the local time at the point of data entry recognized as a local time needed to be converted to UTC, without having to deal with changing Time.zone.
I found something that works. Check out this post: http://steveluscher.com/archives/changing-a-times-zone-in-rails-keeping-the-same-local-representation. I added a new file (time_zone_support.rb) to my config/initializers directory. Here are the contents:
module ActiveSupport
class TimeWithZone
def zone=(new_zone = ::Time.zone)
# Reinitialize with the new zone and the local time
initialize(nil, ::Time.__send__(:get_zone, new_zone), time)
end
end
end
This allows the following in the console:
>> e.starts = Time.zone.parse("2010-09-12 10:00 am")
=> Sun, 12 Sep 2010 10:00:00 UTC +00:00
>> e.starts.zone = e.time_zone
=> "Pacific Time (US & Canada)"
>> e.starts
=> Sun, 12 Sep 2010 10:00:00 PDT -07:00
>> e.save
=> true
Hope that helps you too!