SlideShare a Scribd company logo
Time zone in Rails
Shinjuku.rb #34
Yuki Matsukura
Today’s topic
• gem “zonebie”
• Understanding time zone issues
• Case study
https://spike.cc/
• DB
– UTC
• Program
– UTC
• View
– Multi time zone.
Software stack
1. config.active_record.default_timezone
2. config.time_zone
3. View
Approach 1
Approach 2
(Seems standard)
CASE STUDY
> require 'timecop’
> Timecop.freeze(Time.utc(2015, 12, 31, 23, 0, 0))
> Time.zone = 'Tokyo’
> Time.zone.now.utc.month == Date.today.month
=> false
Both are correct, but different
validates_inclusion_of :birth_day, in: 130.years.ago.utc..20.years.ago.utc
validates_inclusion_of :birth_day, in: 130.years.ago..20.years.ago
Special time zones, > 12
Be careful with birthday handling.
• 1982-06-04 +13:00
– Physically born on 6/5
– Logically born on 6/4
Meaning is different.
> 60.days.since(Time.utc(2014, 2, 7, 23, 59,
59).in_time_zone('Wellington')).utc
> 60.days.since(Time.utc(2014, 2, 7, 23, 59,
59).in_time_zone('Wellington').utc)
Need to care about time zone
conversion
> 60.days.since(Time.utc(2014, 2, 7, 23, 59,
59).in_time_zone('Wellington')).utc
=> 2014-04-09 00:59:59 UTC
> 60.days.since(Time.utc(2014, 2, 7, 23, 59,
59).in_time_zone('Wellington').utc)
=> 2014-04-08 23:59:59 UTC
Example
• Bad
– Time.now
– Time.zone.today
– Time.parse('2013/10/01’)
– Time.utc(2013,10,1)
• Bad (Might work some cases)
– Time.zone.now
– 1.hour.ago
– Time.zone.parse('2013/10/01’)
• Instance’s timezone depends on context.
– DateTime.now.utc
• To avoid using DateTime class
• Good
– Time.zone.now.utc
– 1.year.since.utc
– 3.months.ago.utc.to_date
• (Time.zone.now.utc.to_date - 3.months)
– Time.utc(2007, 2, 10, 20, 30, 45).in_time_zone('UTC')
– Time.zone.parse('2016-02-10T00:00:00+09:00’)
CODING RULE
Programming issue
• 1. Ruby and Rails time zone
– http://qiita.com/joker1007/items/2c277cca5bd50
e4cce5e
• 2. Coding rule
Time zone coding rule
• Convert time zone to UTC
• Avoid to convert to numeric.
– xxx.year, xxx.month
Ad

More Related Content

Viewers also liked (11)

Altseed
AltseedAltseed
Altseed
京大 マイコンクラブ
 
うちのRedmineの使い方
うちのRedmineの使い方うちのRedmineの使い方
うちのRedmineの使い方
Tomohisa Kusukawa
 
Redmineチケットによるプロジェクト火消し戦略!
Redmineチケットによるプロジェクト火消し戦略!Redmineチケットによるプロジェクト火消し戦略!
Redmineチケットによるプロジェクト火消し戦略!
TrinityT _
 
FM音源をいじれるWebサービスを作った
FM音源をいじれるWebサービスを作ったFM音源をいじれるWebサービスを作った
FM音源をいじれるWebサービスを作った
CHY72
 
初めてのSpark streaming 〜kafka+sparkstreamingの紹介〜
初めてのSpark streaming 〜kafka+sparkstreamingの紹介〜初めてのSpark streaming 〜kafka+sparkstreamingの紹介〜
初めてのSpark streaming 〜kafka+sparkstreamingの紹介〜
Tanaka Yuichi
 
数値解析と物理学
数値解析と物理学数値解析と物理学
数値解析と物理学
すずしめ
 
タイ文字と若干情報科学[修正版]
タイ文字と若干情報科学[修正版]タイ文字と若干情報科学[修正版]
タイ文字と若干情報科学[修正版]
. きぷ
 
フォントの選び方・使い方
フォントの選び方・使い方フォントの選び方・使い方
フォントの選び方・使い方
k maztani
 
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
kiki utagawa
 
Facebook Powerpoint
Facebook PowerpointFacebook Powerpoint
Facebook Powerpoint
myra14
 
Design Your Career 2018
Design Your Career 2018Design Your Career 2018
Design Your Career 2018
Slides That Rock
 
うちのRedmineの使い方
うちのRedmineの使い方うちのRedmineの使い方
うちのRedmineの使い方
Tomohisa Kusukawa
 
Redmineチケットによるプロジェクト火消し戦略!
Redmineチケットによるプロジェクト火消し戦略!Redmineチケットによるプロジェクト火消し戦略!
Redmineチケットによるプロジェクト火消し戦略!
TrinityT _
 
FM音源をいじれるWebサービスを作った
FM音源をいじれるWebサービスを作ったFM音源をいじれるWebサービスを作った
FM音源をいじれるWebサービスを作った
CHY72
 
初めてのSpark streaming 〜kafka+sparkstreamingの紹介〜
初めてのSpark streaming 〜kafka+sparkstreamingの紹介〜初めてのSpark streaming 〜kafka+sparkstreamingの紹介〜
初めてのSpark streaming 〜kafka+sparkstreamingの紹介〜
Tanaka Yuichi
 
数値解析と物理学
数値解析と物理学数値解析と物理学
数値解析と物理学
すずしめ
 
タイ文字と若干情報科学[修正版]
タイ文字と若干情報科学[修正版]タイ文字と若干情報科学[修正版]
タイ文字と若干情報科学[修正版]
. きぷ
 
フォントの選び方・使い方
フォントの選び方・使い方フォントの選び方・使い方
フォントの選び方・使い方
k maztani
 
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
kiki utagawa
 
Facebook Powerpoint
Facebook PowerpointFacebook Powerpoint
Facebook Powerpoint
myra14
 

More from Yuki Matsukura (9)

IRONMAN World Championshipを旅行としてもオススメする理由
IRONMAN World Championshipを旅行としてもオススメする理由IRONMAN World Championshipを旅行としてもオススメする理由
IRONMAN World Championshipを旅行としてもオススメする理由
Yuki Matsukura
 
20170823 shinjuku rb_hanami
20170823 shinjuku rb_hanami20170823 shinjuku rb_hanami
20170823 shinjuku rb_hanami
Yuki Matsukura
 
20171005 webrtc
20171005 webrtc20171005 webrtc
20171005 webrtc
Yuki Matsukura
 
LINE Bot ops
LINE Bot opsLINE Bot ops
LINE Bot ops
Yuki Matsukura
 
LINE Botでテラレンの受付を自動化してみた
LINE Botでテラレンの受付を自動化してみたLINE Botでテラレンの受付を自動化してみた
LINE Botでテラレンの受付を自動化してみた
Yuki Matsukura
 
Payment Service (Overview and Payment Gateways)
Payment Service (Overview and Payment Gateways)Payment Service (Overview and Payment Gateways)
Payment Service (Overview and Payment Gateways)
Yuki Matsukura
 
Zulu trade signal provider observation
Zulu trade signal provider observationZulu trade signal provider observation
Zulu trade signal provider observation
Yuki Matsukura
 
Ethna PSR-0 support and Composer support.
Ethna PSR-0 support and Composer support.Ethna PSR-0 support and Composer support.
Ethna PSR-0 support and Composer support.
Yuki Matsukura
 
tmux and tmux-powerline
tmux and tmux-powerlinetmux and tmux-powerline
tmux and tmux-powerline
Yuki Matsukura
 
IRONMAN World Championshipを旅行としてもオススメする理由
IRONMAN World Championshipを旅行としてもオススメする理由IRONMAN World Championshipを旅行としてもオススメする理由
IRONMAN World Championshipを旅行としてもオススメする理由
Yuki Matsukura
 
20170823 shinjuku rb_hanami
20170823 shinjuku rb_hanami20170823 shinjuku rb_hanami
20170823 shinjuku rb_hanami
Yuki Matsukura
 
LINE Botでテラレンの受付を自動化してみた
LINE Botでテラレンの受付を自動化してみたLINE Botでテラレンの受付を自動化してみた
LINE Botでテラレンの受付を自動化してみた
Yuki Matsukura
 
Payment Service (Overview and Payment Gateways)
Payment Service (Overview and Payment Gateways)Payment Service (Overview and Payment Gateways)
Payment Service (Overview and Payment Gateways)
Yuki Matsukura
 
Zulu trade signal provider observation
Zulu trade signal provider observationZulu trade signal provider observation
Zulu trade signal provider observation
Yuki Matsukura
 
Ethna PSR-0 support and Composer support.
Ethna PSR-0 support and Composer support.Ethna PSR-0 support and Composer support.
Ethna PSR-0 support and Composer support.
Yuki Matsukura
 
tmux and tmux-powerline
tmux and tmux-powerlinetmux and tmux-powerline
tmux and tmux-powerline
Yuki Matsukura
 
Ad

Recently uploaded (20)

How to Switch Hosting Providers in Vancouver Without Any Downtime
How to Switch Hosting Providers in Vancouver Without Any DowntimeHow to Switch Hosting Providers in Vancouver Without Any Downtime
How to Switch Hosting Providers in Vancouver Without Any Downtime
steve198109
 
Determining Glass is mechanical textile
Determining  Glass is mechanical textileDetermining  Glass is mechanical textile
Determining Glass is mechanical textile
Azizul Hakim
 
White and Red Clean Car Business Pitch Presentation.pptx
White and Red Clean Car Business Pitch Presentation.pptxWhite and Red Clean Car Business Pitch Presentation.pptx
White and Red Clean Car Business Pitch Presentation.pptx
canumatown
 
highend-srxseries-services-gateways-customer-presentation.pptx
highend-srxseries-services-gateways-customer-presentation.pptxhighend-srxseries-services-gateways-customer-presentation.pptx
highend-srxseries-services-gateways-customer-presentation.pptx
elhadjcheikhdiop
 
final project for icpna b08 if someone want.pptx
final project for icpna b08 if someone want.pptxfinal project for icpna b08 if someone want.pptx
final project for icpna b08 if someone want.pptx
ESTEFANOANDREYGARCIA
 
Reliable Vancouver Web Hosting with Local Servers & 24/7 Support
Reliable Vancouver Web Hosting with Local Servers & 24/7 SupportReliable Vancouver Web Hosting with Local Servers & 24/7 Support
Reliable Vancouver Web Hosting with Local Servers & 24/7 Support
steve198109
 
Understanding the Tor Network and Exploring the Deep Web
Understanding the Tor Network and Exploring the Deep WebUnderstanding the Tor Network and Exploring the Deep Web
Understanding the Tor Network and Exploring the Deep Web
nabilajabin35
 
Top Vancouver Green Business Ideas for 2025 Powered by 4GoodHosting
Top Vancouver Green Business Ideas for 2025 Powered by 4GoodHostingTop Vancouver Green Business Ideas for 2025 Powered by 4GoodHosting
Top Vancouver Green Business Ideas for 2025 Powered by 4GoodHosting
steve198109
 
(Hosting PHising Sites) for Cryptography and network security
(Hosting PHising Sites) for Cryptography and network security(Hosting PHising Sites) for Cryptography and network security
(Hosting PHising Sites) for Cryptography and network security
aluacharya169
 
Computers Networks Computers Networks Computers Networks
Computers Networks Computers Networks Computers NetworksComputers Networks Computers Networks Computers Networks
Computers Networks Computers Networks Computers Networks
Tito208863
 
APNIC -Policy Development Process, presented at Local APIGA Taiwan 2025
APNIC -Policy Development Process, presented at Local APIGA Taiwan 2025APNIC -Policy Development Process, presented at Local APIGA Taiwan 2025
APNIC -Policy Development Process, presented at Local APIGA Taiwan 2025
APNIC
 
Mobile database for your company telemarketing or sms marketing campaigns. Fr...
Mobile database for your company telemarketing or sms marketing campaigns. Fr...Mobile database for your company telemarketing or sms marketing campaigns. Fr...
Mobile database for your company telemarketing or sms marketing campaigns. Fr...
DataProvider1
 
Smart Mobile App Pitch Deck丨AI Travel App Presentation Template
Smart Mobile App Pitch Deck丨AI Travel App Presentation TemplateSmart Mobile App Pitch Deck丨AI Travel App Presentation Template
Smart Mobile App Pitch Deck丨AI Travel App Presentation Template
yojeari421237
 
IT Services Workflow From Request to Resolution
IT Services Workflow From Request to ResolutionIT Services Workflow From Request to Resolution
IT Services Workflow From Request to Resolution
mzmziiskd
 
Perguntas dos animais - Slides ilustrados de múltipla escolha
Perguntas dos animais - Slides ilustrados de múltipla escolhaPerguntas dos animais - Slides ilustrados de múltipla escolha
Perguntas dos animais - Slides ilustrados de múltipla escolha
socaslev
 
What's going on with IPv6? presented by Geoff Huston
What's going on with IPv6? presented by Geoff HustonWhat's going on with IPv6? presented by Geoff Huston
What's going on with IPv6? presented by Geoff Huston
APNIC
 
Virtualization Trends Streamlining Operations in Telecom with David Bernard ...
Virtualization Trends  Streamlining Operations in Telecom with David Bernard ...Virtualization Trends  Streamlining Operations in Telecom with David Bernard ...
Virtualization Trends Streamlining Operations in Telecom with David Bernard ...
David Bernard Ezell
 
Best web hosting Vancouver 2025 for you business
Best web hosting Vancouver 2025 for you businessBest web hosting Vancouver 2025 for you business
Best web hosting Vancouver 2025 for you business
steve198109
 
data science data stoger Presentation1.pptx
data science data stoger Presentation1.pptxdata science data stoger Presentation1.pptx
data science data stoger Presentation1.pptx
sandeepsherkhane830
 
5-Proses-proses Akuisisi Citra Digital.pptx
5-Proses-proses Akuisisi Citra Digital.pptx5-Proses-proses Akuisisi Citra Digital.pptx
5-Proses-proses Akuisisi Citra Digital.pptx
andani26
 
How to Switch Hosting Providers in Vancouver Without Any Downtime
How to Switch Hosting Providers in Vancouver Without Any DowntimeHow to Switch Hosting Providers in Vancouver Without Any Downtime
How to Switch Hosting Providers in Vancouver Without Any Downtime
steve198109
 
Determining Glass is mechanical textile
Determining  Glass is mechanical textileDetermining  Glass is mechanical textile
Determining Glass is mechanical textile
Azizul Hakim
 
White and Red Clean Car Business Pitch Presentation.pptx
White and Red Clean Car Business Pitch Presentation.pptxWhite and Red Clean Car Business Pitch Presentation.pptx
White and Red Clean Car Business Pitch Presentation.pptx
canumatown
 
highend-srxseries-services-gateways-customer-presentation.pptx
highend-srxseries-services-gateways-customer-presentation.pptxhighend-srxseries-services-gateways-customer-presentation.pptx
highend-srxseries-services-gateways-customer-presentation.pptx
elhadjcheikhdiop
 
final project for icpna b08 if someone want.pptx
final project for icpna b08 if someone want.pptxfinal project for icpna b08 if someone want.pptx
final project for icpna b08 if someone want.pptx
ESTEFANOANDREYGARCIA
 
Reliable Vancouver Web Hosting with Local Servers & 24/7 Support
Reliable Vancouver Web Hosting with Local Servers & 24/7 SupportReliable Vancouver Web Hosting with Local Servers & 24/7 Support
Reliable Vancouver Web Hosting with Local Servers & 24/7 Support
steve198109
 
Understanding the Tor Network and Exploring the Deep Web
Understanding the Tor Network and Exploring the Deep WebUnderstanding the Tor Network and Exploring the Deep Web
Understanding the Tor Network and Exploring the Deep Web
nabilajabin35
 
Top Vancouver Green Business Ideas for 2025 Powered by 4GoodHosting
Top Vancouver Green Business Ideas for 2025 Powered by 4GoodHostingTop Vancouver Green Business Ideas for 2025 Powered by 4GoodHosting
Top Vancouver Green Business Ideas for 2025 Powered by 4GoodHosting
steve198109
 
(Hosting PHising Sites) for Cryptography and network security
(Hosting PHising Sites) for Cryptography and network security(Hosting PHising Sites) for Cryptography and network security
(Hosting PHising Sites) for Cryptography and network security
aluacharya169
 
Computers Networks Computers Networks Computers Networks
Computers Networks Computers Networks Computers NetworksComputers Networks Computers Networks Computers Networks
Computers Networks Computers Networks Computers Networks
Tito208863
 
APNIC -Policy Development Process, presented at Local APIGA Taiwan 2025
APNIC -Policy Development Process, presented at Local APIGA Taiwan 2025APNIC -Policy Development Process, presented at Local APIGA Taiwan 2025
APNIC -Policy Development Process, presented at Local APIGA Taiwan 2025
APNIC
 
Mobile database for your company telemarketing or sms marketing campaigns. Fr...
Mobile database for your company telemarketing or sms marketing campaigns. Fr...Mobile database for your company telemarketing or sms marketing campaigns. Fr...
Mobile database for your company telemarketing or sms marketing campaigns. Fr...
DataProvider1
 
Smart Mobile App Pitch Deck丨AI Travel App Presentation Template
Smart Mobile App Pitch Deck丨AI Travel App Presentation TemplateSmart Mobile App Pitch Deck丨AI Travel App Presentation Template
Smart Mobile App Pitch Deck丨AI Travel App Presentation Template
yojeari421237
 
IT Services Workflow From Request to Resolution
IT Services Workflow From Request to ResolutionIT Services Workflow From Request to Resolution
IT Services Workflow From Request to Resolution
mzmziiskd
 
Perguntas dos animais - Slides ilustrados de múltipla escolha
Perguntas dos animais - Slides ilustrados de múltipla escolhaPerguntas dos animais - Slides ilustrados de múltipla escolha
Perguntas dos animais - Slides ilustrados de múltipla escolha
socaslev
 
What's going on with IPv6? presented by Geoff Huston
What's going on with IPv6? presented by Geoff HustonWhat's going on with IPv6? presented by Geoff Huston
What's going on with IPv6? presented by Geoff Huston
APNIC
 
Virtualization Trends Streamlining Operations in Telecom with David Bernard ...
Virtualization Trends  Streamlining Operations in Telecom with David Bernard ...Virtualization Trends  Streamlining Operations in Telecom with David Bernard ...
Virtualization Trends Streamlining Operations in Telecom with David Bernard ...
David Bernard Ezell
 
Best web hosting Vancouver 2025 for you business
Best web hosting Vancouver 2025 for you businessBest web hosting Vancouver 2025 for you business
Best web hosting Vancouver 2025 for you business
steve198109
 
data science data stoger Presentation1.pptx
data science data stoger Presentation1.pptxdata science data stoger Presentation1.pptx
data science data stoger Presentation1.pptx
sandeepsherkhane830
 
5-Proses-proses Akuisisi Citra Digital.pptx
5-Proses-proses Akuisisi Citra Digital.pptx5-Proses-proses Akuisisi Citra Digital.pptx
5-Proses-proses Akuisisi Citra Digital.pptx
andani26
 
Ad

Time zone in Rails

  • 1. Time zone in Rails Shinjuku.rb #34 Yuki Matsukura
  • 2. Today’s topic • gem “zonebie” • Understanding time zone issues • Case study
  • 3. https://spike.cc/ • DB – UTC • Program – UTC • View – Multi time zone.
  • 8. > require 'timecop’ > Timecop.freeze(Time.utc(2015, 12, 31, 23, 0, 0)) > Time.zone = 'Tokyo’ > Time.zone.now.utc.month == Date.today.month => false
  • 9. Both are correct, but different validates_inclusion_of :birth_day, in: 130.years.ago.utc..20.years.ago.utc validates_inclusion_of :birth_day, in: 130.years.ago..20.years.ago
  • 11. Be careful with birthday handling. • 1982-06-04 +13:00 – Physically born on 6/5 – Logically born on 6/4
  • 12. Meaning is different. > 60.days.since(Time.utc(2014, 2, 7, 23, 59, 59).in_time_zone('Wellington')).utc > 60.days.since(Time.utc(2014, 2, 7, 23, 59, 59).in_time_zone('Wellington').utc)
  • 13. Need to care about time zone conversion > 60.days.since(Time.utc(2014, 2, 7, 23, 59, 59).in_time_zone('Wellington')).utc => 2014-04-09 00:59:59 UTC > 60.days.since(Time.utc(2014, 2, 7, 23, 59, 59).in_time_zone('Wellington').utc) => 2014-04-08 23:59:59 UTC
  • 14. Example • Bad – Time.now – Time.zone.today – Time.parse('2013/10/01’) – Time.utc(2013,10,1) • Bad (Might work some cases) – Time.zone.now – 1.hour.ago – Time.zone.parse('2013/10/01’) • Instance’s timezone depends on context. – DateTime.now.utc • To avoid using DateTime class • Good – Time.zone.now.utc – 1.year.since.utc – 3.months.ago.utc.to_date • (Time.zone.now.utc.to_date - 3.months) – Time.utc(2007, 2, 10, 20, 30, 45).in_time_zone('UTC') – Time.zone.parse('2016-02-10T00:00:00+09:00’)
  • 16. Programming issue • 1. Ruby and Rails time zone – http://qiita.com/joker1007/items/2c277cca5bd50 e4cce5e • 2. Coding rule
  • 17. Time zone coding rule • Convert time zone to UTC • Avoid to convert to numeric. – xxx.year, xxx.month

Editor's Notes

  • #7: In this case, - batch: config.time_zone - logic triggered by a user results user’s timezone.
  • #10: もし、Date.new
  • #13: DSTがあるTimezoneで日付計算をすると、予期しない計算が行われる。 日付の演算をするときには、UTCに明示的に変換してから行ったほうがよい。
  • #14: DSTがあるTimezoneで日付計算をすると、予期しない計算が行われる。 日付の演算をするときには、UTCに明示的に変換してから行ったほうがよい。