14
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RailsでTimeZone系のいろいろを取得する

Last updated at Posted at 2014-02-18

タイムゾーンの国を取得

タイムゾーンの国をすべて取得

tz_countries = TZInfo::Country.all

特定の国のタイムゾーン(地域)を取得する

tz = TZInfo::Country.get("JP")
=> #<TZInfo::Country: JP>

tz.zone_names
=> ["Asia/Tokyo"]

一つの国の中に複数のタイムゾーンが存在する国もある。

tz = TZInfo::Country.get("US")
=> #<TZInfo::Country: US>

tz.zone_names
=> ["America/New_York",
 "America/Detroit",
 "America/Kentucky/Louisville",
 "America/Kentucky/Monticello",
 "America/Indiana/Indianapolis",
 "America/Indiana/Vincennes",
 "America/Indiana/Winamac",
 "America/Indiana/Marengo",
 "America/Indiana/Petersburg",
 "America/Indiana/Vevay",
 "America/Chicago",
 "America/Indiana/Tell_City",
 "America/Indiana/Knox",
 "America/Menominee",
 "America/North_Dakota/Center",
 "America/North_Dakota/New_Salem",
 "America/North_Dakota/Beulah",
 "America/Denver",
 "America/Boise",
 "America/Shiprock",
 "America/Phoenix",
 "America/Los_Angeles",
 "America/Anchorage",
 "America/Juneau",
 "America/Sitka",
 "America/Yakutat",
 "America/Nome",
 "America/Adak",
 "America/Metlakatla",
 "Pacific/Honolulu"]

協定世界時(UTC)との時差を取得する

協定世界時との時差を秒を単位とする数値で取得する

name = "Asia/Tokyo"
=> "Asia/Tokyo"

ActiveSupport::TimeZone.create(name).utc_offset
=> 32400 #(秒)

協定世界時との時差を時間形式(+00:00/-00:00)のフォーマットで表示する

name/identifier(地域名)から取得する

name = "Asia/Tokyo"
=> "Asia/Tokyo"

ActiveSupport::TimeZone.create(name).now.strftime('%z').insert(3, ':')
=> "+09:00"

utc_offset(時差(秒))から取得する

name = "Asia/Tokyo"
=> "Asia/Tokyo"

utc_offset = ActiveSupport::TimeZone.create(name).utc_offset
=> 32400 #(秒)

ActiveSupport::TimeZone.seconds_to_utc_offset(utc_offset)
=> "+09:00"
14
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?