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

Peek in Calendar implementation of swift-founda...

Peek in Calendar implementation of swift-foundation

Takuma Shimizu

October 27, 2023
Tweet

More Decks by Takuma Shimizu

Other Decks in Programming

Transcript

  1. TXJGUGPVOEBUJPO w *OUFSOBUJPOBM$PNQPOFOUTGPS6OJDPEF w 6OJDPEFͱJOΛఏڙ͢ΔϥΠϒϥϦ w ࣌ؒʹؔ͢Δԋࢉ ྐྵͷऔΓѻ͍΍λΠϜκʔϯԋࢉ  w

    ਺஋ɺ೔࣌ɺ௨՟ͳͲͷϑΥʔϚολʔ w ଞɺ6OJDPEFͷαϙʔτɺςΩετڥքɺਖ਼نදݱͳͲ *$6
  2. TXJGUGPVOEBUJPO w NSCalendarΫϥεͷ୯ͳΔϥούʔ w ΄ͱΜͲͷ৔߹ɺNSCalendarͷಉ໊ͷϝιουʹॲཧΛҠৡ͍ͯ͠Δ͚ͩ w $PSF'PVOEBUJPO΋όϯυϧ͞Ε͓ͯΓɺCFCalendarͰ*$6Λݺͼग़͢ w TXJGUGPVOEBUJPO JDV

    ొ৔Ҏલ͔Βৄࡉͳ࣮૷ΛݟΔ͜ͱ͸Ͱ͖͍ͯͨ w TXJGUGPVOEBUJPOͱൺֱͯ͠Ͳͷఔ౓ͷৄࡉ͔͞͸ະ֬ೝ w Calendar → NSCalendar (CFCalendar) → ICU4C طଘͷTXJGUDPSFMJCTGPVOEBUJPO Calendarͷ৔߹
  3. struct Calendar class _Calendar class Calendar class GregorianCalendar class JapaneseCalendar

    ɾ ɾ ɾ class EraRules (ucal.cpp) TXJGUGPVOEBUJPO TXJGUGPVOEBUJPOJDV
  4. struct Calendar class _Calendar class Calendar class GregorianCalendar class JapaneseCalendar

    ɾ ɾ ɾ class EraRules (ucal.cpp) TXJGUGPVOEBUJPO TXJGUGPVOEBUJPOJDV
  5. TXJGUGPVOEBUJPOJDV w 4XJGU͔Β*$6 $ ࣮૷ Λݺͼग़ͨ͢Ίͷ''* 'PSFJHO'VODUJPO*OUFSGBDF  w 4XJGUͰ$

    Λ௚઀ݺ΂ΔΑ͏ʹͳͬͨͷͰ͸ʁ w *$6ͷCalendarΫϥεͷϙΠϯλΛఏڙ w ઌͷ_CalendarΫϥε͔Βؒ઀తʹ*$6ͷCalendarΫϥεʹΞΫηεՄೳʹ ucal.cpp
  6. TXJGUGPVOEBUJPOJDV w *$6 w ྐྵಠࣗͷࣄ৘Λߟྀͨ͠೿ੜΫϥε w GregorianCalendar ϢϦ΢εྐྵ άϨΰϦΦྐྵ 

    w JapaneseCalendar ࿨ྐྵ  w ਖ਼֬ʹ͸GregorianCalendarͷαϒΫϥε w FSBͱZFBSͷಈ͖͕ಛघͳͨΊɺ͞ΒʹEraRulesΫϥε͕༻ҙ͞Ε͍ͯΔ class Calendar
  7. struct Calendar class _Calendar class Calendar class GregorianCalendar class JapaneseCalendar

    ɾ ɾ ɾ class EraRules (ucal.cpp) TXJGUGPVOEBUJPO TXJGUGPVOEBUJPOJDV
  8. • date(bySetting:value:for:) -> Date? • date(byAdding:value:to:wrappingComponents:) -> Date? w υΩϡϝϯςʔγϣϯ͔Β͸ɺOJM͕ฦ͞ΕΔ৚͕͍݅·͍ͪΘ͔Βͳ͍

    w NSCalendarͰ͸ॻ͔Ε͍ͯΔ಺༰͕ඍົʹҧ͏ w ͚Ͳ΍ͬͺΓΘ͔Βͳ͍ w ͱΓ͋͑ͣɺvalueҾ਺ʹInt.maxΛ౉͕ͪ͠ w ༗ޮൣғΛ୳ΔͷఘΊ͕ͪ TXJGUGPVOEBUJPOΛ೷͘
  9. date(bySetting:value:for:) componentx value ͷ༗ޮൣғ component era year value (Gregorian) d

    d value (Japanese) d ্ݶ͸೥߸͕ มΘΔ͝ͱʹ૿Ճ dάϨΰϦΦྐྵͷ্ݶ͔Β dateͷ೥߸͕։࢝ͨ͠೥Λݮࢉ ྫ ݱࡏ  ྩ࿨։࢝ͷ೥  Լݶͷิ଍ Լݶ͸ɺdateҾ਺ͷ஋ΑΓ΋ԼճΔ஋Λࢦఆ͢Δͱnil͕ฦΔ  ྫ ݱࡏ ྐྵʹ.gregorianΛɺdateʹ.nowΛࢦఆɹɹɹɹྐྵʹ.japaneseΛɺdateʹ.nowΛࢦఆ FSB ੢ྐྵ ˠ⭕ɺ لݩલ ˠ❌ɹɹɹɹɹɹɹɹFSB ྩ࿨ ˠ⭕ɺ ฏ੒ ˠ❌ ZFBSˠ⭕ɺҎԼˠ❌ɹɹɹɹɹɹɹɹZFBSˠ⭕ɺҎԼˠ❌
  10. date(bySetting:value:for:) componentx value ͷ༗ޮൣғ component month day hour minute, second

    weekday quarter weekOfMonth weekOfYear nanosecond value (ڞ௨) d d d d d d ͸ɺdateΛ ͦͷ··ฦ͢ d d d 
  11. w ϢϦ΢ε೔͔ΒTXJGUGPVOEBUJPO͕نఆ͢Δ࣌఺·Ͱ w طଘ࣮૷ͱͷޓ׵ੑͷͨΊʁ w  d  Date(timeIntervalSinceReferenceDate: -211845067200.0)…

    Date(timeIntervalSinceReferenceDate: 15927175497600.0) w date͕͜ͷൣғΛ௒͑Δͱnil͕ฦ͞ΕΔ w date(byAdding:value:to:wrappingComponents:)ͱ͸ҟͳΔ date date(bySetting:value:for:)
  12. date(byAdding:value:to:wrappingComponents:) componentx value ͷ༗ޮൣғ component era, year, month, day, hour,

    minute, second, weekday, weekdayOrdinal, quarter, weekOfMonth, weekOfYear, yearForWeekOfYear, nanosecond calendar timeZone isLeapMonth value IntͷൣғͰ ੍ݶͳ͠ ৗʹnil͕ฦ͞ΕΔৗʹnil͕ฦ͞ΕΔৗʹnil͕ฦ͞ΕΔ
  13. date(byAdding:value:to:wrappingComponents:) w ϢϦ΢ε೔͔ΒTXJGUGPVOEBUJPO͕نఆ͢Δ࣌఺·Ͱ w  d  Date(timeIntervalSinceReferenceDate: -211845067200.0)… Date(timeIntervalSinceReferenceDate:

    15927175497600.0) w date͸ɺԋࢉલʹ͜ͷൣғʹऩ·ΔΑ͏ʹmax(min())͕औΒΕΔ w ԋࢉͷ݁Ռɺ͜ͷൣғ֎ʹͳͬͯ΋ͦͷ··ฦ͢ w *$6ଆͰ͸΋ͬͱେ͖ͳൣғ لݩલສ೥ ~ ੢ྐྵສ೥ ͕ѻ͑Δ ࠷ॳͷાஔΛߦ͏ͷ͸ɺTXJGUGPVOEBUJPOͷ੍໿ʁ date
  14. • date(bySetting:value:for:) -> Date? • date(byAdding:value:to:wrappingComponents:) -> Date? w υΩϡϝϯςʔγϣϯ͔Β͸ɺOJM͕ฦ͞ΕΔ৚͕͍݅·͍ͪΘ͔Βͳ͍

    w NSCalendarͰ͸ॻ͔Ε͍ͯΔ಺༰͕ඍົʹҧ͏ w ͚Ͳ΍ͬͺΓΘ͔Βͳ͍ wͱΓ͋͑ͣɺvalueҾ਺ʹInt.maxΛ౉͕ͪ͠ w ༗ޮൣғΛ୳ΔͷఘΊ͕ͪ TXJGUGPVOEBUJPOΛ೷͘