From svakil at gmail.com Thu Jun 15 12:04:09 2006 From: svakil at gmail.com (Sanjay Vakil) Date: Thu, 15 Jun 2006 12:04:09 -0400 Subject: [TZInfo-users] selection of select popup based on browser timezone? Message-ID: First off, thanks so much for doing this. It just dropped in. It made me happy in the way that ruby itself does. My question is: I'm using this line to create my selection popup <%= time_zone_select 'user', 'time_zone', TZInfo::Timezone.us_zones, :model => TZInfo::Timezone %> Is there some clever way to have it default (if otherwise unset) the the TZ reported by the browser? I know that there is javascript out there to grab the TZ info (or at least an offset). it'd be slick if the top n entries corresponded to possible (correct) timezones. cheers, sanjay -- The world tends to work out for me. I try to give it reason to. From philip at ross.org.uk Thu Jun 15 16:06:56 2006 From: philip at ross.org.uk (Philip Ross) Date: Thu, 15 Jun 2006 21:06:56 +0100 Subject: [TZInfo-users] selection of select popup based on browser timezone? In-Reply-To: References: Message-ID: <4491BDE0.4050205@ross.org.uk> Sanjay Vakil wrote: > First off, thanks so much for doing this. It just dropped in. It > made me happy in the way that ruby itself does. > > My question is: > > I'm using this line to create my selection popup > > > <%= time_zone_select 'user', 'time_zone', > TZInfo::Timezone.us_zones, :model => TZInfo::Timezone %> > > Is there some clever way to have it default (if otherwise unset) the > the TZ reported by the browser? I know that there is javascript out > there to grab the TZ info (or at least an offset). it'd be slick if > the top n entries corresponded to possible (correct) timezones. Michael Smedberg has done something similar to what you are trying to achieve. He's given me permission to distribute his solution to the problem. The attachments referenced can be found in http://philross.co.uk/files/tzinfo/offset_js.zip ---------- Forwarded message ---------- From: Michael Smedberg Date: 06-May-2006 01:05 Subject: Re: Question/suggestion related to TZInfo To: Philip Ross I made some minor changes to that file. I switched from some less generic timezone identifiers (like "America/Los_Angeles") to more generic (like "US/Pacific".) I thought your other user might want the same thing, so I've attached the latest version of my file... (tz2.js) ---------- Forwarded message ---------- From: Michael Smedberg Date: 02-Nov-2005 03:19 Subject: Re: Question/suggestion related to TZInfo To: Philip Ross I did something similar to what you suggest. You outlined two major problems. First, looking up timezones by offset is slow. Second, looking up timezones by offset is non-deterministic. I think I solved these problems (in a relatively cheesy way.) Basically, I wrote some code that runs through TZInfo::Timezone.all and lists all the timezones and their offsets. Then I group those with the same offset. For each group, I choose a "most likely" choice, based on how many hits Google generates for that timezone (on the theory that the most popular timezones would be mentioned on the Web the most often.) Then I simply map offsets to the most likely timezone based on a big switch statement (more or less.) I've attached this code as tzinfo_extra.rb After writing this, I realized that I really want to do this on the client side. When a user signs up for an account I want to show a time_zone_select select control, and I want it pre-selected to the most likely timezone. So I took the Ruby stuff and converted it to some terse JavaScript. I've attached this code as tz.js. I'm using SaltedLoginGenerator, and I added the timezone stuff to the _edit partial for User. Here's how I call the JavaScript in that partial:
<%= form_input :hidden_field, 'form', :value => 'edit' %> <%= form_input changeable(user, "name"), "name" %> <%= form_input changeable(user, "login"), "login", :size => 30 %>
<%= form_input changeable(user, "email"), "email" %> <%= time_zone_select 'user', 'time_zone', TZInfo::Timezone.all.sort, :model => TZInfo::Timezone %> <% if submit %> <%= form_input :submit_button, user.new_record? ? 'signup' : 'change_settings', :class => 'two_columns' %> <% end %> <% #MES- If the user hasn't selected a timezone, try to guess the timezone for the # user, and select it in the select element. if 'Unknown' == @user.tz.identifier -%> <% end -%>
Doing the GeoIP stuff would probably yield more precise matches, but I figure if the match seems to show the right time for DST and non-DST, most users will probably be pretty satisfied. In my app, I'm usually showing dates near the current date, so I don't care so much if the timezones diverge in the more distant past or more distant future. For apps that have to show larger ranges of time, my solution may well be inadequate, and GeoIP might be more worth the trouble. Thanks! From rails at j-f.dk Wed Jun 21 06:23:23 2006 From: rails at j-f.dk (Jesper Andersen) Date: Wed, 21 Jun 2006 18:23:23 +0800 Subject: [TZInfo-users] Time zone Message-ID: <200606211823.24453.rails@j-f.dk> I seem to have some problem with this timezone. The problem is that any time, no matter what timezone is reported to be UTC time. The times are correct, just the timezone information returned with the time is wrong. Below are some examples. Am I doing something wrong or is this a bug? >> c=Timezone.get 'Europe/Copenhagen' => Europe - Copenhagen >> t=Time.now.utc => Wed Jun 21 03:17:24 UTC 2006 >> c.utc_to_local(t) => Wed Jun 21 05:17:24 UTC 2006 >> t.utc? => true >> c.utc_to_local(t).utc? => true >> c.now => Wed Jun 21 06:49:51 UTC 2006 >> c.now.rfc2822 => "Wed, 21 Jun 2006 06:51:18 -0000" >> c.now.zone => "UTC" I am using: tzinfo (0.2.2) ruby 1.8.4 -- Jesper 11:18:28 up 6 days, 3:25, 12 users, load average: 0.73, 0.63, 0.59 From philip at ross.org.uk Wed Jun 21 09:37:34 2006 From: philip at ross.org.uk (Philip Ross) Date: Wed, 21 Jun 2006 14:37:34 +0100 Subject: [TZInfo-users] Time zone In-Reply-To: <200606211823.24453.rails@j-f.dk> References: <200606211823.24453.rails@j-f.dk> Message-ID: <44994B9E.8020508@ross.org.uk> Jesper Andersen wrote: > I seem to have some problem with this timezone. The problem is that any > time, no matter what timezone is reported to be UTC time. The times are > correct, just the timezone information returned with the time is wrong. > Below are some examples. Am I doing something wrong or is this a bug? You're not doing anything wrong - this is the intended behaviour at present. I haven't yet been able to find a way to construct a Ruby Time with a specific timezone. Time can either represent times in the local timezone of the machine/environment or in UTC. As a result, TZInfo always constructs Times in UTC and ignores the timezone of any instances passed in. There's a tracker item for this issue: http://rubyforge.org/tracker/index.php?func=detail&aid=4134&group_id=894&atid=3528 If you want to get hold of the abbreviation for the time, you can use the period_for_utc method: c = Timezone.get 'Europe/Copenhagen' => Europe - Copenhagen c.period_for_utc(Time.now.utc).abbreviation => :CEST -- Philip Ross http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby