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

🕗Perlの時間はモジュールで #perlwakate

koluku
July 01, 2020

🕗Perlの時間はモジュールで #perlwakate

2020/07/01 にオンラインで開催された 「Perl若手の会(https://connpass.com/event/179235/)」でのLT資料です

koluku

July 01, 2020
Tweet

More Decks by koluku

Other Decks in Programming

Transcript

  1. Perlͷ࣌ؒ͸ϞδϡʔϧͰ
    Perlएखͷձ
    2020-07-01 @koluku

    View full-size slide

  2. ࣗݾ঺հ
    • koluku ʢίϧΫʣ
    • एख, Perlܦݧ͸2ϲ݄, ొஃܦݧແ͠
    • Python/Go͕ϝΠϯ

    View full-size slide

  3. ࿩͢͜ͱɾ࿩͞ͳ͍͜ͱ
    • ࿩͢͜ͱ
    • ࣌ؒͷ੍ޚͷ͋Ε͜Ε
    • Time::Pieceָ͕ͩΑ
    • ࿩͞ͳ͍͜ͱ
    • ࣮૷ͷ࿩

    View full-size slide

  4. $^T
    • ʮϓϩάϥϜΛ࣮ߦ։࢝ͨ࣌͠ࠁΛɺلݩ (1970೥ͷ࢝Ί) ͔Βͷඵ
    ਺Ͱ ࣔͨ͠΋ͷͰ͢ʯ
    • لݩ (1970೥ͷ࢝Ί) ͬͯԿΑʁ

    View full-size slide

  5. time
    • ʮ΄ͱΜͲͷγεςϜͰ͸لݩ͸UTC1970೥1݄1೔00:00:00Ͱ͢;
    ಛ௃తͳྫ֎ͱͯ͠͸ɺݹ͍MacOSͰ͸ϩʔΧϧλΠϜκʔϯͷ
    1904೥1݄1೔00:00:00Λلݩͱͯ͠࢖͍·͢ɻʯ
    • UTC1970೥1݄1೔00:00:00͸͍ΘΏΔUNIX࣌ؒ
    • UTC1904೥1݄1೔00:00:00͸͏Δ͏೥ͷྫ֎ճආͷͨΊͷ೔෇
    • 1904೥͔Β2096೥·Ͱ͏Δ͏೥͸ଘࡏ͢Δ

    View full-size slide

  6. $^Tͱtime
    • ͲͪΒ΋γεςϜ࣌ؒΛฦ͢ͷͰ࣮࣭ಉ͡
    • ݱࡏ࣌ؒΛ஌Ζ͏ʹ΋UNIX࣌ؒʢඵ਺ʣͰฦ͞ΕΔͷͰී௨ʹ͸ѻ
    ͑ͳ͍
    • ͦ͜ͰlocaltimeͱgmtimeΛ࢖͏

    View full-size slide

  7. localtime
    • ʮtimeؔ਺͕ฦ࣌͢ࠁΛɺϩʔΧϧͳλΠϜκʔϯͰଌͬͨ࣌ࠁͱ͠
    ͯɺ9ཁૉͷ഑ྻʹม׵͠·͢ɻී௨͸ɺҎԼͷΑ͏ʹͯ͠࢖͍·͢:ʯ
    • $wday͸೔༵೔Λ0ͱ༵ͨ͠೔ɺ $yday ͸ͦͷ೥ͷԿ೔໨͕ؼͬͯ͘
    Δ
    • εΧϥʔͷͱ͖ʹѻ͏ͱจࣈྻͰؼͬͯ͘Δ
    • ศརʂ

    View full-size slide

  8. gmtime
    • localtimeͩͱϚγϯͷ࣌ؒʹґଘ͢ΔͨΊ೔ຊͩͱGMT+9:00
    • άϦχοδඪ४࣌ʹ໭͢ʹ͸-9࣌ؒ͢Δඞཁ͕…
    • localtimeͱಉ͡࢓૊ΈͰάϦχοδඪ४࣌Λѻ͏gmtime͕͋ΔͷͰ
    ҆৺ʂ

    View full-size slide

  9. localtimeͱgmtimeͷ᠘
    • ʮ$year ͸ 1900 ೥͔Βͷ೥਺Λ࣋ͪ·͢ɻʯ
    • ਓ͕ؒಡΈ΍͍͢Α͏ʹԼೋܻͰ൑அͰ͖Δ։࢝஍఺ͷͨΊ
    • ͍ΘΏΔPOSIX४ڌ
    • ೥͚ͩͰݟΔͱ3ύλʔϯ͋Δ

    View full-size slide

  10. localtimeɺศརʹ͸ศརͳΜ͚ͩͲ…
    • ਺஋৘ใ͔͠ͳ͍͔Βѻ͏ʹ͸݁ہؔ਺Λ࡞Δඞཁ͕͋Δ
    • ศརϝιου͕ੜ͑ͨΦϒδΣΫτͰฦͯ͠΄͘͠ͳ͍ʁ

    View full-size slide

  11. Time::Piece
    • Timepiece͸࣌ܭͱ͍͏ҙຯ
    • localtime, gmtime͕Time::PieceΦϒδΣΫτͰؼͬͯ͘Δ
    • ཉ͍͠ػೳ͸େମἧ͍ͬͯΔ

    View full-size slide

  12. Time::PieceͰՃࢉɾݮࢉ
    • Time::SecondsͰ1෼ʢONE_MINUTEʣͳͲͷఆ͕ٛݺͼग़ͤΔ
    • Time::PieceʹTime::SecondsͰՃࢉɾݮࢉ͢Δ͜ͱ͕Ͱ͖Δ
    • localtime + ONE_DAY
    • Time::Pieceಉ࢜Ͱݮࢉ͢ΔͱTime::SecondsͰؼͬͯ͘Δʢඵ਺ʣ
    • localtime - gmtime

    View full-size slide

  13. Time::PieceͰൺֱ
    • Time::Pieceಉ࢜Ͱൺֱ͢Δࣄ΋Ͱ͖Δ
    • return if (localtime - gmtime == ONE_HOUR * 9);
    • Time::Pieceศར͗͢ͳ͍ʁ

    View full-size slide

  14. Ͱ΋Ͳ͏΍ͬͯՃࢉ΍ൺֱΛͯ͠Δʁ
    • ͪΐͬͱدΓಓ
    • Α͘ߟ͑ͨΒTime::Piece͸ΦϒδΣΫτ͔ͩΒී௨ͷԋࢉࢠͰܭࢉ
    Ͱ͖Δͷ͸͓͔͍͠

    View full-size slide

  15. ͜͏͍͏ͱ͖͸ίʔυΛಡΉͷ͕ૣ͍
    • ͱ͏͍͏͜ͱͰmeta::cpan͔ΒBrowseͰίʔυΛݟͯΔͱ
    Time::Seconndsʹoverloadͱ͍͏໊લ͕ग़ͯ͘Δ

    View full-size slide

  16. overload
    • perldoc.jpΛݟͯΈΔͱʮPerl ͷԋࢉࢠͷΦʔόʔϩʔυΛߦ͏ύο
    έʔδʯͱ͋Δ
    • ͭ·Γࢉज़ԋࢉࢠ΍ൺֱԋࢉࢠΛผͷؔ਺ʹ্ॻ͖Ͱ͖Δ
    • Time::SecondsͰ+ԋࢉࢠͷ৔߹͸ࠨӈ͔Β஋Λऔͬͯؔ਺ʹೖΕͯ
    ܭࢉͨ͠΋ͷΛฦ͍ͯ͠Δ
    • ͳΔ΄ͲͶ

    View full-size slide

  17. Ͱ΋େମѻ͍͍ͨͷ͸MySQLͷͱ͖…
    • ࿩͸໭ͬͯɺ࣌ؒͱ͍͑͹MySQLͰͷѻ͍͸ͳ͔ͳ͔໘౗
    • fracʢখ਺఺ҎԼʣ͕௥Ճ͞Ε͍ͯΔ͜ͱ΋͋Δ
    • DATETIMEͱTIMESTAMP͸ಉ͡ϑΥʔϚοτͰ΋ɺ
    • αϙʔτ͞Ε͍ͯΔൣғ͕ڱ͍, DATETIME͸݄ɾ೔ʹ00Λڐ༰
    • ಉ͡஋Ͱ΋ҧ͏஋Ͱొ࿥͞ΕΔ͕࣌͋Δ
    • ͜Μͳͷී௨ʹѻ͍ͨ͘ͳ͍ʂ

    View full-size slide

  18. Time::Piece::MySQL
    • Time::Pieceͷ֦ுϝιου
    • Time::Piece->mysql_dateͰMySQLͷDATEܕʹม׵
    • Time::Piece->from_mysql_date($datetime)ͰMySQLͷDATEܕ͔Β
    Time::PieceΦϒδΣΫτʹม׵
    • ͜ΕͰMySQLͰͷ࣌ؒͷॲཧʹۤ࿑͢Δ͜ͱ͸ͳ͘ͳͬͨ…ʂ

    View full-size slide

  19. ΍ͬͨʂੈքʹฏ࿨͕๚ΕͨʂͰ΋…
    • ϛϦඵɾϚΠΫϩඵͷѻ͍͸Ͳ͏͢Δͷʁ
    • ϓϩάϥϜͷ࣮ߦ࣌ؒΛѻ͍͍ͨΜ͚ͩͲ
    • Q: Ұఆ࣌ؒॲཧΛsleepͤ͞Δʹ͸
    • ೔෇Λ࿨ྐྵʹม׵͍ͨ͠
    • ೔෇ͷൣғʹ͋Δ͔Ͳ͏͔Λָʹܭࢉ͍ͨ͠
    • ೔ຊҎ֎ͷ࣌ࠁΛϩέʔϧ͍ͨ͠
    • ҧ͏ϩέʔϧ࣌ؒͲ͏͠Ͱࠩ෼Λग़͍ͨ͠
    • etc…

    View full-size slide

  20. ͝ΊΜʂLT͡Ό࣌ؒ଍Γͳ͍͔Β
    ࣗ෼Ͱௐ΂ͯʂ

    View full-size slide