python-dateutil-1.5

所属分类:其他书籍
开发工具:Python
文件大小:226KB
下载次数:12
上传日期:2012-10-05 10:59:27
上 传 者zhaoxiaolin00
说明:  python dateutil 版本1.15
(python dateutil)

文件列表:
python-dateutil-1.5\dateutil\easter.py (2633, 2007-11-20)
python-dateutil-1.5\dateutil\parser.py (32464, 2008-02-28)
python-dateutil-1.5\dateutil\relativedelta.py (17135, 2010-03-30)
python-dateutil-1.5\dateutil\rrule.py (40402, 2010-03-30)
python-dateutil-1.5\dateutil\tz.py (32741, 2008-02-28)
python-dateutil-1.5\dateutil\tzwin.py (5828, 2007-11-20)
python-dateutil-1.5\dateutil\zoneinfo\zoneinfo-2010g.tar.gz (171995, 2010-03-30)
python-dateutil-1.5\dateutil\zoneinfo\__init__.py (2575, 2005-12-23)
python-dateutil-1.5\dateutil\__init__.py (252, 2010-03-30)
python-dateutil-1.5\example.py (529, 2004-07-17)
python-dateutil-1.5\LICENSE (12930, 2003-10-09)
python-dateutil-1.5\Makefile (317, 2005-02-24)
python-dateutil-1.5\MANIFEST.in (166, 2005-02-24)
python-dateutil-1.5\NEWS (4116, 2010-03-30)
python-dateutil-1.5\PKG-INFO (402, 2010-03-30)
python-dateutil-1.5\python_dateutil.egg-info\dependency_links.txt (1, 2010-03-30)
python-dateutil-1.5\python_dateutil.egg-info\not-zip-safe (1, 2008-08-07)
python-dateutil-1.5\python_dateutil.egg-info\PKG-INFO (402, 2010-03-30)
python-dateutil-1.5\python_dateutil.egg-info\SOURCES.txt (538, 2010-03-30)
python-dateutil-1.5\python_dateutil.egg-info\top_level.txt (9, 2010-03-30)
python-dateutil-1.5\sandbox\rrulewrapper.py (476, 2004-09-17)
python-dateutil-1.5\sandbox\scheduler.py (4049, 2005-02-25)
python-dateutil-1.5\setup.cfg (99, 2010-03-30)
python-dateutil-1.5\setup.py (922, 2010-01-11)
python-dateutil-1.5\test.py (173682, 2010-03-30)
python-dateutil-1.5\updatezinfo.py (1230, 2005-02-25)
python-dateutil-1.5\dateutil\zoneinfo (0, 2012-08-05)
python-dateutil-1.5\dateutil (0, 2012-08-05)
python-dateutil-1.5\python_dateutil.egg-info (0, 2012-08-05)
python-dateutil-1.5\sandbox (0, 2012-08-05)
python-dateutil-1.5 (0, 2012-08-05)

