Slide 1

Slide 1 text

How people build software ! " Dealing with time and dates A challenge for developers

Slide 2

Slide 2 text

How people build software ! Jakob Class Application Engineer
 
 ! github.com/mikrobi 2 $

Slide 3

Slide 3 text

How people build software ! ! GitHubbers around the clock 3

Slide 4

Slide 4 text

How people build software ! ! 4

Slide 5

Slide 5 text

How people build software ! 2015-12-31 23:59:59
 + 1 sec
 =
 2016-01-01 00:00:00 ! Is this correct? 5

Slide 6

Slide 6 text

How people build software ! 2016-12-31 23:59:59
 + 1 sec
 =
 2017-01-01 00:00:00 ! Is this correct? 6

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

How people build software ! 2017-03-26 01:59:59
 + 1 sec
 =
 2017-03-26 02:00:00 ! Is this correct? 8

Slide 9

Slide 9 text

How people build software ! 2017-03-26 01:59:59
 + 1 sec
 =
 2017-03-26 03:00:00 ! Daylight saving time → 9

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

How people build software ! ! Why time calculation is so complex 11 • Earth’s rotation is not constant • Leap seconds • Leap years • Daylight saving time

Slide 12

Slide 12 text

How people build software ! ! But… 12 Most modern programming languages offer tools for time calculation.

Slide 13

Slide 13 text

How people build software ! ! Why time calculation is still complex (even in Ruby) 13 > ActiveSupport::TimeZone.all.map(&:utc_offset).uniq.size => 32

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

How people build software ! ! Time and time zones with Rails 15 Rails handles time zones pretty well… …when you use the right methods!

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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”)

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

How people build software ! ! Useful tools 20 • https://github.com/travisjeffery/timecop • http://everytimezone.com

Slide 21

Slide 21 text

How people build software ! " QUESTIONS?