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

DateComponentsと仲良くなる

028088964f255d677d471558da865b72?s=47 sato-shin
September 19, 2021

 DateComponentsと仲良くなる

iOSDC 2021で発表したスライドです。
* asken tech blog: https://tech.asken.inc
* youtube link: アップロードされたら

028088964f255d677d471558da865b72?s=128

sato-shin

September 19, 2021
Tweet

Transcript

  1. TBUPTIJO!BTLFO*OD %BUF$PNQPOFOUTͱ ஥ྑ͘ͳΔɹ J04%$+BQBO

  2. ຊηογϣϯʹ͍ͭͯ %BUF$PNQPOFOUTͱ஥ྑ͘ͳΔɹ

  3. ຊηογϣϯʹ͍ͭͯ %BUF$PNQPOFOUTͱ஥ྑ͘ͳΔɹ ࠓɺ೤͍࿩୊ʁ

  4. ຊηογϣϯʹ͍ͭͯ %BUF$PNQPOFOUTͱ஥ྑ͘ͳΔɹ ࠓɺ೤͍࿩୊ʁ %BUF$PNQPOFOUT͸J04

  5. ຊηογϣϯʹ͍ͭͯ %BUF$PNQPOFOUTͱ஥ྑ͘ͳΔɹ J04ʹؔ͢Δʁ

  6. ຊηογϣϯʹ͍ͭͯ %BUF$PNQPOFOUTͱ஥ྑ͘ͳΔɹ J04ʹؔ͢Δʁ

  7. ຊηογϣϯʹ͍ͭͯ %BUF$PNQPOFOUTͱ஥ྑ͘ͳΔɹ ೔࣌ܭࢉΛ%BUFͰࡁ·ͤͪΌͬͯΔਓ %BUF$PNQPOFOUTΛ͋Μ·Γ࢖ͬͨ͜ͱ͕ͳ͍ਓ ͦΜͳਓͷͨΊͷηογϣϯͰ͢ɻ

  8. ࣌ؒͷجૅ

  9. ࣌ؒ͸ͲͷΑ͏ʹఆٛ͞ΕΔʁ

  10. ࣌ؒ͸ͲͷΑ͏ʹఆٛ͞ΕΔʁ ࣌ؒ࣠

  11. ࣌ؒ͸ͲͷΑ͏ʹఆٛ͞ΕΔʁ ࠓ ࣌ؒ࣠

  12. ࣌ؒ͸ͲͷΑ͏ʹఆٛ͞ΕΔʁ ࠓ ج४఺ ࣌ؒ࣠

  13. ࣌ؒ͸ͲͷΑ͏ʹఆٛ͞ΕΔʁ ࠓ ج४఺ ௕͞ ࣌ؒ࣠

  14. ࣌ؒ͸ͲͷΑ͏ʹఆٛ͞ΕΔʁ ࠓ ج४఺ ௕͞ ࣌ؒ࣠ ༗໊ͳج४఺ɿ 6/*9ΤϙοΫ 65$

  15. ࣌ؒ͸ͲͷΑ͏ʹఆٛ͞ΕΔʁ ࠓ ج४఺ ௕͞<ඵ> ࠃࡍ୯Ґܥʢ4*ʣͰఆٛ͞ΕΔ࣌ؒͷ୯Ґ͕ඵ ࣌ؒ࣠ ༗໊ͳج४఺ɿ 6/*9ΤϙοΫ 65$

  16. 4XJGUͰ࣌ؒΛදݱ͢Δܕ͸ʁ %BUF

  17. 4XJGUͰ͸ͲͷΑ͏ʹදݱ͞ΕΔʁ %BUF init(timeIntervalSinceNow: ) init(timeIntervalSince1970: ) init(timeIntervalSinceReferenceDate: ) init(timeInterval: since:

    ) init()
  18. 4XJGUͰ͸ͲͷΑ͏ʹදݱ͞ΕΔʁ init(timeIntervalSinceNow: ) init(timeIntervalSince1970: ) init(timeIntervalSinceReferenceDate: ) init(timeInterval: since: )

    init() %BUF
  19. 4XJGUͰ͸ͲͷΑ͏ʹදݱ͞ΕΔʁ init(timeIntervalSinceNow: ) init(timeIntervalSince1970: ) init(timeIntervalSinceReferenceDate: ) init(timeInterval: since: )

    init() = init(timeIntervalSinceNow: 0) %BUF
  20. %BUF㱠೔෇ %BUF࣌ؒͷ఺

  21. ೥ʁ݄ʁ೔ʁ ඵ͔͠஌Βͳ͍Αʂ %BUF㱠೔෇ %BUF࣌ؒͷ఺

  22. ೔࣌ܭࢉͷ೉͠͞

  23. ଍͠ࢉ͢Δ let after10Sec = Date() + 10 let after1Minute =

    Date() + 60 let after1Day = Date() + (60 * 60 * 24) let after1Month = Date() + (60 * 60 * 24 * 30)
  24. ଍͠ࢉ͢Δ ! let after10Sec = Date() + 10 let after1Minute

    = Date() + 60 let after1Day = Date() + (60 * 60 * 24) let after1Month = Date() + (60 * 60 * 24 * 30)
  25. ೔࣌ܭࢉͰߟྀ͠ͳ͚Ε͹͍͚ͳ͍͜ͱ ͏Δ͏೥ͷଘࡏ ϲ݄ͷఆٛ αϚʔλΠϜ ͏Δ͏ඵͷଘࡏ ೥   ೔ ϲ݄

        ೔ ೔    ࣌ؒ ෼    ඵ
  26.  ʌůɾ㱼ɾA ŷžŷž

  27. ೔࣌ܭࢉΫϥε$BMFOEBS

  28. ೔࣌ܭࢉΫϥε$BMFOEBS let calendar = Calendar(identifier: .gregorian) let after1Month = calendar.date(byAdding:

    .month, value: 1, to: Date())
  29. ໰୊ ʮੜ೥݄೔Λ%BUFͰදݱͰ͖Δ͔ʁʯ

  30. ੜ೥݄೔Λ%BUFͰදݱͯ͠ΈΔ let birthday = Date(timeIntervalSince1970: 656_175_600) let formatter = DateFormatter()

    formatter.dateFormat = "yyyy-MM-dd" formatter.timeZone = TimeZone(abbreviation: "UTC") print(formatter.string(from: birthday))
  31. ੜ೥݄೔Λ%BUFͰදݱͯ͠ΈΔ let birthday = Date(timeIntervalSince1970: 656_175_600) let formatter = DateFormatter()

    formatter.dateFormat = "yyyy-MM-dd" formatter.timeZone = TimeZone(abbreviation: "UTC") print(formatter.string(from: birthday)) 1990-10-18 00:00:00 +09:00
  32. ੜ೥݄೔Λ%BUFͰදݱͯ͠ΈΔ let birthday = Date(timeIntervalSince1970: 656_175_600) let formatter = DateFormatter()

    formatter.dateFormat = "yyyy-MM-dd" formatter.timeZone = TimeZone(abbreviation: "UTC") print(formatter.string(from: birthday)) 1990-10-18 00:00:00 +09:00 1990-10-17
  33. %BUF࣌ؒͷ఺ ஀ੜ೔ͳͲͷදݱʹ͸޲͔ͳ͍

  34. %BUF࣌ؒͷ఺ ੜ೥݄೔ͳͲͷදݱʹ͸޲͔ͳ͍

  35. %BUF࣌ؒͷ఺ ੜ೥݄೔ͳͲͷදݱʹ͸޲͔ͳ͍ ࠓ೔ɺࠓ݄ɺࠓ೥ɺه೦೔ɺFUD λΠϜκʔϯʹґଘ͠ͳ͍೔࣌Λਖ਼֬ʹදݱͰ͖ͳ͍

  36. ೔෇ͰදݱͰ͖Δܕ͕͋ͬͨΒͳ͊ɻɻɻ

  37. %BUF$PNQPOFOUT

  38. let calendar = Calendar(identifier: .gregorian) let birthday = DateComponents(calendar: calendar,

    year: 1990, month: 10, day: 18)
  39. %BUF$PNQPOFOUTͷ *OJUJBMJ[FS͔Βಛ௃ΛݟΔ

  40. %BUF$PNQPOFOUT JOJUJBMJ[FS DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year: Int?,

    month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  41. ͭш㱬 ŠƄŢŠƄŢ

  42. %BUF$PNQPOFOUT JOJUJBMJ[FS DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year: Int?,

    month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  43. %BUF$PNQPOFOUT JOJUJBMJ[FS ͳΜͱҾ਺ݸʂ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  44. %BUF$PNQPOFOUT ೔࣌Λߏ੒͢Δཁૉ܈

  45. %BUF$PNQPOFOUT JOJUJBMJ[FS શͯ೔࣌ͷߏ੒ཁૉ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  46. ͭͷཁૉຖ͸؆୯

  47. %BUF$PNQPOFOUT JOJUJBMJ[FS ΧϨϯμʔ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  48. %BUF$PNQPOFOUT JOJUJBMJ[FS λΠϜκʔϯ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  49. %BUF$PNQPOFOUT JOJUJBMJ[FS ࣌୅ ݩ߸ HSFHPSJBOΧϨϯμʔͷ৔߹ لݩલ ੢ྐྵ ࿨ྐྵΧϨϯμʔͷ৔߹ େԽ ฏ੒

    ྩ࿨ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year: Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  50. %BUF$PNQPOFOUT JOJUJBMJ[FS ೥݄೔࣌෼ඵφϊඵ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  51. %BUF$PNQPOFOUT JOJUJBMJ[FS ༵೔ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  52. %BUF$PNQPOFOUT JOJUJBMJ[FS ͦͷ༵೔͕Կ൪໨͔ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?) ݄ͷୈ ೔༵೔
  53. %BUF$PNQPOFOUT JOJUJBMJ[FS ࢛൒ظ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  54. %BUF$PNQPOFOUT JOJUJBMJ[FS ͦͷ݄ͷԿि໨͔ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?) ݄ͷୈि໨
  55. %BUF$PNQPOFOUT JOJUJBMJ[FS ͦͷ೥ͷԿि໨͔ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  56. %BUF$PNQPOFOUT JOJUJBMJ[FS ೥ʢXFFLPGZFBS༻ʣ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  57. %BUF$PNQPOFOUT͸Ͳ͏࢖͏ʁ

  58. ࡾͭͷ໾ׂ ೔࣌ ྔ ύλʔϯϚον

  59. ࡾͭͷ໾ׂ ೔࣌ %BUF$PNQPOFOUT%BUF

  60. ೔࣌ DateComponents => Date let today = DateComponents(calendar: gregorianCalendar, year:

    2021, month: 9, day: 19) var todayInJp = today todayInJp.timeZone = TimeZone(abbreviation: "JST") todayInJp.date // 2021-09-19 00:00:00 +09:00
  61. ೔࣌ DateComponents => Date let today = DateComponents(calendar: gregorianCalendar, year:

    2021, month: 9, day: 19) var todayInJp = today todayInJp.timeZone = TimeZone(abbreviation: "JST") todayInJp.date // 2021-09-19 00:00:00 +09:00
  62. ೔࣌ Date => DateComponents let today = gregorianCalendar .dateComponents([.calendar, .year,

    .month, .day], from: Date())
  63. ೔࣌ let today = gregorianCalendar .dateComponents([.calendar, .year, .month, .day], from:

    Date()) Date͔Β Calendarͱ೥݄೔͕ઃఆ͞Εͨ DateComponentsΛੜ੒ Date => DateComponents
  64. ʮࠓ೔ʯͷදݱྫ

  65. ʮࠓ೔ʯͷදݱྫ

  66. ೔࣌ ʮࠓ೔ʯͷදݱྫ // 2021೥9݄19೔ let today = DateComponents(calendar: gregorianCalendar, year:

    2021, month: 9, day: 19) today.isValidDate // true
  67. ೔࣌ ʮࠓ೔ʯͷදݱྫ // 2021೥9݄ͷୈ3,೔༵೔ let today = DateComponents(calendar: gregorianCalendar, year:

    2021, month: 9, weekday: 1, weekdayOrdinal: 3) today.isValidDate // true
  68. ೔࣌ ʮࠓ೔ʯͷදݱྫ // 2021೥ͷ262೔໨ let today = DateComponents(calendar: gregorianCalendar, year:

    2021, day: 262) today.isValidDate // false
  69. ೔࣌ ʮࠓ೔ʯͷදݱྫʢ❌ʣ // 2021೥ͷ262೔໨ let today = DateComponents(calendar: gregorianCalendar, year:

    2021, day: 262) today.isValidDate // false 2021೥1݄262೔ͱ൑ఆ͞Εɺfalse
  70. ೔࣌ ʮࠓ೔ʯͷදݱྫ // 2021೥ͷୈ38,೔༵೔ let today = DateComponents(calendar: gregorianCalendar, year:

    2021, weekday: 1, weekdayOrdinal: 38) today.isValidDate // false
  71. ೔࣌ ʮࠓ೔ʯͷදݱྫʢ❌ʣ // 2021೥ͷୈ38,೔༵೔ let today = DateComponents(calendar: gregorianCalendar, year:

    2021, weekday: 1, weekdayOrdinal: 38) today.isValidDate // false 2021೥1݄ͷୈ38,೔༵೔ͱ ൑ఆ͞Εɺfalse
  72. ࡾͭͷ໾ׂ ྔ ௕͞

  73. ྔ ೔࣌ܭࢉͷ͓ڙʹ let oneMonth = DateComponents(month: 1) // ̍ϲ݄ let

    oneMonthLater = calendar.date(byAdding: oneMonth, to: Date())
  74. ྔ ೔࣌ܭࢉͷ͓ڙʹ let oneMonth = DateComponents(month: 1) // ̍ϲ݄ let

    oneMonthLater = calendar.date(byAdding: oneMonth, to: Date())
  75. ࡾͭͷ໾ׂ ύλʔϯϚον

  76. ύλʔϯϚον UserNotificationͷTriggerදݱͳͲ let wakeUpAlertTime = DateComponents(hour: 8, minute: 30) let

    trigger = UNCalendarNotificationTrigger( dateMatching: wakeUpAlertTime, repeats: true)
  77. ύλʔϯϚον UserNotificationͷTriggerදݱͳͲ let wakeUpAlertTime = DateComponents(hour: 8, minute: 30) let

    trigger = UNCalendarNotificationTrigger( dateMatching: wakeUpAlertTime, repeats: true)
  78. ύλʔϯϚον UserNotificationͷTriggerදݱͳͲ let wakeUpAlertTime = DateComponents(hour: 8, minute: 30 weekday:

    1) let trigger = UNCalendarNotificationTrigger( dateMatching: wakeUpAlertTime, repeats: true)
  79. ύλʔϯϚον UserNotificationͷTriggerදݱͳͲ let wakeUpAlertTime = DateComponents(hour: 8, minute: 30 weekday:

    1) let trigger = UNCalendarNotificationTrigger( dateMatching: wakeUpAlertTime, repeats: true) ೔༵೔ͷ8࣌30෼
  80. 5JQT

  81. 5JQT DateComponents(hour: 12)

  82. 5JQT ྔΛද͢ͷ࣌ؒʁ ύλʔϯϚονͷ࣌ʁ DateComponents(hour: 12)

  83. 5JQT %BUF$PNQPOFOUT͕ͲΜͳҙຯΛ͔࣋ͭ͸ɺίϯςΩετʹΑͬͯҟͳΔɻ ࠞཚ͢Δ৔߹ʹ͸ɺϥούʔΫϥεΛ࡞Δͷ͕͓͢͢Ίɻ // ྔతදݱʹݶఆ͢Δ struct DateLength { var value:

    DateComponents }
  84. ·ͱΊ %BUF͸࣌ؒͷ఺Ͱ͋Γɺѻ͏୯Ґ͸ඵͷΈ ඵҎ֎୯ҐͰܭࢉΛߦ͏৔߹͸$BMFOEBSΛ࢖͏ %BUF$PNQPOFOUT͸೔࣌Λߏ੒͢Δཁૉ܈Ͱ ओʹʮ೔࣌ɺྔɺύλʔϯϚονʯͷ໾ׂͰ࢖ΘΕΔ

  85. ͚͋͢Μઈࢍ֦େதʂ J04%$ΰʔϧυεϙϯαʔɻ͚͋͢ΜϝΨω১͖࢖ͬͯͶʂ ࠃ಺ʮϔϧεέΞʯΧςΰϦͰ೥౓ͷ%-਺ɺऩӹ/Pʂ 5FDIϒϩά࢝Ί·ͨ͠ʂ 🥇 📖 🎩

  86. ͚͋͢Μઈࢍ֦େதʂ 5FDIϒϩάϦϯΫ ຊηογϣϯͷิ଍هࣄ΋ॻ͖·ͨ͠ɻ ͥͻνΣοΫͯ͠Έ͍ͯͩ͘͞ʂ https://tech.asken.inc

  87. None