Share
Embed
Transcript
21 slides
-
How people build software ! " Dealing with time and
dates A challenge for developers -
How people build software ! Jakob Class Application Engineer
! github.com/mikrobi 2 $ -
How people build software ! 2015-12-31 23:59:59 + 1 sec
= 2016-01-01 00:00:00 ! Is this correct? 5 -
How people build software ! 2016-12-31 23:59:59 + 1 sec
= 2017-01-01 00:00:00 ! Is this correct? 6 -
How people build software ! 2016-12-31 23:59:59 + 1 sec
= 2016-12-31 23:59:60 ! Did you celebrate the end of 2016 too early? 7 -
How people build software ! 2017-03-26 01:59:59 + 1 sec
= 2017-03-26 02:00:00 ! Is this correct? 8 -
How people build software ! 2017-03-26 01:59:59 + 1 sec
= 2017-03-26 03:00:00 ! Daylight saving time → 9 -
How people build software ! ! Daylight saving time is
not a global standard 10 Northern hemisphere summer Southern hemisphere summer Formerly used daylight savings Never used daylight savings Source: https://en.wikipedia.org/wiki/Daylight_saving_time -
How people build software ! ! Why time calculation is
so complex 11 • Earth’s rotation is not constant • Leap seconds • Leap years • Daylight saving time -
How people build software ! ! But… 12 Most modern
programming languages offer tools for time calculation. -
How people build software ! ! Why time calculation is
still complex (even in Ruby) 13 > ActiveSupport::TimeZone.all.map(&:utc_offset).uniq.size => 32 -
How people build software ! ! Challenges of global web
services 14 • Developed and tested in different time zones • Deployed to servers in different time zones • Used by people from different time zones -
How people build software ! ! Time and time zones
with Rails 15 Rails handles time zones pretty well… …when you use the right methods! -
How people build software ! ! Time and time zones
with Rails 16 > Time.zone = “Melbourne" => "Melbourne" > system = Time.now => 2017-01-05 19:30:34 +0100 > system.utc => 2017-01-05 18:30:34 UTC > system.hour => 19 > Time.zone = “Melbourne" => "Melbourne" > local = Time.zone.now => Fri, 06 Jan 2017 05:30:34 AEDT +11:00 > local.utc => 2017-01-05 18:30:34 UTC > local.hour => 5 -
How people build software ! ! Avoid the time zone
trap! ☝ 17 Good ✅ Time.zone.now Time.zone.today Time.zone.local(2017, 1, 5, 19, 30) Time.zone.parse(“2017-01-05 19:30”) 3.minutes.ago 5.days.from_now Dangerous ❌ Time.now Date.today Time.new(2017, 1, 5, 19, 30) Time.parse(“2017-01-05 19:30”) -
How people build software ! ! Be explicit about time
in your tests! 18 Good Timecop.freeze(Time.zone.now) do user = User.create! assert_equal Time.zone.now, user.created_at end Better ✅ Time.use_zone "Australia/Melbourne" do now = Time.zone.parse(“2017-01-05 19:30”) Timecop.freeze(now) do user = User.create! assert_equal Time.zone.now, user.created_at end end -
How people build software ! ! Think ahead! 19 •
Consider edge cases • Today in San Francisco is tomorrow in Melbourne • End of a month: now.day > tomorrow.day • End of a year: now.month > tomorrow.month • There is something like leap seconds • Change your local time and time zone and run time critical tests -
How people build software ! ! Useful tools 20 •
https://github.com/travisjeffery/timecop • http://everytimezone.com