Rails 时区(偏移量和 dst)

发布于 2024-12-11 15:41:59 字数 373 浏览 0 评论 0原文

我正在寻求一些帮助,了解如何从为我提供 UTC 偏移量和 DST 的数据中存储正确的时区。

数据来自 http://openflights.org/data.html

时区与 UTC 的小时偏移量。小数小时表示为小数,例如。印度是5.5。

DST 夏令时。 E(欧洲)、A(美国/加拿大)、S(南美洲)、O(澳大利亚)、Z(新西兰)、N(无)或 U(未知)之一。

我想知道如何在 Rails 中使用这些数据将这些机场的时区作为 Rails 可以识别的时区列中的字符串存储。

I am looking for some help in how I can store the correct timezone in Rails, from data that provides me with a UTC offset, and DST.

The data comes from http://openflights.org/data.html

Timezone Hours offset from UTC. Fractional hours are expressed as decimals, eg. India is 5.5.

DST Daylight savings time. One of E (Europe), A (US/Canada), S (South America), O (Australia), Z (New Zealand), N (None) or U (Unknown).

I am wondering how I could use this data in Rails to store the timezone of these airports as a string in a Timezone column that Rails would recognise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖上的星空 2024-12-18 15:41:59

我认为没有解决方案,除非您创建 DST 的自定义映射并偏移到特定时区。

我最终使用 geonames.org 网络服务来根据纬度/经度找出时区。

I don't think there there is a solution for this, unless you create a custom mapping of the DST and offset to a particular timezone.

I ended up using the geonames.org webservice, to find out the timezone based on latitude/longitude.

依 靠 2024-12-18 15:41:59

在美国邮政编码数据库的稍微简单的情况下,我需要相同的功能,其中包括 UTC 小时偏移量和指示其是否参与夏令时的 Y/N 字段,因此该数据库中的美国西海岸邮政编码都有“-8”和“Y”。

我找不到内置的 Rails 方法来执行此操作,因此我手动创建了一个查找哈希,其中键是 UTC 和 UTC 时间。 DST 字段相连,例如“#{utc}#{dst}”,值为时区名称。此方法也适用于 utc 偏移量,例如“5.5”。

在执行查找的方法中,给定 utc 和 dst 值,在哈希查找返回 nil 的情况下,我指定一个默认时区(例如美国西海岸),因为出现意外值,例如“-5N”(因为东海岸)海岸没有任何不应该发生的非 DST 状态)。

但同样的方法可以通过创建一个散列来全局应用,该散列代表所有可能的时区,其中包含夏令时的 Y 和 N 值。

class MyZip

HOUR_DST_TO_TIMEZONE_NAME = {
  "-5Y" => "Eastern Time (US & Canada)",
  "-6Y" => "Central Time (US & Canada)",
  "-7Y" => "Mountain Time (US & Canada)",
  "-7N" => "Arizona",
  "-8Y" => "Pacific Time (US & Canada)",
  "-9Y" => "Alaska",
  "-10N" => "Hawaii"
  }

def self.timezone_name_from_zip_hour_dst(utc, dst)
  key = "#{utc}#{dst}" 
  return HOUR_DST_TO_TIMEZONE_NAME[key] || MyZip.unknown_timezone_name
end
...

I needed the same functionality in the slightly simpler case of a database of USA zipcodes which includes a UTC hours offset and Y/N field indicating if it participates in daylight savings time, so West coast USA zipcodes in this database all have "-8" and "Y".

I could not find a built-in Rails method to do it, so I manually created a lookup hash where the key is the UTC & DST fields catenated, eg, "#{utc}#{dst}" and the value is the timezone name. This method could work for utc offsets such as "5.5" as well.

In the method that does the lookup is given the utc and dst values, I specify a default timezone (USA west coast for example) in the case where the hash lookup returns nil because an unexpected value such as "-5N" (since the east coast does not have any non-DST states that should never occur).

But the same method could be applied globally by creating a hash that represented all the possible timezone with both Y and N values for daylight savings time.

class MyZip

HOUR_DST_TO_TIMEZONE_NAME = {
  "-5Y" => "Eastern Time (US & Canada)",
  "-6Y" => "Central Time (US & Canada)",
  "-7Y" => "Mountain Time (US & Canada)",
  "-7N" => "Arizona",
  "-8Y" => "Pacific Time (US & Canada)",
  "-9Y" => "Alaska",
  "-10N" => "Hawaii"
  }

def self.timezone_name_from_zip_hour_dst(utc, dst)
  key = "#{utc}#{dst}" 
  return HOUR_DST_TO_TIMEZONE_NAME[key] || MyZip.unknown_timezone_name
end
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文