Python third-party library recommendation-dateutil

Source: Internet
Author: User

In dateutil, there are two items that attract me, one is parser and the other is rrule.

Parser parses the string into datetime, while rrule generates datetime according to the defined rules.

Install

You do not need to download the source code package for manual installation. You can directly use easy_install or pip for online installation.
Easy_install Python-dateutil

PIP install Python-dateutil

About parser

Strings can be casual. You can use English words of time and date, and use hyphens, commas, and spaces as separators.

If no time is specified, the default value is 0. if no date is specified, the default value is today. If no year is specified, the default value is this year.

> From dateutil. parser import parse

> Parse ("wed, Nov 12 ")
Datetime. datetime (2013, 11, 12, 0, 0)
> Parse ("2013-08-20 ")
Datetime. datetime (2013, 8, 20, 0, 0)
> Parse ("20130820 ")
Datetime. datetime (2013, 8, 20, 0, 0)
> Parse ("Maid, 20 ")
Datetime. datetime (2013, 8, 20, 0, 0)
> Parse ("08,20 ")
Datetime. datetime (2013, 8, 20, 0, 0)
> Parse ("12:00:00 ")
Datetime. datetime (2013, 8, 20, 12, 0)

> Parse ("this is the wonderful moment 12:00:00, I feel good", fuzzy = true) # enable fuzzy match to filter out unrecognized time and date characters

Datetime. datetime (2013, 8, 20, 12, 0)

About rrule

The function prototype is as follows.
Rrule (self, freq, dtstart = none, interval = 1, wkst = none, Count = none, until = none, bysetpos = none,
Bymonth = none, bymonthday = none, byyearday = none, byeaster = none, byweekno = none, byweekday = none, byhour = none, byminute = none, bysecond = none, cache = false)
Where
Freq: unit. It can be yearly, monthly, weekly, daily, hourly, minutely, and secondly. That is, year, month, day, week, minute, and second.
Dtstart, until: the start time and end time.
Wkst: the start time of the week.
Interval: interval.
Count: number of instances generated.
Byxxx: Specifies the matching period. For example, byweekday = (Mo, tu) only matches Monday and Tuesday. Byweekday can specify Mo, tu, we, Th, FR, SA, Su. Monday to Sunday.

> From dateutil. rrule import *
> List (rrule (daily, dtstart = parse ('2017-08-01 '), until = parse ('2017-08-07') #2013 to 2013 daily
[Datetime. datetime (2013, 8, 1, 0, 0 ),
Datetime. datetime (2013, 8, 2, 0, 0 ),
Datetime. datetime (2013, 8, 3, 0, 0 ),
Datetime. datetime (2013, 8, 4, 0, 0 ),
Datetime. datetime (2013, 8, 5, 0, 0 ),
Datetime. datetime (2013, 8, 6, 0, 0 ),
Datetime. datetime (2013, 8, 7, 0, 0)]

> List (rrule (daily, interval = 3, dtstart = parse ('2017-08-01 '), until = parse ('2017-08-07') #3
[Datetime. datetime (2013, 8, 1, 0, 0 ),
Datetime. datetime (2013, 8, 4, 0, 0 ),
Datetime. datetime (2013, 8, 7, 0, 0)]

> List (rrule (daily, Count = 3, dtstart = parse ('2017-08-01 '), until = parse ('2017-08-07') # generate only three
[Datetime. datetime (2013, 8, 1, 0, 0 ),
Datetime. datetime (2013, 8, 2, 0, 0 ),
Datetime. datetime (2013, 8, 3, 0, 0)]

> List (rrule (daily, byweekday = (Mo, tu), dtstart = parse ('2017-08-01 '), until = parse ('2017-08-07 '))) # match only Monday and Tuesday
[Datetime. datetime (2013, 8, 5, 0, 0), datetime. datetime (2013, 8, 6, 0, 0)]

> List (rrule (monthly, dtstart = parse ('2017-05-19 '), until = parse ('2017-08-20') # monthly
[Datetime. datetime (2013, 5, 19, 0, 0 ),
Datetime. datetime (2013, 6, 19, 0, 0 ),
Datetime. datetime (2013, 7, 19, 0, 0 ),
Datetime. datetime (2013, 8, 19, 0, 0)]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.