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

What time is it now?

What time is it now?

A talk about date and time representation and parsing in Python on Piter Py #4 http://piterpy.com

200442040b9d038626d92d408f499642?s=128

Serge Matveenko

November 04, 2017
Tweet

Transcript

  1. What time is it now? Serge Matveenko

  2. https://imgs.xkcd.com/comics/yogurt.png

  3. What is time?

  4. What is time? • Date ◦ Calendars ▪ Unix —

    355316580 ▪ Ordinal — 1981-095 ▪ Gregorian — 1981-04-05 ▪ Hebrew — 1st of Nisan, 5741 ◦ Representation ▪ 1981-04-05; 5.04.1981; 4/5/1981; 1981/5/4; Apr 5, 1981; 5 Апр 1981 • Time ◦ Time zone ◦ Daylight savings ◦ Representation ▪ 11:03:00; 3 past eleven; 11-03; 11.03am
  5. Standards • ISO ◦ 1981-04-05T15:03:00+04:00 ◦ 1981-04-05T15:03:00.0000+04:00 ◦ 1981-04-05T11:03:00Z ◦

    1981-04-05T11:03:00.0000Z • RFC ◦ Sun, 5 Apr 1981 15:03:00 +04:00 • UNIX timestamp ◦ 355316580
  6. UNIX timestamp is handy? 355316580 (seconds since 1 Jan 1970

    UTC) • Monotonic :) • The same everywhere :) • Starts from 1 Jan 1970 UTC :( • C# time starts at 1 Jan 0001 :( • JavaScript timestamp uses milliseconds :(
  7. Date and time in Python

  8. Date and time in Python from datetime import datetime, date

    >>> datetime.now() datetime.datetime(2017, 11, 4, 10, 24, 49, 917507) >>> date.today() datetime.date(2017, 11, 4)
  9. UNIX timestamp is handy? >>> datetime.now().timestamp() 1509780709.870386

  10. UNIX timestamp is handy? >>> datetime.now().timestamp() # Python 3.3+ 1509780709.870386

  11. UNIX timestamp is handy? >>> datetime.now().timestamp() # Python 3.3+ 1509780709.870386

    >>> # Before Python 3.3 >>> from datetime import datetime, timedelta, timezone >>> (datetime.now() - datetime(1970, 1, 1)) / timedelta(seconds=1) 1509792841.875203
  12. UNIX timestamp is handy? >>> datetime.now().timestamp() 1509780709.870386 >>> datetime.fromtimestamp(datetime.now().timestamp()), datetime.datetime(2017,

    11, 4, 10, 35, 7, 963429) >>> from datetime import timezone >>> datetime.fromtimestamp(datetime.now().replace(tzinfo=timezone.utc).timestamp()) datetime.datetime(2017, 11, 4, 13, 35, 7, 963475)
  13. Huh?

  14. >>> from datetime import datetime >>> datetime.now() datetime.datetime(2017, 11, 4,

    10, 58, 21, 377726) Know your timezone
  15. >>> from datetime import datetime >>> datetime.now() datetime.datetime(2017, 11, 4,

    10, 58, 21, 377726) >>> datetime.utcnow() datetime.datetime(2017, 11, 4, 7, 58, 21, 377724) Know your timezone
  16. >>> from datetime import datetime, timezone >>> datetime.now() datetime.datetime(2017, 11,

    4, 10, 58, 21, 377726) >>> datetime.utcnow() datetime.datetime(2017, 11, 4, 7, 58, 21, 377724) >>> datetime.now(tz=timezone.utc) datetime.datetime(2017, 11, 4, 7, 58, 21, 377716, tzinfo=datetime.timezone.utc) Know your timezone
  17. pytz http://pythonhosted.org/pytz/ pip install pytz >>> from datetime import datetime

    >>> from pytz import timezone >>> datetime.now(tz=timezone('Europe/Moscow')) datetime.datetime( 2017, 11, 4, 11, 7, 55, 2540, tzinfo=<DstTzInfo 'Europe/Moscow' MSK+3:00:00 STD>)
  18. pytz >>> from datetime import datetime >>> from pytz import

    timezone >>> datetime.now(tz=timezone('Europe/Moscow')) datetime.datetime( 2017, 11, 4, 11, 7, 55, 2540, tzinfo=<DstTzInfo 'Europe/Moscow' MSK+3:00:00 STD>) >>> datetime.now(tz=timezone('UTC')) datetime.datetime(2017, 11, 4, 8, 9, 53, 395881, tzinfo=<UTC>)
  19. Daylight savings >>> # Before Python 3.6 >>> eastern =

    timezone('US/Eastern') >>> eastern.localize(datetime(2002, 10, 27, 1, 30, 0), is_dst=False) datetime.datetime( 2002, 10, 27, 1, 30, tzinfo=<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>) >>> eastern.localize(datetime(2002, 10, 27, 1, 30, 0), is_dst=True) datetime.datetime( 2002, 10, 27, 1, 30, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)
  20. Daylight savings >>> # Python 3.6+ >>> eastern = timezone('US/Eastern')

    >>> eastern.localize(datetime(2002, 10, 27, 1, 30, 0, fold=0)) >>> eastern.localize(datetime(2002, 10, 27, 1, 30, 0, fold=1))
  21. Daylight savings >>> # Python 3.6+ >>> eastern = timezone('US/Eastern')

    >>> eastern.localize(datetime(2002, 10, 27, 1, 30, 0, fold=0)) datetime.datetime( 2002, 10, 27, 1, 30, tzinfo=<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>) >>> eastern.localize(datetime(2002, 10, 27, 1, 30, 0, fold=1)) datetime.datetime( 2002, 10, 27, 1, 30, tzinfo=<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>)
  22. Daylight savings >>> # Python 3.6+ >>> eastern = timezone('US/Eastern')

    >>> eastern.localize(datetime(2002, 10, 27, 1, 30, 0, fold=0)) datetime.datetime( 2002, 10, 27, 1, 30, tzinfo=<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>) >>> eastern.localize(datetime(2002, 10, 27, 1, 30, 0, fold=1)) datetime.datetime( 2002, 10, 27, 1, 30, tzinfo=<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>)
  23. Daylight savings >>> # Use `is_dst` for now >>> eastern

    = timezone('US/Eastern') >>> eastern.localize(datetime(2002, 10, 27, 1, 30, 0), is_dst=False) datetime.datetime( 2002, 10, 27, 1, 30, tzinfo=<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>) >>> eastern.localize(datetime(2002, 10, 27, 1, 30, 0), is_dst=True) datetime.datetime( 2002, 10, 27, 1, 30, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)
  24. The Real World

  25. From ISO >>> datetime.strptime("2017-11-04T11:41:35.450686Z", "%Y-%m-%dT%H:%M:%S.%fZ") datetime.datetime(2017, 11, 4, 11, 41,

    35, 450686)
  26. From ISO >>> datetime.strptime("2017-11-04T11:41:35.450686Z", "%Y-%m-%dT%H:%M:%S.%fZ") datetime.datetime(2017, 11, 4, 11, 41,

    35, 450686) >>> datetime.strptime("2017-11-04T11:41:35.450686+0300", "%Y-%m-%dT%H:%M:%S.%f%z") datetime.datetime( 2017, 11, 4, 11, 41, 35, 450686, tzinfo=datetime.timezone(datetime.timedelta(0, 10800)))
  27. From ISO >>> datetime.strptime("2017-11-04T11:41:35.450686Z", "%Y-%m-%dT%H:%M:%S.%fZ") datetime.datetime(2017, 11, 4, 11, 41,

    35, 450686) >>> datetime.strptime("2017-11-04T11:41:35.450686+0300", "%Y-%m-%dT%H:%M:%S.%f%z") datetime.datetime( 2017, 11, 4, 11, 41, 35, 450686, tzinfo=datetime.timezone(datetime.timedelta(0, 10800)))
  28. The Real World [ "2017-11-04T11:41:35.450686Z", "2017-11-04T11:41:35.450686+0300", "2017-11-04T11:41:35.450686MSK", "2017-11-04 11:41:35.450686MSK", "2017-11-04

    11:41:35", "2017-11-04 11:41", "2017-11-04", "2017/11/04", ]
  29. The Real World [ "2017-11-04T11:41:35.450686Z", "2017-11-04T11:41:35.450686+0300", "2017-11-04T11:41:35.450686MSK", "2017-11-04 11:41:35.450686MSK", "2017-11-04

    11:41:35", "2017-11-04 11:41", "2017-11-04", "2017/11/04", "11/04/2017", "11/04/17", "04.11.17", "04/11/17", "4 Nov 17", "4 Ноя 17", "4 Ноября 17", ]
  30. The Real World [ "2017-11-04T11:41:35.450686Z", "2017-11-04T11:41:35.450686+0300", "2017-11-04T11:41:35.450686MSK", "2017-11-04 11:41:35.450686MSK", "2017-11-04

    11:41:35", "2017-11-04 11:41", "2017-11-04", "2017/11/04", "11/04/2017", "11/04/17", "04.11.17", "04/11/17", "4 Nov 17", "4 Ноя 17", "4 Ноября 17", "Tomorrow 11:41", "Tomorrow 11.41am", "Tomorrow 2pm", ]
  31. The Real World [ "2017-11-04T11:41:35.450686Z", "2017-11-04T11:41:35.450686+0300", "2017-11-04T11:41:35.450686MSK", "2017-11-04 11:41:35.450686MSK", "2017-11-04

    11:41:35", "2017-11-04 11:41", "2017-11-04", "2017/11/04", "11/04/2017", "11/04/17", "04.11.17", "04/11/17", "4 Nov 17", "4 Ноя 17", "4 Ноября 17", "Tomorrow 11:41", "Tomorrow 11.41am", "Tomorrow 2pm", "ASAP", ]
  32. Tools for the Real World

  33. Tools for the Real World • dateutil parser https://dateutil.readthedocs.io/en/stable/parser.html •

    parsedatetime https://github.com/bear/parsedatetime • dateparser https://github.com/scrapinghub/dateparser
  34. Tools for the Real World!!! >>> 2017-11-04T11:41:35.450686Z dateutil.parser 2017-11-04 11:41:35.450686+00:00

    parsedatetime 2017-11-04 20:17:00 dateparser 2017-11-04 11:41:35.450686+00:00 >>> 2017-11-04T11:41:35.450686+0300 dateutil.parser 2017-11-04 11:41:35.450686+03:00 parsedatetime 2017-11-04 20:17:00 dateparser 2017-11-04 11:41:35.450686+03:00
  35. Tools for the Real World!!! >>> 2017-11-04T11:41:35.450686MSK dateutil.parser 2017-11-04 11:41:35.450686+03:00

    parsedatetime 2017-11-04 20:17:00 dateparser 2017-11-04 11:41:35.450686+03:00 >>> 2017-11-04 11:41:35.450686MSK dateutil.parser 2017-11-04 11:41:35.450686+03:00 parsedatetime 2017-11-04 11:41:35 dateparser 2017-11-04 11:41:35.450686+03:00
  36. Tools for the Real World!!! >>> 2017-11-04 11:41:35 dateutil.parser 2017-11-04

    11:41:35 parsedatetime 2017-11-04 11:41:35 dateparser 2017-11-04 11:41:35 >>> 2017-11-04 11:41 dateutil.parser 2017-11-04 11:41:00 parsedatetime 2017-11-04 11:41:00 dateparser 2017-11-04 11:41:00
  37. Tools for the Real World!!! >>> 2017-11-04 dateutil.parser 2017-11-04 00:00:00

    parsedatetime 2017-11-04 12:23:56 dateparser 2017-11-04 00:00:00 >>> 2017/11/04 dateutil.parser 2017-11-04 00:00:00 parsedatetime 2017-11-04 12:23:56 dateparser 2017-11-04 00:00:00
  38. Tools for the Real World!!! >>> 11/04/2017 dateutil.parser 2017-11-04 00:00:00

    parsedatetime 2017-11-04 12:23:56 dateparser 2017-11-04 00:00:00 >>> 11/04/17 dateutil.parser 2017-11-04 00:00:00 parsedatetime 2017-11-04 12:23:56 dateparser 2017-11-04 00:00:00
  39. Tools for the Real World!!! >>> 04.11.17 dateutil.parser 2017-04-11 00:00:00

    parsedatetime 2017-04-11 12:23:57 dateparser 2017-04-11 00:00:00 >>> 04/11/17 dateutil.parser 2017-04-11 00:00:00 parsedatetime 2017-04-11 12:23:57 dateparser 2017-04-11 00:00:00
  40. Tools for the Real World!!! >>> 4 Nov 17 dateutil.parser

    2017-11-04 00:00:00 parsedatetime 2017-11-17 12:23:57 dateparser 2017-11-04 00:00:00 >>> 4 Ноя 17 parsedatetime 2017-11-04 12:23:57 dateparser 2017-11-04 00:00:00
  41. Tools for the Real World!!! >>> 4 Ноября 17 parsedatetime

    2017-11-04 12:23:57 dateparser 2017-11-04 00:00:00 >>> Tomorrow 11:41 parsedatetime 2017-11-05 11:41:00 dateparser 2017-11-05 11:41:00 >>> Tomorrow 11.41am parsedatetime 2017-11-05 09:00:00 dateparser 2017-11-05 12:23:57.303421
  42. Tools for the Real World!!! >>> Tomorrow 2pm parsedatetime 2017-11-05

    14:00:00 dateparser 2017-11-05 14:00:00 >>> ASAP parsedatetime 2017-11-04 12:23:57 dateutil.parser: 13 parsed 0.68 rate parsedatetime: 19 parsed 1.0 rate dateparser: 18 parsed 0.95 rate
  43. Serge Matveenko Twitter @lig1 github.com/lig Questions?