如何从 Twitter API 可靠地确定用户的时区? [英] How to reliably determine time zone for a user from the Twitter API?

查看:25
本文介绍了如何从 Twitter API 可靠地确定用户的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当从 Twitter 的 API 为用户获取信息时,他们提供两个与用户时区相关的字段:

When getting information from Twitter's API for a user, they provide two fields related to the user's time zone:

utc_offset: -14400,
time_zone: "Indiana (East)"

不幸的是,这并不能说明全部情况,因为我不知道 UTC 偏移量是在标准时间还是夏令时计算的.除以 3600 秒后,我得到 -4 小时,这在夏季有效,但在冬季,正确的值为 -5 小时.

Unfortunately, this doesn't tell the full story because I don't know if that UTC offset was calculated during standard time or daylight savings time. After dividing by 3600 seconds, I get -4 hours, which is valid during the summer months, but in the winter the correct value would be -5 hours.

如果该值始终由夏令时值确定,那么我可以为此编写一个算法,但是在对该主题进行一些搜索之后,我看到了几个与该假设相矛盾的粘贴输出.(举个简单的例子,这个问题显示他/她的偏移量为-21600,然后他/她说他/她在中央时间,如果在夏令时计算将是 -18000).

If the value was ALWAYS determined by the daylight savings time value then I could write an algorithm for that, however after some searching on the subject I've seen several pasted outputs that contradict that assumption. (as a quick example, this question shows his/her offset as -21600 and then he/she says he/she is on central time, which if calculated during daylight savings time would be -18000).

对我来说,该值将从 1 月 1 日开始计算,并且我在网上找到的几个粘贴输出属于该类别,但我自己的 Twitter 帐户显示了上面列出的值,对此假设无效.我的下一个想法是它可能是在我创建帐户时计算的,但这似乎也是错误的,因为我可以在以后的任何时间更改我的时区(即使如此,我在 11 月创建了我的帐户,当时我本来可以在标准时间而不是夏令时!).

It would make sense to me that the value would be calculated as of Jan 1 and the several pasted outputs I've found online fall into that category, but my own Twitter account shows the values listed above for which this assumption is invalid. My next thought was maybe it was calculated at the time I created my account, but then that seems erroneous as well because I can change my time zone at any later point (and even so, I created my account in November when I would have been on standard and not daylight time!).

我最后的想法是,该值可能是按 API 请求的日期计算的.这很有意义,我拥有的 Twitter 帐户似乎都证实了这一点.但是,我之前链接的 SA 问题表明此人在 6 月 2 日回答了该问题,即夏令时和他的/她的值 -21600 反映了中部时区的标准时间.

My last thought was that maybe the value is being calculated by the date of the API request. This makes a lot of sense and the Twitter accounts I own all seem to validate this. BUT, the SA question I linked to earlier shows that the person answered the question on June 2nd, which is daylight savings time and his/her value of -21600 reflects a standard time for the Central time zone.

有人解决这个问题吗?非常感谢!

Anyone out there solve this problem? Thanks so much!

推荐答案

Twitter 的前端使用 Ruby on Rails.如果您转到您自己的 twitter 帐户设置并查看时区的可能选项(在下拉列表中查看源代码),您会发现它们与 ActiveSupport::TimeZone 提供的匹配,在本文档中显示.尽管 Twitter 忽略了 Rails 理解的某些区域,但所有 Twitter 区域键名称都在该列表中.

Twitter's front end uses Ruby on Rails. If you go to your own twitter account settings and look at the possible options for time zones (view source on the dropdown list), you will find that they match up with those provided by ActiveSupport::TimeZone, shown in this documentation. Although there appears to be some zones understood by Rails that Twitter has omitted, all of the Twitter zone key names are in that list.

我已要求 Twitter 在未来使用标准时区名称,在此开发者请求中.

I have asked Twitter to use standard time zone names in the future, in this developer request.

为什么 Rails 限制这个列表并使用自己的键值?谁知道.我之前问过,得到的回应很少.阅读此处.

Why does Rails limit this list and use their own key values? Who knows. I have asked before, and gotten very little response. Read here.

但是您当然可以使用他们的映射字典将 time_zone 值转换为标准的 IANA 时区标识符.例如:

But you can certainly use their mapping dictionary to turn the time_zone value into a standard IANA time zone identifier. For example:

  • 印第安纳(东部)" =>美国/印第安纳州/印第安纳波利斯"
  • 中部时间(美国和加拿大)" =>美国/芝加哥"
  • "Indiana (East)" => "America/Indiana/Indianapolis"
  • "Central Time (US & Canada)" => "America/Chicago"

这可以在 Rails 文档源代码.(向下滚动到 MAPPING.)

This can be found in the Rails documentation, and in the source code. (Scroll down to MAPPING.)

然后您可以使用任何您希望的标准 IANA/Olson/TZDB 实现.它们几乎适用于所有语言和平台.有关更多详细信息,请参阅时区标签维基.如果您需要特定实现方面的帮助,则需要扩展您的问题,以告诉我们您使用的语言以及到目前为止您尝试过的语言.(或者考虑就这部分内容提出一个新问题.)

Then you can use any standard IANA/Olson/TZDB implementation you wish. They exist for just about every language and platform. For further details, see the timezone tag wiki. If you need help with a specific implementation, you'll need to expand your question to tell us what language you are using and what you have tried so far. (Or consider asking a new question about just that part of it.)

关于 utc_offset 字段,twitter 没有说明他们使用什么基础来计算它.我的猜测是它是用户的当前偏移量,基于您调用 API 的时间.

In regards to the utc_offset field, twitter does not make it clear what basis they use to calculate it. My guess is that it is the user's current offset, based on the time that you call the API.

更新 1

我在 TimeZoneConverter<中添加了对将 Rails 时区名称转换为 IANA 和 Windows 标准时区标识符的支持/a> .NET 库.如果您使用的是 .NET,则可以使用此库来简化转换并更轻松地掌握更新.

I have added support for converting Rails time zone names to both IANA and Windows standard time zone identifiers in my TimeZoneConverter library for .NET. If you are using .NET, you can use this library to simplify your conversions and stay on top of updates more easily.

更新 2

Twitter 的 API 现在以这种格式返回时区:

Twitter's API now returns the time zone in this format:

"time_zone": {
    "name": "Pacific Time (US & Canada)",
    "tzinfo_name": "America/Los_Angeles",
    "utc_offset": -28800
},

使用 tzinfo_name 字段.完毕.:)

Use the tzinfo_name field. Done. :)

这篇关于如何从 Twitter API 可靠地确定用户的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