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

Serge Matveenko

November 04, 2017
Tweet

More Decks by Serge Matveenko

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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 :(
  4. 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)
  5. 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
  6. 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)
  7. >>> 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
  8. >>> 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
  9. 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>)
  10. 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>)
  11. 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>)
  12. 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))
  13. 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>)
  14. 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>)
  15. 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>)
  16. 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)))
  17. 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)))
  18. 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", ]
  19. 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", ]
  20. 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", ]
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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