Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Railsのtimezone設定(Rails4系の場合)
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
publichtml
June 16, 2016
Programming
100
0
Share
Railsのtimezone設定(Rails4系の場合)
publichtml
June 16, 2016
More Decks by publichtml
See All by publichtml
初めまして、株式会社万葉です!! @RailsGirlsNagoya4th /everyleaf-on-railsgirlsnagoya4th
publichtml
0
83
ダムで学ぶRailsのRoutes入門
publichtml
2
330
Let's join RailsGirlsTokyo, More!
publichtml
0
95
Other Decks in Programming
See All in Programming
Coding as Prompting Since 2025
ragingwind
0
620
Mastering Event Sourcing: Your Parents Holidayed in Yugoslavia
super_marek
0
130
脱 雰囲気実装!AgentCoreを良い感じにWEBアプリケーションに組み込むために
takuyay0ne
3
420
Rethinking API Platform Filters
vinceamstoutz
0
4.8k
AIコードレビューの導入・運用と AI駆動開発における「AI4QA」の取り組みについて
hagevvashi
0
580
最初からAWS CDKで技術検証してもいいんじゃない?
akihisaikeda
4
180
Symfonyの特性(設計思想)を手軽に活かす特性(trait)
ickx
0
110
Symfony + NelmioApiDocBundle を使った スキーマ駆動開発 / Schema Driven Development with NelmioApiDocBundle
okashoi
0
250
テレメトリーシグナルが導くパフォーマンス最適化 / Performance Optimization Driven by Telemetry Signals
seike460
PRO
2
200
20260313 - Grafana & Friends Taipei #1 - Kubernetes v1.36 的開發雜記:那些困在 Alpha 加護病房太久的 Metrics
tico88612
0
240
年間50登壇、単著出版、雑誌寄稿、Podcast出演、YouTube、CM、カンファレンス主催……全部やってみたので面白さ等を比較してみよう / I’ve tried them all, so let’s compare how interesting they are.
nrslib
4
610
AI活用のコスパを最大化する方法
ochtum
0
360
Featured
See All Featured
Git: the NoSQL Database
bkeepers
PRO
432
67k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
160
Between Models and Reality
mayunak
2
250
Skip the Path - Find Your Career Trail
mkilby
1
93
Become a Pro
speakerdeck
PRO
31
5.9k
It's Worth the Effort
3n
188
29k
Designing Powerful Visuals for Engaging Learning
tmiket
1
320
Side Projects
sachag
455
43k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
94
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.2k
Optimizing for Happiness
mojombo
378
71k
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