## This file is in the moin format. The latest version is found ## at https://moin.conectiva.com.br/DateUtil == Contents == [[TableOfContents]] == Description == The '''dateutil''' module provides powerful extensions to the standard '''datetime''' module, available in Python 2.3+. == 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 [ftp://ftp.rfc-editor.org/in-notes/rfc2445.txt 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; * More than 400 test cases. == Quick example == Here's a snapshot, just to give an idea about the power of the package. For more examples, look at the documentation below. 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 * import commands import os now = parse(commands.getoutput("date")) today = now.date() year = rrule(YEARLY,bymonth=8,bymonthday=13,byweekday=FR)[0].year rdelta = relativedelta(easter(year), today) print "Today is:", today print "Year with next Aug 13th on a Friday is:", year print "How far is the Easter of that year:", rdelta print "And the Easter of that year is:", today+rdelta }}} And here's the output: {{{ Today is: 2003-10-11 Year with next Aug 13th on a Friday is: 2004 How far is the Easter of that year: relativedelta(months=+6) And the Easter of that year is: 2004-04-11 }}} {i} Being exactly 6 months ahead was '''really''' a coincidence :) == Download == The following files are available. * attachment:python-dateutil-1.0.tar.bz2 * attachment:python-dateutil-1.0-1.noarch.rpm == Author == The dateutil module was written by GustavoNiemeyer . == Documentation == The following modules are available. === relativedelta === This module offers the '''relativedelta''' type, which is based on the specification of the excelent work done by M.-A. Lemburg in his [http://www.egenix.com/files/python/mxDateTime.html mxDateTime] extension. However, notice that this type '''does not''' implement the same algorithm as his work. Do not expect it to behave like {{{mxDateTime}}}'s counterpart. ==== relativedelta type ==== There's two different ways to build a relativedelta instance. The first one is passing it two {{{date}}}/{{{datetime}}} instances: {{{ relativedelta(datetime1, datetime2) }}} This will build the relative difference between {{{datetime1}}} and {{{datetime2}}}, so that the following constraint is always true: {{{ datetime2+relativedelta(datetime1, datetime2) == datetime1 }}} Notice that instead of {{{datetime}}} instances, you may use {{{date}}} instances, or a mix of both. And the other way is to use any of the following keyword arguments: year, month, day, hour, minute, second, microsecond:: Absolute information. years, months, weeks, days, hours, minutes, seconds, microseconds:: Relative information, may be negative. weekday:: One of the weekday instances ({{{MO}}}, {{{TU}}}, etc). These instances may receive a parameter {{{n}}}, specifying the {{{n}}}th weekday, which could be positive or negative (like {{{MO(+2)}}} or {{{MO(-3)}}}. Not specifying it is the same as specifying {{{+1}}}. You can also use an integer, where {{{0=MO}}}. Notice that, for example, if the calculated date is already Monday, using {{{MO}}} or {{{MO(+1)}}} (which is the same thing in this context), won't change the day. leapdays:: Will add given days to the date found, but only if the computed year is a leap year and the computed date is post 28 of february. yearday, nlyearday:: Set the yearday or the non-leap year day (jump leap days). These are converted to {{{day}}}/{{{month}}}/{{{leapdays}}} information. ==== Behavior of operations ==== If you're curious about exactly how the relative delta will act on operations, here is a description of its behavior. 1. Calculate the absolute year, using the {{{year}}} argument, or the original datetime year, if the argument is not present. 1. Add the relative {{{years}}} argument to the absolute year. 1. Do steps 1 and 2 for {{{month}}}/{{{months}}}. 1. Calculate the absolute day, using the {{{day}}} argument, or the original datetime day, if the argument is not present. Then, subtract from the day until it fits in the year and month found after their operations. 1. Add the relative {{{days}}} argument to the absolute day. Notice that the {{{weeks}}} argument is multiplied by 7 and added to {{{days}}}. 1. If {{{leapdays}}} is present, the computed year is a leap year, and the computed month is after february, remove one day from the found date. 1. Do steps 1 and 2 for {{{hour}}}/{{{hours}}}, {{{minute}}}/{{{minutes}}}, {{{second}}}/{{{seconds}}}, {{{microsecond}}}/{{{microseconds}}}. 1. If the {{{weekday}}} argument is present, calculate the {{{n}}}th occurrence of the given weekday. ==== Examples ==== Let's begin our trip. {{{ >>> from datetime import *; from dateutil.relativedelta import * >>> import calendar }}} Store some values. {{{ >>> NOW = datetime.now() >>> TODAY = date.today() >>> NOW datetime.datetime(2003, 9, 17, 20, 54, 47, 282310) >>> TODAY datetime.date(2003, 9, 17) }}} Next month. {{{ >>> NOW+relativedelta(months=+1) datetime.datetime(2003, 10, 17, 20, 54, 47, 282310) }}} Next month, plus one week. {{{ >>> NOW+relativedelta(months=+1, weeks=+1) datetime.datetime(2003, 10, 24, 20, 54, 47, 282310) }}} Next month, plus one week, at 10am. {{{ >>> TODAY+relativedelta(months=+1, weeks=+1, hour=10) datetime.datetime(2003, 10, 24, 10, 0) }}} Let's try the other way around. Notice that the hour setting we get in the relativedelta is relative, since it's a difference, and the weeks parameter has gone. {{{ >>> relativedelta(datetime(2003, 10, 24, 10, 0), TODAY) relativedelta(months=+1, days=+7, hours=+10) }}} One month before one year. {{{ >>> NOW+relativedelta(years=+1, months=-1) datetime.datetime(2004, 8, 17, 20, 54, 47, 282310) }}} How does it handle months with different numbers of days? Notice that adding one month will never cross the month boundary. {{{ >>> date(2003,1,27)+relativedelta(months=+1) datetime.date(2003, 2, 27) >>> date(2003,1,31)+relativedelta(months=+1) datetime.date(2003, 2, 28) >>> date(2003,1,31)+relativedelta(months=+2) datetime.date(2003, 3, 31) }}} The logic for years is the same, even on leap years. {{{ >>> date(2000,2,28)+relativedelta(years=+1) datetime.date(2001, 2, 28) >>> date(2000,2,29)+relativedelta(years=+1) datetime.date(2001, 2, 28) >>> date(1999,2,28)+relativedelta(years=+1) datetime.date(2000, 2, 28) >>> date(1999,3,1)+relativedelta(years=+1) datetime.date(2000, 3, 1) >>> date(2001,2,28)+relativedelta(years=-1) datetime.date(2000, 2, 28) >>> date(2001,3,1)+relativedelta(years=-1) datetime.date(2000, 3, 1) }}} Next friday. {{{ >>> TODAY+relativedelta(weekday=FR) datetime.date(2003, 9, 19) >>> TODAY+relativedelta(weekday=calendar.FRIDAY) datetime.date(2003, 9, 19) }}} Last friday in this month. {{{ >>> TODAY+relativedelta(day=31, weekday=FR(-1)) datetime.date(2003, 9, 26) }}} Next wednesday (it's today!). {{{ >>> TODAY+relativedelta(weekday=WE(+1)) datetime.date(2003, 9, 17) }}} Next wednesday, but not today. {{{ >>> TODAY+relativedelta(days=+1, weekday=WE(+1)) datetime.date(2003, 9, 24) }}} Following [http://www.cl.cam.ac.uk/~mgk25/iso-time.html ISO year week number notation] find the first day of the 15th week of 1997. {{{ >>> datetime(1997,1,1)+relativedelta(day=4, weekday=MO(-1), weeks=+14) datetime.datetime(1997, 4, 7, 0, 0) }}} How long ago has the millennium changed? {{{ >>> relativedelta(NOW, date(2001,1,1)) relativedelta(years=+2, months=+8, days=+16, hours=+20, minutes=+54, seconds=+47, microseconds=+282310) }}} How old is John? {{{ >>> johnbirthday = datetime(1978, 4, 5, 12, 0) >>> relativedelta(NOW, johnbirthday) relativedelta(years=+25, months=+5, days=+12, hours=+8, minutes=+54, seconds=+47, microseconds=+282310) }}} It works with dates too. {{{ >>> relativedelta(TODAY, johnbirthday) relativedelta(years=+25, months=+5, days=+11, hours=+12) }}} Obtain today's date using the yearday: {{{ >>> date(2003, 1, 1)+relativedelta(yearday=260) datetime.date(2003, 9, 17) }}} We can use today's date, since yearday should be absolute in the given year: {{{ >>> TODAY+relativedelta(yearday=260) datetime.date(2003, 9, 17) }}} Last year it should be in the same day: {{{ >>> date(2002, 1, 1)+relativedelta(yearday=260) datetime.date(2002, 9, 17) }}} But not in a leap year: {{{ >>> date(2000, 1, 1)+relativedelta(yearday=260) datetime.date(2000, 9, 16) }}} We can use the non-leap year day to ignore this: {{{ >>> date(2000, 1, 1)+relativedelta(nlyearday=260) datetime.date(2000, 9, 17) }}} === rrule === The rrule module offers a small, complete, and very fast, implementation of the recurrence rules documented in the [ftp://ftp.rfc-editor.org/in-notes/rfc2445.txt iCalendar RFC], including support for caching of results. ==== rrule type ==== That's the base of the rrule operation. It accepts all the keywords defined in the RFC as its constructor parameters (except {{{byday}}}, which was renamed to {{{byweekday}}}) and more. The constructor prototype is: {{{ rrule(freq) }}} Where {{{freq}}} must be one of {{{YEARLY}}}, {{{MONTHLY}}}, {{{WEEKLY}}}, {{{DAILY}}}, {{{HOURLY}}}, {{{MINUTELY}}}, or {{{SECONDLY}}}. Additionally, it supports the following keyword arguments: cache:: If given, it must be a boolean value specifying to enable or disable caching of results. If you will use the same {{{rrule}}} instance multiple times, enabling caching will improve the performance considerably. dtstart:: The recurrence start. Besides being the base for the recurrence, missing parameters in the final recurrence instances will also be extracted from this date. If not given, {{{datetime.now()}}} will be used instead. interval:: The interval between each {{{freq}}} iteration. For example, when using {{{YEARLY}}}, an interval of {{{2}}} means once every two years, but with {{{HOURLY}}}, it means once every two hours. The default interval is {{{1}}}. wkst:: The week start day. Must be one of the {{{MO}}}, {{{TU}}}, {{{WE}}} constants, or an integer, specifying the first day of the week. This will affect recurrences based on weekly periods. The default week start is got from {{{calendar.firstweekday()}}}, and may be modified by {{{calendar.setfirstweekday()}}}. count:: How many occurrences will be generated. until:: If given, this must be a {{{datetime}}} instance, that will specify the limit of the recurrence. If a recurrence instance happens to be the same as the {{{datetime}}} instance given in the {{{until}}} keyword, this will be the last occurrence. bysetpos:: If given, it must be either an integer, or a sequence of integers, positive or negative. Each given integer will specify an occurrence number, corresponding to the nth occurrence of the rule inside the frequency period. For example, a {{{bysetpos}}} of {{{-1}}} if combined with a {{{MONTHLY}}} frequency, and a {{{byweekday}}} of {{{(MO, TU, WE, TH, FR)}}}, will result in the last work day of every month. bymonth:: If given, it must be either an integer, or a sequence of integers, meaning the months to apply the recurrence to. bymonthday:: If given, it must be either an integer, or a sequence of integers, meaning the month days to apply the recurrence to. byyearday:: If given, it must be either an integer, or a sequence of integers, meaning the year days to apply the recurrence to. byweekno:: If given, it must be either an integer, or a sequence of integers, meaning the week numbers to apply the recurrence to. Week numbers have the meaning described in ISO8601, that is, the first week of the year is that containing at least four days of the new year. byweekday:: If given, it must be either an integer ({{{0 == MO}}}), a sequence of integers, one of the weekday constants ({{{MO}}}, {{{TU}}}, etc), or a sequence of these constants. When given, these variables will define the weekdays where the recurrence will be applied. It's also possible to use an argument {{{n}}} for the weekday instances, which will mean the {{{n}}}''th'' occurrence of this weekday in the period. For example, with {{{MONTHLY}}}, or with {{{YEARLY}}} and {{{BYMONTH}}}, using {{{FR(+1)}}} in {{{byweekday}}} will specify the first friday of the month where the recurrence happens. Notice that in the RFC documentation, this is specified as {{{BYDAY}}}, but was renamed to avoid the ambiguity of that keyword. byhour:: If given, it must be either an integer, or a sequence of integers, meaning the hours to apply the recurrence to. byminute:: If given, it must be either an integer, or a sequence of integers, meaning the minutes to apply the recurrence to. bysecond:: If given, it must be either an integer, or a sequence of integers, meaning the seconds to apply the recurrence to. byeaster:: If given, it must be either an integer, or a sequence of integers, positive or negative. Each integer will define an offset from the Easter Sunday. Passing the offset {{{0}}} to {{{byeaster}}} will yield the Easter Sunday itself. This is an extension to the RFC specification. ==== rrule methods ==== The following methods are available in {{{rrule}}} instances: rrule.before(dt, inc=False):: Returns the last recurrence before the given {{{datetime}}} instance. The {{{inc}}} keyword defines what happens if {{{dt}}} '''is''' an occurrence. With {{{inc == True}}}, if {{{dt}}} itself is an occurrence, it will be returned. rrule.after(dt, inc=False):: Returns the first recurrence after the given {{{datetime}}} instance. The {{{inc}}} keyword defines what happens if {{{dt}}} '''is''' an occurrence. With {{{inc == True}}}, if {{{dt}}} itself is an occurrence, it will be returned. rrule.between(after, before, inc=False):: Returns all the occurrences of the rrule between {{{after}}} and {{{before}}}. The {{{inc}}} keyword defines what happens if {{{after}}} and/or {{{before}}} are themselves occurrences. With {{{inc == True}}}, they will be included in the list, if they are found in the recurrence set. rrule.count():: Returns the number of recurrences in this set. It will have go trough the whole recurrence, if this hasn't been done before. Besides these methods, {{{rrule}}} instances also support the {{{__getitem__()}}} and {{{__contains__()}}} special methods, meaning that these are valid expressions: {{{ rr = rrule(...) if datetime(...) in rr: ... print rr[0] print rr[-1] print rr[1:2] print rr[::-2] }}} The getitem/slicing mechanism is smart enough to avoid getting the whole recurrence set, if possible. ==== Notes ==== * The rrule type has no {{{byday}}} keyword. The equivalent keyword has been replaced by the {{{byweekday}}} keyword, to remove the ambiguity present in the original keyword. * Unlike documented in the RFC, the starting datetime ({{{dtstart}}}) is not the first recurrence instance, unless it does fit in the specified rules. In a python module context, this behavior makes more sense than otherwise. Notice that you can easily get the original behavior by using a rruleset and adding the {{{dtstart}}} as an {{{rdate}}} recurrence. * Unlike documented in the RFC, every keyword is valid on every frequency (the RFC documents that {{{byweekno}}} is only valid on yearly frequencies, for example). * In addition to the documented keywords, a {{{byeaster}}} keyword was introduced, making it easy to compute recurrent events relative to the Easter Sunday. ==== rrule examples ==== These examples were converted from the RFC. Prepare the environment. {{{ >>> from dateutil.rrule import * >>> from dateutil.parser import * >>> from datetime import * >>> import pprint >>> import sys >>> sys.displayhook = pprint.pprint }}} Daily, for 10 occurrences. {{{ >>> list(rrule(DAILY, count=10, dtstart=parse("19970902T090000"))) [datetime.datetime(1997, 9, 2, 9, 0), datetime.datetime(1997, 9, 3, 9, 0), datetime.datetime(1997, 9, 4, 9, 0), datetime.datetime(1997, 9, 5, 9, 0), datetime.datetime(1997, 9, 6, 9, 0), datetime.datetime(1997, 9, 7, 9, 0), datetime.datetime(1997, 9, 8, 9, 0), datetime.datetime(1997, 9, 9, 9, 0), datetime.datetime(1997, 9, 10, 9, 0), datetime.datetime(1997, 9, 11, 9, 0)] }}} Daily until December 24, 1997 {{{ >>> list(rrule(DAILY, dtstart=parse("19970902T090000"), until=parse("19971224T000000"))) [datetime.datetime(1997, 9, 2, 9, 0), datetime.datetime(1997, 9, 3, 9, 0), datetime.datetime(1997, 9, 4, 9, 0), (...) datetime.datetime(1997, 12, 21, 9, 0), datetime.datetime(1997, 12, 22, 9, 0), datetime.datetime(1997, 12, 23, 9, 0)] }}} Every other day, 5 occurrences. {{{ >>> list(rrule(DAILY, interval=2, count=5, dtstart=parse("19970902T090000"))) [datetime.datetime(1997, 9, 2, 9, 0), datetime.datetime(1997, 9, 4, 9, 0), datetime.datetime(1997, 9, 6, 9, 0), datetime.datetime(1997, 9, 8, 9, 0), datetime.datetime(1997, 9, 10, 9, 0)] }}} Every 10 days, 5 occurrences. {{{ >>> list(rrule(DAILY, interval=10, count=5, dtstart=parse("19970902T090000"))) [datetime.datetime(1997, 9, 2, 9, 0), datetime.datetime(1997, 9, 12, 9, 0), datetime.datetime(1997, 9, 22, 9, 0), datetime.datetime(1997, 10, 2, 9, 0), datetime.datetime(1997, 10, 12, 9, 0)] }}} Everyday in January, for 3 years. {{{ >>> list(rrule(YEARLY, bymonth=1, byweekday=range(7), dtstart=parse("19***0101T090000"), until=parse("20000131T090000"))) [datetime.datetime(19***, 1, 1, 9, 0), datetime.datetime(19***, 1, 2, 9, 0), (...) datetime.datetime(19***, 1, 30, 9, 0), datetime.datetime(19***, 1, 31, 9, 0), datetime.datetime(1999, 1, 1, 9, 0), datetime.datetime(1999, 1, 2, 9, 0), (...) datetime.datetime(1999, 1, 30, 9, 0), datetime.datetime(1999, 1, 31, 9, 0), datetime.datetime(2000, 1, 1, 9, 0), datetime.datetime(2000, 1, 2, 9, 0), ... ...

近期下载者

相关文件


收藏者