Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Date-ing Game - SwiftCraft, Folkestone, UK,...

The Date-ing Game - SwiftCraft, Folkestone, UK, May 2024

A fun journey through how we take something as simple as a single point in time and find all kinds of new and interesting ways to screw it up royally, especially within the Apple ecosystem.

Ellen Shapiro

May 23, 2024
Tweet

More Decks by Ellen Shapiro

Other Decks in Technology

Transcript

  1. THE DATE-ING GAME SWIFTCRAFT | FOLKESTONE, UK | MAY 2024

    ELLEN SHAPIRO | MASTODON.SOCIAL/@DESIGNATEDNERD | PIXITEAPPS.COM
  2. ! "

  3. let now = Date.now let oneYear = TimeInterval(60 * 60

    * 24 * 365) let oneYearLater = now.addingTimeInterval(oneYear) print("Now: \(now).\nOne year from now: \(oneYearLater)")
  4. let now = Date.now let oneYear = TimeInterval(60 * 60

    * 24 * 365) let oneYearLater = now.addingTimeInterval(oneYear) print("Now: \(now).\nOne year from now: \(oneYearLater)")
  5. !

  6. .timeIntervalSince1970 00:00:00 UTC on 1 January 1970 .timeIntervalSinceReferenceDate 00:00:00 UTC

    on 1 January 2001 .timeIntervalBetween1970AndReferenceDate 978,307,200 seconds
  7. ! !

  8. THE ! DEFINITION A definition of the relationships between calendar

    units and absolute points in time, providing features for calculation and comparison of dates
  9. MY DEFINITION The only way to relate an absolute point

    in time to anything humans will actually be able to understand
  10. ! !

  11. !

  12. !

  13. THE ! DEFINITION Instances of DateFormatter create string representations of

    Date objects, and convert textual representations of dates and times into Date objects.
  14. MY DEFINITION The thing you should use to turn a

    Date into a user-facing string.
  15. let now = Date() let formatter = DateFormatter() formatter.calendar =

    Calendar.autoupdatingCurrent print(formatter.string(from: now))
  16. let now = Date() let formatter = DateFormatter() formatter.calendar =

    Calendar.autoupdatingCurrent print(formatter.string(from: now)) // prints: (nothing!)
  17. let now = Date() let formatter = DateFormatter() formatter.calendar =

    Calendar.autoupdatingCurrent formatter.dateFormat = "MM-dd-yyyy" print(formatter.string(from: now)) // prints: "05-23-2024"
  18. let now = Date() let formatter = DateFormatter() formatter.calendar =

    Calendar.autoupdatingCurrent formatter.dateStyle = .full print(formatter.string(from: now)) // prints: "Thursday, 23 May 2024"
  19. let now = Date() let formatter = DateFormatter() formatter.calendar =

    Calendar.autoupdatingCurrent formatter.dateStyle = .full print(formatter.string(from: now)) // prints: "Thursday, 23 May 2024" formatter.locale = Locale(identifier: "en_US")
  20. let now = Date() let formatter = DateFormatter() formatter.calendar =

    Calendar.autoupdatingCurrent formatter.dateStyle = .full print(formatter.string(from: now)) // prints: "Thursday, 23 May 2024" formatter.locale = Locale(identifier: "en_US") print(formatter.string(from: now)) // prints: "Thursday, May 23 2024"
  21. let now = Date() let aucFormatter = DateFormatter() aucFormatter.calendar =

    Calendar.autoupdatingCurrent print(formatter.string(from: now)) // prints: "Thursday, 23 May 2024" let islamicFormatter = DateFormatter() islamicFormatter.calendar = Calendar(identifier: .islamic)
  22. let now = Date() let aucFormatter = DateFormatter() aucFormatter.calendar =

    Calendar.autoupdatingCurrent print(formatter.string(from: now)) // prints: "Thursday, 23 May 2024" let islamicFormatter = DateFormatter() islamicFormatter.calendar = Calendar(identifier: .islamic) islamicFormatter.dateStyle = .full // prints "Thursday, 15 Dhuʻl-Qiʻdah, 1445 AH"
  23. let now = Date() let aucFormatter = DateFormatter() aucFormatter.calendar =

    Calendar.autoupdatingCurrent print(formatter.string(from: now)) // prints: "Friday, 19 January 2024" let islamicFormatter = DateFormatter() islamicFormatter.calendar = Calendar(identifier: .islamic) islamicFormatter.dateStyle = .full print(islamicFormatter.string(from: now)) // prints "Thursday, 15 Dhuʻl-Qiʻdah, 1445 AH" islamicFormatter.locale = Locale(identifier: "en_US") print(islamicFormatter.string(from: now)) // prints "Thursday, Dhuʻl-Qiʻdah 15, 1445 AH"
  24. let isoFormat = Date.ISO8601FormatStyle( dateSeparator: .dash, dateTimeSeparator: .standard, timeSeparator: .colon,

    timeZoneSeparator: .colon, includingFractionalSeconds: true, timeZone: TimeZone(identifier: "America/New York") ) Date(timeIntervalSinceReferenceDate: 0) .formatted(isoFormat) // "2000-12-31T19:00.000Z"
  25. // TimeInterval let referenceDay = Date(timeIntervalSinceReferenceDate: 0) (referenceDay ..< referenceDay.addingTimeInterval(200))

    .formatted() // "1/1/01, 12:00 – 12:03 AM" via https://goshdarnformatstyle.com/
  26. THE ! DEFINITION A date or time specified in terms

    of units (such as year, month, day, hour, and minute) to be evaluated in a calendar system and time zone.
  27. let talkTime = ISO8601DateFormatter() .date(from: "2024-05-23T14:30:00+01:00")! let timeComponents = gregorianCalendar

    .dateComponents([.hour, .minute], from: talkTime) let hour = timeComponents.hour print(hour) // prints "Optional(14)" let minute = timeComponents.minute print(minute) // prints "Optional(30)"
  28. let talkTime = ISO8601DateFormatter() .date(from: "2024-05-23T14:30:00+01:00")! let timeComponents = gregorianCalendar

    .dateComponents([.hour, .minute], from: talkTime) let hour = timeComponents.hour print(hour) // prints "Optional(14)" let minute = timeComponents.minute print(minute) // prints "Optional(30)" let second = timeComponents.second
  29. let talkTime = ISO8601DateFormatter() .date(from: "2024-05-23T14:30:00+01:00")! let timeComponents = gregorianCalendar

    .dateComponents([.hour, .minute], from: talkTime) let hour = timeComponents.hour print(hour) // prints "Optional(14)" let minute = timeComponents.minute print(minute) // prints "Optional(10)" let second = timeComponents.second print(second) // prints "nil"
  30. let components = DateComponents(year: 2024, month: 4, day: 23) let

    gregorianResult = gregorianCalendar.date(from: components)! print(gregorianFormatter.string(from: gregorianResult))
  31. let components = DateComponents(year: 2024, month: 5, day: 23) let

    gregorianResult = gregorianCalendar.date(from: components)! print(gregorianFormatter.string(from: gregorianResult)) // prints "Thursday, 23 May 2024"
  32. let components = DateComponents(year: 2024, month: 5, day: 23) let

    gregorianResult = gregorianCalendar.date(from: components)! gregorianFormatter.timeStyle = .short print(gregorianFormatter.string(from: gregorianResult))
  33. let components = DateComponents(year: 2024, month: 5, day: 23) let

    gregorianResult = gregorianCalendar.date(from: components)! gregorianFormatter.timeStyle = .short print(gregorianFormatter.string(from: gregorianResult)) // prints "Thursday, 23 May 2024 at 12:00AM"
  34. let components = DateComponents(year: 2024) let gregorianResult = gregorianCalendar.date(from: components)!

    gregorianFormatter.timeStyle = .short print(gregorianFormatter.string(from: gregorianResult)) // prints "Monday, 1 January 2024 at 12:00 AM"
  35. let components = DateComponents(year: 2024, month: 5, day: 23, hour:

    14, minute: 30) let gregorianResult = gregorianCalendar.date(from: components)! gregorianFormatter.timeStyle = .short print(gregorianFormatter.string(from: gregorianResult)) // prints "Thursday, 23 May 2024 at 2:30 PM"
  36. let components = DateComponents(year: 2024, month: 5, day: 23) let

    gregorianResult = gregorianCalendar.date(from: components)! print(gregorianFormatter.string(from: gregorianResult)) // prints "Thursday, 23 May 2024"
  37. let components = DateComponents(year: 2024, month: 5, day: 23) let

    gregorianResult = gregorianCalendar.date(from: components)! print(gregorianFormatter.string(from: gregorianResult)) // prints "Thursday, 23 May 2024" let islamicResult = islamicCalendar.date(from: components)!
  38. let components = DateComponents(year: 2024, month: 5, day: 23) let

    gregorianResult = gregorianCalendar.date(from: components)! print(gregorianFormatter.string(from: gregorianResult)) // prints "Thursday, 23 May 2024" let islamicResult = islamicCalendar.date(from: components)! print(gregorianFormatter.string(from: islamicResult))
  39. let components = DateComponents(year: 2024, month: 5, day: 23) let

    gregorianResult = gregorianCalendar.date(from: components)! print(gregorianFormatter.string(from: gregorianResult)) // prints "Thusrday, 23 May 2024" let islamicResult = islamicCalendar.date(from: components)! print(gregorianFormatter.string(from: islamicResult)) // prints "Thursday, 8 September 2585"
  40. let components = DateComponents(year: 2024, month: 5, day: 23) let

    gregorianResult = gregorianCalendar.date(from: components)! print(gregorianFormatter.string(from: gregorianResult)) // prints "Thursday, 23 May 2024" let islamicResult = islamicCalendar.date(from: components)! print(gregorianFormatter.string(from: islamicResult)) // prints "Thursday, 8 September 2585" print(islamicFormatter.string(from: islamicResult))
  41. let components = DateComponents(year: 2024, month: 5, day: 23) let

    gregorianResult = gregorianCalendar.date(from: components)! print(gregorianFormatter.string(from: gregorianResult)) // prints "Thursday, 23 May 2024" let islamicResult = islamicCalendar.date(from: components)! print(gregorianFormatter.string(from: islamicResult)) // prints "Thursday, 8 September 2585" print(islamicFormatter.string(from: islamicResult)) // prints "Thursday, Jumada I 23, 2024 AH"
  42. let components = DateComponents(year: 2024, month: 5, day: 23) let

    gregorianResult = gregorianCalendar.date(from: components)! print(gregorianFormatter.string(from: gregorianResult)) // prints "Thursday, 23 May 2024" let islamicResult = islamicCalendar.date(from: components)! print(gregorianFormatter.string(from: islamicResult)) // prints "Thursday, 8 September 2585" print(islamicFormatter.string(from: islamicResult)) // prints "Thursday, Jumada I 23, 2024 AH"
  43. let components = DateComponents(year: 2024, month: 5, day: 23) let

    gregorianResult = gregorianCalendar.date(from: components)! print(gregorianFormatter.string(from: gregorianResult)) // prints "Thursday, 23 May 2024 print(islamicFormatter.string(from: gregorianResult)) // prints "Thursday, Dhuʻl-Qiʻdah 15, 1445 AH"
  44. .timeIntervalSince1970 00:00:00 UTC on 1 January 1970 .timeIntervalSinceReferenceDate 00:00:00 UTC

    on 1 January 2001 .timeIntervalBetween1970AndReferenceDate 978,307,200 seconds
  45. UTC

  46. MY DEFINITION The way humans reconcile the rotation of the

    earth and a need to coordinate time across physical and political boundaries
  47. !

  48. ! "

  49. !

  50. MY DEFINITION The way humans reconcile the rotation of the

    earth and a need to coordinate time across physical and political boundaries
  51. MY DEFINITION The way humans reconcile the rotation of the

    earth* and a need to coordinate time across physical and political boundaries * - mostly
  52. OCCASIONAL WEIRDNESS > Leap Year Rollovers > Years With Extra

    Months in Lunisolar Calendars > Daylight Savings Time Rollovers
  53. extension Date { func formattedNextHoursAdjsutingComponents(count: Int) -> [String] { var

    components = Calendar.autoupdatingCurrent .dateComponents([.year, .month, .day, .hour, .minute], from: self) let originalHour = components.hour! var formattedHours = [String]() for hour in 0..<count { components.hour = originalHour + hour let updatedDate = Calendar.autoupdatingCurrent .date(from: components) let formatted = formatter.string(from: updatedDate) formattedHours.append(formatted) } return formattedHours } }
  54. extension Date { func formattedNextHoursAdjsutingComponents(count: Int) -> [String] { var

    components = Calendar.autoupdatingCurrent .dateComponents([.year, .month, .day, .hour, .minute], from: self) let originalHour = components.hour! var formattedHours = [String]() for hour in 0..<count { components.hour = originalHour + hour let updatedDate = Calendar.autoupdatingCurrent .date(from: components) let formatted = formatter.string(from: updatedDate) formattedHours.append(formatted) } return formattedHours } }
  55. func formattedNextHoursAddingComponents(count: Int) -> [String] { var formattedHours = [String]()

    for hour in 0..<count { let updatedDate = Calendar.autoupdatingCurrent .date(byAdding: DateComponents(hour: hour), to: self)! let formatted = formatter.string(from: updatedDate) formattedHours.append(formatted) } return formattedHours }
  56. func formattedNextHoursAddingComponents(count: Int) -> [String] { var formattedHours = [String]()

    for hour in 0..<count { let updatedDate = Calendar.autoupdatingCurrent .date(byAdding: DateComponents(hour: hour), to: self)! let formatted = formatter.string(from: updatedDate) formattedHours.append(formatted) } return formattedHours }
  57. let now = Date() let nowAdjustingComponents = now .formattedNextHoursAdjustingComponents(count: 12)

    .joined(separator: "\n") let nowAddingComponents = now .formattedNextHoursAddingComponents(count: 12) .joined(separator: "\n")
  58. // March 9, 2024 at 7pm Eastern Time let springForward

    = ISO8601DateFormatter().date(from: "2024-03-10T00:00:00Z")! formatter.timeZone = TimeZone(identifier: "America/New_York")! let springForwardAdjustingComponents = springForward .formattedNextHoursAdjustingComponents(count: 12) .joined(separator: "\n") let springForwardAddingComponents = springForward .formattedNextHoursAddingComponents(count: 12) .joined(separator: "\n")
  59. NOW

  60. CALENDAR + DATECOMPONENTS /// Find the closest matching date to

    the given date components Calendar.autoupdatingCurrent.date(from dateComponents: DateComponents) Calendar.autoupdatingCurrent.date(byAdding dateComponents: DateComponents, to date: Date)
  61. March 9 2024 25:00 -> March 10 2024 01:00 ->

    March 10 2024 01:00 March 9 2024 26:00
  62. March 9 2024 25:00 -> March 10 2024 01:00 ->

    March 10 2024 01:00 March 9 2024 26:00 -> March 10 2024 02:00 ->
  63. March 9 2024 25:00 -> March 10 2024 01:00 ->

    March 10 2024 01:00 March 9 2024 26:00 -> March 10 2024 02:00 -> Does not exist, what's the closest match?
  64. March 9 2024 25:00 -> March 10 2024 01:00 ->

    March 10 2024 01:00 March 9 2024 26:00 -> March 10 2024 02:00 -> Does not exist, what's the closest match? -> March 10 2024 03:00
  65. March 9 2024 25:00 -> March 10 2024 01:00 ->

    March 10 2024 01:00 March 9 2024 26:00 -> March 10 2024 02:00 -> Does not exist, what's the closest match? -> March 10 2024 03:00 March 9 2024 27:00
  66. March 9 2024 25:00 -> March 10 2024 01:00 ->

    March 10 2024 01:00 March 9 2024 26:00 -> March 10 2024 02:00 -> Does not exist, what's the closest match? -> March 10 2024 03:00 March 9 2024 27:00 -> March 10 2024 03:00
  67. March 9 2024 25:00 -> March 10 2024 01:00 ->

    March 10 2024 01:00 March 9 2024 26:00 -> March 10 2024 02:00 -> Does not exist, what's the closest match? -> March 10 2024 03:00 March 9 2024 27:00 -> March 10 2024 03:00 -> March 10 2024 03:00
  68. March 9 2024 25:00 -> March 10 2024 01:00 ->

    March 10 2024 01:00 March 9 2024 26:00 -> March 10 2024 02:00 -> Does not exist, what's the closest match? -> March 10 2024 03:00 ! March 9 2024 27:00 -> March 10 2024 03:00 -> March 10 2024 03:00 !
  69. CALENDAR + DATECOMPONENTS /// Find the closest matching date to

    the given date components Calendar.autoupdatingCurrent.date(from dateComponents: DateComponents) Calendar.autoupdatingCurrent.date(byAdding dateComponents: DateComponents, to date: Date)
  70. CALENDAR + DATECOMPONENTS /// Find the closest matching date to

    the given date components Calendar.autoupdatingCurrent.date(from dateComponents: DateComponents) /// Take the passed-in date and add the passed-in components Calendar.autoupdatingCurrent.date(byAdding dateComponents: DateComponents, to date: Date)
  71. March 9 2024 17:00 + 8h -> March 10 2024

    01:00 March 9 2024 17:00 + 9h
  72. March 9 2024 17:00 + 8h -> March 10 2024

    01:00 March 9 2024 17:00 + 9h -> March 10 2024 03:00
  73. March 9 2024 17:00 + 8h -> March 10 2024

    01:00 March 9 2024 17:00 + 9h -> March 10 2024 03:00 March 9 2024 17:00 + 10h
  74. March 9 2024 17:00 + 8h -> March 10 2024

    01:00 March 9 2024 17:00 + 9h -> March 10 2024 03:00 March 9 2024 17:00 + 10h -> March 10 2024 04:00
  75. OBLIGATORY SUMMARY SLIDE > Computers think about dates and time

    as numeric offsets from a specific point in time
  76. OBLIGATORY SUMMARY SLIDE > Computers think about dates and time

    as numeric offsets from a specific point in time > Humans think about dates in a lot of different ways
  77. OBLIGATORY SUMMARY SLIDE > Computers think about dates and time

    as numeric offsets from a specific point in time > Humans think about dates in a lot of different ways > Let ! handle as much of this for you as possible
  78. OBLIGATORY SUMMARY SLIDE > Computers think about dates and time

    as numeric offsets from a specific point in time > Humans think about dates in a lot of different ways > Let ! handle as much of this for you as possible > Use date and time styles rather than specific formats if you're displaying a date to a user
  79. OBLIGATORY SUMMARY SLIDE > Computers think about dates and time

    as numeric offsets from a specific point in time > Humans think about dates in a lot of different ways > Let ! handle as much of this for you as possible > Use date and time styles rather than specific formats if you're displaying a date to a user > Test the crap out of transition points
  80. DATE AND TIME LINKS! > https://developer.apple.com/videos/play/ wwdc2020/10160/ - Apple's guide

    to displaying content humans understand across the world > https://www.youtube.com/watch?v=-5wpm-gesOY - Tom Scott from 2013 on the Problem With Time And Time Zones, capturing the exasperation beautifully > https://goshdarnformatstyle.com/ - A guide to SwiftUI FormatStyle funtimes
  81. LINKS: TIME EDITION > https://www.esa.int/Applications/ Satellite_navigation/Telling_time_on_the_Moon, the initial proposal the

    ESA made for coming up with a lunar time zone > https://www.smithsonianmag.com/smart-news/the- moon-will-get-its-own-time-zone-called-coordinated- lunar-time-under-nasas-lead-180984076/, a very readable summary of the effort that will be led by
  82. LINKS: SERIOUSLY, LISTEN TO DAVE DELONG EDITION > https://yourcalendricalfallacyis.com, a

    fine listing of just a few of the many, many, many ways to screw up dates, times, and calendars. > https://vimeo.com/865876497, his 2023 NSSpain talk "The Temporal Axis of Space-Time" drawing parallels between how we think about space and how we think about time