$30 off During Our Annual Pro Sale. View Details »

Dates and Times by Example (AltConf 2018)

Dates and Times by Example (AltConf 2018)

Have you ever had a bug that dealt with dates and times? Dealing with time zones, Daylight Saving Time, and the like can make the hair on the back of even the most experienced developer's neck stand up. We all know how to tell time and how calendars work, and yet these bugs are terrifying. Why? Using Swift as a lens to walk through common problems, we’ll explore writing date and time code that’s correct and easy to understand. You’ll leave with an approach to solving calendrical calculations without fear, new tools for handling these problems in your code, and a huge appreciation for the people behind the libraries that make this possible.

Jeff Kelley

June 07, 2018
Tweet

More Decks by Jeff Kelley

Other Decks in Programming

Transcript

  1. Dates and Times by Example
    Jeff Kelley @SlaunchaMan

    View Slide

  2. About Me
    • iOS Developer for 10 years
    • Recently: web frontend/middleware
    using TypeScript
    • Author of Learn Cocoa Touch for iOS
    and Developing for Apple Watch
    Jeff Kelley @SlaunchaMan

    View Slide

  3. About Me
    • iOS Developer for 10 years
    • Recently: web frontend/middleware
    using TypeScript
    • Author of Learn Cocoa Touch for iOS
    and Developing for Apple Watch
    Jeff Kelley @SlaunchaMan

    View Slide

  4. About Me
    • iOS Developer for 10 years
    • Recently: web frontend/middleware
    using TypeScript
    • Author of Learn Cocoa Touch for iOS
    and Developing for Apple Watch
    Jeff Kelley @SlaunchaMan

    View Slide

  5. I still struggle with date and time issues.
    Jeff Kelley @SlaunchaMan

    View Slide

  6. Previous Talks
    • Dates and Times in iOS, AltConf 2014
    • Advanced Dates and Times in Swift, 360|iDev 2017
    Jeff Kelley @SlaunchaMan

    View Slide

  7. Quick Recap
    • A Date is a moment in time.
    • Stop thinking about Date in terms of time zones
    • When you po a Date, you get something like this:
    2018-06-05 20:26:22 +0000
    Jeff Kelley @SlaunchaMan

    View Slide

  8. Quick Recap
    • A Date is a moment in time.
    • Stop thinking about Date in terms of time zones
    • When you po a Date, you get something like this:
    2018-06-05 20:26:22 +0000
    - timeIntervalSinceReferenceDate :
    549923182.35240304
    Jeff Kelley @SlaunchaMan

    View Slide

  9. Formatters
    • Use them!
    • We have gotten new formatters since NSDateFormatter,
    check them out to see if they meet your needs.
    Jeff Kelley @SlaunchaMan

    View Slide

  10. Demo: Formatting Intervals
    Jeff Kelley @SlaunchaMan

    View Slide

  11. Problems With Solutions
    Jeff Kelley @SlaunchaMan

    View Slide

  12. Problems With Solutions
    This talk will have two main parts: problems with solutions,
    and problems without solutions. The previous example was
    the first case.
    Jeff Kelley @SlaunchaMan

    View Slide

  13. Time Zones and Locations
    Jeff Kelley @SlaunchaMan

    View Slide

  14. View Slide

  15. Demo: Time Zones and Locations
    Jeff Kelley @SlaunchaMan

    View Slide

  16. Demo: Time Zone-Agnostic Notifications
    Jeff Kelley @SlaunchaMan

    View Slide

  17. Problems Without Solutions
    Jeff Kelley @SlaunchaMan

    View Slide

  18. Problems Without Solutions
    What if there's no “right answer?”
    Jeff Kelley @SlaunchaMan

    View Slide

  19. Problems Without Solutions
    It’s 11:55 PM. Your user says, “In fifteen minutes, remind me to
    go to bed.” But there’s a leap second at 11:59:60. Does the
    timer go off at 12:10:00 AM or 12:09:59 AM?
    Jeff Kelley @SlaunchaMan

    View Slide

  20. Problems Without Solutions
    Year-Over-Year Graphs
    Jeff Kelley @SlaunchaMan

    View Slide

  21. Problems Without Solutions
    Year-Over-Year Graphs
    Jeff Kelley @SlaunchaMan

    View Slide

  22. Problems Without Solutions
    Year-Over-Year Graphs
    Jeff Kelley @SlaunchaMan

    View Slide

  23. Problems Without Solutions
    Month-over-Month Graphs
    Jeff Kelley @SlaunchaMan

    View Slide

  24. Problems Without Solutions
    Week-over-Week Graphs
    Jeff Kelley @SlaunchaMan

    View Slide

  25. How We Talk About Dates
    “We entered the timestamps in the location’s time zone.”
    TIMESTAMP 2018-06-07 14:45
    Jeff Kelley @SlaunchaMan

    View Slide

  26. How We Talk About Dates
    TIMESTAMP 2018-06-07 14:45 AT TIMEZONE 'PDT'
    TIMESTAMP 2018-06-07 14:45 AT TIMEZONE 'America/
    Los_Angeles'
    Jeff Kelley @SlaunchaMan

    View Slide

  27. Demo: Date Ranges with DateInterval
    Jeff Kelley @SlaunchaMan

    View Slide

  28. More Resources
    • Date and Time Programming Guide, Apple
    • YourCalendricalFallacyIs.com, Dave DeLong
    • Chronology, Dave DeLong
    Jeff Kelley @SlaunchaMan

    View Slide

  29. Thank You!
    Jeff Kelley, [email protected]
    @SlaunchaMan on Twitter

    View Slide