iansinnott / jstz

🌐Timezone detection for JavaScript
Other
175 stars 33 forks source link

America/Montreal is not supported. #7

Closed AndrewSouthpaw closed 7 years ago

AndrewSouthpaw commented 7 years ago

jstz sometimes returns a timezone of America/Montreal, which is not one of the official time zones (AFAICT, as it's not in the list of zones for Rails).

I tried digging around in the source code but couldn't exactly figure out how to fix this.

Steps to repro: set your time zone with EST with closest city being Montreal (for Mac).

iansinnott commented 7 years ago

No longer an issue?

AndrewSouthpaw commented 7 years ago

I'm not exactly sure... I can't tell if I'm using Rails wrong to translate from what jstz gives me, or if jstz is giving me invalid timezones.

AndrewSouthpaw commented 7 years ago

But I'm getting some unexpected results, e.g. New_York or Montreal, both of which don't correspond to time zones in ActiveSupport::TimeZone.all.

AndrewSouthpaw commented 7 years ago

Looks like someone else ran into a similar problem. Maybe it would be worth considering their solution?

AndrewSouthpaw commented 7 years ago

The post I linked before correctly identified that Intl is being unhelpful and sometimes incompatible with Rails, as with the case of America/Montreal where there's no such definition in Rails.

Also, my use of jstz was also wrong, since I was trying to translate from jstz to ActiveSupport::TimeZone instead of TZInfo which has ALL the time zones. In my use case, I was trying to use the user's browser timezone to default the value of a time zone selector. Here was the solution (if others run into this problem):

    begin
      browser_tz = ActiveSupport::TimeZone.find_tzinfo(cookies['browser.timezone'])
      @timezone = ActiveSupport::TimeZone.all.reject { |zone| zone.tzinfo != browser_tz }.first.name
    rescue
      @timezone = nil
    end

and then in the view...

          <%= f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones,
                                 include_blank: false,
                                 required: true,
                                 default: @timezone || Time.zone.name %>

It seems like this library is pretty commonly used by folks with Rails backends to determine the time zone, and the translation doesn't work perfectly because ActiveSupport::TimeZone uses more human-readable names (like Eastern Time (US & Canada) instead of America/New_York). If you think it's worth including in the README, I'd be happy to write something up.

iansinnott commented 7 years ago

Yeah if you want to write something up I would happily merge it in to the README. I had never thought about the Rails <-> JSTZ use case but it would probably help people at least to see a link to this issue

        On Jun 12 2017, at 1:34 pm, Andrew Smith <notifications@github.com> wrote:

          Closed #7.

—You are receiving this because you commented.Reply to this email directly, view it on GitHub, or mute the thread.