Skip to content

Latest commit

 

History

History
111 lines (75 loc) · 7.62 KB

File metadata and controls

111 lines (75 loc) · 7.62 KB

python-dateutil

  • dateutil - powerful extensions to datetime — dateutil 2.8.0 documentation

    • The dateutil module provides powerful extensions to the standard datetime module, available in Python.

    Features

    • Computing of relative deltas (next month, next year, next monday, last week of month, etc);
    • Computing of relative deltas between two given date and/or datetime objects;
    • Computing of dates based on very flexible recurrence rules, using a superset of the iCalendar specification. Parsing of RFC strings is supported as well.
    • GENERIC PARSING of dates in almost any string format;
    • Timezone (tzinfo) implementations for tzfile(5) format files (/etc/localtime, /usr/share/zoneinfo, etc), TZ environment string (in all known formats), iCalendar format files, given ranges (with help from relative deltas), local machine timezone, fixed offset timezone, UTC timezone, and Windows registry-based time zones.
    • Internal UP-TO-DATE world timezone information based on Olson’s database.
    • Computing of Easter Sunday dates for any given year, using Western, Orthodox or Julian algorithms;
    • A comprehensive test suite.
  • dateutil - powerful extensions to datetime — dateutil 2.6.1 documentation 抬頭寫著 "powerful extensions to datetime",又 "Generic parsing of dates in almost any string format" 似乎是它的強項,另外也提供了 tzinfo 的實作。

  • python - How to parse an ISO 8601-formatted date? - Stack Overflow 由於 datetime.strptime() 不好用,許多人推薦用 python-dateutil 來解析字串。

跟 pytz 的關係?

dateutil 主要是用它的工具 (尤其是 parsing),需要用到 timezone 時,習慣上還是會用 pytz。但 Python 3.6 之後似乎建議用 dateutil.tz?

參考資料:

新手上路 {: #getting-started }

  • dateutil - powerful extensions to datetime — dateutil 2.8.0 documentation

    • Suppose you want to know HOW MUCH TIME IS LEFT, in years/months/days/etc, before the next easter happening on a year with a Friday 13th in August, and you want to get today’s date out of the “date” unix system command. Here is the code:

      >>> from dateutil.relativedelta import *
      >>> from dateutil.easter import *
      >>> from dateutil.rrule import *
      >>> from dateutil.parser import *
      >>> from datetime import *
      >>> now = parse("Sat Oct 11 17:13:46 UTC 2003")
      >>> today = now.date()
      >>> year = rrule(YEARLY,dtstart=now,bymonth=8,bymonthday=13,byweekday=FR)[0].year
      >>> rdelta = relativedelta(easter(year), today)
      >>> print("Today is: %s" % today)
      Today is: 2003-10-11
      >>> print("Year with next Aug 13th on a Friday is: %s" % year)
      Year with next Aug 13th on a Friday is: 2004
      >>> print("How far is the Easter of that year: %s" % rdelta)
      How far is the Easter of that year: relativedelta(months=+6)
      >>> print("And the Easter of that year is: %s" % (today+rdelta))
      And the Easter of that year is: 2004-04-11
      

      範例有點難懂 ??

  • dateutil examples — dateutil 2.8.0 documentation #ril

  • Exercises — dateutil 2.8.0 documentation #ril

Parsing ??

  • parser — dateutil 2.8.0 documentation #ril

    • This module offers a GENERIC date/time string parser which is able to parse MOST KNOWN FORMATS to represent a date and/or time.

    • This module attempts to be FORGIVING with regards to unlikely input formats, returning a datetime object even for dates which are ambiguous. If an element of a date/time stamp is omitted, the following rules are applied:

      • If AM or PM is left unspecified, a 24-hour clock is assumed, however, an hour on a 12-hour clock (0 <= hour <= 12) must be specified if AM or PM is specified.

      • If a time zone is omitted, a timezone-NAIVE datetime is returned.

      • If any other elements are missing, they are taken from the datetime.datetime object passed to the parameter default.

        但為何 parser.parse('May 3') 會得到 datetime.datetime(2019, 5, 3, 0, 0)? 原來 dateutil.parser.parse 的實作 會拿當天的零時零分做為預設值:

        default = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
        

        If this results in a day number exceeding the valid number of days per month, the value FALLS BACK TO THE END OF THE MONTH.

        但為何 parser.parse('Feb. 30') 會丟 ValueError: day is out of range for month 的錯誤 ??

    • Additional resources about date/time string formats can be found below:

  • parser — dateutil 2.6.1 documentation "This module offers a generic date/time string parser which is able to parse most known formats to represent a date and/or time."

安裝設置 {: #setup }

pip 安裝 python-dateutil 套件即可:(注意不是 dateutil)

pip install python-dateutil

參考資料 {: #reference }

手冊: