In a web application I’m developing (Ruby on Rails), the user can specify a local time of any location in the world.
(That location is nothing to do with the user’s location or the server’s location.)
And if Daylight Savings time is used at that location, the user types that time.
I need to get the local time without Daylight Savings for the location from the specified Daylight Savings time.
e.g. The server is in San Francisco. The user is in New York. In winter, the user enters the local time during summer (with Daylight Savings) of Minneapolis. And I need to get the equivalent local time of Minneapolis without Daylight Savings offset. Suppose that we don’t have information if Daylight Savings Time is used in Minneapolis or not.
Because the user can specify any location in the world, we cannot tell if that location uses Daylight Savings Time. Also the UTC offset information itself doesn’t tell if it’s in Daylight Savings Time or not.
How can I do this?
Any help will be greatly appreciated?
The olson TZ database (available via the tzinfo gem) does contain information about whether each location uses daylight savings or not (and when that period starts/finishes). You should usually use the timezone objects it provides rather than raw utc offsets. When you’ve got a timezone object the
local_to_utcmethod will do the conversion for you, applying DST as appropriate.The only ambiguous case is when the clocks go back: there is one hour a year during which the local time can correspond to two distinct points in time. In the uk for example at the end of October the clocks go back one hour at 1:59:59 – if a user tells you the local time 1:30:00 you won’t know if it is the first or second 1:30. Tzinfo can tell you both of the possibilities so that you can pick based in your own logic.