开发者

How to get the data to populate time zone switcher widget on website?

开发者 https://www.devze.com 2023-01-14 14:36 出处:网络
The task: Time zone chooser widget that allows site visitors to choose their time zone should be generated and populated with reasonable data. It should offer 开发者_StackOverflow社区choices like thi

The task:

Time zone chooser widget that allows site visitors to choose their time zone should be generated and populated with reasonable data. It should offer 开发者_StackOverflow社区choices like this:

GMT-11 (HH:MM actual time in that zone)
GMT-10 (...)
GMT-1 (...)
... now for the tricky part:
GMT (...)
GMT+1 (...)
... and then all the way until +12.

When user chooses different time zone, server should be able to figure out what time zone to set. This second part isn't much of a problem, once list is populated with correct data.

The problem:

Tricky part is getting actual timezones which are currently in use world-wide. I know how to get entire list of timezones, and how to get their offsets and current DST settings. But, each time zone has two variants: regular time and DST time.

In above list I have marked certain "tricky" part. This is where time zones like Europe/London are. During regular time, Europe/London = GMT, but during DST, Europe/London = GMT+1... Can you see a problem?

What troubles me also is that not all time zones change from DST to regular time and vice-versa, so I can't just populate list with generic data like -11 = GMT-11, -10 = GMT-10, ... and then just apply some logic to locate time zone with appropriate time difference and then set that time zone as a default from that point on.

I'm interested to hear how other sites, that utilize this time zone switching feature, come up with a list of time zones and a logic to choose right time zone, depending on user's choices? Something else you'd like to add or ask?


The most commonly used source for time zone data is the tz/zoneinfo database - it's publically available for free.

As you have noticed yourself, a timezone is an political/administrative entity consisting of a base offset, DST switching dates, and even historical data about changes in these, which you need to correctly deal with historical dates (such as six and a half minutes that officially don't exist).

The tz database (and most apps that use it) therefore represent a timezone not through a base offset, but through the name of the continent and a large City that uses it, often the capital of the country that uses that particular time zone. Examples: "Europe/Berlin" or "Australia/Darwin".


Have a look at what Google Calendar does. It displays the standard time offset as well as the city name, e.g.

(GMT+01:00) Tunis

That seems a pretty reasonable way of representing them, too me. On the other hand, I'm aware that some UIs list the current offset from UTC along with the city. Either way you'll confuse people, to be honest.

0

精彩评论

暂无评论...
验证码 换一张
取 消