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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
93
Other Decks in Programming
See All in Programming
そのAIレビュー、レビューしてますか? / Are you reviewing those AI reviews?
rkaga
6
4.6k
CSC307 Lecture 03
javiergs
PRO
1
490
Lambda のコードストレージ容量に気をつけましょう
tattwan718
0
140
要求定義・仕様記述・設計・検証の手引き - 理論から学ぶ明確で統一された成果物定義
orgachem
PRO
1
190
Grafana:建立系統全知視角的捷徑
blueswen
0
330
2026年 エンジニアリング自己学習法
yumechi
0
140
24時間止められないシステムを守る-医療ITにおけるランサムウェア対策の実際
koukimiura
1
120
React 19でつくる「気持ちいいUI」- 楽観的UIのすすめ
himorishige
11
7.5k
Data-Centric Kaggle
isax1015
2
780
AIで開発はどれくらい加速したのか?AIエージェントによるコード生成を、現場の評価と研究開発の評価の両面からdeep diveしてみる
daisuketakeda
1
2.5k
Unicodeどうしてる? PHPから見たUnicode対応と他言語での対応についてのお伺い
youkidearitai
PRO
1
2.6k
CSC307 Lecture 05
javiergs
PRO
0
500
Featured
See All Featured
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Building Applications with DynamoDB
mza
96
6.9k
The Curse of the Amulet
leimatthew05
1
8.7k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
0
2.3k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
83
The Power of CSS Pseudo Elements
geoffreycrofte
80
6.2k
How to Think Like a Performance Engineer
csswizardry
28
2.5k
The Curious Case for Waylosing
cassininazir
0
240
AI: The stuff that nobody shows you
jnunemaker
PRO
2
270
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
170
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
420
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
67
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