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

A Tale about Time - LaravelLive India 2018

A Tale about Time - LaravelLive India 2018

Timezones, DST, human readable diffs, standardised formats, intervals, getting the last Tuesday of next month… Dealing with timestamps seems easy, but there’s a lot to it. It’s very easy to do it wrong. Learn how to do this properly and you’ll never be chasing weird time related bugs anymore!

Hannes Van De Vreken

March 17, 2018
Tweet

More Decks by Hannes Van De Vreken

Other Decks in Programming

Transcript

  1. A Tale about Time
    @hannesvdvreken
    @laravellivein

    View full-size slide

  2. Hi, my name is Hannes.

    View full-size slide

  3. A Tale about Time

    View full-size slide

  4. 2018-03-17T10:15:00+05:30

    View full-size slide

  5. EST = UTC-05:00

    View full-size slide

  6. Helsinki
    Sunrise: 9:24 - 3:54
    Sunset: 15:13 - 22:50
    Quito
    Sunrise: 6:08 - 6:12
    Sunset: 18:16 - 18:19

    View full-size slide

  7. EST/EDT - March 12
    CET/CEST - March 24

    View full-size slide

  8. Storing vs Expressing time

    View full-size slide

  9. 6:00PM vs 18:00

    View full-size slide

  10. 1521261900
    >1970-01-01T00:00:00+00:00

    View full-size slide

  11. 1521261900
    >1970-01-01T00:00:00+00:00
    <2038-01-19T03:14:07+00:00

    View full-size slide

  12. RFC3339:
    2018-03-17T10:15:00+05:30

    View full-size slide

  13. RFC3339 extended:
    2018-03-17T10:15:00.000+05:30

    View full-size slide

  14. Sortable:
    2018-03-17T05:45:00.000+00:00

    View full-size slide

  15. Weeks - 7 days

    View full-size slide

  16. Months
    Lunar vs Tropical

    View full-size slide

  17. Years
    Lunisolar vs Tropical

    View full-size slide

  18. 2018AD
    1500BC

    View full-size slide

  19. Leap years
    Leap seconds

    View full-size slide

  20. DateTimeInterface
    - DateTime (date, time, tz)
    - DateTimeImmutable

    View full-size slide

  21. $date = $date->setTimezone(‘Asia/Kolkata');

    View full-size slide

  22. $date = DateTimeImmutable::createFromMutable($mutable);

    View full-size slide

  23. Change in PHP 7.1: microseconds

    View full-size slide

  24. // Magic
    $date = new DateTimeImmutable(‘now’);
    $date = $date->modify(‘first day of next month’);

    View full-size slide

  25. “now + 5 hours”
    “first day of next month”
    "first weekday”

    View full-size slide

  26. “2016-03-00” → “2016-02-29”
    “2018-06-31” → “2018-07-01”

    View full-size slide

  27. “69-12-31” → “2069-12-31”
    “70-01-01” → “1970-01-01”

    View full-size slide

  28. nesbot/carbon - Mutable
    cakephp/chronos - Immutable

    View full-size slide

  29. interface Clock
    {
    public function now(): DateTimeImmutable
    }

    View full-size slide

  30. c ~ 299.000.000m/s

    View full-size slide

  31. Thanks for your time!
    @hannesvdvreken
    @laravellivein

    View full-size slide

  32. Time for questions.
    @hannesvdvreken
    @laravellivein

    View full-size slide