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

More Related Content

PDF
Timezone Mess
PPTX
Deploy via CircleCI
PDF
Introducing CandyCane @第2回品川Redmine
PDF
Al mininum 47redmine-2nd
PDF
数千人が利用する楽天Redmineの過去と未来 - The past and future of Rakuten Redmine that is the...
PPTX
545人のインフラを支えたNOCチーム!
PDF
CEDEC-Net 2015 テクニカルレビュー
PDF
暗号技術入門 秘密の国のアリス 総集編
Timezone Mess
Deploy via CircleCI
Introducing CandyCane @第2回品川Redmine
Al mininum 47redmine-2nd
数千人が利用する楽天Redmineの過去と未来 - The past and future of Rakuten Redmine that is the...
545人のインフラを支えたNOCチーム!
CEDEC-Net 2015 テクニカルレビュー
暗号技術入門 秘密の国のアリス 総集編

Viewers also liked (11)

PDF
うちのRedmineの使い方
PDF
Redmineチケットによるプロジェクト火消し戦略!
PDF
FM音源をいじれるWebサービスを作った
PPTX
初めてのSpark streaming 〜kafka+sparkstreamingの紹介〜
PDF
数値解析と物理学
PDF
タイ文字と若干情報科学[修正版]
PDF
フォントの選び方・使い方
PDF
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
PPTX
Facebook Powerpoint
PDF
Design Your Career 2018
うちのRedmineの使い方
Redmineチケットによるプロジェクト火消し戦略!
FM音源をいじれるWebサービスを作った
初めてのSpark streaming 〜kafka+sparkstreamingの紹介〜
数値解析と物理学
タイ文字と若干情報科学[修正版]
フォントの選び方・使い方
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Facebook Powerpoint
Design Your Career 2018
Ad

More from Yuki Matsukura (9)

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

Recently uploaded (20)

PDF
Testing WebRTC applications at scale.pdf
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PDF
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PPT
tcp ip networks nd ip layering assotred slides
PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
PPTX
international classification of diseases ICD-10 review PPT.pptx
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
PPTX
Digital Literacy And Online Safety on internet
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PDF
Decoding a Decade: 10 Years of Applied CTI Discipline
PPTX
Internet___Basics___Styled_ presentation
PPTX
artificial intelligence overview of it and more
PDF
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
Testing WebRTC applications at scale.pdf
PptxGenJS_Demo_Chart_20250317130215833.pptx
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
Unit-1 introduction to cyber security discuss about how to secure a system
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
The New Creative Director: How AI Tools for Social Media Content Creation Are...
Slides PDF The World Game (s) Eco Economic Epochs.pdf
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
tcp ip networks nd ip layering assotred slides
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
international classification of diseases ICD-10 review PPT.pptx
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
Digital Literacy And Online Safety on internet
introduction about ICD -10 & ICD-11 ppt.pptx
Job_Card_System_Styled_lorem_ipsum_.pptx
Decoding a Decade: 10 Years of Applied CTI Discipline
Internet___Basics___Styled_ presentation
artificial intelligence overview of it and more
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf

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に明示的に変換してから行ったほうがよい。