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

Time vs Calendar: One is Easy, the Other Hard

Time vs Calendar: One is Easy, the Other Hard

Leap Years!
Daylight Savings Time!
Time Zones!

Approach Calendars with humility - this presentation compares Time and Calendars, shows you some common pitfalls, and concludes with Swift 5.5 examples.

Patrick Weigel

October 27, 2021

Transcript

33 slides
  1. CALENDAR ROMAN (year #, month names, day names, manual lunar

    to solar fix) JULIAN (year #, month names, day numbers, automatic lunar to solar fix) GREGORIAN (slightly better automatic lunar to solar fix) HOLOCENE (year #) November 2nd, 2021
  2. TIME GREGORIAN CALENDAR November 2nd, 2021 0 1582 leap year

    inconsistent month length leap second time zones daylight savings
  3. STEP 1: In what domain is my problem? November 2nd,

    2021 STEP 2: If Calendar, have some humility
  4. CALENDAR DOMAIN MISTAKE 1: Calendar Day = (seconds / minute

    ) * (minutes / hour ) * (hours / day) = 60 * 60 * 24 Tomorrow = current day + Calendar Day November 2nd, 2021 CALENDAR DOMAIN MISTAKE 2: I’m smart enough to avoid Mistake 1! I’ll use Time and handle the edge cases myself!
  5. EXAMPLE 1: STOPWATCH November 2nd, 2021 Show elapsed time between

    two clicks Is the domain TIME or CALENDAR?
  6. EXAMPLE 1: STOPWATCH November 2nd, 2021 Show elapsed time between

    two clicks Is the domain TIME or CALENDAR? Store Current Time of Start Click Store Current Time End Click Print End - Start
  7. EXAMPLE 1: STOPWATCH November 2nd, 2021 let timeStart = Date().timeIntervalSinceReferenceDate

    print(timeStart) let timeEnd = Date().timeIntervalSinceReferenceDate print(timeEnd) print(timeEnd - timeStart)
  8. EXAMPLE 2: APPOINTMENT November 2nd, 2021 Show appointment day, month,

    year Is the domain TIME or CALENDAR? Create Calendar Object Set day, month, year Create and print calendar object
  9. EXAMPLE 2: APPOINTMENT November 2nd, 2021 let exampleCalendar = Calendar(identifier:

    .gregorian) var exampleDateComponents = DateComponents() exampleDateComponents.year = 2021 exampleDateComponents.month = 11 exampleDateComponents.day = 2 exampleDateComponents.timeZone = TimeZone(abbreviation: "CST") exampleDateComponents.hour = 22 exampleDateComponents.minute = 34 let appointmentDate = exampleCalendar.date(from: exampleDateComponents) ?? Date() print(appointmentDate)
  10. EXAMPLE 3: HOURS UNTIL APPOINTMENT November 2nd, 2021 Show hours

    from now until appointment Is the domain TIME or CALENDAR?
  11. EXAMPLE 3: HOURS UNTIL APPOINTMENT November 2nd, 2021 Show hours

    from now until appointment Is the domain TIME or CALENDAR? Store Current Time A Get Time of Appointment B Print B - A
  12. EXAMPLE 3: HOURS UNTIL APPOINTMENT November 2nd, 2021 let timeCurrent

    = Date().timeIntervalSinceReferenceDate let appointmentDateAsTime = appointmentDate.timeIntervalSinceReferenceDate print((appointmentDateAsTime - timeCurrent) / (60 * 60))

More Decks by Patrick Weigel

Other Decks in Programming