Time zone in Rails

1,135 views

Published on

Sharing knowledge handling time zone in Ruby on Rails.

Published in: Internet
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,135
On SlideShare
0
From Embeds
0
Number of Embeds
185
Actions
Shares
0
Downloads
1
Comments
0
Likes
1
Embeds 0
No embeds

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

    1. 1. Time zone in Rails Shinjuku.rb #34 Yuki Matsukura
    2. 2. Today’s topic • gem “zonebie” • Understanding time zone issues • Case study
    3. 3. https://spike.cc/ • DB – UTC • Program – UTC • View – Multi time zone.
    4. 4. Software stack 1. config.active_record.default_timezone 2. config.time_zone 3. View
    5. 5. Approach 1
    6. 6. Approach 2 (Seems standard)
    7. 7. CASE STUDY
    8. 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. 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
    10. 10. Special time zones, > 12
    11. 11. Be careful with birthday handling. • 1982-06-04 +13:00 – Physically born on 6/5 – Logically born on 6/4
    12. 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. 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. 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’)
    15. 15. CODING RULE
    16. 16. Programming issue • 1. Ruby and Rails time zone – http://qiita.com/joker1007/items/2c277cca5bd50 e4cce5e • 2. Coding rule
    17. 17. Time zone coding rule • Convert time zone to UTC • Avoid to convert to numeric. – xxx.year, xxx.month

    ×