最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TimeZone vs Offset to store user timezone - Stack Overflow

programmeradmin3浏览0评论

I realize this is a monly asked question but I couldn't find any posts that point out the disadvantages of using/storing offset for user's time zone. Is this not a better and more efficient way?

Long drop down lists of time zones are not user friendly and most of such lists don't have all the cities anyway. They also require user to specify their time zone. I feel it may be much better to simply detect it. In my case, my app is an ASP.NET Core app with Reach front end and it's pretty easy to capture user's time zone offset via JavaScript.

Any reason why storing the offset of user's timezone is NOT a good idea?

I realize this is a monly asked question but I couldn't find any posts that point out the disadvantages of using/storing offset for user's time zone. Is this not a better and more efficient way?

Long drop down lists of time zones are not user friendly and most of such lists don't have all the cities anyway. They also require user to specify their time zone. I feel it may be much better to simply detect it. In my case, my app is an ASP.NET Core app with Reach front end and it's pretty easy to capture user's time zone offset via JavaScript.

Any reason why storing the offset of user's timezone is NOT a good idea?

Share Improve this question asked Oct 20, 2017 at 17:58 SamSam 30.3k75 gold badges252 silver badges460 bronze badges 5
  • 2 Timezone and offset are not the same thing. The same offset can be used by more than 1 timezone, and the same timezone can have more than 1 offset during history, due to Daylight Saving Time (or simply because they decided to move to another offset). As it's a N-to-N relationship, storing just the offset will give you a hint (a list of possible zones), but not the exact zone. – user7605325 Commented Oct 20, 2017 at 18:11
  • Fair point. I think the ultimate answer depends on one’s needs. In my case, I simply want to display all date time values in user’s current time zone so that they’re meaningful to the user. This includes those times when the user is away from his/her home time zone but still using my app. I feel detecting the offset change and automatically displaying all date/time values in user’s current time zone is pretty user friendly. It may be a good idea to display a notification upon detecting a change though. – Sam Commented Oct 20, 2017 at 18:16
  • In this case, you could store all in UTC, and convert to the user's timezone when displaying – user7605325 Commented Oct 20, 2017 at 18:24
  • Exactly. Currently I store all date/time values in UTC and store user’s time zone too. But more and more I feel, I have no use for user’s time zone. All I need is the offset for my conversion. – Sam Commented Oct 20, 2017 at 18:27
  • 2 Actually, a timezone is a set of all offsets that a region had, has and will have during history (including DST changes), so only with the timezone you can get the correct offset for each UTC instant. – user7605325 Commented Oct 20, 2017 at 18:30
Add a ment  | 

1 Answer 1

Reset to default 16

Any reason why storing the offset of user's timezone is NOT a good idea?

Yes. Many. A time zone and an offset are not the same thing. A time zone represents a geographical area in which local time is aligned. A time zone may undergo several different changes in its offset from UTC. Some of which are regular (like daylight saving time), and some of which are irregular (like when a government changes its standard time or dst rules).

... In my case, I simply want to display all date time values in user’s current time zone so that they’re meaningful to the user.

Ok, so let's say you check the user's current time zone offset and it is UTC-7. So you apply that to some dates and times in your application and done - so you think. Except that you didn't take into account that the user is in California, and one of your dates is in December when the offset should be UTC-8.

So you try to correct for that, and work out the rules of "when I see -7, it might be -8 sometimes". Except now you have a user e along who is in Colorado, where it is -7 during the winter and -6 during the summer. Or another user from Arizona, where most of the state is in -7 for the whole year. How do you know which set of rules to follow? Without referencing an actual time zone, it cannot be done.

This gets even more plex worldwide. For example, the number of variations for UTC+2 is just crazy. Even for countries that switch between UTC+2 and UTC+3 - they don't all switch on the same dates or at the same time of day!

See also: The Problem with Time & Timezones - Computerphile (YouTube) and the StackOverflow timezone tag wiki.

发布评论

评论列表(0)

  1. 暂无评论