Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Railsのtimezone設定(Rails4系の場合)
Search
publichtml
June 16, 2016
Programming
0
100
Railsのtimezone設定(Rails4系の場合)
publichtml
June 16, 2016
Tweet
Share
More Decks by publichtml
See All by publichtml
初めまして、株式会社万葉です!! @RailsGirlsNagoya4th /everyleaf-on-railsgirlsnagoya4th
publichtml
0
80
ダムで学ぶRailsのRoutes入門
publichtml
2
330
Let's join RailsGirlsTokyo, More!
publichtml
0
92
Other Decks in Programming
See All in Programming
ローカルLLMを⽤いてコード補完を⾏う VSCode拡張機能を作ってみた
nearme_tech
PRO
0
100
Findy AI+の開発、運用におけるMCP活用事例
starfish719
0
1.2k
MAP, Jigsaw, Code Golf 振り返り会 by 関東Kaggler会|Jigsaw 15th Solution
hasibirok0
0
250
脳の「省エネモード」をデバッグする ~System 1(直感)と System 2(論理)の切り替え~
panda728
PRO
0
100
20251127_ぼっちのための懇親会対策会議
kokamoto01_metaps
2
440
Giselleで作るAI QAアシスタント 〜 Pull Requestレビューに継続的QAを
codenote
0
230
非同期処理の迷宮を抜ける: 初学者がつまづく構造的な原因
pd1xx
1
730
WebRTC と Rust と8K 60fps
tnoho
2
2k
tsgolintはいかにしてtypescript-goの非公開APIを呼び出しているのか
syumai
7
2.2k
Socio-Technical Evolution: Growing an Architecture and Its Organization for Fast Flow
cer
PRO
0
360
AIの誤りが許されない業務システムにおいて“信頼されるAI” を目指す / building-trusted-ai-systems
yuya4
6
3.7k
Graviton と Nitro と私
maroon1st
0
100
Featured
See All Featured
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.6k
BBQ
matthewcrist
89
9.9k
Designing for Performance
lara
610
69k
Why Our Code Smells
bkeepers
PRO
340
57k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Designing for humans not robots
tammielis
254
26k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
Testing 201, or: Great Expectations
jmmastey
46
7.8k
Making the Leap to Tech Lead
cromwellryan
135
9.7k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
A better future with KSS
kneath
240
18k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
980
Transcript
Railsのtimezone設定 (Rails4系の場合) 2016/06/16
Railsの2種類のtimezone設定 • confg.time_zone – 各地の都市名を指定(e.g. 'Tokyo') – アプリ内で使われるタイムゾーン “Time.zone” の
default値 • confg.active_record.default_timezone – :utc or :local – DBに保存するタイムゾーン どちらも confg/application.rb に書く
例えば、こんな風にRailsアプリが あるとするじゃろ? DB Active Record Railsアプリ
役割分担はこうじゃ DB Active Record Railsアプリ confg.active_record. default_timezone confg.time_zone アプリ内タイムゾーン DBに保存するタイムゾーン
まず左側 DB Active Record Railsアプリ confg.active_record. default_timezone confg.time_zone アプリ内タイムゾーン DBに保存するタイムゾーン
confg.time_zone • アプリ内で使われるタイムゾーン “Time.zone” のdefault値 • defaultということは上書きできる • 複数タイムゾーンを扱うアプリは、リクエスト 毎にログインユーザに合わせて切り替えるのが
定番 • 設定さえしておけば、後のタイムゾーン変換は ActiveRecordにお任せ
たぶん定番的な書き方 すべてのControllerの基底クラス(ApplicationController) で、ログインユーザに合わせて上書き class ApplicationContoller < ActionController::Base before_action :set_timezone def
set_timezone # ログインユーザのtimezoneで上書き Time.zone = current_user.timezone if current_user end # ログインユーザを返すメソッド def current_user # ... end end
ActiveRecordによる自動変換 Railsアプリ Time.zone=“America/New_York” DB start_at = '2017-01-01 13:00:00 UTC' start_at
= '2017-01-01 08:00:00 EST-05' Railsアプリ Time.zoneさえ設定しておけば、後はActiveRecordが DBの値の取得/保存時に自動変換してくれる Railsアプリ DB start_at = '2017-01-01 13:00:00 UTC' start_at = '2017-01-01 22:00:00 JST_09' Time.zone=“Asia/Tokyo”
便利!!
次は右側 DB Active Record Railsアプリ confg.active_record. default_timezone confg.time_zone アプリ内タイムゾーン DBに保存するタイムゾーン
confg.active_record. default_timezone • DBに保存する値に適用するtimezone – 日時はTime.zoneからこのタイムゾーンに変換され る • 逆に「DB内の日時をすべてこのタイムゾーン で解釈する」という基準タイムゾーンでもある
設定可能な値 • :utc – すべての日時をUTCとして扱う • :local – すべての日時をWebサーバ(Railsが動くサーバ)の システムのタイムゾーンとして扱う
• 実はActiveRecordは、DBサーバやDB自体の タイムゾーン設定を全く考慮しない – ※ MySQLの場合
:utc設定の場合 Railsアプリ DB time_zone= UTC start_at = '2017-01-01 13:00:00 UTC'
DB time_zone= EST start_at = '2017-01-01 13:00:00 EST' DB time_zone= JST start_at = '2017-01-01 13:00:00 JST' ActiveRecord start_atを リクエスト Time.zoneに 合わせて 自動変換された 値を返す DBがどの場合でも '2017-01-01 13:00:00 UTC' として解釈
:local設定の場合 • :utcの場合と同様、接続先DBのタイムゾーン 設定に関わらず、すべての日時をWebサーバ のシステムのタイムゾーンで保存されたもの と解釈する ※ Webサーバ複数台構成の場合は、すべての サーバのタイムゾーンを揃えることに特に注 意しましょう
:utcと:localの使い分け • 国際化対応する場合はとりあえず標準ぽい :utcがお薦め – 複数国のそれぞれシステムタイムゾーンが違うサーバに置いて も大丈夫 – Railsのdefaultでもある •
国際化不要で、DBの生データの見易さ重視なら :local – 手動更新作業や他のアプリがDBを修正する時に、タイムゾーン 変換不要な方がミスが起こりにくい • 究極的にはケースバイケース • (個人的には、DBのタイムゾーン設定とRailsが保存する タイムゾーンは極力同じにしておきたい) – RailsはDBの設定は見ないけど、何かあった時に混乱の元
END