Reverse lookup? #22

Closed
bradpauly opened this Issue Jun 27, 2014 · 1 comment

Comments

Projects
None yet
2 participants
@bradpauly

Hi, I hope it's okay to ask a question here. I was going to post to the mailing list, but rubyforge seems to be gone.

I'd like to find the zone identifiers where the local time is a certain time. For example, in which zones is it currently 6pm?

The best I could come up with on my own was to look at all zones and check the time in each one. Is there another way to do this? I've been browsing http://rubydoc.info/gems/tzinfo/TZInfo without much luck. Any ideas or suggestions on where to read on rubydoc would be great. Thanks!

@philr

This comment has been minimized.

Show comment
Hide comment
@philr

philr Jun 30, 2014

Member

There's nothing in TZInfo that will do what you want directly.

Depending on exactly how you want to handle daylight savings transitions, your best option might be to calculate and store the offset of each time zone and use this to group the time zones you are interested in.

This will give you the base offset from UTC in seconds for timezone:

timezone.current_period.utc_offset

The Timezone#current_period method returns a TimezonePeriod object representing the offset that is currently in force for the time zone.

The TimezonePeriod#utc_offset method doesn't include the additional +1 hour for daylight savings, so will be the same all year round for most time zones. Use TimezonePeriod#utc_total_offset instead of you want to get the offset including the effect of daylight savings.

Member

philr commented Jun 30, 2014

There's nothing in TZInfo that will do what you want directly.

Depending on exactly how you want to handle daylight savings transitions, your best option might be to calculate and store the offset of each time zone and use this to group the time zones you are interested in.

This will give you the base offset from UTC in seconds for timezone:

timezone.current_period.utc_offset

The Timezone#current_period method returns a TimezonePeriod object representing the offset that is currently in force for the time zone.

The TimezonePeriod#utc_offset method doesn't include the additional +1 hour for daylight savings, so will be the same all year round for most time zones. Use TimezonePeriod#utc_total_offset instead of you want to get the offset including the effect of daylight savings.

@philr philr added the question label Jun 30, 2014

@philr philr closed this Jun 30, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment