summaryrefslogtreecommitdiffhomepage
path: root/libs/dateutil
diff options
context:
space:
mode:
authorpanni <[email protected]>2018-10-31 17:08:29 +0100
committerpanni <[email protected]>2018-10-31 17:08:29 +0100
commit8f584143f8afc46a75a83dab5243739772e3562b (patch)
treec7dae21e993880af8bee71ad7b5a63f2977db577 /libs/dateutil
parent4beaeaa99e84bbe1ed87d0466a55a22ba25c8437 (diff)
downloadbazarr-8f584143f8afc46a75a83dab5243739772e3562b.tar.gz
bazarr-8f584143f8afc46a75a83dab5243739772e3562b.zip
update deps
Diffstat (limited to 'libs/dateutil')
-rw-r--r--libs/dateutil/__init__.py8
-rw-r--r--libs/dateutil/_common.py12
-rw-r--r--libs/dateutil/easter.py4
-rw-r--r--libs/dateutil/parser.py1360
-rw-r--r--libs/dateutil/relativedelta.py169
-rw-r--r--libs/dateutil/rrule.py149
-rw-r--r--libs/dateutil/test/__init__.py0
-rw-r--r--libs/dateutil/test/_common.py288
-rw-r--r--libs/dateutil/test/test_easter.py99
-rw-r--r--libs/dateutil/test/test_imports.py149
-rw-r--r--libs/dateutil/test/test_parser.py861
-rw-r--r--libs/dateutil/test/test_relativedelta.py571
-rw-r--r--libs/dateutil/test/test_rrule.py4701
-rw-r--r--libs/dateutil/test/test_tz.py2114
-rw-r--r--libs/dateutil/tz/__init__.py15
-rw-r--r--libs/dateutil/tz/_common.py101
-rw-r--r--libs/dateutil/tz/tz.py611
-rw-r--r--libs/dateutil/tz/win.py13
-rw-r--r--libs/dateutil/tzwin.py2
-rw-r--r--libs/dateutil/zoneinfo/__init__.py50
-rw-r--r--libs/dateutil/zoneinfo/dateutil-zoneinfo.tar.gzbin139130 -> 139671 bytes
-rw-r--r--libs/dateutil/zoneinfo/rebuild.py10
22 files changed, 10470 insertions, 817 deletions
diff --git a/libs/dateutil/__init__.py b/libs/dateutil/__init__.py
index 0defb82e2..ba89aa70b 100644
--- a/libs/dateutil/__init__.py
+++ b/libs/dateutil/__init__.py
@@ -1,8 +1,2 @@
# -*- coding: utf-8 -*-
-try:
- from ._version import version as __version__
-except ImportError:
- __version__ = 'unknown'
-
-__all__ = ['easter', 'parser', 'relativedelta', 'rrule', 'tz',
- 'utils', 'zoneinfo']
+__version__ = "2.6.0"
diff --git a/libs/dateutil/_common.py b/libs/dateutil/_common.py
index 4eb2659bd..cd2a33860 100644
--- a/libs/dateutil/_common.py
+++ b/libs/dateutil/_common.py
@@ -2,7 +2,6 @@
Common code used in multiple modules.
"""
-
class weekday(object):
__slots__ = ["weekday", "n"]
@@ -24,14 +23,7 @@ class weekday(object):
return False
return True
- def __hash__(self):
- return hash((
- self.weekday,
- self.n,
- ))
-
- def __ne__(self, other):
- return not (self == other)
+ __hash__ = None
def __repr__(self):
s = ("MO", "TU", "WE", "TH", "FR", "SA", "SU")[self.weekday]
@@ -39,5 +31,3 @@ class weekday(object):
return s
else:
return "%s(%+d)" % (s, self.n)
-
-# vim:ts=4:sw=4:et
diff --git a/libs/dateutil/easter.py b/libs/dateutil/easter.py
index 53b7c7893..e4def97f9 100644
--- a/libs/dateutil/easter.py
+++ b/libs/dateutil/easter.py
@@ -41,11 +41,11 @@ def easter(year, method=EASTER_WESTERN):
More about the algorithm may be found at:
- `GM Arts: Easter Algorithms <http://www.gmarts.org/index.php?go=415>`_
+ http://users.chariot.net.au/~gmarts/eastalg.htm
and
- `The Calendar FAQ: Easter <https://www.tondering.dk/claus/cal/easter.php>`_
+ http://www.tondering.dk/claus/calendar.html
"""
diff --git a/libs/dateutil/parser.py b/libs/dateutil/parser.py
new file mode 100644
index 000000000..147b3f2ca
--- /dev/null
+++ b/libs/dateutil/parser.py
@@ -0,0 +1,1360 @@
+# -*- coding:iso-8859-1 -*-
+"""
+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
+:class:`datetime.datetime` object passed to the parameter ``default``. 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.
+
+Additional resources about date/time string formats can be found below:
+
+- `A summary of the international standard date and time notation
+ <http://www.cl.cam.ac.uk/~mgk25/iso-time.html>`_
+- `W3C Date and Time Formats <http://www.w3.org/TR/NOTE-datetime>`_
+- `Time Formats (Planetary Rings Node) <http://pds-rings.seti.org/tools/time_formats.html>`_
+- `CPAN ParseDate module
+ <http://search.cpan.org/~muir/Time-modules-2013.0912/lib/Time/ParseDate.pm>`_
+- `Java SimpleDateFormat Class
+ <https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html>`_
+"""
+from __future__ import unicode_literals
+
+import datetime
+import string
+import time
+import collections
+import re
+from io import StringIO
+from calendar import monthrange, isleap
+
+from six import text_type, binary_type, integer_types
+
+from . import relativedelta
+from . import tz
+
+__all__ = ["parse", "parserinfo"]
+
+
+class _timelex(object):
+ # Fractional seconds are sometimes split by a comma
+ _split_decimal = re.compile("([\.,])")
+
+ def __init__(self, instream):
+ if isinstance(instream, binary_type):
+ instream = instream.decode()
+
+ if isinstance(instream, text_type):
+ instream = StringIO(instream)
+
+ if getattr(instream, 'read', None) is None:
+ raise TypeError('Parser must be a string or character stream, not '
+ '{itype}'.format(itype=instream.__class__.__name__))
+
+ self.instream = instream
+ self.charstack = []
+ self.tokenstack = []
+ self.eof = False
+
+ def get_token(self):
+ """
+ This function breaks the time string into lexical units (tokens), which
+ can be parsed by the parser. Lexical units are demarcated by changes in
+ the character set, so any continuous string of letters is considered
+ one unit, any continuous string of numbers is considered one unit.
+
+ The main complication arises from the fact that dots ('.') can be used
+ both as separators (e.g. "Sep.20.2009") or decimal points (e.g.
+ "4:30:21.447"). As such, it is necessary to read the full context of
+ any dot-separated strings before breaking it into tokens; as such, this
+ function maintains a "token stack", for when the ambiguous context
+ demands that multiple tokens be parsed at once.
+ """
+ if self.tokenstack:
+ return self.tokenstack.pop(0)
+
+ seenletters = False
+ token = None
+ state = None
+
+ while not self.eof:
+ # We only realize that we've reached the end of a token when we
+ # find a character that's not part of the current token - since
+ # that character may be part of the next token, it's stored in the
+ # charstack.
+ if self.charstack:
+ nextchar = self.charstack.pop(0)
+ else:
+ nextchar = self.instream.read(1)
+ while nextchar == '\x00':
+ nextchar = self.instream.read(1)
+
+ if not nextchar:
+ self.eof = True
+ break
+ elif not state:
+ # First character of the token - determines if we're starting
+ # to parse a word, a number or something else.
+ token = nextchar
+ if self.isword(nextchar):
+ state = 'a'
+ elif self.isnum(nextchar):
+ state = '0'
+ elif self.isspace(nextchar):
+ token = ' '
+ break # emit token
+ else:
+ break # emit token
+ elif state == 'a':
+ # If we've already started reading a word, we keep reading
+ # letters until we find something that's not part of a word.
+ seenletters = True
+ if self.isword(nextchar):
+ token += nextchar
+ elif nextchar == '.':
+ token += nextchar
+ state = 'a.'
+ else:
+ self.charstack.append(nextchar)
+ break # emit token
+ elif state == '0':
+ # If we've already started reading a number, we keep reading
+ # numbers until we find something that doesn't fit.
+ if self.isnum(nextchar):
+ token += nextchar
+ elif nextchar == '.' or (nextchar == ',' and len(token) >= 2):
+ token += nextchar
+ state = '0.'
+ else:
+ self.charstack.append(nextchar)
+ break # emit token
+ elif state == 'a.':
+ # If we've seen some letters and a dot separator, continue
+ # parsing, and the tokens will be broken up later.
+ seenletters = True
+ if nextchar == '.' or self.isword(nextchar):
+ token += nextchar
+ elif self.isnum(nextchar) and token[-1] == '.':
+ token += nextchar
+ state = '0.'
+ else:
+ self.charstack.append(nextchar)
+ break # emit token
+ elif state == '0.':
+ # If we've seen at least one dot separator, keep going, we'll
+ # break up the tokens later.
+ if nextchar == '.' or self.isnum(nextchar):
+ token += nextchar
+ elif self.isword(nextchar) and token[-1] == '.':
+ token += nextchar
+ state = 'a.'
+ else:
+ self.charstack.append(nextchar)
+ break # emit token
+
+ if (state in ('a.', '0.') and (seenletters or token.count('.') > 1 or
+ token[-1] in '.,')):
+ l = self._split_decimal.split(token)
+ token = l[0]
+ for tok in l[1:]:
+ if tok:
+ self.tokenstack.append(tok)
+
+ if state == '0.' and token.count('.') == 0:
+ token = token.replace(',', '.')
+
+ return token
+
+ def __iter__(self):
+ return self
+
+ def __next__(self):
+ token = self.get_token()
+ if token is None:
+ raise StopIteration
+
+ return token
+
+ def next(self):
+ return self.__next__() # Python 2.x support
+
+ @classmethod
+ def split(cls, s):
+ return list(cls(s))
+
+ @classmethod
+ def isword(cls, nextchar):
+ """ Whether or not the next character is part of a word """
+ return nextchar.isalpha()
+
+ @classmethod
+ def isnum(cls, nextchar):
+ """ Whether the next character is part of a number """
+ return nextchar.isdigit()
+
+ @classmethod
+ def isspace(cls, nextchar):
+ """ Whether the next character is whitespace """
+ return nextchar.isspace()
+
+
+class _resultbase(object):
+
+ def __init__(self):
+ for attr in self.__slots__:
+ setattr(self, attr, None)
+
+ def _repr(self, classname):
+ l = []
+ for attr in self.__slots__:
+ value = getattr(self, attr)
+ if value is not None:
+ l.append("%s=%s" % (attr, repr(value)))
+ return "%s(%s)" % (classname, ", ".join(l))
+
+ def __len__(self):
+ return (sum(getattr(self, attr) is not None
+ for attr in self.__slots__))
+
+ def __repr__(self):
+ return self._repr(self.__class__.__name__)
+
+
+class parserinfo(object):
+ """
+ Class which handles what inputs are accepted. Subclass this to customize
+ the language and acceptable values for each parameter.
+
+ :param dayfirst:
+ Whether to interpret the first value in an ambiguous 3-integer date
+ (e.g. 01/05/09) as the day (``True``) or month (``False``). If
+ ``yearfirst`` is set to ``True``, this distinguishes between YDM
+ and YMD. Default is ``False``.
+
+ :param yearfirst:
+ Whether to interpret the first value in an ambiguous 3-integer date
+ (e.g. 01/05/09) as the year. If ``True``, the first number is taken
+ to be the year, otherwise the last number is taken to be the year.
+ Default is ``False``.
+ """
+
+ # m from a.m/p.m, t from ISO T separator
+ JUMP = [" ", ".", ",", ";", "-", "/", "'",
+ "at", "on", "and", "ad", "m", "t", "of",
+ "st", "nd", "rd", "th"]
+
+ WEEKDAYS = [("Mon", "Monday"),
+ ("Tue", "Tuesday"),
+ ("Wed", "Wednesday"),
+ ("Thu", "Thursday"),
+ ("Fri", "Friday"),
+ ("Sat", "Saturday"),
+ ("Sun", "Sunday")]
+ MONTHS = [("Jan", "January"),
+ ("Feb", "February"),
+ ("Mar", "March"),
+ ("Apr", "April"),
+ ("May", "May"),
+ ("Jun", "June"),
+ ("Jul", "July"),
+ ("Aug", "August"),
+ ("Sep", "Sept", "September"),
+ ("Oct", "October"),
+ ("Nov", "November"),
+ ("Dec", "December")]
+ HMS = [("h", "hour", "hours"),
+ ("m", "minute", "minutes"),
+ ("s", "second", "seconds")]
+ AMPM = [("am", "a"),
+ ("pm", "p")]
+ UTCZONE = ["UTC", "GMT", "Z"]
+ PERTAIN = ["of"]
+ TZOFFSET = {}
+
+ def __init__(self, dayfirst=False, yearfirst=False):
+ self._jump = self._convert(self.JUMP)
+ self._weekdays = self._convert(self.WEEKDAYS)
+ self._months = self._convert(self.MONTHS)
+ self._hms = self._convert(self.HMS)
+ self._ampm = self._convert(self.AMPM)
+ self._utczone = self._convert(self.UTCZONE)
+ self._pertain = self._convert(self.PERTAIN)
+
+ self.dayfirst = dayfirst
+ self.yearfirst = yearfirst
+
+ self._year = time.localtime().tm_year
+ self._century = self._year // 100 * 100
+
+ def _convert(self, lst):
+ dct = {}
+ for i, v in enumerate(lst):
+ if isinstance(v, tuple):
+ for v in v:
+ dct[v.lower()] = i
+ else:
+ dct[v.lower()] = i
+ return dct
+
+ def jump(self, name):
+ return name.lower() in self._jump
+
+ def weekday(self, name):
+ if len(name) >= 3:
+ try:
+ return self._weekdays[name.lower()]
+ except KeyError:
+ pass
+ return None
+
+ def month(self, name):
+ if len(name) >= 3:
+ try:
+ return self._months[name.lower()] + 1
+ except KeyError:
+ pass
+ return None
+
+ def hms(self, name):
+ try:
+ return self._hms[name.lower()]
+ except KeyError:
+ return None
+
+ def ampm(self, name):
+ try:
+ return self._ampm[name.lower()]
+ except KeyError:
+ return None
+
+ def pertain(self, name):
+ return name.lower() in self._pertain
+
+ def utczone(self, name):
+ return name.lower() in self._utczone
+
+ def tzoffset(self, name):
+ if name in self._utczone:
+ return 0
+
+ return self.TZOFFSET.get(name)
+
+ def convertyear(self, year, century_specified=False):
+ if year < 100 and not century_specified:
+ year += self._century
+ if abs(year - self._year) >= 50:
+ if year < self._year:
+ year += 100
+ else:
+ year -= 100
+ return year
+
+ def validate(self, res):
+ # move to info
+ if res.year is not None:
+ res.year = self.convertyear(res.year, res.century_specified)
+
+ if res.tzoffset == 0 and not res.tzname or res.tzname == 'Z':
+ res.tzname = "UTC"
+ res.tzoffset = 0
+ elif res.tzoffset != 0 and res.tzname and self.utczone(res.tzname):
+ res.tzoffset = 0
+ return True
+
+
+class _ymd(list):
+ def __init__(self, tzstr, *args, **kwargs):
+ super(self.__class__, self).__init__(*args, **kwargs)
+ self.century_specified = False
+ self.tzstr = tzstr
+
+ @staticmethod
+ def token_could_be_year(token, year):
+ try:
+ return int(token) == year
+ except ValueError:
+ return False
+
+ @staticmethod
+ def find_potential_year_tokens(year, tokens):
+ return [token for token in tokens if _ymd.token_could_be_year(token, year)]
+
+ def find_probable_year_index(self, tokens):
+ """
+ attempt to deduce if a pre 100 year was lost
+ due to padded zeros being taken off
+ """
+ for index, token in enumerate(self):
+ potential_year_tokens = _ymd.find_potential_year_tokens(token, tokens)
+ if len(potential_year_tokens) == 1 and len(potential_year_tokens[0]) > 2:
+ return index
+
+ def append(self, val):
+ if hasattr(val, '__len__'):
+ if val.isdigit() and len(val) > 2:
+ self.century_specified = True
+ elif val > 100:
+ self.century_specified = True
+
+ super(self.__class__, self).append(int(val))
+
+ def resolve_ymd(self, mstridx, yearfirst, dayfirst):
+ len_ymd = len(self)
+ year, month, day = (None, None, None)
+
+ if len_ymd > 3:
+ raise ValueError("More than three YMD values")
+ elif len_ymd == 1 or (mstridx != -1 and len_ymd == 2):
+ # One member, or two members with a month string
+ if mstridx != -1:
+ month = self[mstridx]
+ del self[mstridx]
+
+ if len_ymd > 1 or mstridx == -1:
+ if self[0] > 31:
+ year = self[0]
+ else:
+ day = self[0]
+
+ elif len_ymd == 2:
+ # Two members with numbers
+ if self[0] > 31:
+ # 99-01
+ year, month = self
+ elif self[1] > 31:
+ # 01-99
+ month, year = self
+ elif dayfirst and self[1] <= 12:
+ # 13-01
+ day, month = self
+ else:
+ # 01-13
+ month, day = self
+
+ elif len_ymd == 3:
+ # Three members
+ if mstridx == 0:
+ month, day, year = self
+ elif mstridx == 1:
+ if self[0] > 31 or (yearfirst and self[2] <= 31):
+ # 99-Jan-01
+ year, month, day = self
+ else:
+ # 01-Jan-01
+ # Give precendence to day-first, since
+ # two-digit years is usually hand-written.
+ day, month, year = self
+
+ elif mstridx == 2:
+ # WTF!?
+ if self[1] > 31:
+ # 01-99-Jan
+ day, year, month = self
+ else:
+ # 99-01-Jan
+ year, day, month = self
+
+ else:
+ if self[0] > 31 or \
+ self.find_probable_year_index(_timelex.split(self.tzstr)) == 0 or \
+ (yearfirst and self[1] <= 12 and self[2] <= 31):
+ # 99-01-01
+ if dayfirst and self[2] <= 12:
+ year, day, month = self
+ else:
+ year, month, day = self
+ elif self[0] > 12 or (dayfirst and self[1] <= 12):
+ # 13-01-01
+ day, month, year = self
+ else:
+ # 01-13-01
+ month, day, year = self
+
+ return year, month, day
+
+
+class parser(object):
+ def __init__(self, info=None):
+ self.info = info or parserinfo()
+
+ def parse(self, timestr, default=None, ignoretz=False, tzinfos=None, **kwargs):
+ """
+ Parse the date/time string into a :class:`datetime.datetime` object.
+
+ :param timestr:
+ Any date/time string using the supported formats.
+
+ :param default:
+ The default datetime object, if this is a datetime object and not
+ ``None``, elements specified in ``timestr`` replace elements in the
+ default object.
+
+ :param ignoretz:
+ If set ``True``, time zones in parsed strings are ignored and a
+ naive :class:`datetime.datetime` object is returned.
+
+ :param tzinfos:
+ Additional time zone names / aliases which may be present in the
+ string. This argument maps time zone names (and optionally offsets
+ from those time zones) to time zones. This parameter can be a
+ dictionary with timezone aliases mapping time zone names to time
+ zones or a function taking two parameters (``tzname`` and
+ ``tzoffset``) and returning a time zone.
+
+ The timezones to which the names are mapped can be an integer
+ offset from UTC in minutes or a :class:`tzinfo` object.
+
+ .. doctest::
+ :options: +NORMALIZE_WHITESPACE
+
+ >>> from dateutil.parser import parse
+ >>> from dateutil.tz import gettz
+ >>> tzinfos = {"BRST": -10800, "CST": gettz("America/Chicago")}
+ >>> parse("2012-01-19 17:21:00 BRST", tzinfos=tzinfos)
+ datetime.datetime(2012, 1, 19, 17, 21, tzinfo=tzoffset(u'BRST', -10800))
+ >>> parse("2012-01-19 17:21:00 CST", tzinfos=tzinfos)
+ datetime.datetime(2012, 1, 19, 17, 21,
+ tzinfo=tzfile('/usr/share/zoneinfo/America/Chicago'))
+
+ This parameter is ignored if ``ignoretz`` is set.
+
+ :param **kwargs:
+ Keyword arguments as passed to ``_parse()``.
+
+ :return:
+ Returns a :class:`datetime.datetime` object or, if the
+ ``fuzzy_with_tokens`` option is ``True``, returns a tuple, the
+ first element being a :class:`datetime.datetime` object, the second
+ a tuple containing the fuzzy tokens.
+
+ :raises ValueError:
+ Raised for invalid or unknown string format, if the provided
+ :class:`tzinfo` is not in a valid format, or if an invalid date
+ would be created.
+
+ :raises OverflowError:
+ Raised if the parsed date exceeds the largest valid C integer on
+ your system.
+ """
+
+ if default is None:
+ effective_dt = datetime.datetime.now()
+ default = datetime.datetime.now().replace(hour=0, minute=0,
+ second=0, microsecond=0)
+ else:
+ effective_dt = default
+
+ res, skipped_tokens = self._parse(timestr, **kwargs)
+
+ if res is None:
+ raise ValueError("Unknown string format")
+
+ if len(res) == 0:
+ raise ValueError("String does not contain a date.")
+
+ repl = {}
+ for attr in ("year", "month", "day", "hour",
+ "minute", "second", "microsecond"):
+ value = getattr(res, attr)
+ if value is not None:
+ repl[attr] = value
+
+ if 'day' not in repl:
+ # If the default day exceeds the last day of the month, fall back to
+ # the end of the month.
+ cyear = default.year if res.year is None else res.year
+ cmonth = default.month if res.month is None else res.month
+ cday = default.day if res.day is None else res.day
+
+ if cday > monthrange(cyear, cmonth)[1]:
+ repl['day'] = monthrange(cyear, cmonth)[1]
+
+ ret = default.replace(**repl)
+
+ if res.weekday is not None and not res.day:
+ ret = ret+relativedelta.relativedelta(weekday=res.weekday)
+
+ if not ignoretz:
+ if (isinstance(tzinfos, collections.Callable) or
+ tzinfos and res.tzname in tzinfos):
+
+ if isinstance(tzinfos, collections.Callable):
+ tzdata = tzinfos(res.tzname, res.tzoffset)
+ else:
+ tzdata = tzinfos.get(res.tzname)
+
+ if isinstance(tzdata, datetime.tzinfo):
+ tzinfo = tzdata
+ elif isinstance(tzdata, text_type):
+ tzinfo = tz.tzstr(tzdata)
+ elif isinstance(tzdata, integer_types):
+ tzinfo = tz.tzoffset(res.tzname, tzdata)
+ else:
+ raise ValueError("Offset must be tzinfo subclass, "
+ "tz string, or int offset.")
+ ret = ret.replace(tzinfo=tzinfo)
+ elif res.tzname and res.tzname in time.tzname:
+ ret = ret.replace(tzinfo=tz.tzlocal())
+ elif res.tzoffset == 0:
+ ret = ret.replace(tzinfo=tz.tzutc())
+ elif res.tzoffset:
+ ret = ret.replace(tzinfo=tz.tzoffset(res.tzname, res.tzoffset))
+
+ if kwargs.get('fuzzy_with_tokens', False):
+ return ret, skipped_tokens
+ else:
+ return ret
+
+ class _result(_resultbase):
+ __slots__ = ["year", "month", "day", "weekday",
+ "hour", "minute", "second", "microsecond",
+ "tzname", "tzoffset", "ampm"]
+
+ def _parse(self, timestr, dayfirst=None, yearfirst=None, fuzzy=False,
+ fuzzy_with_tokens=False):
+ """
+ Private method which performs the heavy lifting of parsing, called from
+ ``parse()``, which passes on its ``kwargs`` to this function.
+
+ :param timestr:
+ The string to parse.
+
+ :param dayfirst:
+ Whether to interpret the first value in an ambiguous 3-integer date
+ (e.g. 01/05/09) as the day (``True``) or month (``False``). If
+ ``yearfirst`` is set to ``True``, this distinguishes between YDM
+ and YMD. If set to ``None``, this value is retrieved from the
+ current :class:`parserinfo` object (which itself defaults to
+ ``False``).
+
+ :param yearfirst:
+ Whether to interpret the first value in an ambiguous 3-integer date
+ (e.g. 01/05/09) as the year. If ``True``, the first number is taken
+ to be the year, otherwise the last number is taken to be the year.
+ If this is set to ``None``, the value is retrieved from the current
+ :class:`parserinfo` object (which itself defaults to ``False``).
+
+ :param fuzzy:
+ Whether to allow fuzzy parsing, allowing for string like "Today is
+ January 1, 2047 at 8:21:00AM".
+
+ :param fuzzy_with_tokens:
+ If ``True``, ``fuzzy`` is automatically set to True, and the parser
+ will return a tuple where the first element is the parsed
+ :class:`datetime.datetime` datetimestamp and the second element is
+ a tuple containing the portions of the string which were ignored:
+
+ .. doctest::
+
+ >>> from dateutil.parser import parse
+ >>> parse("Today is January 1, 2047 at 8:21:00AM", fuzzy_with_tokens=True)
+ (datetime.datetime(2047, 1, 1, 8, 21), (u'Today is ', u' ', u'at '))
+
+ """
+ if fuzzy_with_tokens:
+ fuzzy = True
+
+ info = self.info
+
+ if dayfirst is None:
+ dayfirst = info.dayfirst
+
+ if yearfirst is None:
+ yearfirst = info.yearfirst
+
+ res = self._result()
+ l = _timelex.split(timestr) # Splits the timestr into tokens
+
+ # keep up with the last token skipped so we can recombine
+ # consecutively skipped tokens (-2 for when i begins at 0).
+ last_skipped_token_i = -2
+ skipped_tokens = list()
+
+ try:
+ # year/month/day list
+ ymd = _ymd(timestr)
+
+ # Index of the month string in ymd
+ mstridx = -1
+
+ len_l = len(l)
+ i = 0
+ while i < len_l:
+
+ # Check if it's a number
+ try:
+ value_repr = l[i]
+ value = float(value_repr)
+ except ValueError:
+ value = None
+
+ if value is not None:
+ # Token is a number
+ len_li = len(l[i])
+ i += 1
+
+ if (len(ymd) == 3 and len_li in (2, 4)
+ and res.hour is None and (i >= len_l or (l[i] != ':' and
+ info.hms(l[i]) is None))):
+ # 19990101T23[59]
+ s = l[i-1]
+ res.hour = int(s[:2])
+
+ if len_li == 4:
+ res.minute = int(s[2:])
+
+ elif len_li == 6 or (len_li > 6 and l[i-1].find('.') == 6):
+ # YYMMDD or HHMMSS[.ss]
+ s = l[i-1]
+
+ if not ymd and l[i-1].find('.') == -1:
+ #ymd.append(info.convertyear(int(s[:2])))
+
+ ymd.append(s[:2])
+ ymd.append(s[2:4])
+ ymd.append(s[4:])
+ else:
+ # 19990101T235959[.59]
+ res.hour = int(s[:2])
+ res.minute = int(s[2:4])
+ res.second, res.microsecond = _parsems(s[4:])
+
+ elif len_li in (8, 12, 14):
+ # YYYYMMDD
+ s = l[i-1]
+ ymd.append(s[:4])
+ ymd.append(s[4:6])
+ ymd.append(s[6:8])
+
+ if len_li > 8:
+ res.hour = int(s[8:10])
+ res.minute = int(s[10:12])
+
+ if len_li > 12:
+ res.second = int(s[12:])
+
+ elif ((i < len_l and info.hms(l[i]) is not None) or
+ (i+1 < len_l and l[i] == ' ' and
+ info.hms(l[i+1]) is not None)):
+
+ # HH[ ]h or MM[ ]m or SS[.ss][ ]s
+ if l[i] == ' ':
+ i += 1
+
+ idx = info.hms(l[i])
+
+ while True:
+ if idx == 0:
+ res.hour = int(value)
+
+ if value % 1:
+ res.minute = int(60*(value % 1))
+
+ elif idx == 1:
+ res.minute = int(value)
+
+ if value % 1:
+ res.second = int(60*(value % 1))
+
+ elif idx == 2:
+ res.second, res.microsecond = \
+ _parsems(value_repr)
+
+ i += 1
+
+ if i >= len_l or idx == 2:
+ break
+
+ # 12h00
+ try:
+ value_repr = l[i]
+ value = float(value_repr)
+ except ValueError:
+ break
+ else:
+ i += 1
+ idx += 1
+
+ if i < len_l:
+ newidx = info.hms(l[i])
+
+ if newidx is not None:
+ idx = newidx
+
+ elif (i == len_l and l[i-2] == ' ' and
+ info.hms(l[i-3]) is not None):
+ # X h MM or X m SS
+ idx = info.hms(l[i-3]) + 1
+
+ if idx == 1:
+ res.minute = int(value)
+
+ if value % 1:
+ res.second = int(60*(value % 1))
+ elif idx == 2:
+ res.second, res.microsecond = \
+ _parsems(value_repr)
+ i += 1
+
+ elif i+1 < len_l and l[i] == ':':
+ # HH:MM[:SS[.ss]]
+ res.hour = int(value)
+ i += 1
+ value = float(l[i])
+ res.minute = int(value)
+
+ if value % 1:
+ res.second = int(60*(value % 1))
+
+ i += 1
+
+ if i < len_l and l[i] == ':':
+ res.second, res.microsecond = _parsems(l[i+1])
+ i += 2
+
+ elif i < len_l and l[i] in ('-', '/', '.'):
+ sep = l[i]
+ ymd.append(value_repr)
+ i += 1
+
+ if i < len_l and not info.jump(l[i]):
+ try:
+ # 01-01[-01]
+ ymd.append(l[i])
+ except ValueError:
+ # 01-Jan[-01]
+ value = info.month(l[i])
+
+ if value is not None:
+ ymd.append(value)
+ assert mstridx == -1
+ mstridx = len(ymd)-1
+ else:
+ return None, None
+
+ i += 1
+
+ if i < len_l and l[i] == sep:
+ # We have three members
+ i += 1
+ value = info.month(l[i])
+
+ if value is not None:
+ ymd.append(value)
+ mstridx = len(ymd)-1
+ assert mstridx == -1
+ else:
+ ymd.append(l[i])
+
+ i += 1
+ elif i >= len_l or info.jump(l[i]):
+ if i+1 < len_l and info.ampm(l[i+1]) is not None:
+ # 12 am
+ res.hour = int(value)
+
+ if res.hour < 12 and info.ampm(l[i+1]) == 1:
+ res.hour += 12
+ elif res.hour == 12 and info.ampm(l[i+1]) == 0:
+ res.hour = 0
+
+ i += 1
+ else:
+ # Year, month or day
+ ymd.append(value)
+ i += 1
+ elif info.ampm(l[i]) is not None:
+
+ # 12am
+ res.hour = int(value)
+
+ if res.hour < 12 and info.ampm(l[i]) == 1:
+ res.hour += 12
+ elif res.hour == 12 and info.ampm(l[i]) == 0:
+ res.hour = 0
+ i += 1
+
+ elif not fuzzy:
+ return None, None
+ else:
+ i += 1
+ continue
+
+ # Check weekday
+ value = info.weekday(l[i])
+ if value is not None:
+ res.weekday = value
+ i += 1
+ continue
+
+ # Check month name
+ value = info.month(l[i])
+ if value is not None:
+ ymd.append(value)
+ assert mstridx == -1
+ mstridx = len(ymd)-1
+
+ i += 1
+ if i < len_l:
+ if l[i] in ('-', '/'):
+ # Jan-01[-99]
+ sep = l[i]
+ i += 1
+ ymd.append(l[i])
+ i += 1
+
+ if i < len_l and l[i] == sep:
+ # Jan-01-99
+ i += 1
+ ymd.append(l[i])
+ i += 1
+
+ elif (i+3 < len_l and l[i] == l[i+2] == ' '
+ and info.pertain(l[i+1])):
+ # Jan of 01
+ # In this case, 01 is clearly year
+ try:
+ value = int(l[i+3])
+ except ValueError:
+ # Wrong guess
+ pass
+ else:
+ # Convert it here to become unambiguous
+ ymd.append(str(info.convertyear(value)))
+ i += 4
+ continue
+
+ # Check am/pm
+ value = info.ampm(l[i])
+ if value is not None:
+ # For fuzzy parsing, 'a' or 'am' (both valid English words)
+ # may erroneously trigger the AM/PM flag. Deal with that
+ # here.
+ val_is_ampm = True
+
+ # If there's already an AM/PM flag, this one isn't one.
+ if fuzzy and res.ampm is not None:
+ val_is_ampm = False
+
+ # If AM/PM is found and hour is not, raise a ValueError
+ if res.hour is None:
+ if fuzzy:
+ val_is_ampm = False
+ else:
+ raise ValueError('No hour specified with ' +
+ 'AM or PM flag.')
+ elif not 0 <= res.hour <= 12:
+ # If AM/PM is found, it's a 12 hour clock, so raise
+ # an error for invalid range
+ if fuzzy:
+ val_is_ampm = False
+ else:
+ raise ValueError('Invalid hour specified for ' +
+ '12-hour clock.')
+
+ if val_is_ampm:
+ if value == 1 and res.hour < 12:
+ res.hour += 12
+ elif value == 0 and res.hour == 12:
+ res.hour = 0
+
+ res.ampm = value
+
+ i += 1
+ continue
+
+ # Check for a timezone name
+ if (res.hour is not None and len(l[i]) <= 5 and
+ res.tzname is None and res.tzoffset is None and
+ not [x for x in l[i] if x not in
+ string.ascii_uppercase]):
+ res.tzname = l[i]
+ res.tzoffset = info.tzoffset(res.tzname)
+ i += 1
+
+ # Check for something like GMT+3, or BRST+3. Notice
+ # that it doesn't mean "I am 3 hours after GMT", but
+ # "my time +3 is GMT". If found, we reverse the
+ # logic so that timezone parsing code will get it
+ # right.
+ if i < len_l and l[i] in ('+', '-'):
+ l[i] = ('+', '-')[l[i] == '+']
+ res.tzoffset = None
+ if info.utczone(res.tzname):
+ # With something like GMT+3, the timezone
+ # is *not* GMT.
+ res.tzname = None
+
+ continue
+
+ # Check for a numbered timezone
+ if res.hour is not None and l[i] in ('+', '-'):
+ signal = (-1, 1)[l[i] == '+']
+ i += 1
+ len_li = len(l[i])
+
+ if len_li == 4:
+ # -0300
+ res.tzoffset = int(l[i][:2])*3600+int(l[i][2:])*60
+ elif i+1 < len_l and l[i+1] == ':':
+ # -03:00
+ res.tzoffset = int(l[i])*3600+int(l[i+2])*60
+ i += 2
+ elif len_li <= 2:
+ # -[0]3
+ res.tzoffset = int(l[i][:2])*3600
+ else:
+ return None, None
+ i += 1
+
+ res.tzoffset *= signal
+
+ # Look for a timezone name between parenthesis
+ if (i+3 < len_l and
+ info.jump(l[i]) and l[i+1] == '(' and l[i+3] == ')' and
+ 3 <= len(l[i+2]) <= 5 and
+ not [x for x in l[i+2]
+ if x not in string.ascii_uppercase]):
+ # -0300 (BRST)
+ res.tzname = l[i+2]
+ i += 4
+ continue
+
+ # Check jumps
+ if not (info.jump(l[i]) or fuzzy):
+ return None, None
+
+ if last_skipped_token_i == i - 1:
+ # recombine the tokens
+ skipped_tokens[-1] += l[i]
+ else:
+ # just append
+ skipped_tokens.append(l[i])
+ last_skipped_token_i = i
+ i += 1
+
+ # Process year/month/day
+ year, month, day = ymd.resolve_ymd(mstridx, yearfirst, dayfirst)
+ if year is not None:
+ res.year = year
+ res.century_specified = ymd.century_specified
+
+ if month is not None:
+ res.month = month
+
+ if day is not None:
+ res.day = day
+
+ except (IndexError, ValueError, AssertionError):
+ return None, None
+
+ if not info.validate(res):
+ return None, None
+
+ if fuzzy_with_tokens:
+ return res, tuple(skipped_tokens)
+ else:
+ return res, None
+
+DEFAULTPARSER = parser()
+
+
+def parse(timestr, parserinfo=None, **kwargs):
+ """
+
+ Parse a string in one of the supported formats, using the
+ ``parserinfo`` parameters.
+
+ :param timestr:
+ A string containing a date/time stamp.
+
+ :param parserinfo:
+ A :class:`parserinfo` object containing parameters for the parser.
+ If ``None``, the default arguments to the :class:`parserinfo`
+ constructor are used.
+
+ The ``**kwargs`` parameter takes the following keyword arguments:
+
+ :param default:
+ The default datetime object, if this is a datetime object and not
+ ``None``, elements specified in ``timestr`` replace elements in the
+ default object.
+
+ :param ignoretz:
+ If set ``True``, time zones in parsed strings are ignored and a naive
+ :class:`datetime` object is returned.
+
+ :param tzinfos:
+ Additional time zone names / aliases which may be present in the
+ string. This argument maps time zone names (and optionally offsets
+ from those time zones) to time zones. This parameter can be a
+ dictionary with timezone aliases mapping time zone names to time
+ zones or a function taking two parameters (``tzname`` and
+ ``tzoffset``) and returning a time zone.
+
+ The timezones to which the names are mapped can be an integer
+ offset from UTC in minutes or a :class:`tzinfo` object.
+
+ .. doctest::
+ :options: +NORMALIZE_WHITESPACE
+
+ >>> from dateutil.parser import parse
+ >>> from dateutil.tz import gettz
+ >>> tzinfos = {"BRST": -10800, "CST": gettz("America/Chicago")}
+ >>> parse("2012-01-19 17:21:00 BRST", tzinfos=tzinfos)
+ datetime.datetime(2012, 1, 19, 17, 21, tzinfo=tzoffset(u'BRST', -10800))
+ >>> parse("2012-01-19 17:21:00 CST", tzinfos=tzinfos)
+ datetime.datetime(2012, 1, 19, 17, 21,
+ tzinfo=tzfile('/usr/share/zoneinfo/America/Chicago'))
+
+ This parameter is ignored if ``ignoretz`` is set.
+
+ :param dayfirst:
+ Whether to interpret the first value in an ambiguous 3-integer date
+ (e.g. 01/05/09) as the day (``True``) or month (``False``). If
+ ``yearfirst`` is set to ``True``, this distinguishes between YDM and
+ YMD. If set to ``None``, this value is retrieved from the current
+ :class:`parserinfo` object (which itself defaults to ``False``).
+
+ :param yearfirst:
+ Whether to interpret the first value in an ambiguous 3-integer date
+ (e.g. 01/05/09) as the year. If ``True``, the first number is taken to
+ be the year, otherwise the last number is taken to be the year. If
+ this is set to ``None``, the value is retrieved from the current
+ :class:`parserinfo` object (which itself defaults to ``False``).
+
+ :param fuzzy:
+ Whether to allow fuzzy parsing, allowing for string like "Today is
+ January 1, 2047 at 8:21:00AM".
+
+ :param fuzzy_with_tokens:
+ If ``True``, ``fuzzy`` is automatically set to True, and the parser
+ will return a tuple where the first element is the parsed
+ :class:`datetime.datetime` datetimestamp and the second element is
+ a tuple containing the portions of the string which were ignored:
+
+ .. doctest::
+
+ >>> from dateutil.parser import parse
+ >>> parse("Today is January 1, 2047 at 8:21:00AM", fuzzy_with_tokens=True)
+ (datetime.datetime(2011, 1, 1, 8, 21), (u'Today is ', u' ', u'at '))
+
+ :return:
+ Returns a :class:`datetime.datetime` object or, if the
+ ``fuzzy_with_tokens`` option is ``True``, returns a tuple, the
+ first element being a :class:`datetime.datetime` object, the second
+ a tuple containing the fuzzy tokens.
+
+ :raises ValueError:
+ Raised for invalid or unknown string format, if the provided
+ :class:`tzinfo` is not in a valid format, or if an invalid date
+ would be created.
+
+ :raises OverflowError:
+ Raised if the parsed date exceeds the largest valid C integer on
+ your system.
+ """
+ if parserinfo:
+ return parser(parserinfo).parse(timestr, **kwargs)
+ else:
+ return DEFAULTPARSER.parse(timestr, **kwargs)
+
+
+class _tzparser(object):
+
+ class _result(_resultbase):
+
+ __slots__ = ["stdabbr", "stdoffset", "dstabbr", "dstoffset",
+ "start", "end"]
+
+ class _attr(_resultbase):
+ __slots__ = ["month", "week", "weekday",
+ "yday", "jyday", "day", "time"]
+
+ def __repr__(self):
+ return self._repr("")
+
+ def __init__(self):
+ _resultbase.__init__(self)
+ self.start = self._attr()
+ self.end = self._attr()
+
+ def parse(self, tzstr):
+ res = self._result()
+ l = _timelex.split(tzstr)
+ try:
+
+ len_l = len(l)
+
+ i = 0
+ while i < len_l:
+ # BRST+3[BRDT[+2]]
+ j = i
+ while j < len_l and not [x for x in l[j]
+ if x in "0123456789:,-+"]:
+ j += 1
+ if j != i:
+ if not res.stdabbr:
+ offattr = "stdoffset"
+ res.stdabbr = "".join(l[i:j])
+ else:
+ offattr = "dstoffset"
+ res.dstabbr = "".join(l[i:j])
+ i = j
+ if (i < len_l and (l[i] in ('+', '-') or l[i][0] in
+ "0123456789")):
+ if l[i] in ('+', '-'):
+ # Yes, that's right. See the TZ variable
+ # documentation.
+ signal = (1, -1)[l[i] == '+']
+ i += 1
+ else:
+ signal = -1
+ len_li = len(l[i])
+ if len_li == 4:
+ # -0300
+ setattr(res, offattr, (int(l[i][:2])*3600 +
+ int(l[i][2:])*60)*signal)
+ elif i+1 < len_l and l[i+1] == ':':
+ # -03:00
+ setattr(res, offattr,
+ (int(l[i])*3600+int(l[i+2])*60)*signal)
+ i += 2
+ elif len_li <= 2:
+ # -[0]3
+ setattr(res, offattr,
+ int(l[i][:2])*3600*signal)
+ else:
+ return None
+ i += 1
+ if res.dstabbr:
+ break
+ else:
+ break
+
+ if i < len_l:
+ for j in range(i, len_l):
+ if l[j] == ';':
+ l[j] = ','
+
+ assert l[i] == ','
+
+ i += 1
+
+ if i >= len_l:
+ pass
+ elif (8 <= l.count(',') <= 9 and
+ not [y for x in l[i:] if x != ','
+ for y in x if y not in "0123456789"]):
+ # GMT0BST,3,0,30,3600,10,0,26,7200[,3600]
+ for x in (res.start, res.end):
+ x.month = int(l[i])
+ i += 2
+ if l[i] == '-':
+ value = int(l[i+1])*-1
+ i += 1
+ else:
+ value = int(l[i])
+ i += 2
+ if value:
+ x.week = value
+ x.weekday = (int(l[i])-1) % 7
+ else:
+ x.day = int(l[i])
+ i += 2
+ x.time = int(l[i])
+ i += 2
+ if i < len_l:
+ if l[i] in ('-', '+'):
+ signal = (-1, 1)[l[i] == "+"]
+ i += 1
+ else:
+ signal = 1
+ res.dstoffset = (res.stdoffset+int(l[i]))*signal
+ elif (l.count(',') == 2 and l[i:].count('/') <= 2 and
+ not [y for x in l[i:] if x not in (',', '/', 'J', 'M',
+ '.', '-', ':')
+ for y in x if y not in "0123456789"]):
+ for x in (res.start, res.end):
+ if l[i] == 'J':
+ # non-leap year day (1 based)
+ i += 1
+ x.jyday = int(l[i])
+ elif l[i] == 'M':
+ # month[-.]week[-.]weekday
+ i += 1
+ x.month = int(l[i])
+ i += 1
+ assert l[i] in ('-', '.')
+ i += 1
+ x.week = int(l[i])
+ if x.week == 5:
+ x.week = -1
+ i += 1
+ assert l[i] in ('-', '.')
+ i += 1
+ x.weekday = (int(l[i])-1) % 7
+ else:
+ # year day (zero based)
+ x.yday = int(l[i])+1
+
+ i += 1
+
+ if i < len_l and l[i] == '/':
+ i += 1
+ # start time
+ len_li = len(l[i])
+ if len_li == 4:
+ # -0300
+ x.time = (int(l[i][:2])*3600+int(l[i][2:])*60)
+ elif i+1 < len_l and l[i+1] == ':':
+ # -03:00
+ x.time = int(l[i])*3600+int(l[i+2])*60
+ i += 2
+ if i+1 < len_l and l[i+1] == ':':
+ i += 2
+ x.time += int(l[i])
+ elif len_li <= 2:
+ # -[0]3
+ x.time = (int(l[i][:2])*3600)
+ else:
+ return None
+ i += 1
+
+ assert i == len_l or l[i] == ','
+
+ i += 1
+
+ assert i >= len_l
+
+ except (IndexError, ValueError, AssertionError):
+ return None
+
+ return res
+
+
+DEFAULTTZPARSER = _tzparser()
+
+
+def _parsetz(tzstr):
+ return DEFAULTTZPARSER.parse(tzstr)
+
+
+def _parsems(value):
+ """Parse a I[.F] seconds value into (seconds, microseconds)."""
+ if "." not in value:
+ return int(value), 0
+ else:
+ i, f = value.split(".")
+ return int(i), int(f.ljust(6, "0")[:6])
+
+
+# vim:ts=4:sw=4:et
diff --git a/libs/dateutil/relativedelta.py b/libs/dateutil/relativedelta.py
index 1e0d61653..7e3bd12ac 100644
--- a/libs/dateutil/relativedelta.py
+++ b/libs/dateutil/relativedelta.py
@@ -10,7 +10,7 @@ from warnings import warn
from ._common import weekday
-MO, TU, WE, TH, FR, SA, SU = weekdays = tuple(weekday(x) for x in range(7))
+MO, TU, WE, TH, FR, SA, SU = weekdays = tuple([weekday(x) for x in range(7)])
__all__ = ["relativedelta", "MO", "TU", "WE", "TH", "FR", "SA", "SU"]
@@ -19,7 +19,7 @@ class relativedelta(object):
"""
The relativedelta type is based on the specification of the excellent
work done by M.-A. Lemburg in his
- `mx.DateTime <https://www.egenix.com/products/python/mxBase/mxDateTime/>`_ extension.
+ `mx.DateTime <http://www.egenix.com/files/python/mxDateTime.html>`_ extension.
However, notice that this type does *NOT* implement the same algorithm as
his work. Do *NOT* expect it to behave like mx.DateTime's counterpart.
@@ -34,7 +34,7 @@ class relativedelta(object):
year, month, day, hour, minute, second, microsecond:
Absolute information (argument is singular); adding or subtracting a
- relativedelta with absolute information does not perform an arithmetic
+ relativedelta with absolute information does not perform an aritmetic
operation, but rather REPLACES the corresponding value in the
original datetime with the value(s) in relativedelta.
@@ -44,14 +44,12 @@ class relativedelta(object):
the corresponding aritmetic operation on the original datetime value
with the information in the relativedelta.
- weekday:
- One of the weekday instances (MO, TU, etc). These
- instances may receive a parameter N, specifying the Nth
- weekday, which could be positive or negative (like MO(+1)
- or MO(-2). Not specifying it is the same as specifying
- +1. You can also use an integer, where 0=MO. Notice that
- if the calculated date is already Monday, for example,
- using MO(1) or MO(-1) won't change the day.
+ weekday:
+ One of the weekday instances (MO, TU, etc). These instances may
+ receive a parameter N, specifying the Nth weekday, which could
+ be positive or negative (like MO(+1) or MO(-2). Not specifying
+ it is the same as specifying +1. You can also use an integer,
+ where 0=MO.
leapdays:
Will add given days to the date found, if year is a leap
@@ -61,36 +59,33 @@ class relativedelta(object):
Set the yearday or the non-leap year day (jump leap days).
These are converted to day/month/leapdays information.
- There are relative and absolute forms of the keyword
- arguments. The plural is relative, and the singular is
- absolute. For each argument in the order below, the absolute form
- is applied first (by setting each attribute to that value) and
- then the relative form (by adding the value to the attribute).
+ Here is the behavior of operations with relativedelta:
- The order of attributes considered when this relativedelta is
- added to a datetime is:
+ 1. Calculate the absolute year, using the 'year' argument, or the
+ original datetime year, if the argument is not present.
- 1. Year
- 2. Month
- 3. Day
- 4. Hours
- 5. Minutes
- 6. Seconds
- 7. Microseconds
+ 2. Add the relative 'years' argument to the absolute year.
- Finally, weekday is applied, using the rule described above.
+ 3. Do steps 1 and 2 for month/months.
- For example
+ 4. 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.
- >>> dt = datetime(2018, 4, 9, 13, 37, 0)
- >>> delta = relativedelta(hours=25, day=1, weekday=MO(1))
- datetime(2018, 4, 2, 14, 37, 0)
+ 5. Add the relative 'days' argument to the absolute day. Notice
+ that the 'weeks' argument is multiplied by 7 and added to
+ 'days'.
- First, the day is set to 1 (the first of the month), then 25 hours
- are added, to get to the 2nd day and 14th hour, finally the
- weekday is applied, but since the 2nd is already a Monday there is
- no effect.
+ 6. Do steps 1 and 2 for hour/hours, minute/minutes, second/seconds,
+ microsecond/microseconds.
+ 7. If the 'weekday' argument is present, calculate the weekday,
+ with the given (wday, nth) tuple. wday is the index of the
+ weekday (0-6, 0=Mon), and nth is the number of weeks to add
+ forward or backward, depending on its signal. Notice that if
+ the calculated date is already Monday, for example, using
+ (0, 1) or (0, -1) won't change the day.
"""
def __init__(self, dt1=None, dt2=None,
@@ -100,6 +95,11 @@ class relativedelta(object):
yearday=None, nlyearday=None,
hour=None, minute=None, second=None, microsecond=None):
+ # Check for non-integer values in integer-only quantities
+ if any(x is not None and x != int(x) for x in (years, months)):
+ raise ValueError("Non-integer years and months are "
+ "ambiguous and not currently supported.")
+
if dt1 and dt2:
# datetime is a subclass of date. So both must be date
if not (isinstance(dt1, datetime.date) and
@@ -159,14 +159,9 @@ class relativedelta(object):
self.seconds = delta.seconds + delta.days * 86400
self.microseconds = delta.microseconds
else:
- # Check for non-integer values in integer-only quantities
- if any(x is not None and x != int(x) for x in (years, months)):
- raise ValueError("Non-integer years and months are "
- "ambiguous and not currently supported.")
-
# Relative information
- self.years = int(years)
- self.months = int(months)
+ self.years = years
+ self.months = months
self.days = days + weeks * 7
self.leapdays = leapdays
self.hours = hours
@@ -191,6 +186,7 @@ class relativedelta(object):
"This is not a well-defined condition and will raise " +
"errors in future versions.", DeprecationWarning)
+
if isinstance(weekday, integer_types):
self.weekday = weekdays[weekday]
else:
@@ -254,8 +250,7 @@ class relativedelta(object):
@property
def weeks(self):
- return int(self.days / 7.0)
-
+ return self.days // 7
@weeks.setter
def weeks(self, value):
self.days = self.days - (self.weeks * 7) + value * 7
@@ -316,22 +311,14 @@ class relativedelta(object):
microseconds=(other.microseconds +
self.microseconds),
leapdays=other.leapdays or self.leapdays,
- year=(other.year if other.year is not None
- else self.year),
- month=(other.month if other.month is not None
- else self.month),
- day=(other.day if other.day is not None
- else self.day),
- weekday=(other.weekday if other.weekday is not None
- else self.weekday),
- hour=(other.hour if other.hour is not None
- else self.hour),
- minute=(other.minute if other.minute is not None
- else self.minute),
- second=(other.second if other.second is not None
- else self.second),
- microsecond=(other.microsecond if other.microsecond
- is not None else
+ year=other.year or self.year,
+ month=other.month or self.month,
+ day=other.day or self.day,
+ weekday=other.weekday or self.weekday,
+ hour=other.hour or self.hour,
+ minute=other.minute or self.minute,
+ second=other.second or self.second,
+ microsecond=(other.microsecond or
self.microsecond))
if isinstance(other, datetime.timedelta):
return self.__class__(years=self.years,
@@ -409,41 +396,14 @@ class relativedelta(object):
seconds=self.seconds - other.seconds,
microseconds=self.microseconds - other.microseconds,
leapdays=self.leapdays or other.leapdays,
- year=(self.year if self.year is not None
- else other.year),
- month=(self.month if self.month is not None else
- other.month),
- day=(self.day if self.day is not None else
- other.day),
- weekday=(self.weekday if self.weekday is not None else
- other.weekday),
- hour=(self.hour if self.hour is not None else
- other.hour),
- minute=(self.minute if self.minute is not None else
- other.minute),
- second=(self.second if self.second is not None else
- other.second),
- microsecond=(self.microsecond if self.microsecond
- is not None else
- other.microsecond))
-
- def __abs__(self):
- return self.__class__(years=abs(self.years),
- months=abs(self.months),
- days=abs(self.days),
- hours=abs(self.hours),
- minutes=abs(self.minutes),
- seconds=abs(self.seconds),
- microseconds=abs(self.microseconds),
- leapdays=self.leapdays,
- year=self.year,
- month=self.month,
- day=self.day,
- weekday=self.weekday,
- hour=self.hour,
- minute=self.minute,
- second=self.second,
- microsecond=self.microsecond)
+ year=self.year or other.year,
+ month=self.month or other.month,
+ day=self.day or other.day,
+ weekday=self.weekday or other.weekday,
+ hour=self.hour or other.hour,
+ minute=self.minute or other.minute,
+ second=self.second or other.second,
+ microsecond=self.microsecond or other.microsecond)
def __neg__(self):
return self.__class__(years=-self.years,
@@ -535,25 +495,7 @@ class relativedelta(object):
self.second == other.second and
self.microsecond == other.microsecond)
- def __hash__(self):
- return hash((
- self.weekday,
- self.years,
- self.months,
- self.days,
- self.hours,
- self.minutes,
- self.seconds,
- self.microseconds,
- self.leapdays,
- self.year,
- self.month,
- self.day,
- self.hour,
- self.minute,
- self.second,
- self.microsecond,
- ))
+ __hash__ = None
def __ne__(self, other):
return not self.__eq__(other)
@@ -583,7 +525,6 @@ class relativedelta(object):
return "{classname}({attrs})".format(classname=self.__class__.__name__,
attrs=", ".join(l))
-
def _sign(x):
return int(copysign(1, x))
diff --git a/libs/dateutil/rrule.py b/libs/dateutil/rrule.py
index 8e9c2af18..da94351b9 100644
--- a/libs/dateutil/rrule.py
+++ b/libs/dateutil/rrule.py
@@ -2,13 +2,12 @@
"""
The rrule module offers a small, complete, and very fast, implementation of
the recurrence rules documented in the
-`iCalendar RFC <https://tools.ietf.org/html/rfc5545>`_,
+`iCalendar RFC <http://www.ietf.org/rfc/rfc2445.txt>`_,
including support for caching of results.
"""
import itertools
import datetime
import calendar
-import re
import sys
try:
@@ -21,7 +20,6 @@ from six.moves import _thread, range
import heapq
from ._common import weekday as weekdaybase
-from .tz import tzutc, tzlocal
# For warning about deprecation of until and count
from warnings import warn
@@ -48,7 +46,7 @@ del M29, M30, M31, M365MASK[59], MDAY365MASK[59], NMDAY365MASK[31]
MDAY365MASK = tuple(MDAY365MASK)
M365MASK = tuple(M365MASK)
-FREQNAMES = ['YEARLY', 'MONTHLY', 'WEEKLY', 'DAILY', 'HOURLY', 'MINUTELY', 'SECONDLY']
+FREQNAMES = ['YEARLY','MONTHLY','WEEKLY','DAILY','HOURLY','MINUTELY','SECONDLY']
(YEARLY,
MONTHLY,
@@ -62,7 +60,6 @@ FREQNAMES = ['YEARLY', 'MONTHLY', 'WEEKLY', 'DAILY', 'HOURLY', 'MINUTELY', 'SECO
easter = None
parser = None
-
class weekday(weekdaybase):
"""
This version of weekday does not allow n = 0.
@@ -73,8 +70,7 @@ class weekday(weekdaybase):
super(weekday, self).__init__(wkday, n)
-
-MO, TU, WE, TH, FR, SA, SU = weekdays = tuple(weekday(x) for x in range(7))
+MO, TU, WE, TH, FR, SA, SU = weekdays = tuple([weekday(x) for x in range(7)])
def _invalidates_cache(f):
@@ -260,13 +256,13 @@ class rrulebase(object):
n = 0
for d in gen:
if comp(d, dt):
+ yield d
+
if count is not None:
n += 1
- if n > count:
+ if n >= count:
break
- yield d
-
def between(self, after, before, inc=False, count=1):
""" Returns all the occurrences of the rrule between after and before.
The inc keyword defines what happens if after and/or before are
@@ -337,6 +333,10 @@ class rrule(rrulebase):
Additionally, it supports the following keyword arguments:
+ :param 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.
:param dtstart:
The recurrence start. Besides being the base for the recurrence,
missing parameters in the final recurrence instances will also be
@@ -357,16 +357,16 @@ class rrule(rrulebase):
.. note::
As of version 2.5.0, the use of the ``until`` keyword together
- with the ``count`` keyword is deprecated per RFC-5545 Sec. 3.3.10.
+ with the ``count`` keyword is deprecated per RFC-2445 Sec. 4.3.10.
:param until:
If given, this must be a datetime instance, that will specify the
limit of the recurrence. The last recurrence in the rule is the greatest
datetime that is less than or equal to the value specified in the
``until`` parameter.
-
+
.. note::
As of version 2.5.0, the use of the ``until`` keyword together
- with the ``count`` keyword is deprecated per RFC-5545 Sec. 3.3.10.
+ with the ``count`` keyword is deprecated per RFC-2445 Sec. 4.3.10.
:param bysetpos:
If given, it must be either an integer, or a sequence of integers,
positive or negative. Each given integer will specify an occurrence
@@ -383,11 +383,6 @@ class rrule(rrulebase):
:param byyearday:
If given, it must be either an integer, or a sequence of integers,
meaning the year days to apply the recurrence to.
- :param 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.
:param 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
@@ -413,10 +408,11 @@ class rrule(rrulebase):
:param bysecond:
If given, it must be either an integer, or a sequence of integers,
meaning the seconds to apply the recurrence to.
- :param 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.
+ :param 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.
"""
def __init__(self, freq, dtstart=None,
interval=1, wkst=None, count=None, until=None, bysetpos=None,
@@ -427,10 +423,7 @@ class rrule(rrulebase):
super(rrule, self).__init__(cache)
global easter
if not dtstart:
- if until and until.tzinfo:
- dtstart = datetime.datetime.now(tz=until.tzinfo).replace(microsecond=0)
- else:
- dtstart = datetime.datetime.now().replace(microsecond=0)
+ dtstart = datetime.datetime.now().replace(microsecond=0)
elif not isinstance(dtstart, datetime.datetime):
dtstart = datetime.datetime.fromordinal(dtstart.toordinal())
else:
@@ -451,22 +444,8 @@ class rrule(rrulebase):
until = datetime.datetime.fromordinal(until.toordinal())
self._until = until
- if self._dtstart and self._until:
- if (self._dtstart.tzinfo is not None) != (self._until.tzinfo is not None):
- # According to RFC5545 Section 3.3.10:
- # https://tools.ietf.org/html/rfc5545#section-3.3.10
- #
- # > If the "DTSTART" property is specified as a date with UTC
- # > time or a date with local time and time zone reference,
- # > then the UNTIL rule part MUST be specified as a date with
- # > UTC time.
- raise ValueError(
- 'RRULE UNTIL values must be specified in UTC when DTSTART '
- 'is timezone-aware'
- )
-
- if count is not None and until:
- warn("Using both 'count' and 'until' is inconsistent with RFC 5545"
+ if count and until:
+ warn("Using both 'count' and 'until' is inconsistent with RFC 2445"
" and has been deprecated in dateutil. Future versions will "
"raise an error.", DeprecationWarning)
@@ -554,8 +533,8 @@ class rrule(rrulebase):
bymonthday = set(bymonthday) # Ensure it's unique
- self._bymonthday = tuple(sorted(x for x in bymonthday if x > 0))
- self._bynmonthday = tuple(sorted(x for x in bymonthday if x < 0))
+ self._bymonthday = tuple(sorted([x for x in bymonthday if x > 0]))
+ self._bynmonthday = tuple(sorted([x for x in bymonthday if x < 0]))
# Storing positive numbers first, then negative numbers
if 'bymonthday' not in self._original_rule:
@@ -603,13 +582,13 @@ class rrule(rrulebase):
self._byweekday = tuple(sorted(self._byweekday))
orig_byweekday = [weekday(x) for x in self._byweekday]
else:
- orig_byweekday = ()
+ orig_byweekday = tuple()
if self._bynweekday is not None:
self._bynweekday = tuple(sorted(self._bynweekday))
orig_bynweekday = [weekday(*x) for x in self._bynweekday]
else:
- orig_bynweekday = ()
+ orig_bynweekday = tuple()
if 'byweekday' not in self._original_rule:
self._original_rule['byweekday'] = tuple(itertools.chain(
@@ -618,7 +597,7 @@ class rrule(rrulebase):
# byhour
if byhour is None:
if freq < HOURLY:
- self._byhour = {dtstart.hour}
+ self._byhour = set((dtstart.hour,))
else:
self._byhour = None
else:
@@ -638,7 +617,7 @@ class rrule(rrulebase):
# byminute
if byminute is None:
if freq < MINUTELY:
- self._byminute = {dtstart.minute}
+ self._byminute = set((dtstart.minute,))
else:
self._byminute = None
else:
@@ -693,7 +672,7 @@ class rrule(rrulebase):
def __str__(self):
"""
Output a string that would generate this RRULE if passed to rrulestr.
- This is mostly compatible with RFC5545, except for the
+ This is mostly compatible with RFC2445, except for the
dateutil-specific extension BYEASTER.
"""
@@ -710,7 +689,7 @@ class rrule(rrulebase):
if self._wkst:
parts.append('WKST=' + repr(weekday(self._wkst))[0:2])
- if self._count is not None:
+ if self._count:
parts.append('COUNT=' + str(self._count))
if self._until:
@@ -718,7 +697,7 @@ class rrule(rrulebase):
if self._original_rule.get('byweekday') is not None:
# The str() method on weekday objects doesn't generate
- # RFC5545-compliant strings, so we should modify that.
+ # RFC2445-compliant strings, so we should modify that.
original_rule = dict(self._original_rule)
wday_strings = []
for wday in original_rule['byweekday']:
@@ -749,7 +728,7 @@ class rrule(rrulebase):
parts.append(partfmt.format(name=name, vals=(','.join(str(v)
for v in value))))
- output.append('RRULE:' + ';'.join(parts))
+ output.append(';'.join(parts))
return '\n'.join(output)
def replace(self, **kwargs):
@@ -766,6 +745,7 @@ class rrule(rrulebase):
new_kwargs.update(kwargs)
return rrule(**new_kwargs)
+
def _iter(self):
year, month, day, hour, minute, second, weekday, yearday, _ = \
self._dtstart.timetuple()
@@ -864,13 +844,13 @@ class rrule(rrulebase):
self._len = total
return
elif res >= self._dtstart:
- if count is not None:
+ total += 1
+ yield res
+ if count:
count -= 1
- if count < 0:
+ if not count:
self._len = total
return
- total += 1
- yield res
else:
for i in dayset[start:end]:
if i is not None:
@@ -881,15 +861,14 @@ class rrule(rrulebase):
self._len = total
return
elif res >= self._dtstart:
- if count is not None:
+ total += 1
+ yield res
+ if count:
count -= 1
- if count < 0:
+ if not count:
self._len = total
return
- total += 1
- yield res
-
# Handle frequency and interval
fixday = False
if freq == YEARLY:
@@ -1515,17 +1494,11 @@ class _rrulestr(object):
forceset=False,
compatible=False,
ignoretz=False,
- tzids=None,
tzinfos=None):
global parser
if compatible:
forceset = True
unfold = True
-
- TZID_NAMES = dict(map(
- lambda x: (x.upper(), x),
- re.findall('TZID=(?P<name>[^:]+):', s)
- ))
s = s.upper()
if not s.strip():
raise ValueError("empty string")
@@ -1582,52 +1555,15 @@ class _rrulestr(object):
elif name == "EXDATE":
for parm in parms:
if parm != "VALUE=DATE-TIME":
- raise ValueError("unsupported EXDATE parm: "+parm)
+ raise ValueError("unsupported RDATE parm: "+parm)
exdatevals.append(value)
elif name == "DTSTART":
- # RFC 5445 3.8.2.4: The VALUE parameter is optional, but
- # may be found only once.
- value_found = False
- TZID = None
- valid_values = {"VALUE=DATE-TIME", "VALUE=DATE"}
for parm in parms:
- if parm.startswith("TZID="):
- try:
- tzkey = TZID_NAMES[parm.split('TZID=')[-1]]
- except KeyError:
- continue
- if tzids is None:
- from . import tz
- tzlookup = tz.gettz
- elif callable(tzids):
- tzlookup = tzids
- else:
- tzlookup = getattr(tzids, 'get', None)
- if tzlookup is None:
- msg = ('tzids must be a callable, ' +
- 'mapping, or None, ' +
- 'not %s' % tzids)
- raise ValueError(msg)
-
- TZID = tzlookup(tzkey)
- continue
- if parm not in valid_values:
- raise ValueError("unsupported DTSTART parm: "+parm)
- else:
- if value_found:
- msg = ("Duplicate value parameter found in " +
- "DTSTART: " + parm)
- raise ValueError(msg)
- value_found = True
+ raise ValueError("unsupported DTSTART parm: "+parm)
if not parser:
from dateutil import parser
dtstart = parser.parse(value, ignoretz=ignoretz,
tzinfos=tzinfos)
- if TZID is not None:
- if dtstart.tzinfo is None:
- dtstart = dtstart.replace(tzinfo=TZID)
- else:
- raise ValueError('DTSTART specifies multiple timezones')
else:
raise ValueError("unsupported property: "+name)
if (forceset or len(rrulevals) > 1 or rdatevals
@@ -1666,7 +1602,6 @@ class _rrulestr(object):
def __call__(self, s, **kwargs):
return self._parse_rfc(s, **kwargs)
-
rrulestr = _rrulestr()
# vim:ts=4:sw=4:et
diff --git a/libs/dateutil/test/__init__.py b/libs/dateutil/test/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/libs/dateutil/test/__init__.py
diff --git a/libs/dateutil/test/_common.py b/libs/dateutil/test/_common.py
new file mode 100644
index 000000000..f77b53e4f
--- /dev/null
+++ b/libs/dateutil/test/_common.py
@@ -0,0 +1,288 @@
+from __future__ import unicode_literals
+try:
+ import unittest2 as unittest
+except ImportError:
+ import unittest
+
+import os
+import datetime
+import time
+import subprocess
+import warnings
+import tempfile
+import pickle
+
+
+class WarningTestMixin(object):
+ # Based on https://stackoverflow.com/a/12935176/467366
+ class _AssertWarnsContext(warnings.catch_warnings):
+ def __init__(self, expected_warnings, parent, **kwargs):
+ super(WarningTestMixin._AssertWarnsContext, self).__init__(**kwargs)
+
+ self.parent = parent
+ try:
+ self.expected_warnings = list(expected_warnings)
+ except TypeError:
+ self.expected_warnings = [expected_warnings]
+
+ self._warning_log = []
+
+ def __enter__(self, *args, **kwargs):
+ rv = super(WarningTestMixin._AssertWarnsContext, self).__enter__(*args, **kwargs)
+
+ if self._showwarning is not self._module.showwarning:
+ super_showwarning = self._module.showwarning
+ else:
+ super_showwarning = None
+
+ def showwarning(*args, **kwargs):
+ if super_showwarning is not None:
+ super_showwarning(*args, **kwargs)
+
+ self._warning_log.append(warnings.WarningMessage(*args, **kwargs))
+
+ self._module.showwarning = showwarning
+ return rv
+
+ def __exit__(self, *args, **kwargs):
+ super(WarningTestMixin._AssertWarnsContext, self).__exit__(self, *args, **kwargs)
+
+ self.parent.assertTrue(any(issubclass(item.category, warning)
+ for warning in self.expected_warnings
+ for item in self._warning_log))
+
+ def assertWarns(self, warning, callable=None, *args, **kwargs):
+ warnings.simplefilter('always')
+ context = self.__class__._AssertWarnsContext(warning, self)
+ if callable is None:
+ return context
+ else:
+ with context:
+ callable(*args, **kwargs)
+
+
+class PicklableMixin(object):
+ def _get_nobj_bytes(self, obj, dump_kwargs, load_kwargs):
+ """
+ Pickle and unpickle an object using ``pickle.dumps`` / ``pickle.loads``
+ """
+ pkl = pickle.dumps(obj, **dump_kwargs)
+ return pickle.loads(pkl, **load_kwargs)
+
+ def _get_nobj_file(self, obj, dump_kwargs, load_kwargs):
+ """
+ Pickle and unpickle an object using ``pickle.dump`` / ``pickle.load`` on
+ a temporary file.
+ """
+ with tempfile.TemporaryFile('w+b') as pkl:
+ pickle.dump(obj, pkl, **dump_kwargs)
+ pkl.seek(0) # Reset the file to the beginning to read it
+ nobj = pickle.load(pkl, **load_kwargs)
+
+ return nobj
+
+ def assertPicklable(self, obj, asfile=False,
+ dump_kwargs=None, load_kwargs=None):
+ """
+ Assert that an object can be pickled and unpickled. This assertion
+ assumes that the desired behavior is that the unpickled object compares
+ equal to the original object, but is not the same object.
+ """
+ get_nobj = self._get_nobj_file if asfile else self._get_nobj_bytes
+ dump_kwargs = dump_kwargs or {}
+ load_kwargs = load_kwargs or {}
+
+ nobj = get_nobj(obj, dump_kwargs, load_kwargs)
+ self.assertIsNot(obj, nobj)
+ self.assertEqual(obj, nobj)
+
+
+class TZContextBase(object):
+ """
+ Base class for a context manager which allows changing of time zones.
+
+ Subclasses may define a guard variable to either block or or allow time
+ zone changes by redefining ``_guard_var_name`` and ``_guard_allows_change``.
+ The default is that the guard variable must be affirmatively set.
+
+ Subclasses must define ``get_current_tz`` and ``set_current_tz``.
+ """
+ _guard_var_name = "DATEUTIL_MAY_CHANGE_TZ"
+ _guard_allows_change = True
+
+ def __init__(self, tzval):
+ self.tzval = tzval
+ self._old_tz = None
+
+ @classmethod
+ def tz_change_allowed(cls):
+ """
+ Class method used to query whether or not this class allows time zone
+ changes.
+ """
+ guard = bool(os.environ.get(cls._guard_var_name, False))
+
+ # _guard_allows_change gives the "default" behavior - if True, the
+ # guard is overcoming a block. If false, the guard is causing a block.
+ # Whether tz_change is allowed is therefore the XNOR of the two.
+ return guard == cls._guard_allows_change
+
+ @classmethod
+ def tz_change_disallowed_message(cls):
+ """ Generate instructions on how to allow tz changes """
+ msg = ('Changing time zone not allowed. Set {envar} to {gval} '
+ 'if you would like to allow this behavior')
+
+ return msg.format(envar=cls._guard_var_name,
+ gval=cls._guard_allows_change)
+
+ def __enter__(self):
+ if not self.tz_change_allowed():
+ raise ValueError(self.tz_change_disallowed_message())
+
+ self._old_tz = self.get_current_tz()
+ self.set_current_tz(self.tzval)
+
+ def __exit__(self, type, value, traceback):
+ if self._old_tz is not None:
+ self.set_current_tz(self._old_tz)
+
+ self._old_tz = None
+
+ def get_current_tz(self):
+ raise NotImplementedError
+
+ def set_current_tz(self):
+ raise NotImplementedError
+
+
+class TZEnvContext(TZContextBase):
+ """
+ Context manager that temporarily sets the `TZ` variable (for use on
+ *nix-like systems). Because the effect is local to the shell anyway, this
+ will apply *unless* a guard is set.
+
+ If you do not want the TZ environment variable set, you may set the
+ ``DATEUTIL_MAY_NOT_CHANGE_TZ_VAR`` variable to a truthy value.
+ """
+ _guard_var_name = "DATEUTIL_MAY_NOT_CHANGE_TZ_VAR"
+ _guard_allows_change = False
+
+ def get_current_tz(self):
+ return os.environ.get('TZ', UnsetTz)
+
+ def set_current_tz(self, tzval):
+ if tzval is UnsetTz and 'TZ' in os.environ:
+ del os.environ['TZ']
+ else:
+ os.environ['TZ'] = tzval
+
+ time.tzset()
+
+
+class TZWinContext(TZContextBase):
+ """
+ Context manager for changing local time zone on Windows.
+
+ Because the effect of this is system-wide and global, it may have
+ unintended side effect. Set the ``DATEUTIL_MAY_CHANGE_TZ`` environment
+ variable to a truthy value before using this context manager.
+ """
+ def get_current_tz(self):
+ p = subprocess.Popen(['tzutil', '/g'], stdout=subprocess.PIPE)
+
+ ctzname, err = p.communicate()
+ ctzname = ctzname.decode() # Popen returns
+
+ if p.returncode:
+ raise OSError('Failed to get current time zone: ' + err)
+
+ return ctzname
+
+ def set_current_tz(self, tzname):
+ p = subprocess.Popen('tzutil /s "' + tzname + '"')
+
+ out, err = p.communicate()
+
+ if p.returncode:
+ raise OSError('Failed to set current time zone: ' +
+ (err or 'Unknown error.'))
+
+
+###
+# Compatibility functions
+
+def _total_seconds(td):
+ # Python 2.6 doesn't have a total_seconds() method on timedelta objects
+ return ((td.seconds + td.days * 86400) * 1000000 +
+ td.microseconds) // 1000000
+
+total_seconds = getattr(datetime.timedelta, 'total_seconds', _total_seconds)
+
+
+###
+# Utility classes
+class NotAValueClass(object):
+ """
+ A class analogous to NaN that has operations defined for any type.
+ """
+ def _op(self, other):
+ return self # Operation with NotAValue returns NotAValue
+
+ def _cmp(self, other):
+ return False
+
+ __add__ = __radd__ = _op
+ __sub__ = __rsub__ = _op
+ __mul__ = __rmul__ = _op
+ __div__ = __rdiv__ = _op
+ __truediv__ = __rtruediv__ = _op
+ __floordiv__ = __rfloordiv__ = _op
+
+ __lt__ = __rlt__ = _op
+ __gt__ = __rgt__ = _op
+ __eq__ = __req__ = _op
+ __le__ = __rle__ = _op
+ __ge__ = __rge__ = _op
+
+NotAValue = NotAValueClass()
+
+
+class ComparesEqualClass(object):
+ """
+ A class that is always equal to whatever you compare it to.
+ """
+
+ def __eq__(self, other):
+ return True
+
+ def __ne__(self, other):
+ return False
+
+ def __le__(self, other):
+ return True
+
+ def __ge__(self, other):
+ return True
+
+ def __lt__(self, other):
+ return False
+
+ def __gt__(self, other):
+ return False
+
+ __req__ = __eq__
+ __rne__ = __ne__
+ __rle__ = __le__
+ __rge__ = __ge__
+ __rlt__ = __lt__
+ __rgt__ = __gt__
+
+ComparesEqual = ComparesEqualClass()
+
+class UnsetTzClass(object):
+ """ Sentinel class for unset time zone variable """
+ pass
+
+UnsetTz = UnsetTzClass()
+
diff --git a/libs/dateutil/test/test_easter.py b/libs/dateutil/test/test_easter.py
new file mode 100644
index 000000000..b45d7fe89
--- /dev/null
+++ b/libs/dateutil/test/test_easter.py
@@ -0,0 +1,99 @@
+from dateutil.easter import easter
+from dateutil.easter import EASTER_WESTERN, EASTER_ORTHODOX, EASTER_JULIAN
+
+from datetime import date
+
+try:
+ import unittest2 as unittest
+except ImportError:
+ import unittest
+
+# List of easters between 1990 and 2050
+western_easter_dates = [
+ date(1990, 4, 15), date(1991, 3, 31), date(1992, 4, 19), date(1993, 4, 11),
+ date(1994, 4, 3), date(1995, 4, 16), date(1996, 4, 7), date(1997, 3, 30),
+ date(1998, 4, 12), date(1999, 4, 4),
+
+ date(2000, 4, 23), date(2001, 4, 15), date(2002, 3, 31), date(2003, 4, 20),
+ date(2004, 4, 11), date(2005, 3, 27), date(2006, 4, 16), date(2007, 4, 8),
+ date(2008, 3, 23), date(2009, 4, 12),
+
+ date(2010, 4, 4), date(2011, 4, 24), date(2012, 4, 8), date(2013, 3, 31),
+ date(2014, 4, 20), date(2015, 4, 5), date(2016, 3, 27), date(2017, 4, 16),
+ date(2018, 4, 1), date(2019, 4, 21),
+
+ date(2020, 4, 12), date(2021, 4, 4), date(2022, 4, 17), date(2023, 4, 9),
+ date(2024, 3, 31), date(2025, 4, 20), date(2026, 4, 5), date(2027, 3, 28),
+ date(2028, 4, 16), date(2029, 4, 1),
+
+ date(2030, 4, 21), date(2031, 4, 13), date(2032, 3, 28), date(2033, 4, 17),
+ date(2034, 4, 9), date(2035, 3, 25), date(2036, 4, 13), date(2037, 4, 5),
+ date(2038, 4, 25), date(2039, 4, 10),
+
+ date(2040, 4, 1), date(2041, 4, 21), date(2042, 4, 6), date(2043, 3, 29),
+ date(2044, 4, 17), date(2045, 4, 9), date(2046, 3, 25), date(2047, 4, 14),
+ date(2048, 4, 5), date(2049, 4, 18), date(2050, 4, 10)
+ ]
+
+orthodox_easter_dates = [
+ date(1990, 4, 15), date(1991, 4, 7), date(1992, 4, 26), date(1993, 4, 18),
+ date(1994, 5, 1), date(1995, 4, 23), date(1996, 4, 14), date(1997, 4, 27),
+ date(1998, 4, 19), date(1999, 4, 11),
+
+ date(2000, 4, 30), date(2001, 4, 15), date(2002, 5, 5), date(2003, 4, 27),
+ date(2004, 4, 11), date(2005, 5, 1), date(2006, 4, 23), date(2007, 4, 8),
+ date(2008, 4, 27), date(2009, 4, 19),
+
+ date(2010, 4, 4), date(2011, 4, 24), date(2012, 4, 15), date(2013, 5, 5),
+ date(2014, 4, 20), date(2015, 4, 12), date(2016, 5, 1), date(2017, 4, 16),
+ date(2018, 4, 8), date(2019, 4, 28),
+
+ date(2020, 4, 19), date(2021, 5, 2), date(2022, 4, 24), date(2023, 4, 16),
+ date(2024, 5, 5), date(2025, 4, 20), date(2026, 4, 12), date(2027, 5, 2),
+ date(2028, 4, 16), date(2029, 4, 8),
+
+ date(2030, 4, 28), date(2031, 4, 13), date(2032, 5, 2), date(2033, 4, 24),
+ date(2034, 4, 9), date(2035, 4, 29), date(2036, 4, 20), date(2037, 4, 5),
+ date(2038, 4, 25), date(2039, 4, 17),
+
+ date(2040, 5, 6), date(2041, 4, 21), date(2042, 4, 13), date(2043, 5, 3),
+ date(2044, 4, 24), date(2045, 4, 9), date(2046, 4, 29), date(2047, 4, 21),
+ date(2048, 4, 5), date(2049, 4, 25), date(2050, 4, 17)
+]
+
+# A random smattering of Julian dates.
+# Pulled values from http://www.kevinlaughery.com/east4099.html
+julian_easter_dates = [
+ date( 326, 4, 3), date( 375, 4, 5), date( 492, 4, 5), date( 552, 3, 31),
+ date( 562, 4, 9), date( 569, 4, 21), date( 597, 4, 14), date( 621, 4, 19),
+ date( 636, 3, 31), date( 655, 3, 29), date( 700, 4, 11), date( 725, 4, 8),
+ date( 750, 3, 29), date( 782, 4, 7), date( 835, 4, 18), date( 849, 4, 14),
+ date( 867, 3, 30), date( 890, 4, 12), date( 922, 4, 21), date( 934, 4, 6),
+ date(1049, 3, 26), date(1058, 4, 19), date(1113, 4, 6), date(1119, 3, 30),
+ date(1242, 4, 20), date(1255, 3, 28), date(1257, 4, 8), date(1258, 3, 24),
+ date(1261, 4, 24), date(1278, 4, 17), date(1333, 4, 4), date(1351, 4, 17),
+ date(1371, 4, 6), date(1391, 3, 26), date(1402, 3, 26), date(1412, 4, 3),
+ date(1439, 4, 5), date(1445, 3, 28), date(1531, 4, 9), date(1555, 4, 14)
+]
+
+
+class EasterTest(unittest.TestCase):
+ def testEasterWestern(self):
+ for easter_date in western_easter_dates:
+ self.assertEqual(easter_date,
+ easter(easter_date.year, EASTER_WESTERN))
+
+ def testEasterOrthodox(self):
+ for easter_date in orthodox_easter_dates:
+ self.assertEqual(easter_date,
+ easter(easter_date.year, EASTER_ORTHODOX))
+
+ def testEasterJulian(self):
+ for easter_date in julian_easter_dates:
+ self.assertEqual(easter_date,
+ easter(easter_date.year, EASTER_JULIAN))
+
+ def testEasterBadMethod(self):
+ # Invalid methods raise ValueError
+ with self.assertRaises(ValueError):
+ easter(1975, 4)
diff --git a/libs/dateutil/test/test_imports.py b/libs/dateutil/test/test_imports.py
new file mode 100644
index 000000000..1d8ac171e
--- /dev/null
+++ b/libs/dateutil/test/test_imports.py
@@ -0,0 +1,149 @@
+import sys
+
+try:
+ import unittest2 as unittest
+except ImportError:
+ import unittest
+
+
+class ImportEasterTest(unittest.TestCase):
+ """ Test that dateutil.easter-related imports work properly """
+
+ def testEasterDirect(self):
+ import dateutil.easter
+
+ def testEasterFrom(self):
+ from dateutil import easter
+
+ def testEasterStar(self):
+ from dateutil.easter import easter
+
+
+class ImportParserTest(unittest.TestCase):
+ """ Test that dateutil.parser-related imports work properly """
+ def testParserDirect(self):
+ import dateutil.parser
+
+ def testParserFrom(self):
+ from dateutil import parser
+
+ def testParserAll(self):
+ # All interface
+ from dateutil.parser import parse
+ from dateutil.parser import parserinfo
+
+ # Other public classes
+ from dateutil.parser import parser
+
+ for var in (parse, parserinfo, parser):
+ self.assertIsNot(var, None)
+
+
+class ImportRelativeDeltaTest(unittest.TestCase):
+ """ Test that dateutil.relativedelta-related imports work properly """
+ def testRelativeDeltaDirect(self):
+ import dateutil.relativedelta
+
+ def testRelativeDeltaFrom(self):
+ from dateutil import relativedelta
+
+ def testRelativeDeltaAll(self):
+ from dateutil.relativedelta import relativedelta
+ from dateutil.relativedelta import MO, TU, WE, TH, FR, SA, SU
+
+ for var in (relativedelta, MO, TU, WE, TH, FR, SA, SU):
+ self.assertIsNot(var, None)
+
+ # In the public interface but not in all
+ from dateutil.relativedelta import weekday
+ self.assertIsNot(weekday, None)
+
+
+class ImportRRuleTest(unittest.TestCase):
+ """ Test that dateutil.rrule related imports work properly """
+ def testRRuleDirect(self):
+ import dateutil.rrule
+
+ def testRRuleFrom(self):
+ from dateutil import rrule
+
+ def testRRuleAll(self):
+ from dateutil.rrule import rrule
+ from dateutil.rrule import rruleset
+ from dateutil.rrule import rrulestr
+ from dateutil.rrule import YEARLY, MONTHLY, WEEKLY, DAILY
+ from dateutil.rrule import HOURLY, MINUTELY, SECONDLY
+ from dateutil.rrule import MO, TU, WE, TH, FR, SA, SU
+
+ rr_all = (rrule, rruleset, rrulestr,
+ YEARLY, MONTHLY, WEEKLY, DAILY,
+ HOURLY, MINUTELY, SECONDLY,
+ MO, TU, WE, TH, FR, SA, SU)
+
+ for var in rr_all:
+ self.assertIsNot(var, None)
+
+ # In the public interface but not in all
+ from dateutil.rrule import weekday
+ self.assertIsNot(weekday, None)
+
+
+class ImportTZTest(unittest.TestCase):
+ """ Test that dateutil.tz related imports work properly """
+ def testTzDirect(self):
+ import dateutil.tz
+
+ def testTzFrom(self):
+ from dateutil import tz
+
+ def testTzAll(self):
+ from dateutil.tz import tzutc
+ from dateutil.tz import tzoffset
+ from dateutil.tz import tzlocal
+ from dateutil.tz import tzfile
+ from dateutil.tz import tzrange
+ from dateutil.tz import tzstr
+ from dateutil.tz import tzical
+ from dateutil.tz import gettz
+ from dateutil.tz import tzwin
+ from dateutil.tz import tzwinlocal
+
+ tz_all = ["tzutc", "tzoffset", "tzlocal", "tzfile", "tzrange",
+ "tzstr", "tzical", "gettz"]
+
+ tz_all += ["tzwin", "tzwinlocal"] if sys.platform.startswith("win") else []
+ lvars = locals()
+
+ for var in tz_all:
+ self.assertIsNot(lvars[var], None)
+
+
[email protected](sys.platform.startswith('win'), "Requires Windows")
+class ImportTZWinTest(unittest.TestCase):
+ """ Test that dateutil.tzwin related imports work properly """
+ def testTzwinDirect(self):
+ import dateutil.tzwin
+
+ def testTzwinFrom(self):
+ from dateutil import tzwin
+
+ def testTzwinStar(self):
+ tzwin_all = ["tzwin", "tzwinlocal"]
+
+
+class ImportZoneInfoTest(unittest.TestCase):
+ def testZoneinfoDirect(self):
+ import dateutil.zoneinfo
+
+ def testZoneinfoFrom(self):
+ from dateutil import zoneinfo
+
+ def testZoneinfoStar(self):
+ from dateutil.zoneinfo import gettz
+ from dateutil.zoneinfo import gettz_db_metadata
+ from dateutil.zoneinfo import rebuild
+
+ zi_all = (gettz, gettz_db_metadata, rebuild)
+
+ for var in zi_all:
+ self.assertIsNot(var, None)
diff --git a/libs/dateutil/test/test_parser.py b/libs/dateutil/test/test_parser.py
new file mode 100644
index 000000000..1115bbf65
--- /dev/null
+++ b/libs/dateutil/test/test_parser.py
@@ -0,0 +1,861 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+from ._common import unittest
+
+from datetime import datetime, timedelta, date
+
+from dateutil.tz import tzoffset
+from dateutil.parser import *
+
+import six
+from six import assertRaisesRegex, PY3
+from six.moves import StringIO
+
+class ParserTest(unittest.TestCase):
+
+ def setUp(self):
+ self.tzinfos = {"BRST": -10800}
+ self.brsttz = tzoffset("BRST", -10800)
+ self.default = datetime(2003, 9, 25)
+
+ # Parser should be able to handle bytestring and unicode
+ base_str = '2014-05-01 08:00:00'
+ try:
+ # Python 2.x
+ self.uni_str = unicode(base_str)
+ self.str_str = str(base_str)
+ except NameError:
+ self.uni_str = str(base_str)
+ self.str_str = bytes(base_str.encode())
+
+ def testEmptyString(self):
+ with self.assertRaises(ValueError):
+ parse('')
+
+ def testNone(self):
+ with self.assertRaises(TypeError):
+ parse(None)
+
+ def testInvalidType(self):
+ with self.assertRaises(TypeError):
+ parse(13)
+
+ def testDuckTyping(self):
+ # We want to support arbitrary classes that implement the stream
+ # interface.
+
+ class StringPassThrough(object):
+ def __init__(self, stream):
+ self.stream = stream
+
+ def read(self, *args, **kwargs):
+ return self.stream.read(*args, **kwargs)
+
+
+ dstr = StringPassThrough(StringIO('2014 January 19'))
+
+ self.assertEqual(parse(dstr), datetime(2014, 1, 19))
+
+ def testParseStream(self):
+ dstr = StringIO('2014 January 19')
+
+ self.assertEqual(parse(dstr), datetime(2014, 1, 19))
+
+ def testParseStr(self):
+ self.assertEqual(parse(self.str_str),
+ parse(self.uni_str))
+
+ def testParserParseStr(self):
+ from dateutil.parser import parser
+
+ self.assertEqual(parser().parse(self.str_str),
+ parser().parse(self.uni_str))
+
+ def testParseUnicodeWords(self):
+
+ class rus_parserinfo(parserinfo):
+ MONTHS = [("янв", "Январь"),
+ ("фев", "Февраль"),
+ ("мар", "Март"),
+ ("апр", "Апрель"),
+ ("май", "Май"),
+ ("июн", "Июнь"),
+ ("июл", "Июль"),
+ ("авг", "Август"),
+ ("сен", "Сентябрь"),
+ ("окт", "Октябрь"),
+ ("ноя", "Ноябрь"),
+ ("дек", "Декабрь")]
+
+ self.assertEqual(parse('10 Сентябрь 2015 10:20',
+ parserinfo=rus_parserinfo()),
+ datetime(2015, 9, 10, 10, 20))
+
+ def testParseWithNulls(self):
+ # This relies on the from __future__ import unicode_literals, because
+ # explicitly specifying a unicode literal is a syntax error in Py 3.2
+ # May want to switch to u'...' if we ever drop Python 3.2 support.
+ pstring = '\x00\x00August 29, 1924'
+
+ self.assertEqual(parse(pstring),
+ datetime(1924, 8, 29))
+
+ def testDateCommandFormat(self):
+ self.assertEqual(parse("Thu Sep 25 10:36:28 BRST 2003",
+ tzinfos=self.tzinfos),
+ datetime(2003, 9, 25, 10, 36, 28,
+ tzinfo=self.brsttz))
+
+ def testDateCommandFormatUnicode(self):
+ self.assertEqual(parse("Thu Sep 25 10:36:28 BRST 2003",
+ tzinfos=self.tzinfos),
+ datetime(2003, 9, 25, 10, 36, 28,
+ tzinfo=self.brsttz))
+
+
+ def testDateCommandFormatReversed(self):
+ self.assertEqual(parse("2003 10:36:28 BRST 25 Sep Thu",
+ tzinfos=self.tzinfos),
+ datetime(2003, 9, 25, 10, 36, 28,
+ tzinfo=self.brsttz))
+
+ def testDateCommandFormatWithLong(self):
+ if not PY3:
+ self.assertEqual(parse("Thu Sep 25 10:36:28 BRST 2003",
+ tzinfos={"BRST": long(-10800)}),
+ datetime(2003, 9, 25, 10, 36, 28,
+ tzinfo=self.brsttz))
+ def testDateCommandFormatIgnoreTz(self):
+ self.assertEqual(parse("Thu Sep 25 10:36:28 BRST 2003",
+ ignoretz=True),
+ datetime(2003, 9, 25, 10, 36, 28))
+
+ def testDateCommandFormatStrip1(self):
+ self.assertEqual(parse("Thu Sep 25 10:36:28 2003"),
+ datetime(2003, 9, 25, 10, 36, 28))
+
+ def testDateCommandFormatStrip2(self):
+ self.assertEqual(parse("Thu Sep 25 10:36:28", default=self.default),
+ datetime(2003, 9, 25, 10, 36, 28))
+
+ def testDateCommandFormatStrip3(self):
+ self.assertEqual(parse("Thu Sep 10:36:28", default=self.default),
+ datetime(2003, 9, 25, 10, 36, 28))
+
+ def testDateCommandFormatStrip4(self):
+ self.assertEqual(parse("Thu 10:36:28", default=self.default),
+ datetime(2003, 9, 25, 10, 36, 28))
+
+ def testDateCommandFormatStrip5(self):
+ self.assertEqual(parse("Sep 10:36:28", default=self.default),
+ datetime(2003, 9, 25, 10, 36, 28))
+
+ def testDateCommandFormatStrip6(self):
+ self.assertEqual(parse("10:36:28", default=self.default),
+ datetime(2003, 9, 25, 10, 36, 28))
+
+ def testDateCommandFormatStrip7(self):
+ self.assertEqual(parse("10:36", default=self.default),
+ datetime(2003, 9, 25, 10, 36))
+
+ def testDateCommandFormatStrip8(self):
+ self.assertEqual(parse("Thu Sep 25 2003"),
+ datetime(2003, 9, 25))
+
+ def testDateCommandFormatStrip9(self):
+ self.assertEqual(parse("Sep 25 2003"),
+ datetime(2003, 9, 25))
+
+ def testDateCommandFormatStrip10(self):
+ self.assertEqual(parse("Sep 2003", default=self.default),
+ datetime(2003, 9, 25))
+
+ def testDateCommandFormatStrip11(self):
+ self.assertEqual(parse("Sep", default=self.default),
+ datetime(2003, 9, 25))
+
+ def testDateCommandFormatStrip12(self):
+ self.assertEqual(parse("2003", default=self.default),
+ datetime(2003, 9, 25))
+
+ def testDateRCommandFormat(self):
+ self.assertEqual(parse("Thu, 25 Sep 2003 10:49:41 -0300"),
+ datetime(2003, 9, 25, 10, 49, 41,
+ tzinfo=self.brsttz))
+
+ def testISOFormat(self):
+ self.assertEqual(parse("2003-09-25T10:49:41.5-03:00"),
+ datetime(2003, 9, 25, 10, 49, 41, 500000,
+ tzinfo=self.brsttz))
+
+ def testISOFormatStrip1(self):
+ self.assertEqual(parse("2003-09-25T10:49:41-03:00"),
+ datetime(2003, 9, 25, 10, 49, 41,
+ tzinfo=self.brsttz))
+
+ def testISOFormatStrip2(self):
+ self.assertEqual(parse("2003-09-25T10:49:41"),
+ datetime(2003, 9, 25, 10, 49, 41))
+
+ def testISOFormatStrip3(self):
+ self.assertEqual(parse("2003-09-25T10:49"),
+ datetime(2003, 9, 25, 10, 49))
+
+ def testISOFormatStrip4(self):
+ self.assertEqual(parse("2003-09-25T10"),
+ datetime(2003, 9, 25, 10))
+
+ def testISOFormatStrip5(self):
+ self.assertEqual(parse("2003-09-25"),
+ datetime(2003, 9, 25))
+
+ def testISOStrippedFormat(self):
+ self.assertEqual(parse("20030925T104941.5-0300"),
+ datetime(2003, 9, 25, 10, 49, 41, 500000,
+ tzinfo=self.brsttz))
+
+ def testISOStrippedFormatStrip1(self):
+ self.assertEqual(parse("20030925T104941-0300"),
+ datetime(2003, 9, 25, 10, 49, 41,
+ tzinfo=self.brsttz))
+
+ def testISOStrippedFormatStrip2(self):
+ self.assertEqual(parse("20030925T104941"),
+ datetime(2003, 9, 25, 10, 49, 41))
+
+ def testISOStrippedFormatStrip3(self):
+ self.assertEqual(parse("20030925T1049"),
+ datetime(2003, 9, 25, 10, 49, 0))
+
+ def testISOStrippedFormatStrip4(self):
+ self.assertEqual(parse("20030925T10"),
+ datetime(2003, 9, 25, 10))
+
+ def testISOStrippedFormatStrip5(self):
+ self.assertEqual(parse("20030925"),
+ datetime(2003, 9, 25))
+
+ def testPythonLoggerFormat(self):
+ self.assertEqual(parse("2003-09-25 10:49:41,502"),
+ datetime(2003, 9, 25, 10, 49, 41, 502000))
+
+ def testNoSeparator1(self):
+ self.assertEqual(parse("199709020908"),
+ datetime(1997, 9, 2, 9, 8))
+
+ def testNoSeparator2(self):
+ self.assertEqual(parse("19970902090807"),
+ datetime(1997, 9, 2, 9, 8, 7))
+
+ def testDateWithDash1(self):
+ self.assertEqual(parse("2003-09-25"),
+ datetime(2003, 9, 25))
+
+ def testDateWithDash2(self):
+ self.assertEqual(parse("2003-Sep-25"),
+ datetime(2003, 9, 25))
+
+ def testDateWithDash3(self):
+ self.assertEqual(parse("25-Sep-2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithDash4(self):
+ self.assertEqual(parse("25-Sep-2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithDash5(self):
+ self.assertEqual(parse("Sep-25-2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithDash6(self):
+ self.assertEqual(parse("09-25-2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithDash7(self):
+ self.assertEqual(parse("25-09-2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithDash8(self):
+ self.assertEqual(parse("10-09-2003", dayfirst=True),
+ datetime(2003, 9, 10))
+
+ def testDateWithDash9(self):
+ self.assertEqual(parse("10-09-2003"),
+ datetime(2003, 10, 9))
+
+ def testDateWithDash10(self):
+ self.assertEqual(parse("10-09-03"),
+ datetime(2003, 10, 9))
+
+ def testDateWithDash11(self):
+ self.assertEqual(parse("10-09-03", yearfirst=True),
+ datetime(2010, 9, 3))
+
+ def testDateWithDot1(self):
+ self.assertEqual(parse("2003.09.25"),
+ datetime(2003, 9, 25))
+
+ def testDateWithDot2(self):
+ self.assertEqual(parse("2003.Sep.25"),
+ datetime(2003, 9, 25))
+
+ def testDateWithDot3(self):
+ self.assertEqual(parse("25.Sep.2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithDot4(self):
+ self.assertEqual(parse("25.Sep.2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithDot5(self):
+ self.assertEqual(parse("Sep.25.2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithDot6(self):
+ self.assertEqual(parse("09.25.2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithDot7(self):
+ self.assertEqual(parse("25.09.2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithDot8(self):
+ self.assertEqual(parse("10.09.2003", dayfirst=True),
+ datetime(2003, 9, 10))
+
+ def testDateWithDot9(self):
+ self.assertEqual(parse("10.09.2003"),
+ datetime(2003, 10, 9))
+
+ def testDateWithDot10(self):
+ self.assertEqual(parse("10.09.03"),
+ datetime(2003, 10, 9))
+
+ def testDateWithDot11(self):
+ self.assertEqual(parse("10.09.03", yearfirst=True),
+ datetime(2010, 9, 3))
+
+ def testDateWithSlash1(self):
+ self.assertEqual(parse("2003/09/25"),
+ datetime(2003, 9, 25))
+
+ def testDateWithSlash2(self):
+ self.assertEqual(parse("2003/Sep/25"),
+ datetime(2003, 9, 25))
+
+ def testDateWithSlash3(self):
+ self.assertEqual(parse("25/Sep/2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithSlash4(self):
+ self.assertEqual(parse("25/Sep/2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithSlash5(self):
+ self.assertEqual(parse("Sep/25/2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithSlash6(self):
+ self.assertEqual(parse("09/25/2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithSlash7(self):
+ self.assertEqual(parse("25/09/2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithSlash8(self):
+ self.assertEqual(parse("10/09/2003", dayfirst=True),
+ datetime(2003, 9, 10))
+
+ def testDateWithSlash9(self):
+ self.assertEqual(parse("10/09/2003"),
+ datetime(2003, 10, 9))
+
+ def testDateWithSlash10(self):
+ self.assertEqual(parse("10/09/03"),
+ datetime(2003, 10, 9))
+
+ def testDateWithSlash11(self):
+ self.assertEqual(parse("10/09/03", yearfirst=True),
+ datetime(2010, 9, 3))
+
+ def testDateWithSpace1(self):
+ self.assertEqual(parse("2003 09 25"),
+ datetime(2003, 9, 25))
+
+ def testDateWithSpace2(self):
+ self.assertEqual(parse("2003 Sep 25"),
+ datetime(2003, 9, 25))
+
+ def testDateWithSpace3(self):
+ self.assertEqual(parse("25 Sep 2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithSpace4(self):
+ self.assertEqual(parse("25 Sep 2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithSpace5(self):
+ self.assertEqual(parse("Sep 25 2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithSpace6(self):
+ self.assertEqual(parse("09 25 2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithSpace7(self):
+ self.assertEqual(parse("25 09 2003"),
+ datetime(2003, 9, 25))
+
+ def testDateWithSpace8(self):
+ self.assertEqual(parse("10 09 2003", dayfirst=True),
+ datetime(2003, 9, 10))
+
+ def testDateWithSpace9(self):
+ self.assertEqual(parse("10 09 2003"),
+ datetime(2003, 10, 9))
+
+ def testDateWithSpace10(self):
+ self.assertEqual(parse("10 09 03"),
+ datetime(2003, 10, 9))
+
+ def testDateWithSpace11(self):
+ self.assertEqual(parse("10 09 03", yearfirst=True),
+ datetime(2010, 9, 3))
+
+ def testDateWithSpace12(self):
+ self.assertEqual(parse("25 09 03"),
+ datetime(2003, 9, 25))
+
+ def testStrangelyOrderedDate1(self):
+ self.assertEqual(parse("03 25 Sep"),
+ datetime(2003, 9, 25))
+
+ def testStrangelyOrderedDate2(self):
+ self.assertEqual(parse("2003 25 Sep"),
+ datetime(2003, 9, 25))
+
+ def testStrangelyOrderedDate3(self):
+ self.assertEqual(parse("25 03 Sep"),
+ datetime(2025, 9, 3))
+
+ def testHourWithLetters(self):
+ self.assertEqual(parse("10h36m28.5s", default=self.default),
+ datetime(2003, 9, 25, 10, 36, 28, 500000))
+
+ def testHourWithLettersStrip1(self):
+ self.assertEqual(parse("10h36m28s", default=self.default),
+ datetime(2003, 9, 25, 10, 36, 28))
+
+ def testHourWithLettersStrip2(self):
+ self.assertEqual(parse("10h36m", default=self.default),
+ datetime(2003, 9, 25, 10, 36))
+
+ def testHourWithLettersStrip3(self):
+ self.assertEqual(parse("10h", default=self.default),
+ datetime(2003, 9, 25, 10))
+
+ def testHourWithLettersStrip4(self):
+ self.assertEqual(parse("10 h 36", default=self.default),
+ datetime(2003, 9, 25, 10, 36))
+
+ def testAMPMNoHour(self):
+ with self.assertRaises(ValueError):
+ parse("AM")
+
+ with self.assertRaises(ValueError):
+ parse("Jan 20, 2015 PM")
+
+ def testHourAmPm1(self):
+ self.assertEqual(parse("10h am", default=self.default),
+ datetime(2003, 9, 25, 10))
+
+ def testHourAmPm2(self):
+ self.assertEqual(parse("10h pm", default=self.default),
+ datetime(2003, 9, 25, 22))
+
+ def testHourAmPm3(self):
+ self.assertEqual(parse("10am", default=self.default),
+ datetime(2003, 9, 25, 10))
+
+ def testHourAmPm4(self):
+ self.assertEqual(parse("10pm", default=self.default),
+ datetime(2003, 9, 25, 22))
+
+ def testHourAmPm5(self):
+ self.assertEqual(parse("10:00 am", default=self.default),
+ datetime(2003, 9, 25, 10))
+
+ def testHourAmPm6(self):
+ self.assertEqual(parse("10:00 pm", default=self.default),
+ datetime(2003, 9, 25, 22))
+
+ def testHourAmPm7(self):
+ self.assertEqual(parse("10:00am", default=self.default),
+ datetime(2003, 9, 25, 10))
+
+ def testHourAmPm8(self):
+ self.assertEqual(parse("10:00pm", default=self.default),
+ datetime(2003, 9, 25, 22))
+
+ def testHourAmPm9(self):
+ self.assertEqual(parse("10:00a.m", default=self.default),
+ datetime(2003, 9, 25, 10))
+
+ def testHourAmPm10(self):
+ self.assertEqual(parse("10:00p.m", default=self.default),
+ datetime(2003, 9, 25, 22))
+
+ def testHourAmPm11(self):
+ self.assertEqual(parse("10:00a.m.", default=self.default),
+ datetime(2003, 9, 25, 10))
+
+ def testHourAmPm12(self):
+ self.assertEqual(parse("10:00p.m.", default=self.default),
+ datetime(2003, 9, 25, 22))
+
+ def testAMPMRange(self):
+ with self.assertRaises(ValueError):
+ parse("13:44 AM")
+
+ with self.assertRaises(ValueError):
+ parse("January 25, 1921 23:13 PM")
+
+ def testPertain(self):
+ self.assertEqual(parse("Sep 03", default=self.default),
+ datetime(2003, 9, 3))
+ self.assertEqual(parse("Sep of 03", default=self.default),
+ datetime(2003, 9, 25))
+
+ def testWeekdayAlone(self):
+ self.assertEqual(parse("Wed", default=self.default),
+ datetime(2003, 10, 1))
+
+ def testLongWeekday(self):
+ self.assertEqual(parse("Wednesday", default=self.default),
+ datetime(2003, 10, 1))
+
+ def testLongMonth(self):
+ self.assertEqual(parse("October", default=self.default),
+ datetime(2003, 10, 25))
+
+ def testZeroYear(self):
+ self.assertEqual(parse("31-Dec-00", default=self.default),
+ datetime(2000, 12, 31))
+
+ def testFuzzy(self):
+ s = "Today is 25 of September of 2003, exactly " \
+ "at 10:49:41 with timezone -03:00."
+ self.assertEqual(parse(s, fuzzy=True),
+ datetime(2003, 9, 25, 10, 49, 41,
+ tzinfo=self.brsttz))
+
+ def testFuzzyWithTokens(self):
+ s = "Today is 25 of September of 2003, exactly " \
+ "at 10:49:41 with timezone -03:00."
+ self.assertEqual(parse(s, fuzzy_with_tokens=True),
+ (datetime(2003, 9, 25, 10, 49, 41,
+ tzinfo=self.brsttz),
+ ('Today is ', 'of ', ', exactly at ',
+ ' with timezone ', '.')))
+
+ def testFuzzyAMPMProblem(self):
+ # Sometimes fuzzy parsing results in AM/PM flag being set without
+ # hours - if it's fuzzy it should ignore that.
+ s1 = "I have a meeting on March 1, 1974."
+ s2 = "On June 8th, 2020, I am going to be the first man on Mars"
+
+ # Also don't want any erroneous AM or PMs changing the parsed time
+ s3 = "Meet me at the AM/PM on Sunset at 3:00 AM on December 3rd, 2003"
+ s4 = "Meet me at 3:00AM on December 3rd, 2003 at the AM/PM on Sunset"
+
+ self.assertEqual(parse(s1, fuzzy=True), datetime(1974, 3, 1))
+ self.assertEqual(parse(s2, fuzzy=True), datetime(2020, 6, 8))
+ self.assertEqual(parse(s3, fuzzy=True), datetime(2003, 12, 3, 3))
+ self.assertEqual(parse(s4, fuzzy=True), datetime(2003, 12, 3, 3))
+
+ def testFuzzyIgnoreAMPM(self):
+ s1 = "Jan 29, 1945 14:45 AM I going to see you there?"
+
+ self.assertEqual(parse(s1, fuzzy=True), datetime(1945, 1, 29, 14, 45))
+
+ def testExtraSpace(self):
+ self.assertEqual(parse(" July 4 , 1976 12:01:02 am "),
+ datetime(1976, 7, 4, 0, 1, 2))
+
+ def testRandomFormat1(self):
+ self.assertEqual(parse("Wed, July 10, '96"),
+ datetime(1996, 7, 10, 0, 0))
+
+ def testRandomFormat2(self):
+ self.assertEqual(parse("1996.07.10 AD at 15:08:56 PDT",
+ ignoretz=True),
+ datetime(1996, 7, 10, 15, 8, 56))
+
+ def testRandomFormat3(self):
+ self.assertEqual(parse("1996.July.10 AD 12:08 PM"),
+ datetime(1996, 7, 10, 12, 8))
+
+ def testRandomFormat4(self):
+ self.assertEqual(parse("Tuesday, April 12, 1952 AD 3:30:42pm PST",
+ ignoretz=True),
+ datetime(1952, 4, 12, 15, 30, 42))
+
+ def testRandomFormat5(self):
+ self.assertEqual(parse("November 5, 1994, 8:15:30 am EST",
+ ignoretz=True),
+ datetime(1994, 11, 5, 8, 15, 30))
+
+ def testRandomFormat6(self):
+ self.assertEqual(parse("1994-11-05T08:15:30-05:00",
+ ignoretz=True),
+ datetime(1994, 11, 5, 8, 15, 30))
+
+ def testRandomFormat7(self):
+ self.assertEqual(parse("1994-11-05T08:15:30Z",
+ ignoretz=True),
+ datetime(1994, 11, 5, 8, 15, 30))
+
+ def testRandomFormat8(self):
+ self.assertEqual(parse("July 4, 1976"), datetime(1976, 7, 4))
+
+ def testRandomFormat9(self):
+ self.assertEqual(parse("7 4 1976"), datetime(1976, 7, 4))
+
+ def testRandomFormat10(self):
+ self.assertEqual(parse("4 jul 1976"), datetime(1976, 7, 4))
+
+ def testRandomFormat11(self):
+ self.assertEqual(parse("7-4-76"), datetime(1976, 7, 4))
+
+ def testRandomFormat12(self):
+ self.assertEqual(parse("19760704"), datetime(1976, 7, 4))
+
+ def testRandomFormat13(self):
+ self.assertEqual(parse("0:01:02", default=self.default),
+ datetime(2003, 9, 25, 0, 1, 2))
+
+ def testRandomFormat14(self):
+ self.assertEqual(parse("12h 01m02s am", default=self.default),
+ datetime(2003, 9, 25, 0, 1, 2))
+
+ def testRandomFormat15(self):
+ self.assertEqual(parse("0:01:02 on July 4, 1976"),
+ datetime(1976, 7, 4, 0, 1, 2))
+
+ def testRandomFormat16(self):
+ self.assertEqual(parse("0:01:02 on July 4, 1976"),
+ datetime(1976, 7, 4, 0, 1, 2))
+
+ def testRandomFormat17(self):
+ self.assertEqual(parse("1976-07-04T00:01:02Z", ignoretz=True),
+ datetime(1976, 7, 4, 0, 1, 2))
+
+ def testRandomFormat18(self):
+ self.assertEqual(parse("July 4, 1976 12:01:02 am"),
+ datetime(1976, 7, 4, 0, 1, 2))
+
+ def testRandomFormat19(self):
+ self.assertEqual(parse("Mon Jan 2 04:24:27 1995"),
+ datetime(1995, 1, 2, 4, 24, 27))
+
+ def testRandomFormat20(self):
+ self.assertEqual(parse("Tue Apr 4 00:22:12 PDT 1995", ignoretz=True),
+ datetime(1995, 4, 4, 0, 22, 12))
+
+ def testRandomFormat21(self):
+ self.assertEqual(parse("04.04.95 00:22"),
+ datetime(1995, 4, 4, 0, 22))
+
+ def testRandomFormat22(self):
+ self.assertEqual(parse("Jan 1 1999 11:23:34.578"),
+ datetime(1999, 1, 1, 11, 23, 34, 578000))
+
+ def testRandomFormat23(self):
+ self.assertEqual(parse("950404 122212"),
+ datetime(1995, 4, 4, 12, 22, 12))
+
+ def testRandomFormat24(self):
+ self.assertEqual(parse("0:00 PM, PST", default=self.default,
+ ignoretz=True),
+ datetime(2003, 9, 25, 12, 0))
+
+ def testRandomFormat25(self):
+ self.assertEqual(parse("12:08 PM", default=self.default),
+ datetime(2003, 9, 25, 12, 8))
+
+ def testRandomFormat26(self):
+ self.assertEqual(parse("5:50 A.M. on June 13, 1990"),
+ datetime(1990, 6, 13, 5, 50))
+
+ def testRandomFormat27(self):
+ self.assertEqual(parse("3rd of May 2001"), datetime(2001, 5, 3))
+
+ def testRandomFormat28(self):
+ self.assertEqual(parse("5th of March 2001"), datetime(2001, 3, 5))
+
+ def testRandomFormat29(self):
+ self.assertEqual(parse("1st of May 2003"), datetime(2003, 5, 1))
+
+ def testRandomFormat30(self):
+ self.assertEqual(parse("01h02m03", default=self.default),
+ datetime(2003, 9, 25, 1, 2, 3))
+
+ def testRandomFormat31(self):
+ self.assertEqual(parse("01h02", default=self.default),
+ datetime(2003, 9, 25, 1, 2))
+
+ def testRandomFormat32(self):
+ self.assertEqual(parse("01h02s", default=self.default),
+ datetime(2003, 9, 25, 1, 0, 2))
+
+ def testRandomFormat33(self):
+ self.assertEqual(parse("01m02", default=self.default),
+ datetime(2003, 9, 25, 0, 1, 2))
+
+ def testRandomFormat34(self):
+ self.assertEqual(parse("01m02h", default=self.default),
+ datetime(2003, 9, 25, 2, 1))
+
+ def testRandomFormat35(self):
+ self.assertEqual(parse("2004 10 Apr 11h30m", default=self.default),
+ datetime(2004, 4, 10, 11, 30))
+
+ def test_99_ad(self):
+ self.assertEqual(parse('0099-01-01T00:00:00'),
+ datetime(99, 1, 1, 0, 0))
+
+ def test_31_ad(self):
+ self.assertEqual(parse('0031-01-01T00:00:00'),
+ datetime(31, 1, 1, 0, 0))
+
+ def testInvalidDay(self):
+ with self.assertRaises(ValueError):
+ parse("Feb 30, 2007")
+
+ def testUnspecifiedDayFallback(self):
+ # Test that for an unspecified day, the fallback behavior is correct.
+ self.assertEqual(parse("April 2009", default=datetime(2010, 1, 31)),
+ datetime(2009, 4, 30))
+
+ def testUnspecifiedDayFallbackFebNoLeapYear(self):
+ self.assertEqual(parse("Feb 2007", default=datetime(2010, 1, 31)),
+ datetime(2007, 2, 28))
+
+ def testUnspecifiedDayFallbackFebLeapYear(self):
+ self.assertEqual(parse("Feb 2008", default=datetime(2010, 1, 31)),
+ datetime(2008, 2, 29))
+
+ def testErrorType01(self):
+ self.assertRaises(ValueError,
+ parse, 'shouldfail')
+
+ def testCorrectErrorOnFuzzyWithTokens(self):
+ assertRaisesRegex(self, ValueError, 'Unknown string format',
+ parse, '04/04/32/423', fuzzy_with_tokens=True)
+ assertRaisesRegex(self, ValueError, 'Unknown string format',
+ parse, '04/04/04 +32423', fuzzy_with_tokens=True)
+ assertRaisesRegex(self, ValueError, 'Unknown string format',
+ parse, '04/04/0d4', fuzzy_with_tokens=True)
+
+ def testIncreasingCTime(self):
+ # This test will check 200 different years, every month, every day,
+ # every hour, every minute, every second, and every weekday, using
+ # a delta of more or less 1 year, 1 month, 1 day, 1 minute and
+ # 1 second.
+ delta = timedelta(days=365+31+1, seconds=1+60+60*60)
+ dt = datetime(1900, 1, 1, 0, 0, 0, 0)
+ for i in range(200):
+ self.assertEqual(parse(dt.ctime()), dt)
+ dt += delta
+
+ def testIncreasingISOFormat(self):
+ delta = timedelta(days=365+31+1, seconds=1+60+60*60)
+ dt = datetime(1900, 1, 1, 0, 0, 0, 0)
+ for i in range(200):
+ self.assertEqual(parse(dt.isoformat()), dt)
+ dt += delta
+
+ def testMicrosecondsPrecisionError(self):
+ # Skip found out that sad precision problem. :-(
+ dt1 = parse("00:11:25.01")
+ dt2 = parse("00:12:10.01")
+ self.assertEqual(dt1.microsecond, 10000)
+ self.assertEqual(dt2.microsecond, 10000)
+
+ def testMicrosecondPrecisionErrorReturns(self):
+ # One more precision issue, discovered by Eric Brown. This should
+ # be the last one, as we're no longer using floating points.
+ for ms in [100001, 100000, 99999, 99998,
+ 10001, 10000, 9999, 9998,
+ 1001, 1000, 999, 998,
+ 101, 100, 99, 98]:
+ dt = datetime(2008, 2, 27, 21, 26, 1, ms)
+ self.assertEqual(parse(dt.isoformat()), dt)
+
+ def testHighPrecisionSeconds(self):
+ self.assertEqual(parse("20080227T21:26:01.123456789"),
+ datetime(2008, 2, 27, 21, 26, 1, 123456))
+
+ def testCustomParserInfo(self):
+ # Custom parser info wasn't working, as Michael Elsdörfer discovered.
+ from dateutil.parser import parserinfo, parser
+
+ class myparserinfo(parserinfo):
+ MONTHS = parserinfo.MONTHS[:]
+ MONTHS[0] = ("Foo", "Foo")
+ myparser = parser(myparserinfo())
+ dt = myparser.parse("01/Foo/2007")
+ self.assertEqual(dt, datetime(2007, 1, 1))
+
+ def testNoYearFirstNoDayFirst(self):
+ dtstr = '090107'
+
+ # Should be MMDDYY
+ self.assertEqual(parse(dtstr),
+ datetime(2007, 9, 1))
+
+ self.assertEqual(parse(dtstr, yearfirst=False, dayfirst=False),
+ datetime(2007, 9, 1))
+
+ def testYearFirst(self):
+ dtstr = '090107'
+
+ # Should be MMDDYY
+ self.assertEqual(parse(dtstr, yearfirst=True),
+ datetime(2009, 1, 7))
+
+ self.assertEqual(parse(dtstr, yearfirst=True, dayfirst=False),
+ datetime(2009, 1, 7))
+
+ def testDayFirst(self):
+ dtstr = '090107'
+
+ # Should be DDMMYY
+ self.assertEqual(parse(dtstr, dayfirst=True),
+ datetime(2007, 1, 9))
+
+ self.assertEqual(parse(dtstr, yearfirst=False, dayfirst=True),
+ datetime(2007, 1, 9))
+
+ def testDayFirstYearFirst(self):
+ dtstr = '090107'
+ # Should be YYDDMM
+ self.assertEqual(parse(dtstr, yearfirst=True, dayfirst=True),
+ datetime(2009, 7, 1))
+
+ def testUnambiguousYearFirst(self):
+ dtstr = '2015 09 25'
+ self.assertEqual(parse(dtstr, yearfirst=True),
+ datetime(2015, 9, 25))
+
+ def testUnambiguousDayFirst(self):
+ dtstr = '2015 09 25'
+ self.assertEqual(parse(dtstr, dayfirst=True),
+ datetime(2015, 9, 25))
+
+ def testUnambiguousDayFirstYearFirst(self):
+ dtstr = '2015 09 25'
+ self.assertEqual(parse(dtstr, dayfirst=True, yearfirst=True),
+ datetime(2015, 9, 25))
+
diff --git a/libs/dateutil/test/test_relativedelta.py b/libs/dateutil/test/test_relativedelta.py
new file mode 100644
index 000000000..9e1ca7c1a
--- /dev/null
+++ b/libs/dateutil/test/test_relativedelta.py
@@ -0,0 +1,571 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+from ._common import unittest, WarningTestMixin, NotAValue
+
+import calendar
+from datetime import datetime, date, timedelta
+
+from dateutil.relativedelta import *
+
+
+class RelativeDeltaTest(WarningTestMixin, unittest.TestCase):
+ now = datetime(2003, 9, 17, 20, 54, 47, 282310)
+ today = date(2003, 9, 17)
+
+ def testInheritance(self):
+ # Ensure that relativedelta is inheritance-friendly.
+ class rdChildClass(relativedelta):
+ pass
+
+ ccRD = rdChildClass(years=1, months=1, days=1, leapdays=1, weeks=1,
+ hours=1, minutes=1, seconds=1, microseconds=1)
+
+ rd = relativedelta(years=1, months=1, days=1, leapdays=1, weeks=1,
+ hours=1, minutes=1, seconds=1, microseconds=1)
+
+ self.assertEqual(type(ccRD + rd), type(ccRD),
+ msg='Addition does not inherit type.')
+
+ self.assertEqual(type(ccRD - rd), type(ccRD),
+ msg='Subtraction does not inherit type.')
+
+ self.assertEqual(type(-ccRD), type(ccRD),
+ msg='Negation does not inherit type.')
+
+ self.assertEqual(type(ccRD * 5.0), type(ccRD),
+ msg='Multiplication does not inherit type.')
+
+ self.assertEqual(type(ccRD / 5.0), type(ccRD),
+ msg='Division does not inherit type.')
+
+ def testMonthEndMonthBeginning(self):
+ self.assertEqual(relativedelta(datetime(2003, 1, 31, 23, 59, 59),
+ datetime(2003, 3, 1, 0, 0, 0)),
+ relativedelta(months=-1, seconds=-1))
+
+ self.assertEqual(relativedelta(datetime(2003, 3, 1, 0, 0, 0),
+ datetime(2003, 1, 31, 23, 59, 59)),
+ relativedelta(months=1, seconds=1))
+
+ def testMonthEndMonthBeginningLeapYear(self):
+ self.assertEqual(relativedelta(datetime(2012, 1, 31, 23, 59, 59),
+ datetime(2012, 3, 1, 0, 0, 0)),
+ relativedelta(months=-1, seconds=-1))
+
+ self.assertEqual(relativedelta(datetime(2003, 3, 1, 0, 0, 0),
+ datetime(2003, 1, 31, 23, 59, 59)),
+ relativedelta(months=1, seconds=1))
+
+ def testNextMonth(self):
+ self.assertEqual(self.now+relativedelta(months=+1),
+ datetime(2003, 10, 17, 20, 54, 47, 282310))
+
+ def testNextMonthPlusOneWeek(self):
+ self.assertEqual(self.now+relativedelta(months=+1, weeks=+1),
+ datetime(2003, 10, 24, 20, 54, 47, 282310))
+
+ def testNextMonthPlusOneWeek10am(self):
+ self.assertEqual(self.today +
+ relativedelta(months=+1, weeks=+1, hour=10),
+ datetime(2003, 10, 24, 10, 0))
+
+ def testNextMonthPlusOneWeek10amDiff(self):
+ self.assertEqual(relativedelta(datetime(2003, 10, 24, 10, 0),
+ self.today),
+ relativedelta(months=+1, days=+7, hours=+10))
+
+ def testOneMonthBeforeOneYear(self):
+ self.assertEqual(self.now+relativedelta(years=+1, months=-1),
+ datetime(2004, 8, 17, 20, 54, 47, 282310))
+
+ def testMonthsOfDiffNumOfDays(self):
+ self.assertEqual(date(2003, 1, 27)+relativedelta(months=+1),
+ date(2003, 2, 27))
+ self.assertEqual(date(2003, 1, 31)+relativedelta(months=+1),
+ date(2003, 2, 28))
+ self.assertEqual(date(2003, 1, 31)+relativedelta(months=+2),
+ date(2003, 3, 31))
+
+ def testMonthsOfDiffNumOfDaysWithYears(self):
+ self.assertEqual(date(2000, 2, 28)+relativedelta(years=+1),
+ date(2001, 2, 28))
+ self.assertEqual(date(2000, 2, 29)+relativedelta(years=+1),
+ date(2001, 2, 28))
+
+ self.assertEqual(date(1999, 2, 28)+relativedelta(years=+1),
+ date(2000, 2, 28))
+ self.assertEqual(date(1999, 3, 1)+relativedelta(years=+1),
+ date(2000, 3, 1))
+ self.assertEqual(date(1999, 3, 1)+relativedelta(years=+1),
+ date(2000, 3, 1))
+
+ self.assertEqual(date(2001, 2, 28)+relativedelta(years=-1),
+ date(2000, 2, 28))
+ self.assertEqual(date(2001, 3, 1)+relativedelta(years=-1),
+ date(2000, 3, 1))
+
+ def testNextFriday(self):
+ self.assertEqual(self.today+relativedelta(weekday=FR),
+ date(2003, 9, 19))
+
+ def testNextFridayInt(self):
+ self.assertEqual(self.today+relativedelta(weekday=calendar.FRIDAY),
+ date(2003, 9, 19))
+
+ def testLastFridayInThisMonth(self):
+ self.assertEqual(self.today+relativedelta(day=31, weekday=FR(-1)),
+ date(2003, 9, 26))
+
+ def testNextWednesdayIsToday(self):
+ self.assertEqual(self.today+relativedelta(weekday=WE),
+ date(2003, 9, 17))
+
+ def testNextWenesdayNotToday(self):
+ self.assertEqual(self.today+relativedelta(days=+1, weekday=WE),
+ date(2003, 9, 24))
+
+ def test15thISOYearWeek(self):
+ self.assertEqual(date(2003, 1, 1) +
+ relativedelta(day=4, weeks=+14, weekday=MO(-1)),
+ date(2003, 4, 7))
+
+ def testMillenniumAge(self):
+ self.assertEqual(relativedelta(self.now, date(2001, 1, 1)),
+ relativedelta(years=+2, months=+8, days=+16,
+ hours=+20, minutes=+54, seconds=+47,
+ microseconds=+282310))
+
+ def testJohnAge(self):
+ self.assertEqual(relativedelta(self.now,
+ datetime(1978, 4, 5, 12, 0)),
+ relativedelta(years=+25, months=+5, days=+12,
+ hours=+8, minutes=+54, seconds=+47,
+ microseconds=+282310))
+
+ def testJohnAgeWithDate(self):
+ self.assertEqual(relativedelta(self.today,
+ datetime(1978, 4, 5, 12, 0)),
+ relativedelta(years=+25, months=+5, days=+11,
+ hours=+12))
+
+ def testYearDay(self):
+ self.assertEqual(date(2003, 1, 1)+relativedelta(yearday=260),
+ date(2003, 9, 17))
+ self.assertEqual(date(2002, 1, 1)+relativedelta(yearday=260),
+ date(2002, 9, 17))
+ self.assertEqual(date(2000, 1, 1)+relativedelta(yearday=260),
+ date(2000, 9, 16))
+ self.assertEqual(self.today+relativedelta(yearday=261),
+ date(2003, 9, 18))
+
+ def testYearDayBug(self):
+ # Tests a problem reported by Adam Ryan.
+ self.assertEqual(date(2010, 1, 1)+relativedelta(yearday=15),
+ date(2010, 1, 15))
+
+ def testNonLeapYearDay(self):
+ self.assertEqual(date(2003, 1, 1)+relativedelta(nlyearday=260),
+ date(2003, 9, 17))
+ self.assertEqual(date(2002, 1, 1)+relativedelta(nlyearday=260),
+ date(2002, 9, 17))
+ self.assertEqual(date(2000, 1, 1)+relativedelta(nlyearday=260),
+ date(2000, 9, 17))
+ self.assertEqual(self.today+relativedelta(yearday=261),
+ date(2003, 9, 18))
+
+ def testAddition(self):
+ self.assertEqual(relativedelta(days=10) +
+ relativedelta(years=1, months=2, days=3, hours=4,
+ minutes=5, microseconds=6),
+ relativedelta(years=1, months=2, days=13, hours=4,
+ minutes=5, microseconds=6))
+
+ def testAdditionToDatetime(self):
+ self.assertEqual(datetime(2000, 1, 1) + relativedelta(days=1),
+ datetime(2000, 1, 2))
+
+ def testRightAdditionToDatetime(self):
+ self.assertEqual(relativedelta(days=1) + datetime(2000, 1, 1),
+ datetime(2000, 1, 2))
+
+ def testAdditionInvalidType(self):
+ with self.assertRaises(TypeError):
+ relativedelta(days=3) + 9
+
+ def testAdditionUnsupportedType(self):
+ # For unsupported types that define their own comparators, etc.
+ self.assertIs(relativedelta(days=1) + NotAValue, NotAValue)
+
+ def testSubtraction(self):
+ self.assertEqual(relativedelta(days=10) -
+ relativedelta(years=1, months=2, days=3, hours=4,
+ minutes=5, microseconds=6),
+ relativedelta(years=-1, months=-2, days=7, hours=-4,
+ minutes=-5, microseconds=-6))
+
+ def testRightSubtractionFromDatetime(self):
+ self.assertEqual(datetime(2000, 1, 2) - relativedelta(days=1),
+ datetime(2000, 1, 1))
+
+ def testSubractionWithDatetime(self):
+ self.assertRaises(TypeError, lambda x, y: x - y,
+ (relativedelta(days=1), datetime(2000, 1, 1)))
+
+ def testSubtractionInvalidType(self):
+ with self.assertRaises(TypeError):
+ relativedelta(hours=12) - 14
+
+ def testSubtractionUnsupportedType(self):
+ self.assertIs(relativedelta(days=1) + NotAValue, NotAValue)
+
+ def testMultiplication(self):
+ self.assertEqual(datetime(2000, 1, 1) + relativedelta(days=1) * 28,
+ datetime(2000, 1, 29))
+ self.assertEqual(datetime(2000, 1, 1) + 28 * relativedelta(days=1),
+ datetime(2000, 1, 29))
+
+ def testMultiplicationUnsupportedType(self):
+ self.assertIs(relativedelta(days=1) * NotAValue, NotAValue)
+
+ def testDivision(self):
+ self.assertEqual(datetime(2000, 1, 1) + relativedelta(days=28) / 28,
+ datetime(2000, 1, 2))
+
+ def testDivisionUnsupportedType(self):
+ self.assertIs(relativedelta(days=1) / NotAValue, NotAValue)
+
+ def testBoolean(self):
+ self.assertFalse(relativedelta(days=0))
+ self.assertTrue(relativedelta(days=1))
+
+ def testComparison(self):
+ d1 = relativedelta(years=1, months=1, days=1, leapdays=0, hours=1,
+ minutes=1, seconds=1, microseconds=1)
+ d2 = relativedelta(years=1, months=1, days=1, leapdays=0, hours=1,
+ minutes=1, seconds=1, microseconds=1)
+ d3 = relativedelta(years=1, months=1, days=1, leapdays=0, hours=1,
+ minutes=1, seconds=1, microseconds=2)
+
+ self.assertEqual(d1, d2)
+ self.assertNotEqual(d1, d3)
+
+ def testInequalityTypeMismatch(self):
+ # Different type
+ self.assertFalse(relativedelta(year=1) == 19)
+
+ def testInequalityUnsupportedType(self):
+ self.assertIs(relativedelta(hours=3) == NotAValue, NotAValue)
+
+ def testInequalityWeekdays(self):
+ # Different weekdays
+ no_wday = relativedelta(year=1997, month=4)
+ wday_mo_1 = relativedelta(year=1997, month=4, weekday=MO(+1))
+ wday_mo_2 = relativedelta(year=1997, month=4, weekday=MO(+2))
+ wday_tu = relativedelta(year=1997, month=4, weekday=TU)
+
+ self.assertTrue(wday_mo_1 == wday_mo_1)
+
+ self.assertFalse(no_wday == wday_mo_1)
+ self.assertFalse(wday_mo_1 == no_wday)
+
+ self.assertFalse(wday_mo_1 == wday_mo_2)
+ self.assertFalse(wday_mo_2 == wday_mo_1)
+
+ self.assertFalse(wday_mo_1 == wday_tu)
+ self.assertFalse(wday_tu == wday_mo_1)
+
+ def testMonthOverflow(self):
+ self.assertEqual(relativedelta(months=273),
+ relativedelta(years=22, months=9))
+
+ def testWeeks(self):
+ # Test that the weeks property is working properly.
+ rd = relativedelta(years=4, months=2, weeks=8, days=6)
+ self.assertEqual((rd.weeks, rd.days), (8, 8 * 7 + 6))
+
+ rd.weeks = 3
+ self.assertEqual((rd.weeks, rd.days), (3, 3 * 7 + 6))
+
+ def testRelativeDeltaRepr(self):
+ self.assertEqual(repr(relativedelta(years=1, months=-1, days=15)),
+ 'relativedelta(years=+1, months=-1, days=+15)')
+
+ self.assertEqual(repr(relativedelta(months=14, seconds=-25)),
+ 'relativedelta(years=+1, months=+2, seconds=-25)')
+
+ self.assertEqual(repr(relativedelta(month=3, hour=3, weekday=SU(3))),
+ 'relativedelta(month=3, weekday=SU(+3), hour=3)')
+
+ def testRelativeDeltaFractionalYear(self):
+ with self.assertRaises(ValueError):
+ relativedelta(years=1.5)
+
+ def testRelativeDeltaFractionalMonth(self):
+ with self.assertRaises(ValueError):
+ relativedelta(months=1.5)
+
+ def testRelativeDeltaFractionalAbsolutes(self):
+ # Fractional absolute values will soon be unsupported,
+ # check for the deprecation warning.
+ with self.assertWarns(DeprecationWarning):
+ relativedelta(year=2.86)
+
+ with self.assertWarns(DeprecationWarning):
+ relativedelta(month=1.29)
+
+ with self.assertWarns(DeprecationWarning):
+ relativedelta(day=0.44)
+
+ with self.assertWarns(DeprecationWarning):
+ relativedelta(hour=23.98)
+
+ with self.assertWarns(DeprecationWarning):
+ relativedelta(minute=45.21)
+
+ with self.assertWarns(DeprecationWarning):
+ relativedelta(second=13.2)
+
+ with self.assertWarns(DeprecationWarning):
+ relativedelta(microsecond=157221.93)
+
+ def testRelativeDeltaFractionalRepr(self):
+ rd = relativedelta(years=3, months=-2, days=1.25)
+
+ self.assertEqual(repr(rd),
+ 'relativedelta(years=+3, months=-2, days=+1.25)')
+
+ rd = relativedelta(hours=0.5, seconds=9.22)
+ self.assertEqual(repr(rd),
+ 'relativedelta(hours=+0.5, seconds=+9.22)')
+
+ def testRelativeDeltaFractionalWeeks(self):
+ # Equivalent to days=8, hours=18
+ rd = relativedelta(weeks=1.25)
+ d1 = datetime(2009, 9, 3, 0, 0)
+ self.assertEqual(d1 + rd,
+ datetime(2009, 9, 11, 18))
+
+ def testRelativeDeltaFractionalDays(self):
+ rd1 = relativedelta(days=1.48)
+
+ d1 = datetime(2009, 9, 3, 0, 0)
+ self.assertEqual(d1 + rd1,
+ datetime(2009, 9, 4, 11, 31, 12))
+
+ rd2 = relativedelta(days=1.5)
+ self.assertEqual(d1 + rd2,
+ datetime(2009, 9, 4, 12, 0, 0))
+
+ def testRelativeDeltaFractionalHours(self):
+ rd = relativedelta(days=1, hours=12.5)
+ d1 = datetime(2009, 9, 3, 0, 0)
+ self.assertEqual(d1 + rd,
+ datetime(2009, 9, 4, 12, 30, 0))
+
+ def testRelativeDeltaFractionalMinutes(self):
+ rd = relativedelta(hours=1, minutes=30.5)
+ d1 = datetime(2009, 9, 3, 0, 0)
+ self.assertEqual(d1 + rd,
+ datetime(2009, 9, 3, 1, 30, 30))
+
+ def testRelativeDeltaFractionalSeconds(self):
+ rd = relativedelta(hours=5, minutes=30, seconds=30.5)
+ d1 = datetime(2009, 9, 3, 0, 0)
+ self.assertEqual(d1 + rd,
+ datetime(2009, 9, 3, 5, 30, 30, 500000))
+
+ def testRelativeDeltaFractionalPositiveOverflow(self):
+ # Equivalent to (days=1, hours=14)
+ rd1 = relativedelta(days=1.5, hours=2)
+ d1 = datetime(2009, 9, 3, 0, 0)
+ self.assertEqual(d1 + rd1,
+ datetime(2009, 9, 4, 14, 0, 0))
+
+ # Equivalent to (days=1, hours=14, minutes=45)
+ rd2 = relativedelta(days=1.5, hours=2.5, minutes=15)
+ d1 = datetime(2009, 9, 3, 0, 0)
+ self.assertEqual(d1 + rd2,
+ datetime(2009, 9, 4, 14, 45))
+
+ # Carry back up - equivalent to (days=2, hours=2, minutes=0, seconds=1)
+ rd3 = relativedelta(days=1.5, hours=13, minutes=59.5, seconds=31)
+ self.assertEqual(d1 + rd3,
+ datetime(2009, 9, 5, 2, 0, 1))
+
+ def testRelativeDeltaFractionalNegativeDays(self):
+ # Equivalent to (days=-1, hours=-1)
+ rd1 = relativedelta(days=-1.5, hours=11)
+ d1 = datetime(2009, 9, 3, 12, 0)
+ self.assertEqual(d1 + rd1,
+ datetime(2009, 9, 2, 11, 0, 0))
+
+ # Equivalent to (days=-1, hours=-9)
+ rd2 = relativedelta(days=-1.25, hours=-3)
+ self.assertEqual(d1 + rd2,
+ datetime(2009, 9, 2, 3))
+
+ def testRelativeDeltaNormalizeFractionalDays(self):
+ # Equivalent to (days=2, hours=18)
+ rd1 = relativedelta(days=2.75)
+
+ self.assertEqual(rd1.normalized(), relativedelta(days=2, hours=18))
+
+ # Equvalent to (days=1, hours=11, minutes=31, seconds=12)
+ rd2 = relativedelta(days=1.48)
+
+ self.assertEqual(rd2.normalized(),
+ relativedelta(days=1, hours=11, minutes=31, seconds=12))
+
+ def testRelativeDeltaNormalizeFractionalDays(self):
+ # Equivalent to (hours=1, minutes=30)
+ rd1 = relativedelta(hours=1.5)
+
+ self.assertEqual(rd1.normalized(), relativedelta(hours=1, minutes=30))
+
+ # Equivalent to (hours=3, minutes=17, seconds=5, microseconds=100)
+ rd2 = relativedelta(hours=3.28472225)
+
+ self.assertEqual(rd2.normalized(),
+ relativedelta(hours=3, minutes=17, seconds=5, microseconds=100))
+
+ def testRelativeDeltaNormalizeFractionalMinutes(self):
+ # Equivalent to (minutes=15, seconds=36)
+ rd1 = relativedelta(minutes=15.6)
+
+ self.assertEqual(rd1.normalized(),
+ relativedelta(minutes=15, seconds=36))
+
+ # Equivalent to (minutes=25, seconds=20, microseconds=25000)
+ rd2 = relativedelta(minutes=25.33375)
+
+ self.assertEqual(rd2.normalized(),
+ relativedelta(minutes=25, seconds=20, microseconds=25000))
+
+ def testRelativeDeltaNormalizeFractionalSeconds(self):
+ # Equivalent to (seconds=45, microseconds=25000)
+ rd1 = relativedelta(seconds=45.025)
+ self.assertEqual(rd1.normalized(),
+ relativedelta(seconds=45, microseconds=25000))
+
+ def testRelativeDeltaFractionalPositiveOverflow(self):
+ # Equivalent to (days=1, hours=14)
+ rd1 = relativedelta(days=1.5, hours=2)
+ self.assertEqual(rd1.normalized(),
+ relativedelta(days=1, hours=14))
+
+ # Equivalent to (days=1, hours=14, minutes=45)
+ rd2 = relativedelta(days=1.5, hours=2.5, minutes=15)
+ self.assertEqual(rd2.normalized(),
+ relativedelta(days=1, hours=14, minutes=45))
+
+ # Carry back up - equivalent to:
+ # (days=2, hours=2, minutes=0, seconds=2, microseconds=3)
+ rd3 = relativedelta(days=1.5, hours=13, minutes=59.50045,
+ seconds=31.473, microseconds=500003)
+ self.assertEqual(rd3.normalized(),
+ relativedelta(days=2, hours=2, minutes=0,
+ seconds=2, microseconds=3))
+
+ def testRelativeDeltaFractionalNegativeOverflow(self):
+ # Equivalent to (days=-1)
+ rd1 = relativedelta(days=-0.5, hours=-12)
+ self.assertEqual(rd1.normalized(),
+ relativedelta(days=-1))
+
+ # Equivalent to (days=-1)
+ rd2 = relativedelta(days=-1.5, hours=12)
+ self.assertEqual(rd2.normalized(),
+ relativedelta(days=-1))
+
+ # Equivalent to (days=-1, hours=-14, minutes=-45)
+ rd3 = relativedelta(days=-1.5, hours=-2.5, minutes=-15)
+ self.assertEqual(rd3.normalized(),
+ relativedelta(days=-1, hours=-14, minutes=-45))
+
+ # Equivalent to (days=-1, hours=-14, minutes=+15)
+ rd4 = relativedelta(days=-1.5, hours=-2.5, minutes=45)
+ self.assertEqual(rd4.normalized(),
+ relativedelta(days=-1, hours=-14, minutes=+15))
+
+ # Carry back up - equivalent to:
+ # (days=-2, hours=-2, minutes=0, seconds=-2, microseconds=-3)
+ rd3 = relativedelta(days=-1.5, hours=-13, minutes=-59.50045,
+ seconds=-31.473, microseconds=-500003)
+ self.assertEqual(rd3.normalized(),
+ relativedelta(days=-2, hours=-2, minutes=0,
+ seconds=-2, microseconds=-3))
+
+ def testInvalidYearDay(self):
+ with self.assertRaises(ValueError):
+ relativedelta(yearday=367)
+
+ def testAddTimedeltaToUnpopulatedRelativedelta(self):
+ td = timedelta(
+ days=1,
+ seconds=1,
+ microseconds=1,
+ milliseconds=1,
+ minutes=1,
+ hours=1,
+ weeks=1
+ )
+
+ expected = relativedelta(
+ weeks=1,
+ days=1,
+ hours=1,
+ minutes=1,
+ seconds=1,
+ microseconds=1001
+ )
+
+ self.assertEqual(expected, relativedelta() + td)
+
+ def testAddTimedeltaToPopulatedRelativeDelta(self):
+ td = timedelta(
+ days=1,
+ seconds=1,
+ microseconds=1,
+ milliseconds=1,
+ minutes=1,
+ hours=1,
+ weeks=1
+ )
+
+ rd = relativedelta(
+ year=1,
+ month=1,
+ day=1,
+ hour=1,
+ minute=1,
+ second=1,
+ microsecond=1,
+ years=1,
+ months=1,
+ days=1,
+ weeks=1,
+ hours=1,
+ minutes=1,
+ seconds=1,
+ microseconds=1
+ )
+
+ expected = relativedelta(
+ year=1,
+ month=1,
+ day=1,
+ hour=1,
+ minute=1,
+ second=1,
+ microsecond=1,
+ years=1,
+ months=1,
+ weeks=2,
+ days=2,
+ hours=2,
+ minutes=2,
+ seconds=2,
+ microseconds=1002,
+ )
+
+ self.assertEqual(expected, rd + td)
diff --git a/libs/dateutil/test/test_rrule.py b/libs/dateutil/test/test_rrule.py
new file mode 100644
index 000000000..2a1e6e8b4
--- /dev/null
+++ b/libs/dateutil/test/test_rrule.py
@@ -0,0 +1,4701 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+from ._common import WarningTestMixin, unittest
+
+import calendar
+from datetime import datetime, date
+from six import PY3
+
+from dateutil.rrule import *
+
+
+class RRuleTest(WarningTestMixin, unittest.TestCase):
+ def _rrulestr_reverse_test(self, rule):
+ """
+ Call with an `rrule` and it will test that `str(rrule)` generates a
+ string which generates the same `rrule` as the input when passed to
+ `rrulestr()`
+ """
+ rr_str = str(rule)
+ rrulestr_rrule = rrulestr(rr_str)
+
+ self.assertEqual(list(rule), list(rrulestr_rrule))
+
+ def testYearly(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1998, 9, 2, 9, 0),
+ datetime(1999, 9, 2, 9, 0)])
+
+ def testYearlyInterval(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ interval=2,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1999, 9, 2, 9, 0),
+ datetime(2001, 9, 2, 9, 0)])
+
+ def testYearlyIntervalLarge(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ interval=100,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(2097, 9, 2, 9, 0),
+ datetime(2197, 9, 2, 9, 0)])
+
+ def testYearlyByMonth(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ bymonth=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 2, 9, 0),
+ datetime(1998, 3, 2, 9, 0),
+ datetime(1999, 1, 2, 9, 0)])
+
+ def testYearlyByMonthDay(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ bymonthday=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 3, 9, 0),
+ datetime(1997, 10, 1, 9, 0),
+ datetime(1997, 10, 3, 9, 0)])
+
+ def testYearlyByMonthAndMonthDay(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(5, 7),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 5, 9, 0),
+ datetime(1998, 1, 7, 9, 0),
+ datetime(1998, 3, 5, 9, 0)])
+
+ def testYearlyByWeekDay(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 9, 9, 0)])
+
+ def testYearlyByNWeekDay(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 25, 9, 0),
+ datetime(1998, 1, 6, 9, 0),
+ datetime(1998, 12, 31, 9, 0)])
+
+ def testYearlyByNWeekDayLarge(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ byweekday=(TU(3), TH(-3)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 11, 9, 0),
+ datetime(1998, 1, 20, 9, 0),
+ datetime(1998, 12, 17, 9, 0)])
+
+ def testYearlyByMonthAndWeekDay(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 1, 6, 9, 0),
+ datetime(1998, 1, 8, 9, 0)])
+
+ def testYearlyByMonthAndNWeekDay(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 6, 9, 0),
+ datetime(1998, 1, 29, 9, 0),
+ datetime(1998, 3, 3, 9, 0)])
+
+ def testYearlyByMonthAndNWeekDayLarge(self):
+ # This is interesting because the TH(-3) ends up before
+ # the TU(3).
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(3), TH(-3)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 15, 9, 0),
+ datetime(1998, 1, 20, 9, 0),
+ datetime(1998, 3, 12, 9, 0)])
+
+ def testYearlyByMonthDayAndWeekDay(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 2, 3, 9, 0),
+ datetime(1998, 3, 3, 9, 0)])
+
+ def testYearlyByMonthAndMonthDayAndWeekDay(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 3, 3, 9, 0),
+ datetime(2001, 3, 1, 9, 0)])
+
+ def testYearlyByYearDay(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=4,
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 31, 9, 0),
+ datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 4, 10, 9, 0),
+ datetime(1998, 7, 19, 9, 0)])
+
+ def testYearlyByYearDayNeg(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=4,
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 31, 9, 0),
+ datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 4, 10, 9, 0),
+ datetime(1998, 7, 19, 9, 0)])
+
+ def testYearlyByMonthAndYearDay(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 10, 9, 0),
+ datetime(1998, 7, 19, 9, 0),
+ datetime(1999, 4, 10, 9, 0),
+ datetime(1999, 7, 19, 9, 0)])
+
+ def testYearlyByMonthAndYearDayNeg(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 10, 9, 0),
+ datetime(1998, 7, 19, 9, 0),
+ datetime(1999, 4, 10, 9, 0),
+ datetime(1999, 7, 19, 9, 0)])
+
+ def testYearlyByWeekNo(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ byweekno=20,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 5, 11, 9, 0),
+ datetime(1998, 5, 12, 9, 0),
+ datetime(1998, 5, 13, 9, 0)])
+
+ def testYearlyByWeekNoAndWeekDay(self):
+ # That's a nice one. The first days of week number one
+ # may be in the last year.
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ byweekno=1,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 29, 9, 0),
+ datetime(1999, 1, 4, 9, 0),
+ datetime(2000, 1, 3, 9, 0)])
+
+ def testYearlyByWeekNoAndWeekDayLarge(self):
+ # Another nice test. The last days of week number 52/53
+ # may be in the next year.
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ byweekno=52,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 28, 9, 0),
+ datetime(1998, 12, 27, 9, 0),
+ datetime(2000, 1, 2, 9, 0)])
+
+ def testYearlyByWeekNoAndWeekDayLast(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ byweekno=-1,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 28, 9, 0),
+ datetime(1999, 1, 3, 9, 0),
+ datetime(2000, 1, 2, 9, 0)])
+
+ def testYearlyByEaster(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ byeaster=0,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 12, 9, 0),
+ datetime(1999, 4, 4, 9, 0),
+ datetime(2000, 4, 23, 9, 0)])
+
+ def testYearlyByEasterPos(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ byeaster=1,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 13, 9, 0),
+ datetime(1999, 4, 5, 9, 0),
+ datetime(2000, 4, 24, 9, 0)])
+
+ def testYearlyByEasterNeg(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ byeaster=-1,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 11, 9, 0),
+ datetime(1999, 4, 3, 9, 0),
+ datetime(2000, 4, 22, 9, 0)])
+
+ def testYearlyByWeekNoAndWeekDay53(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ byweekno=53,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 12, 28, 9, 0),
+ datetime(2004, 12, 27, 9, 0),
+ datetime(2009, 12, 28, 9, 0)])
+
+ def testYearlyByHour(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ byhour=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 0),
+ datetime(1998, 9, 2, 6, 0),
+ datetime(1998, 9, 2, 18, 0)])
+
+ def testYearlyByMinute(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 6),
+ datetime(1997, 9, 2, 9, 18),
+ datetime(1998, 9, 2, 9, 6)])
+
+ def testYearlyBySecond(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0, 6),
+ datetime(1997, 9, 2, 9, 0, 18),
+ datetime(1998, 9, 2, 9, 0, 6)])
+
+ def testYearlyByHourAndMinute(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 6),
+ datetime(1997, 9, 2, 18, 18),
+ datetime(1998, 9, 2, 6, 6)])
+
+ def testYearlyByHourAndSecond(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ byhour=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 0, 6),
+ datetime(1997, 9, 2, 18, 0, 18),
+ datetime(1998, 9, 2, 6, 0, 6)])
+
+ def testYearlyByMinuteAndSecond(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 6, 6),
+ datetime(1997, 9, 2, 9, 6, 18),
+ datetime(1997, 9, 2, 9, 18, 6)])
+
+ def testYearlyByHourAndMinuteAndSecond(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 6, 6),
+ datetime(1997, 9, 2, 18, 6, 18),
+ datetime(1997, 9, 2, 18, 18, 6)])
+
+ def testYearlyBySetPos(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ bymonthday=15,
+ byhour=(6, 18),
+ bysetpos=(3, -3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 11, 15, 18, 0),
+ datetime(1998, 2, 15, 6, 0),
+ datetime(1998, 11, 15, 18, 0)])
+
+ def testMonthly(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 10, 2, 9, 0),
+ datetime(1997, 11, 2, 9, 0)])
+
+ def testMonthlyInterval(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ interval=2,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 11, 2, 9, 0),
+ datetime(1998, 1, 2, 9, 0)])
+
+ def testMonthlyIntervalLarge(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ interval=18,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1999, 3, 2, 9, 0),
+ datetime(2000, 9, 2, 9, 0)])
+
+ def testMonthlyByMonth(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ bymonth=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 2, 9, 0),
+ datetime(1998, 3, 2, 9, 0),
+ datetime(1999, 1, 2, 9, 0)])
+
+ def testMonthlyByMonthDay(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ bymonthday=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 3, 9, 0),
+ datetime(1997, 10, 1, 9, 0),
+ datetime(1997, 10, 3, 9, 0)])
+
+ def testMonthlyByMonthAndMonthDay(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(5, 7),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 5, 9, 0),
+ datetime(1998, 1, 7, 9, 0),
+ datetime(1998, 3, 5, 9, 0)])
+
+ def testMonthlyByWeekDay(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 9, 9, 0)])
+
+ # Third Monday of the month
+ self.assertEqual(rrule(MONTHLY,
+ byweekday=(MO(+3)),
+ dtstart=datetime(1997, 9, 1)).between(datetime(1997, 9, 1),
+ datetime(1997, 12, 1)),
+ [datetime(1997, 9, 15, 0, 0),
+ datetime(1997, 10, 20, 0, 0),
+ datetime(1997, 11, 17, 0, 0)])
+
+ def testMonthlyByNWeekDay(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 25, 9, 0),
+ datetime(1997, 10, 7, 9, 0)])
+
+ def testMonthlyByNWeekDayLarge(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ byweekday=(TU(3), TH(-3)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 11, 9, 0),
+ datetime(1997, 9, 16, 9, 0),
+ datetime(1997, 10, 16, 9, 0)])
+
+ def testMonthlyByMonthAndWeekDay(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 1, 6, 9, 0),
+ datetime(1998, 1, 8, 9, 0)])
+
+ def testMonthlyByMonthAndNWeekDay(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 6, 9, 0),
+ datetime(1998, 1, 29, 9, 0),
+ datetime(1998, 3, 3, 9, 0)])
+
+ def testMonthlyByMonthAndNWeekDayLarge(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(3), TH(-3)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 15, 9, 0),
+ datetime(1998, 1, 20, 9, 0),
+ datetime(1998, 3, 12, 9, 0)])
+
+ def testMonthlyByMonthDayAndWeekDay(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 2, 3, 9, 0),
+ datetime(1998, 3, 3, 9, 0)])
+
+ def testMonthlyByMonthAndMonthDayAndWeekDay(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 3, 3, 9, 0),
+ datetime(2001, 3, 1, 9, 0)])
+
+ def testMonthlyByYearDay(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=4,
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 31, 9, 0),
+ datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 4, 10, 9, 0),
+ datetime(1998, 7, 19, 9, 0)])
+
+ def testMonthlyByYearDayNeg(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=4,
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 31, 9, 0),
+ datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 4, 10, 9, 0),
+ datetime(1998, 7, 19, 9, 0)])
+
+ def testMonthlyByMonthAndYearDay(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 10, 9, 0),
+ datetime(1998, 7, 19, 9, 0),
+ datetime(1999, 4, 10, 9, 0),
+ datetime(1999, 7, 19, 9, 0)])
+
+ def testMonthlyByMonthAndYearDayNeg(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 10, 9, 0),
+ datetime(1998, 7, 19, 9, 0),
+ datetime(1999, 4, 10, 9, 0),
+ datetime(1999, 7, 19, 9, 0)])
+
+ def testMonthlyByWeekNo(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ byweekno=20,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 5, 11, 9, 0),
+ datetime(1998, 5, 12, 9, 0),
+ datetime(1998, 5, 13, 9, 0)])
+
+ def testMonthlyByWeekNoAndWeekDay(self):
+ # That's a nice one. The first days of week number one
+ # may be in the last year.
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ byweekno=1,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 29, 9, 0),
+ datetime(1999, 1, 4, 9, 0),
+ datetime(2000, 1, 3, 9, 0)])
+
+ def testMonthlyByWeekNoAndWeekDayLarge(self):
+ # Another nice test. The last days of week number 52/53
+ # may be in the next year.
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ byweekno=52,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 28, 9, 0),
+ datetime(1998, 12, 27, 9, 0),
+ datetime(2000, 1, 2, 9, 0)])
+
+ def testMonthlyByWeekNoAndWeekDayLast(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ byweekno=-1,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 28, 9, 0),
+ datetime(1999, 1, 3, 9, 0),
+ datetime(2000, 1, 2, 9, 0)])
+
+ def testMonthlyByWeekNoAndWeekDay53(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ byweekno=53,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 12, 28, 9, 0),
+ datetime(2004, 12, 27, 9, 0),
+ datetime(2009, 12, 28, 9, 0)])
+
+ def testMonthlyByEaster(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ byeaster=0,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 12, 9, 0),
+ datetime(1999, 4, 4, 9, 0),
+ datetime(2000, 4, 23, 9, 0)])
+
+ def testMonthlyByEasterPos(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ byeaster=1,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 13, 9, 0),
+ datetime(1999, 4, 5, 9, 0),
+ datetime(2000, 4, 24, 9, 0)])
+
+ def testMonthlyByEasterNeg(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ byeaster=-1,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 11, 9, 0),
+ datetime(1999, 4, 3, 9, 0),
+ datetime(2000, 4, 22, 9, 0)])
+
+ def testMonthlyByHour(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ byhour=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 0),
+ datetime(1997, 10, 2, 6, 0),
+ datetime(1997, 10, 2, 18, 0)])
+
+ def testMonthlyByMinute(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 6),
+ datetime(1997, 9, 2, 9, 18),
+ datetime(1997, 10, 2, 9, 6)])
+
+ def testMonthlyBySecond(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0, 6),
+ datetime(1997, 9, 2, 9, 0, 18),
+ datetime(1997, 10, 2, 9, 0, 6)])
+
+ def testMonthlyByHourAndMinute(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 6),
+ datetime(1997, 9, 2, 18, 18),
+ datetime(1997, 10, 2, 6, 6)])
+
+ def testMonthlyByHourAndSecond(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ byhour=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 0, 6),
+ datetime(1997, 9, 2, 18, 0, 18),
+ datetime(1997, 10, 2, 6, 0, 6)])
+
+ def testMonthlyByMinuteAndSecond(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 6, 6),
+ datetime(1997, 9, 2, 9, 6, 18),
+ datetime(1997, 9, 2, 9, 18, 6)])
+
+ def testMonthlyByHourAndMinuteAndSecond(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 6, 6),
+ datetime(1997, 9, 2, 18, 6, 18),
+ datetime(1997, 9, 2, 18, 18, 6)])
+
+ def testMonthlyBySetPos(self):
+ self.assertEqual(list(rrule(MONTHLY,
+ count=3,
+ bymonthday=(13, 17),
+ byhour=(6, 18),
+ bysetpos=(3, -3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 13, 18, 0),
+ datetime(1997, 9, 17, 6, 0),
+ datetime(1997, 10, 13, 18, 0)])
+
+ def testWeekly(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 9, 9, 0),
+ datetime(1997, 9, 16, 9, 0)])
+
+ def testWeeklyInterval(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ interval=2,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 16, 9, 0),
+ datetime(1997, 9, 30, 9, 0)])
+
+ def testWeeklyIntervalLarge(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ interval=20,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1998, 1, 20, 9, 0),
+ datetime(1998, 6, 9, 9, 0)])
+
+ def testWeeklyByMonth(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ bymonth=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 6, 9, 0),
+ datetime(1998, 1, 13, 9, 0),
+ datetime(1998, 1, 20, 9, 0)])
+
+ def testWeeklyByMonthDay(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ bymonthday=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 3, 9, 0),
+ datetime(1997, 10, 1, 9, 0),
+ datetime(1997, 10, 3, 9, 0)])
+
+ def testWeeklyByMonthAndMonthDay(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(5, 7),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 5, 9, 0),
+ datetime(1998, 1, 7, 9, 0),
+ datetime(1998, 3, 5, 9, 0)])
+
+ def testWeeklyByWeekDay(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 9, 9, 0)])
+
+ def testWeeklyByNWeekDay(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 9, 9, 0)])
+
+ def testWeeklyByMonthAndWeekDay(self):
+ # This test is interesting, because it crosses the year
+ # boundary in a weekly period to find day '1' as a
+ # valid recurrence.
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 1, 6, 9, 0),
+ datetime(1998, 1, 8, 9, 0)])
+
+ def testWeeklyByMonthAndNWeekDay(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 1, 6, 9, 0),
+ datetime(1998, 1, 8, 9, 0)])
+
+ def testWeeklyByMonthDayAndWeekDay(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 2, 3, 9, 0),
+ datetime(1998, 3, 3, 9, 0)])
+
+ def testWeeklyByMonthAndMonthDayAndWeekDay(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 3, 3, 9, 0),
+ datetime(2001, 3, 1, 9, 0)])
+
+ def testWeeklyByYearDay(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=4,
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 31, 9, 0),
+ datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 4, 10, 9, 0),
+ datetime(1998, 7, 19, 9, 0)])
+
+ def testWeeklyByYearDayNeg(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=4,
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 31, 9, 0),
+ datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 4, 10, 9, 0),
+ datetime(1998, 7, 19, 9, 0)])
+
+ def testWeeklyByMonthAndYearDay(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=4,
+ bymonth=(1, 7),
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 7, 19, 9, 0),
+ datetime(1999, 1, 1, 9, 0),
+ datetime(1999, 7, 19, 9, 0)])
+
+ def testWeeklyByMonthAndYearDayNeg(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=4,
+ bymonth=(1, 7),
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 7, 19, 9, 0),
+ datetime(1999, 1, 1, 9, 0),
+ datetime(1999, 7, 19, 9, 0)])
+
+ def testWeeklyByWeekNo(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ byweekno=20,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 5, 11, 9, 0),
+ datetime(1998, 5, 12, 9, 0),
+ datetime(1998, 5, 13, 9, 0)])
+
+ def testWeeklyByWeekNoAndWeekDay(self):
+ # That's a nice one. The first days of week number one
+ # may be in the last year.
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ byweekno=1,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 29, 9, 0),
+ datetime(1999, 1, 4, 9, 0),
+ datetime(2000, 1, 3, 9, 0)])
+
+ def testWeeklyByWeekNoAndWeekDayLarge(self):
+ # Another nice test. The last days of week number 52/53
+ # may be in the next year.
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ byweekno=52,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 28, 9, 0),
+ datetime(1998, 12, 27, 9, 0),
+ datetime(2000, 1, 2, 9, 0)])
+
+ def testWeeklyByWeekNoAndWeekDayLast(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ byweekno=-1,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 28, 9, 0),
+ datetime(1999, 1, 3, 9, 0),
+ datetime(2000, 1, 2, 9, 0)])
+
+ def testWeeklyByWeekNoAndWeekDay53(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ byweekno=53,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 12, 28, 9, 0),
+ datetime(2004, 12, 27, 9, 0),
+ datetime(2009, 12, 28, 9, 0)])
+
+ def testWeeklyByEaster(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ byeaster=0,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 12, 9, 0),
+ datetime(1999, 4, 4, 9, 0),
+ datetime(2000, 4, 23, 9, 0)])
+
+ def testWeeklyByEasterPos(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ byeaster=1,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 13, 9, 0),
+ datetime(1999, 4, 5, 9, 0),
+ datetime(2000, 4, 24, 9, 0)])
+
+ def testWeeklyByEasterNeg(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ byeaster=-1,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 11, 9, 0),
+ datetime(1999, 4, 3, 9, 0),
+ datetime(2000, 4, 22, 9, 0)])
+
+ def testWeeklyByHour(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ byhour=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 0),
+ datetime(1997, 9, 9, 6, 0),
+ datetime(1997, 9, 9, 18, 0)])
+
+ def testWeeklyByMinute(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 6),
+ datetime(1997, 9, 2, 9, 18),
+ datetime(1997, 9, 9, 9, 6)])
+
+ def testWeeklyBySecond(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0, 6),
+ datetime(1997, 9, 2, 9, 0, 18),
+ datetime(1997, 9, 9, 9, 0, 6)])
+
+ def testWeeklyByHourAndMinute(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 6),
+ datetime(1997, 9, 2, 18, 18),
+ datetime(1997, 9, 9, 6, 6)])
+
+ def testWeeklyByHourAndSecond(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ byhour=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 0, 6),
+ datetime(1997, 9, 2, 18, 0, 18),
+ datetime(1997, 9, 9, 6, 0, 6)])
+
+ def testWeeklyByMinuteAndSecond(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 6, 6),
+ datetime(1997, 9, 2, 9, 6, 18),
+ datetime(1997, 9, 2, 9, 18, 6)])
+
+ def testWeeklyByHourAndMinuteAndSecond(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 6, 6),
+ datetime(1997, 9, 2, 18, 6, 18),
+ datetime(1997, 9, 2, 18, 18, 6)])
+
+ def testWeeklyBySetPos(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ byweekday=(TU, TH),
+ byhour=(6, 18),
+ bysetpos=(3, -3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 0),
+ datetime(1997, 9, 4, 6, 0),
+ datetime(1997, 9, 9, 18, 0)])
+
+ def testDaily(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 3, 9, 0),
+ datetime(1997, 9, 4, 9, 0)])
+
+ def testDailyInterval(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ interval=2,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 6, 9, 0)])
+
+ def testDailyIntervalLarge(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ interval=92,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 12, 3, 9, 0),
+ datetime(1998, 3, 5, 9, 0)])
+
+ def testDailyByMonth(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ bymonth=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 1, 2, 9, 0),
+ datetime(1998, 1, 3, 9, 0)])
+
+ def testDailyByMonthDay(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ bymonthday=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 3, 9, 0),
+ datetime(1997, 10, 1, 9, 0),
+ datetime(1997, 10, 3, 9, 0)])
+
+ def testDailyByMonthAndMonthDay(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(5, 7),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 5, 9, 0),
+ datetime(1998, 1, 7, 9, 0),
+ datetime(1998, 3, 5, 9, 0)])
+
+ def testDailyByWeekDay(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 9, 9, 0)])
+
+ def testDailyByNWeekDay(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 9, 9, 0)])
+
+ def testDailyByMonthAndWeekDay(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 1, 6, 9, 0),
+ datetime(1998, 1, 8, 9, 0)])
+
+ def testDailyByMonthAndNWeekDay(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 1, 6, 9, 0),
+ datetime(1998, 1, 8, 9, 0)])
+
+ def testDailyByMonthDayAndWeekDay(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 2, 3, 9, 0),
+ datetime(1998, 3, 3, 9, 0)])
+
+ def testDailyByMonthAndMonthDayAndWeekDay(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 3, 3, 9, 0),
+ datetime(2001, 3, 1, 9, 0)])
+
+ def testDailyByYearDay(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=4,
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 31, 9, 0),
+ datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 4, 10, 9, 0),
+ datetime(1998, 7, 19, 9, 0)])
+
+ def testDailyByYearDayNeg(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=4,
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 31, 9, 0),
+ datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 4, 10, 9, 0),
+ datetime(1998, 7, 19, 9, 0)])
+
+ def testDailyByMonthAndYearDay(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=4,
+ bymonth=(1, 7),
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 7, 19, 9, 0),
+ datetime(1999, 1, 1, 9, 0),
+ datetime(1999, 7, 19, 9, 0)])
+
+ def testDailyByMonthAndYearDayNeg(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=4,
+ bymonth=(1, 7),
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 9, 0),
+ datetime(1998, 7, 19, 9, 0),
+ datetime(1999, 1, 1, 9, 0),
+ datetime(1999, 7, 19, 9, 0)])
+
+ def testDailyByWeekNo(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ byweekno=20,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 5, 11, 9, 0),
+ datetime(1998, 5, 12, 9, 0),
+ datetime(1998, 5, 13, 9, 0)])
+
+ def testDailyByWeekNoAndWeekDay(self):
+ # That's a nice one. The first days of week number one
+ # may be in the last year.
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ byweekno=1,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 29, 9, 0),
+ datetime(1999, 1, 4, 9, 0),
+ datetime(2000, 1, 3, 9, 0)])
+
+ def testDailyByWeekNoAndWeekDayLarge(self):
+ # Another nice test. The last days of week number 52/53
+ # may be in the next year.
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ byweekno=52,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 28, 9, 0),
+ datetime(1998, 12, 27, 9, 0),
+ datetime(2000, 1, 2, 9, 0)])
+
+ def testDailyByWeekNoAndWeekDayLast(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ byweekno=-1,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 28, 9, 0),
+ datetime(1999, 1, 3, 9, 0),
+ datetime(2000, 1, 2, 9, 0)])
+
+ def testDailyByWeekNoAndWeekDay53(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ byweekno=53,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 12, 28, 9, 0),
+ datetime(2004, 12, 27, 9, 0),
+ datetime(2009, 12, 28, 9, 0)])
+
+ def testDailyByEaster(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ byeaster=0,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 12, 9, 0),
+ datetime(1999, 4, 4, 9, 0),
+ datetime(2000, 4, 23, 9, 0)])
+
+ def testDailyByEasterPos(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ byeaster=1,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 13, 9, 0),
+ datetime(1999, 4, 5, 9, 0),
+ datetime(2000, 4, 24, 9, 0)])
+
+ def testDailyByEasterNeg(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ byeaster=-1,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 11, 9, 0),
+ datetime(1999, 4, 3, 9, 0),
+ datetime(2000, 4, 22, 9, 0)])
+
+ def testDailyByHour(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ byhour=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 0),
+ datetime(1997, 9, 3, 6, 0),
+ datetime(1997, 9, 3, 18, 0)])
+
+ def testDailyByMinute(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 6),
+ datetime(1997, 9, 2, 9, 18),
+ datetime(1997, 9, 3, 9, 6)])
+
+ def testDailyBySecond(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0, 6),
+ datetime(1997, 9, 2, 9, 0, 18),
+ datetime(1997, 9, 3, 9, 0, 6)])
+
+ def testDailyByHourAndMinute(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 6),
+ datetime(1997, 9, 2, 18, 18),
+ datetime(1997, 9, 3, 6, 6)])
+
+ def testDailyByHourAndSecond(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ byhour=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 0, 6),
+ datetime(1997, 9, 2, 18, 0, 18),
+ datetime(1997, 9, 3, 6, 0, 6)])
+
+ def testDailyByMinuteAndSecond(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 6, 6),
+ datetime(1997, 9, 2, 9, 6, 18),
+ datetime(1997, 9, 2, 9, 18, 6)])
+
+ def testDailyByHourAndMinuteAndSecond(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 6, 6),
+ datetime(1997, 9, 2, 18, 6, 18),
+ datetime(1997, 9, 2, 18, 18, 6)])
+
+ def testDailyBySetPos(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(15, 45),
+ bysetpos=(3, -3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 15),
+ datetime(1997, 9, 3, 6, 45),
+ datetime(1997, 9, 3, 18, 15)])
+
+ def testHourly(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 2, 10, 0),
+ datetime(1997, 9, 2, 11, 0)])
+
+ def testHourlyInterval(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ interval=2,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 2, 11, 0),
+ datetime(1997, 9, 2, 13, 0)])
+
+ def testHourlyIntervalLarge(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ interval=769,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 10, 4, 10, 0),
+ datetime(1997, 11, 5, 11, 0)])
+
+ def testHourlyByMonth(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ bymonth=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 0, 0),
+ datetime(1998, 1, 1, 1, 0),
+ datetime(1998, 1, 1, 2, 0)])
+
+ def testHourlyByMonthDay(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ bymonthday=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 3, 0, 0),
+ datetime(1997, 9, 3, 1, 0),
+ datetime(1997, 9, 3, 2, 0)])
+
+ def testHourlyByMonthAndMonthDay(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(5, 7),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 5, 0, 0),
+ datetime(1998, 1, 5, 1, 0),
+ datetime(1998, 1, 5, 2, 0)])
+
+ def testHourlyByWeekDay(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 2, 10, 0),
+ datetime(1997, 9, 2, 11, 0)])
+
+ def testHourlyByNWeekDay(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 2, 10, 0),
+ datetime(1997, 9, 2, 11, 0)])
+
+ def testHourlyByMonthAndWeekDay(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 0, 0),
+ datetime(1998, 1, 1, 1, 0),
+ datetime(1998, 1, 1, 2, 0)])
+
+ def testHourlyByMonthAndNWeekDay(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 0, 0),
+ datetime(1998, 1, 1, 1, 0),
+ datetime(1998, 1, 1, 2, 0)])
+
+ def testHourlyByMonthDayAndWeekDay(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 0, 0),
+ datetime(1998, 1, 1, 1, 0),
+ datetime(1998, 1, 1, 2, 0)])
+
+ def testHourlyByMonthAndMonthDayAndWeekDay(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 0, 0),
+ datetime(1998, 1, 1, 1, 0),
+ datetime(1998, 1, 1, 2, 0)])
+
+ def testHourlyByYearDay(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=4,
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 31, 0, 0),
+ datetime(1997, 12, 31, 1, 0),
+ datetime(1997, 12, 31, 2, 0),
+ datetime(1997, 12, 31, 3, 0)])
+
+ def testHourlyByYearDayNeg(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=4,
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 31, 0, 0),
+ datetime(1997, 12, 31, 1, 0),
+ datetime(1997, 12, 31, 2, 0),
+ datetime(1997, 12, 31, 3, 0)])
+
+ def testHourlyByMonthAndYearDay(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 10, 0, 0),
+ datetime(1998, 4, 10, 1, 0),
+ datetime(1998, 4, 10, 2, 0),
+ datetime(1998, 4, 10, 3, 0)])
+
+ def testHourlyByMonthAndYearDayNeg(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 10, 0, 0),
+ datetime(1998, 4, 10, 1, 0),
+ datetime(1998, 4, 10, 2, 0),
+ datetime(1998, 4, 10, 3, 0)])
+
+ def testHourlyByWeekNo(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ byweekno=20,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 5, 11, 0, 0),
+ datetime(1998, 5, 11, 1, 0),
+ datetime(1998, 5, 11, 2, 0)])
+
+ def testHourlyByWeekNoAndWeekDay(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ byweekno=1,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 29, 0, 0),
+ datetime(1997, 12, 29, 1, 0),
+ datetime(1997, 12, 29, 2, 0)])
+
+ def testHourlyByWeekNoAndWeekDayLarge(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ byweekno=52,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 28, 0, 0),
+ datetime(1997, 12, 28, 1, 0),
+ datetime(1997, 12, 28, 2, 0)])
+
+ def testHourlyByWeekNoAndWeekDayLast(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ byweekno=-1,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 28, 0, 0),
+ datetime(1997, 12, 28, 1, 0),
+ datetime(1997, 12, 28, 2, 0)])
+
+ def testHourlyByWeekNoAndWeekDay53(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ byweekno=53,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 12, 28, 0, 0),
+ datetime(1998, 12, 28, 1, 0),
+ datetime(1998, 12, 28, 2, 0)])
+
+ def testHourlyByEaster(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ byeaster=0,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 12, 0, 0),
+ datetime(1998, 4, 12, 1, 0),
+ datetime(1998, 4, 12, 2, 0)])
+
+ def testHourlyByEasterPos(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ byeaster=1,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 13, 0, 0),
+ datetime(1998, 4, 13, 1, 0),
+ datetime(1998, 4, 13, 2, 0)])
+
+ def testHourlyByEasterNeg(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ byeaster=-1,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 11, 0, 0),
+ datetime(1998, 4, 11, 1, 0),
+ datetime(1998, 4, 11, 2, 0)])
+
+ def testHourlyByHour(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ byhour=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 0),
+ datetime(1997, 9, 3, 6, 0),
+ datetime(1997, 9, 3, 18, 0)])
+
+ def testHourlyByMinute(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 6),
+ datetime(1997, 9, 2, 9, 18),
+ datetime(1997, 9, 2, 10, 6)])
+
+ def testHourlyBySecond(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0, 6),
+ datetime(1997, 9, 2, 9, 0, 18),
+ datetime(1997, 9, 2, 10, 0, 6)])
+
+ def testHourlyByHourAndMinute(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 6),
+ datetime(1997, 9, 2, 18, 18),
+ datetime(1997, 9, 3, 6, 6)])
+
+ def testHourlyByHourAndSecond(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ byhour=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 0, 6),
+ datetime(1997, 9, 2, 18, 0, 18),
+ datetime(1997, 9, 3, 6, 0, 6)])
+
+ def testHourlyByMinuteAndSecond(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 6, 6),
+ datetime(1997, 9, 2, 9, 6, 18),
+ datetime(1997, 9, 2, 9, 18, 6)])
+
+ def testHourlyByHourAndMinuteAndSecond(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 6, 6),
+ datetime(1997, 9, 2, 18, 6, 18),
+ datetime(1997, 9, 2, 18, 18, 6)])
+
+ def testHourlyBySetPos(self):
+ self.assertEqual(list(rrule(HOURLY,
+ count=3,
+ byminute=(15, 45),
+ bysecond=(15, 45),
+ bysetpos=(3, -3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 15, 45),
+ datetime(1997, 9, 2, 9, 45, 15),
+ datetime(1997, 9, 2, 10, 15, 45)])
+
+ def testMinutely(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 2, 9, 1),
+ datetime(1997, 9, 2, 9, 2)])
+
+ def testMinutelyInterval(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ interval=2,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 2, 9, 2),
+ datetime(1997, 9, 2, 9, 4)])
+
+ def testMinutelyIntervalLarge(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ interval=1501,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 3, 10, 1),
+ datetime(1997, 9, 4, 11, 2)])
+
+ def testMinutelyByMonth(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ bymonth=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 0, 0),
+ datetime(1998, 1, 1, 0, 1),
+ datetime(1998, 1, 1, 0, 2)])
+
+ def testMinutelyByMonthDay(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ bymonthday=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 3, 0, 0),
+ datetime(1997, 9, 3, 0, 1),
+ datetime(1997, 9, 3, 0, 2)])
+
+ def testMinutelyByMonthAndMonthDay(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(5, 7),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 5, 0, 0),
+ datetime(1998, 1, 5, 0, 1),
+ datetime(1998, 1, 5, 0, 2)])
+
+ def testMinutelyByWeekDay(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 2, 9, 1),
+ datetime(1997, 9, 2, 9, 2)])
+
+ def testMinutelyByNWeekDay(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 2, 9, 1),
+ datetime(1997, 9, 2, 9, 2)])
+
+ def testMinutelyByMonthAndWeekDay(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 0, 0),
+ datetime(1998, 1, 1, 0, 1),
+ datetime(1998, 1, 1, 0, 2)])
+
+ def testMinutelyByMonthAndNWeekDay(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 0, 0),
+ datetime(1998, 1, 1, 0, 1),
+ datetime(1998, 1, 1, 0, 2)])
+
+ def testMinutelyByMonthDayAndWeekDay(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 0, 0),
+ datetime(1998, 1, 1, 0, 1),
+ datetime(1998, 1, 1, 0, 2)])
+
+ def testMinutelyByMonthAndMonthDayAndWeekDay(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 0, 0),
+ datetime(1998, 1, 1, 0, 1),
+ datetime(1998, 1, 1, 0, 2)])
+
+ def testMinutelyByYearDay(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=4,
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 31, 0, 0),
+ datetime(1997, 12, 31, 0, 1),
+ datetime(1997, 12, 31, 0, 2),
+ datetime(1997, 12, 31, 0, 3)])
+
+ def testMinutelyByYearDayNeg(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=4,
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 31, 0, 0),
+ datetime(1997, 12, 31, 0, 1),
+ datetime(1997, 12, 31, 0, 2),
+ datetime(1997, 12, 31, 0, 3)])
+
+ def testMinutelyByMonthAndYearDay(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 10, 0, 0),
+ datetime(1998, 4, 10, 0, 1),
+ datetime(1998, 4, 10, 0, 2),
+ datetime(1998, 4, 10, 0, 3)])
+
+ def testMinutelyByMonthAndYearDayNeg(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 10, 0, 0),
+ datetime(1998, 4, 10, 0, 1),
+ datetime(1998, 4, 10, 0, 2),
+ datetime(1998, 4, 10, 0, 3)])
+
+ def testMinutelyByWeekNo(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ byweekno=20,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 5, 11, 0, 0),
+ datetime(1998, 5, 11, 0, 1),
+ datetime(1998, 5, 11, 0, 2)])
+
+ def testMinutelyByWeekNoAndWeekDay(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ byweekno=1,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 29, 0, 0),
+ datetime(1997, 12, 29, 0, 1),
+ datetime(1997, 12, 29, 0, 2)])
+
+ def testMinutelyByWeekNoAndWeekDayLarge(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ byweekno=52,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 28, 0, 0),
+ datetime(1997, 12, 28, 0, 1),
+ datetime(1997, 12, 28, 0, 2)])
+
+ def testMinutelyByWeekNoAndWeekDayLast(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ byweekno=-1,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 28, 0, 0),
+ datetime(1997, 12, 28, 0, 1),
+ datetime(1997, 12, 28, 0, 2)])
+
+ def testMinutelyByWeekNoAndWeekDay53(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ byweekno=53,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 12, 28, 0, 0),
+ datetime(1998, 12, 28, 0, 1),
+ datetime(1998, 12, 28, 0, 2)])
+
+ def testMinutelyByEaster(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ byeaster=0,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 12, 0, 0),
+ datetime(1998, 4, 12, 0, 1),
+ datetime(1998, 4, 12, 0, 2)])
+
+ def testMinutelyByEasterPos(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ byeaster=1,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 13, 0, 0),
+ datetime(1998, 4, 13, 0, 1),
+ datetime(1998, 4, 13, 0, 2)])
+
+ def testMinutelyByEasterNeg(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ byeaster=-1,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 11, 0, 0),
+ datetime(1998, 4, 11, 0, 1),
+ datetime(1998, 4, 11, 0, 2)])
+
+ def testMinutelyByHour(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ byhour=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 0),
+ datetime(1997, 9, 2, 18, 1),
+ datetime(1997, 9, 2, 18, 2)])
+
+ def testMinutelyByMinute(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 6),
+ datetime(1997, 9, 2, 9, 18),
+ datetime(1997, 9, 2, 10, 6)])
+
+ def testMinutelyBySecond(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0, 6),
+ datetime(1997, 9, 2, 9, 0, 18),
+ datetime(1997, 9, 2, 9, 1, 6)])
+
+ def testMinutelyByHourAndMinute(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 6),
+ datetime(1997, 9, 2, 18, 18),
+ datetime(1997, 9, 3, 6, 6)])
+
+ def testMinutelyByHourAndSecond(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ byhour=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 0, 6),
+ datetime(1997, 9, 2, 18, 0, 18),
+ datetime(1997, 9, 2, 18, 1, 6)])
+
+ def testMinutelyByMinuteAndSecond(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 6, 6),
+ datetime(1997, 9, 2, 9, 6, 18),
+ datetime(1997, 9, 2, 9, 18, 6)])
+
+ def testMinutelyByHourAndMinuteAndSecond(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 6, 6),
+ datetime(1997, 9, 2, 18, 6, 18),
+ datetime(1997, 9, 2, 18, 18, 6)])
+
+ def testMinutelyBySetPos(self):
+ self.assertEqual(list(rrule(MINUTELY,
+ count=3,
+ bysecond=(15, 30, 45),
+ bysetpos=(3, -3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0, 15),
+ datetime(1997, 9, 2, 9, 0, 45),
+ datetime(1997, 9, 2, 9, 1, 15)])
+
+ def testSecondly(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0, 0),
+ datetime(1997, 9, 2, 9, 0, 1),
+ datetime(1997, 9, 2, 9, 0, 2)])
+
+ def testSecondlyInterval(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ interval=2,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0, 0),
+ datetime(1997, 9, 2, 9, 0, 2),
+ datetime(1997, 9, 2, 9, 0, 4)])
+
+ def testSecondlyIntervalLarge(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ interval=90061,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0, 0),
+ datetime(1997, 9, 3, 10, 1, 1),
+ datetime(1997, 9, 4, 11, 2, 2)])
+
+ def testSecondlyByMonth(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ bymonth=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 0, 0, 0),
+ datetime(1998, 1, 1, 0, 0, 1),
+ datetime(1998, 1, 1, 0, 0, 2)])
+
+ def testSecondlyByMonthDay(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ bymonthday=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 3, 0, 0, 0),
+ datetime(1997, 9, 3, 0, 0, 1),
+ datetime(1997, 9, 3, 0, 0, 2)])
+
+ def testSecondlyByMonthAndMonthDay(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(5, 7),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 5, 0, 0, 0),
+ datetime(1998, 1, 5, 0, 0, 1),
+ datetime(1998, 1, 5, 0, 0, 2)])
+
+ def testSecondlyByWeekDay(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0, 0),
+ datetime(1997, 9, 2, 9, 0, 1),
+ datetime(1997, 9, 2, 9, 0, 2)])
+
+ def testSecondlyByNWeekDay(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0, 0),
+ datetime(1997, 9, 2, 9, 0, 1),
+ datetime(1997, 9, 2, 9, 0, 2)])
+
+ def testSecondlyByMonthAndWeekDay(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 0, 0, 0),
+ datetime(1998, 1, 1, 0, 0, 1),
+ datetime(1998, 1, 1, 0, 0, 2)])
+
+ def testSecondlyByMonthAndNWeekDay(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 0, 0, 0),
+ datetime(1998, 1, 1, 0, 0, 1),
+ datetime(1998, 1, 1, 0, 0, 2)])
+
+ def testSecondlyByMonthDayAndWeekDay(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 0, 0, 0),
+ datetime(1998, 1, 1, 0, 0, 1),
+ datetime(1998, 1, 1, 0, 0, 2)])
+
+ def testSecondlyByMonthAndMonthDayAndWeekDay(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 1, 0, 0, 0),
+ datetime(1998, 1, 1, 0, 0, 1),
+ datetime(1998, 1, 1, 0, 0, 2)])
+
+ def testSecondlyByYearDay(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=4,
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 31, 0, 0, 0),
+ datetime(1997, 12, 31, 0, 0, 1),
+ datetime(1997, 12, 31, 0, 0, 2),
+ datetime(1997, 12, 31, 0, 0, 3)])
+
+ def testSecondlyByYearDayNeg(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=4,
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 31, 0, 0, 0),
+ datetime(1997, 12, 31, 0, 0, 1),
+ datetime(1997, 12, 31, 0, 0, 2),
+ datetime(1997, 12, 31, 0, 0, 3)])
+
+ def testSecondlyByMonthAndYearDay(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 10, 0, 0, 0),
+ datetime(1998, 4, 10, 0, 0, 1),
+ datetime(1998, 4, 10, 0, 0, 2),
+ datetime(1998, 4, 10, 0, 0, 3)])
+
+ def testSecondlyByMonthAndYearDayNeg(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 10, 0, 0, 0),
+ datetime(1998, 4, 10, 0, 0, 1),
+ datetime(1998, 4, 10, 0, 0, 2),
+ datetime(1998, 4, 10, 0, 0, 3)])
+
+ def testSecondlyByWeekNo(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ byweekno=20,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 5, 11, 0, 0, 0),
+ datetime(1998, 5, 11, 0, 0, 1),
+ datetime(1998, 5, 11, 0, 0, 2)])
+
+ def testSecondlyByWeekNoAndWeekDay(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ byweekno=1,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 29, 0, 0, 0),
+ datetime(1997, 12, 29, 0, 0, 1),
+ datetime(1997, 12, 29, 0, 0, 2)])
+
+ def testSecondlyByWeekNoAndWeekDayLarge(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ byweekno=52,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 28, 0, 0, 0),
+ datetime(1997, 12, 28, 0, 0, 1),
+ datetime(1997, 12, 28, 0, 0, 2)])
+
+ def testSecondlyByWeekNoAndWeekDayLast(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ byweekno=-1,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 12, 28, 0, 0, 0),
+ datetime(1997, 12, 28, 0, 0, 1),
+ datetime(1997, 12, 28, 0, 0, 2)])
+
+ def testSecondlyByWeekNoAndWeekDay53(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ byweekno=53,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 12, 28, 0, 0, 0),
+ datetime(1998, 12, 28, 0, 0, 1),
+ datetime(1998, 12, 28, 0, 0, 2)])
+
+ def testSecondlyByEaster(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ byeaster=0,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 12, 0, 0, 0),
+ datetime(1998, 4, 12, 0, 0, 1),
+ datetime(1998, 4, 12, 0, 0, 2)])
+
+ def testSecondlyByEasterPos(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ byeaster=1,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 13, 0, 0, 0),
+ datetime(1998, 4, 13, 0, 0, 1),
+ datetime(1998, 4, 13, 0, 0, 2)])
+
+ def testSecondlyByEasterNeg(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ byeaster=-1,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 4, 11, 0, 0, 0),
+ datetime(1998, 4, 11, 0, 0, 1),
+ datetime(1998, 4, 11, 0, 0, 2)])
+
+ def testSecondlyByHour(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ byhour=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 0, 0),
+ datetime(1997, 9, 2, 18, 0, 1),
+ datetime(1997, 9, 2, 18, 0, 2)])
+
+ def testSecondlyByMinute(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 6, 0),
+ datetime(1997, 9, 2, 9, 6, 1),
+ datetime(1997, 9, 2, 9, 6, 2)])
+
+ def testSecondlyBySecond(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0, 6),
+ datetime(1997, 9, 2, 9, 0, 18),
+ datetime(1997, 9, 2, 9, 1, 6)])
+
+ def testSecondlyByHourAndMinute(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 6, 0),
+ datetime(1997, 9, 2, 18, 6, 1),
+ datetime(1997, 9, 2, 18, 6, 2)])
+
+ def testSecondlyByHourAndSecond(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ byhour=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 0, 6),
+ datetime(1997, 9, 2, 18, 0, 18),
+ datetime(1997, 9, 2, 18, 1, 6)])
+
+ def testSecondlyByMinuteAndSecond(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 6, 6),
+ datetime(1997, 9, 2, 9, 6, 18),
+ datetime(1997, 9, 2, 9, 18, 6)])
+
+ def testSecondlyByHourAndMinuteAndSecond(self):
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 18, 6, 6),
+ datetime(1997, 9, 2, 18, 6, 18),
+ datetime(1997, 9, 2, 18, 18, 6)])
+
+ def testSecondlyByHourAndMinuteAndSecondBug(self):
+ # This explores a bug found by Mathieu Bridon.
+ self.assertEqual(list(rrule(SECONDLY,
+ count=3,
+ bysecond=(0,),
+ byminute=(1,),
+ dtstart=datetime(2010, 3, 22, 12, 1))),
+ [datetime(2010, 3, 22, 12, 1),
+ datetime(2010, 3, 22, 13, 1),
+ datetime(2010, 3, 22, 14, 1)])
+
+ def testLongIntegers(self):
+ if not PY3: # There is no longs in python3
+ self.assertEqual(list(rrule(MINUTELY,
+ count=long(2),
+ interval=long(2),
+ bymonth=long(2),
+ byweekday=long(3),
+ byhour=long(6),
+ byminute=long(6),
+ bysecond=long(6),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 2, 5, 6, 6, 6),
+ datetime(1998, 2, 12, 6, 6, 6)])
+ self.assertEqual(list(rrule(YEARLY,
+ count=long(2),
+ bymonthday=long(5),
+ byweekno=long(2),
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1998, 1, 5, 9, 0),
+ datetime(2004, 1, 5, 9, 0)])
+
+ def testHourlyBadRRule(self):
+ """
+ When `byhour` is specified with `freq=HOURLY`, there are certain
+ combinations of `dtstart` and `byhour` which result in an rrule with no
+ valid values.
+
+ See https://github.com/dateutil/dateutil/issues/4
+ """
+
+ self.assertRaises(ValueError, rrule, HOURLY,
+ **dict(interval=4, byhour=(7, 11, 15, 19),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testMinutelyBadRRule(self):
+ """
+ See :func:`testHourlyBadRRule` for details.
+ """
+
+ self.assertRaises(ValueError, rrule, MINUTELY,
+ **dict(interval=12, byminute=(10, 11, 25, 39, 50),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testSecondlyBadRRule(self):
+ """
+ See :func:`testHourlyBadRRule` for details.
+ """
+
+ self.assertRaises(ValueError, rrule, SECONDLY,
+ **dict(interval=10, bysecond=(2, 15, 37, 42, 59),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testMinutelyBadComboRRule(self):
+ """
+ Certain values of :param:`interval` in :class:`rrule`, when combined
+ with certain values of :param:`byhour` create rules which apply to no
+ valid dates. The library should detect this case in the iterator and
+ raise a :exception:`ValueError`.
+ """
+
+ # In Python 2.7 you can use a context manager for this.
+ def make_bad_rrule():
+ list(rrule(MINUTELY, interval=120, byhour=(10, 12, 14, 16),
+ count=2, dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ self.assertRaises(ValueError, make_bad_rrule)
+
+ def testSecondlyBadComboRRule(self):
+ """
+ See :func:`testMinutelyBadComboRRule' for details.
+ """
+
+ # In Python 2.7 you can use a context manager for this.
+ def make_bad_minute_rrule():
+ list(rrule(SECONDLY, interval=360, byminute=(10, 28, 49),
+ count=4, dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def make_bad_hour_rrule():
+ list(rrule(SECONDLY, interval=43200, byhour=(2, 10, 18, 23),
+ count=4, dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ self.assertRaises(ValueError, make_bad_minute_rrule)
+ self.assertRaises(ValueError, make_bad_hour_rrule)
+
+ def testBadUntilCountRRule(self):
+ """
+ See rfc-2445 4.3.10 - This checks for the deprecation warning, and will
+ eventually check for an error.
+ """
+ with self.assertWarns(DeprecationWarning):
+ rrule(DAILY, dtstart=datetime(1997, 9, 2, 9, 0),
+ count=3, until=datetime(1997, 9, 4, 9, 0))
+
+ def testUntilNotMatching(self):
+ self.assertEqual(list(rrule(DAILY,
+ dtstart=datetime(1997, 9, 2, 9, 0),
+ until=datetime(1997, 9, 5, 8, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 3, 9, 0),
+ datetime(1997, 9, 4, 9, 0)])
+
+ def testUntilMatching(self):
+ self.assertEqual(list(rrule(DAILY,
+ dtstart=datetime(1997, 9, 2, 9, 0),
+ until=datetime(1997, 9, 4, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 3, 9, 0),
+ datetime(1997, 9, 4, 9, 0)])
+
+ def testUntilSingle(self):
+ self.assertEqual(list(rrule(DAILY,
+ dtstart=datetime(1997, 9, 2, 9, 0),
+ until=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0)])
+
+ def testUntilEmpty(self):
+ self.assertEqual(list(rrule(DAILY,
+ dtstart=datetime(1997, 9, 2, 9, 0),
+ until=datetime(1997, 9, 1, 9, 0))),
+ [])
+
+ def testUntilWithDate(self):
+ self.assertEqual(list(rrule(DAILY,
+ dtstart=datetime(1997, 9, 2, 9, 0),
+ until=date(1997, 9, 5))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 3, 9, 0),
+ datetime(1997, 9, 4, 9, 0)])
+
+ def testWkStIntervalMO(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ interval=2,
+ byweekday=(TU, SU),
+ wkst=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 7, 9, 0),
+ datetime(1997, 9, 16, 9, 0)])
+
+ def testWkStIntervalSU(self):
+ self.assertEqual(list(rrule(WEEKLY,
+ count=3,
+ interval=2,
+ byweekday=(TU, SU),
+ wkst=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 14, 9, 0),
+ datetime(1997, 9, 16, 9, 0)])
+
+ def testDTStartIsDate(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ dtstart=date(1997, 9, 2))),
+ [datetime(1997, 9, 2, 0, 0),
+ datetime(1997, 9, 3, 0, 0),
+ datetime(1997, 9, 4, 0, 0)])
+
+ def testDTStartWithMicroseconds(self):
+ self.assertEqual(list(rrule(DAILY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0, 0, 500000))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 3, 9, 0),
+ datetime(1997, 9, 4, 9, 0)])
+
+ def testMaxYear(self):
+ self.assertEqual(list(rrule(YEARLY,
+ count=3,
+ bymonth=2,
+ bymonthday=31,
+ dtstart=datetime(9997, 9, 2, 9, 0, 0))),
+ [])
+
+ def testGetItem(self):
+ self.assertEqual(rrule(DAILY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0))[0],
+ datetime(1997, 9, 2, 9, 0))
+
+ def testGetItemNeg(self):
+ self.assertEqual(rrule(DAILY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0))[-1],
+ datetime(1997, 9, 4, 9, 0))
+
+ def testGetItemSlice(self):
+ self.assertEqual(rrule(DAILY,
+ # count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0))[1:2],
+ [datetime(1997, 9, 3, 9, 0)])
+
+ def testGetItemSliceEmpty(self):
+ self.assertEqual(rrule(DAILY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0))[:],
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 3, 9, 0),
+ datetime(1997, 9, 4, 9, 0)])
+
+ def testGetItemSliceStep(self):
+ self.assertEqual(rrule(DAILY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0))[::-2],
+ [datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 2, 9, 0)])
+
+ def testCount(self):
+ self.assertEqual(rrule(DAILY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0)).count(),
+ 3)
+
+ def testContains(self):
+ rr = rrule(DAILY, count=3, dtstart=datetime(1997, 9, 2, 9, 0))
+ self.assertEqual(datetime(1997, 9, 3, 9, 0) in rr, True)
+
+ def testContainsNot(self):
+ rr = rrule(DAILY, count=3, dtstart=datetime(1997, 9, 2, 9, 0))
+ self.assertEqual(datetime(1997, 9, 3, 9, 0) not in rr, False)
+
+ def testBefore(self):
+ self.assertEqual(rrule(DAILY, # count=5
+ dtstart=datetime(1997, 9, 2, 9, 0)).before(datetime(1997, 9, 5, 9, 0)),
+ datetime(1997, 9, 4, 9, 0))
+
+ def testBeforeInc(self):
+ self.assertEqual(rrule(DAILY,
+ #count=5,
+ dtstart=datetime(1997, 9, 2, 9, 0))
+ .before(datetime(1997, 9, 5, 9, 0), inc=True),
+ datetime(1997, 9, 5, 9, 0))
+
+ def testAfter(self):
+ self.assertEqual(rrule(DAILY,
+ #count=5,
+ dtstart=datetime(1997, 9, 2, 9, 0))
+ .after(datetime(1997, 9, 4, 9, 0)),
+ datetime(1997, 9, 5, 9, 0))
+
+ def testAfterInc(self):
+ self.assertEqual(rrule(DAILY,
+ #count=5,
+ dtstart=datetime(1997, 9, 2, 9, 0))
+ .after(datetime(1997, 9, 4, 9, 0), inc=True),
+ datetime(1997, 9, 4, 9, 0))
+
+ def testXAfter(self):
+ self.assertEqual(list(rrule(DAILY,
+ dtstart=datetime(1997, 9, 2, 9, 0))
+ .xafter(datetime(1997, 9, 8, 9, 0), count=12)),
+ [datetime(1997, 9, 9, 9, 0),
+ datetime(1997, 9, 10, 9, 0),
+ datetime(1997, 9, 11, 9, 0),
+ datetime(1997, 9, 12, 9, 0),
+ datetime(1997, 9, 13, 9, 0),
+ datetime(1997, 9, 14, 9, 0),
+ datetime(1997, 9, 15, 9, 0),
+ datetime(1997, 9, 16, 9, 0),
+ datetime(1997, 9, 17, 9, 0),
+ datetime(1997, 9, 18, 9, 0),
+ datetime(1997, 9, 19, 9, 0),
+ datetime(1997, 9, 20, 9, 0)])
+
+ def testXAfterInc(self):
+ self.assertEqual(list(rrule(DAILY,
+ dtstart=datetime(1997, 9, 2, 9, 0))
+ .xafter(datetime(1997, 9, 8, 9, 0), count=12, inc=True)),
+ [datetime(1997, 9, 8, 9, 0),
+ datetime(1997, 9, 9, 9, 0),
+ datetime(1997, 9, 10, 9, 0),
+ datetime(1997, 9, 11, 9, 0),
+ datetime(1997, 9, 12, 9, 0),
+ datetime(1997, 9, 13, 9, 0),
+ datetime(1997, 9, 14, 9, 0),
+ datetime(1997, 9, 15, 9, 0),
+ datetime(1997, 9, 16, 9, 0),
+ datetime(1997, 9, 17, 9, 0),
+ datetime(1997, 9, 18, 9, 0),
+ datetime(1997, 9, 19, 9, 0)])
+
+ def testBetween(self):
+ self.assertEqual(rrule(DAILY,
+ #count=5,
+ dtstart=datetime(1997, 9, 2, 9, 0))
+ .between(datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 6, 9, 0)),
+ [datetime(1997, 9, 3, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 5, 9, 0)])
+
+ def testBetweenInc(self):
+ self.assertEqual(rrule(DAILY,
+ #count=5,
+ dtstart=datetime(1997, 9, 2, 9, 0))
+ .between(datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 6, 9, 0), inc=True),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 3, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 5, 9, 0),
+ datetime(1997, 9, 6, 9, 0)])
+
+ def testCachePre(self):
+ rr = rrule(DAILY, count=15, cache=True,
+ dtstart=datetime(1997, 9, 2, 9, 0))
+ self.assertEqual(list(rr),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 3, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 5, 9, 0),
+ datetime(1997, 9, 6, 9, 0),
+ datetime(1997, 9, 7, 9, 0),
+ datetime(1997, 9, 8, 9, 0),
+ datetime(1997, 9, 9, 9, 0),
+ datetime(1997, 9, 10, 9, 0),
+ datetime(1997, 9, 11, 9, 0),
+ datetime(1997, 9, 12, 9, 0),
+ datetime(1997, 9, 13, 9, 0),
+ datetime(1997, 9, 14, 9, 0),
+ datetime(1997, 9, 15, 9, 0),
+ datetime(1997, 9, 16, 9, 0)])
+
+ def testCachePost(self):
+ rr = rrule(DAILY, count=15, cache=True,
+ dtstart=datetime(1997, 9, 2, 9, 0))
+ for x in rr: pass
+ self.assertEqual(list(rr),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 3, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 5, 9, 0),
+ datetime(1997, 9, 6, 9, 0),
+ datetime(1997, 9, 7, 9, 0),
+ datetime(1997, 9, 8, 9, 0),
+ datetime(1997, 9, 9, 9, 0),
+ datetime(1997, 9, 10, 9, 0),
+ datetime(1997, 9, 11, 9, 0),
+ datetime(1997, 9, 12, 9, 0),
+ datetime(1997, 9, 13, 9, 0),
+ datetime(1997, 9, 14, 9, 0),
+ datetime(1997, 9, 15, 9, 0),
+ datetime(1997, 9, 16, 9, 0)])
+
+ def testCachePostInternal(self):
+ rr = rrule(DAILY, count=15, cache=True,
+ dtstart=datetime(1997, 9, 2, 9, 0))
+ for x in rr: pass
+ self.assertEqual(rr._cache,
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 3, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 5, 9, 0),
+ datetime(1997, 9, 6, 9, 0),
+ datetime(1997, 9, 7, 9, 0),
+ datetime(1997, 9, 8, 9, 0),
+ datetime(1997, 9, 9, 9, 0),
+ datetime(1997, 9, 10, 9, 0),
+ datetime(1997, 9, 11, 9, 0),
+ datetime(1997, 9, 12, 9, 0),
+ datetime(1997, 9, 13, 9, 0),
+ datetime(1997, 9, 14, 9, 0),
+ datetime(1997, 9, 15, 9, 0),
+ datetime(1997, 9, 16, 9, 0)])
+
+ def testCachePreContains(self):
+ rr = rrule(DAILY, count=3, cache=True,
+ dtstart=datetime(1997, 9, 2, 9, 0))
+ self.assertEqual(datetime(1997, 9, 3, 9, 0) in rr, True)
+
+ def testCachePostContains(self):
+ rr = rrule(DAILY, count=3, cache=True,
+ dtstart=datetime(1997, 9, 2, 9, 0))
+ for x in rr: pass
+ self.assertEqual(datetime(1997, 9, 3, 9, 0) in rr, True)
+
+ def testStr(self):
+ self.assertEqual(list(rrulestr(
+ "DTSTART:19970902T090000\n"
+ "RRULE:FREQ=YEARLY;COUNT=3\n"
+ )),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1998, 9, 2, 9, 0),
+ datetime(1999, 9, 2, 9, 0)])
+
+ def testStrType(self):
+ self.assertEqual(isinstance(rrulestr(
+ "DTSTART:19970902T090000\n"
+ "RRULE:FREQ=YEARLY;COUNT=3\n"
+ ), rrule), True)
+
+ def testStrForceSetType(self):
+ self.assertEqual(isinstance(rrulestr(
+ "DTSTART:19970902T090000\n"
+ "RRULE:FREQ=YEARLY;COUNT=3\n"
+ , forceset=True), rruleset), True)
+
+ def testStrSetType(self):
+ self.assertEqual(isinstance(rrulestr(
+ "DTSTART:19970902T090000\n"
+ "RRULE:FREQ=YEARLY;COUNT=2;BYDAY=TU\n"
+ "RRULE:FREQ=YEARLY;COUNT=1;BYDAY=TH\n"
+ ), rruleset), True)
+
+ def testStrCase(self):
+ self.assertEqual(list(rrulestr(
+ "dtstart:19970902T090000\n"
+ "rrule:freq=yearly;count=3\n"
+ )),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1998, 9, 2, 9, 0),
+ datetime(1999, 9, 2, 9, 0)])
+
+ def testStrSpaces(self):
+ self.assertEqual(list(rrulestr(
+ " DTSTART:19970902T090000 "
+ " RRULE:FREQ=YEARLY;COUNT=3 "
+ )),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1998, 9, 2, 9, 0),
+ datetime(1999, 9, 2, 9, 0)])
+
+ def testStrSpacesAndLines(self):
+ self.assertEqual(list(rrulestr(
+ " DTSTART:19970902T090000 \n"
+ " \n"
+ " RRULE:FREQ=YEARLY;COUNT=3 \n"
+ )),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1998, 9, 2, 9, 0),
+ datetime(1999, 9, 2, 9, 0)])
+
+ def testStrNoDTStart(self):
+ self.assertEqual(list(rrulestr(
+ "RRULE:FREQ=YEARLY;COUNT=3\n"
+ , dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1998, 9, 2, 9, 0),
+ datetime(1999, 9, 2, 9, 0)])
+
+ def testStrValueOnly(self):
+ self.assertEqual(list(rrulestr(
+ "FREQ=YEARLY;COUNT=3\n"
+ , dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1998, 9, 2, 9, 0),
+ datetime(1999, 9, 2, 9, 0)])
+
+ def testStrUnfold(self):
+ self.assertEqual(list(rrulestr(
+ "FREQ=YEA\n RLY;COUNT=3\n", unfold=True,
+ dtstart=datetime(1997, 9, 2, 9, 0))),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1998, 9, 2, 9, 0),
+ datetime(1999, 9, 2, 9, 0)])
+
+ def testStrSet(self):
+ self.assertEqual(list(rrulestr(
+ "DTSTART:19970902T090000\n"
+ "RRULE:FREQ=YEARLY;COUNT=2;BYDAY=TU\n"
+ "RRULE:FREQ=YEARLY;COUNT=1;BYDAY=TH\n"
+ )),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 9, 9, 0)])
+
+ def testStrSetDate(self):
+ self.assertEqual(list(rrulestr(
+ "DTSTART:19970902T090000\n"
+ "RRULE:FREQ=YEARLY;COUNT=1;BYDAY=TU\n"
+ "RDATE:19970904T090000\n"
+ "RDATE:19970909T090000\n"
+ )),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 9, 9, 0)])
+
+ def testStrSetExRule(self):
+ self.assertEqual(list(rrulestr(
+ "DTSTART:19970902T090000\n"
+ "RRULE:FREQ=YEARLY;COUNT=6;BYDAY=TU,TH\n"
+ "EXRULE:FREQ=YEARLY;COUNT=3;BYDAY=TH\n"
+ )),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 9, 9, 0),
+ datetime(1997, 9, 16, 9, 0)])
+
+ def testStrSetExDate(self):
+ self.assertEqual(list(rrulestr(
+ "DTSTART:19970902T090000\n"
+ "RRULE:FREQ=YEARLY;COUNT=6;BYDAY=TU,TH\n"
+ "EXDATE:19970904T090000\n"
+ "EXDATE:19970911T090000\n"
+ "EXDATE:19970918T090000\n"
+ )),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 9, 9, 0),
+ datetime(1997, 9, 16, 9, 0)])
+
+ def testStrSetDateAndExDate(self):
+ self.assertEqual(list(rrulestr(
+ "DTSTART:19970902T090000\n"
+ "RDATE:19970902T090000\n"
+ "RDATE:19970904T090000\n"
+ "RDATE:19970909T090000\n"
+ "RDATE:19970911T090000\n"
+ "RDATE:19970916T090000\n"
+ "RDATE:19970918T090000\n"
+ "EXDATE:19970904T090000\n"
+ "EXDATE:19970911T090000\n"
+ "EXDATE:19970918T090000\n"
+ )),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 9, 9, 0),
+ datetime(1997, 9, 16, 9, 0)])
+
+ def testStrSetDateAndExRule(self):
+ self.assertEqual(list(rrulestr(
+ "DTSTART:19970902T090000\n"
+ "RDATE:19970902T090000\n"
+ "RDATE:19970904T090000\n"
+ "RDATE:19970909T090000\n"
+ "RDATE:19970911T090000\n"
+ "RDATE:19970916T090000\n"
+ "RDATE:19970918T090000\n"
+ "EXRULE:FREQ=YEARLY;COUNT=3;BYDAY=TH\n"
+ )),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 9, 9, 0),
+ datetime(1997, 9, 16, 9, 0)])
+
+ def testStrKeywords(self):
+ self.assertEqual(list(rrulestr(
+ "DTSTART:19970902T090000\n"
+ "RRULE:FREQ=YEARLY;COUNT=3;INTERVAL=3;"
+ "BYMONTH=3;BYWEEKDAY=TH;BYMONTHDAY=3;"
+ "BYHOUR=3;BYMINUTE=3;BYSECOND=3\n"
+ )),
+ [datetime(2033, 3, 3, 3, 3, 3),
+ datetime(2039, 3, 3, 3, 3, 3),
+ datetime(2072, 3, 3, 3, 3, 3)])
+
+ def testStrNWeekDay(self):
+ self.assertEqual(list(rrulestr(
+ "DTSTART:19970902T090000\n"
+ "RRULE:FREQ=YEARLY;COUNT=3;BYDAY=1TU,-1TH\n"
+ )),
+ [datetime(1997, 12, 25, 9, 0),
+ datetime(1998, 1, 6, 9, 0),
+ datetime(1998, 12, 31, 9, 0)])
+
+ def testStrUntil(self):
+ self.assertEqual(list(rrulestr(
+ "DTSTART:19970902T090000\n"
+ "RRULE:FREQ=YEARLY;"
+ "UNTIL=19990101T000000;BYDAY=1TU,-1TH\n"
+ )),
+ [datetime(1997, 12, 25, 9, 0),
+ datetime(1998, 1, 6, 9, 0),
+ datetime(1998, 12, 31, 9, 0)])
+
+ def testStrInvalidUntil(self):
+ with self.assertRaises(ValueError):
+ list(rrulestr("DTSTART:19970902T090000\n"
+ "RRULE:FREQ=YEARLY;"
+ "UNTIL=TheCowsComeHome;BYDAY=1TU,-1TH\n"))
+
+ def testStrEmptyByDay(self):
+ with self.assertRaises(ValueError):
+ list(rrulestr("DTSTART:19970902T090000\n"
+ "FREQ=WEEKLY;"
+ "BYDAY=;" # This part is invalid
+ "WKST=SU"))
+
+ def testStrInvalidByDay(self):
+ with self.assertRaises(ValueError):
+ list(rrulestr("DTSTART:19970902T090000\n"
+ "FREQ=WEEKLY;"
+ "BYDAY=-1OK;" # This part is invalid
+ "WKST=SU"))
+
+ def testBadBySetPos(self):
+ self.assertRaises(ValueError,
+ rrule, MONTHLY,
+ count=1,
+ bysetpos=0,
+ dtstart=datetime(1997, 9, 2, 9, 0))
+
+ def testBadBySetPosMany(self):
+ self.assertRaises(ValueError,
+ rrule, MONTHLY,
+ count=1,
+ bysetpos=(-1, 0, 1),
+ dtstart=datetime(1997, 9, 2, 9, 0))
+
+ # Tests to ensure that str(rrule) works
+ def testToStrYearly(self):
+ rule = rrule(YEARLY, count=3, dtstart=datetime(1997, 9, 2, 9, 0))
+ self._rrulestr_reverse_test(rule)
+
+ def testToStrYearlyInterval(self):
+ rule = rrule(YEARLY, count=3, interval=2,
+ dtstart=datetime(1997, 9, 2, 9, 0))
+ self._rrulestr_reverse_test(rule)
+
+ def testToStrYearlyByMonth(self):
+ rule = rrule(YEARLY, count=3, bymonth=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0))
+
+ self._rrulestr_reverse_test(rule)
+
+ def testToStrYearlyByMonth(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ bymonth=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByMonthDay(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ bymonthday=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByMonthAndMonthDay(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(5, 7),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByWeekDay(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByNWeekDay(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByNWeekDayLarge(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ byweekday=(TU(3), TH(-3)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByMonthAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByMonthAndNWeekDay(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByMonthAndNWeekDayLarge(self):
+ # This is interesting because the TH(-3) ends up before
+ # the TU(3).
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(3), TH(-3)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByMonthDayAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByMonthAndMonthDayAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByYearDay(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=4,
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByYearDayNeg(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=4,
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByMonthAndYearDay(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByMonthAndYearDayNeg(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByWeekNo(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ byweekno=20,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByWeekNoAndWeekDay(self):
+ # That's a nice one. The first days of week number one
+ # may be in the last year.
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ byweekno=1,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByWeekNoAndWeekDayLarge(self):
+ # Another nice test. The last days of week number 52/53
+ # may be in the next year.
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ byweekno=52,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByWeekNoAndWeekDayLast(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ byweekno=-1,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByEaster(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ byeaster=0,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByEasterPos(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ byeaster=1,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByEasterNeg(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ byeaster=-1,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByWeekNoAndWeekDay53(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ byweekno=53,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByHour(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ byhour=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByMinute(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyBySecond(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByHourAndMinute(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByHourAndSecond(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ byhour=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByMinuteAndSecond(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyByHourAndMinuteAndSecond(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrYearlyBySetPos(self):
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=3,
+ bymonthday=15,
+ byhour=(6, 18),
+ bysetpos=(3, -3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthly(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyInterval(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ interval=2,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyIntervalLarge(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ interval=18,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByMonth(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ bymonth=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByMonthDay(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ bymonthday=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByMonthAndMonthDay(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(5, 7),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByWeekDay(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ # Third Monday of the month
+ self.assertEqual(rrule(MONTHLY,
+ byweekday=(MO(+3)),
+ dtstart=datetime(1997, 9, 1)).between(datetime(1997,
+ 9,
+ 1),
+ datetime(1997,
+ 12,
+ 1)),
+ [datetime(1997, 9, 15, 0, 0),
+ datetime(1997, 10, 20, 0, 0),
+ datetime(1997, 11, 17, 0, 0)])
+
+ def testToStrMonthlyByNWeekDay(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByNWeekDayLarge(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ byweekday=(TU(3), TH(-3)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByMonthAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByMonthAndNWeekDay(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByMonthAndNWeekDayLarge(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(3), TH(-3)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByMonthDayAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByMonthAndMonthDayAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByYearDay(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=4,
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByYearDayNeg(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=4,
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByMonthAndYearDay(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByMonthAndYearDayNeg(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByWeekNo(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ byweekno=20,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByWeekNoAndWeekDay(self):
+ # That's a nice one. The first days of week number one
+ # may be in the last year.
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ byweekno=1,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByWeekNoAndWeekDayLarge(self):
+ # Another nice test. The last days of week number 52/53
+ # may be in the next year.
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ byweekno=52,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByWeekNoAndWeekDayLast(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ byweekno=-1,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByWeekNoAndWeekDay53(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ byweekno=53,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByEaster(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ byeaster=0,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByEasterPos(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ byeaster=1,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByEasterNeg(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ byeaster=-1,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByHour(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ byhour=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByMinute(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyBySecond(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByHourAndMinute(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByHourAndSecond(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ byhour=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByMinuteAndSecond(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyByHourAndMinuteAndSecond(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMonthlyBySetPos(self):
+ self._rrulestr_reverse_test(rrule(MONTHLY,
+ count=3,
+ bymonthday=(13, 17),
+ byhour=(6, 18),
+ bysetpos=(3, -3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeekly(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyInterval(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ interval=2,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyIntervalLarge(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ interval=20,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByMonth(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ bymonth=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByMonthDay(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ bymonthday=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByMonthAndMonthDay(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(5, 7),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByWeekDay(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByNWeekDay(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByMonthAndWeekDay(self):
+ # This test is interesting, because it crosses the year
+ # boundary in a weekly period to find day '1' as a
+ # valid recurrence.
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByMonthAndNWeekDay(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByMonthDayAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByMonthAndMonthDayAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByYearDay(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=4,
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByYearDayNeg(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=4,
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByMonthAndYearDay(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=4,
+ bymonth=(1, 7),
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByMonthAndYearDayNeg(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=4,
+ bymonth=(1, 7),
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByWeekNo(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ byweekno=20,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByWeekNoAndWeekDay(self):
+ # That's a nice one. The first days of week number one
+ # may be in the last year.
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ byweekno=1,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByWeekNoAndWeekDayLarge(self):
+ # Another nice test. The last days of week number 52/53
+ # may be in the next year.
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ byweekno=52,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByWeekNoAndWeekDayLast(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ byweekno=-1,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByWeekNoAndWeekDay53(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ byweekno=53,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByEaster(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ byeaster=0,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByEasterPos(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ byeaster=1,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByEasterNeg(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ byeaster=-1,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByHour(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ byhour=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByMinute(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyBySecond(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByHourAndMinute(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByHourAndSecond(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ byhour=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByMinuteAndSecond(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyByHourAndMinuteAndSecond(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrWeeklyBySetPos(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ byweekday=(TU, TH),
+ byhour=(6, 18),
+ bysetpos=(3, -3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDaily(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyInterval(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ interval=2,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyIntervalLarge(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ interval=92,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByMonth(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ bymonth=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByMonthDay(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ bymonthday=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByMonthAndMonthDay(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(5, 7),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByWeekDay(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByNWeekDay(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByMonthAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByMonthAndNWeekDay(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByMonthDayAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByMonthAndMonthDayAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByYearDay(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=4,
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByYearDayNeg(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=4,
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByMonthAndYearDay(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=4,
+ bymonth=(1, 7),
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByMonthAndYearDayNeg(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=4,
+ bymonth=(1, 7),
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByWeekNo(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ byweekno=20,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByWeekNoAndWeekDay(self):
+ # That's a nice one. The first days of week number one
+ # may be in the last year.
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ byweekno=1,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByWeekNoAndWeekDayLarge(self):
+ # Another nice test. The last days of week number 52/53
+ # may be in the next year.
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ byweekno=52,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByWeekNoAndWeekDayLast(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ byweekno=-1,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByWeekNoAndWeekDay53(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ byweekno=53,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByEaster(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ byeaster=0,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByEasterPos(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ byeaster=1,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByEasterNeg(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ byeaster=-1,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByHour(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ byhour=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByMinute(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyBySecond(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByHourAndMinute(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByHourAndSecond(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ byhour=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByMinuteAndSecond(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyByHourAndMinuteAndSecond(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrDailyBySetPos(self):
+ self._rrulestr_reverse_test(rrule(DAILY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(15, 45),
+ bysetpos=(3, -3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourly(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyInterval(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ interval=2,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyIntervalLarge(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ interval=769,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByMonth(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ bymonth=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByMonthDay(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ bymonthday=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByMonthAndMonthDay(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(5, 7),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByWeekDay(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByNWeekDay(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByMonthAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByMonthAndNWeekDay(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByMonthDayAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByMonthAndMonthDayAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByYearDay(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=4,
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByYearDayNeg(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=4,
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByMonthAndYearDay(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByMonthAndYearDayNeg(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByWeekNo(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ byweekno=20,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByWeekNoAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ byweekno=1,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByWeekNoAndWeekDayLarge(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ byweekno=52,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByWeekNoAndWeekDayLast(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ byweekno=-1,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByWeekNoAndWeekDay53(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ byweekno=53,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByEaster(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ byeaster=0,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByEasterPos(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ byeaster=1,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByEasterNeg(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ byeaster=-1,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByHour(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ byhour=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByMinute(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyBySecond(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByHourAndMinute(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByHourAndSecond(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ byhour=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByMinuteAndSecond(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyByHourAndMinuteAndSecond(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrHourlyBySetPos(self):
+ self._rrulestr_reverse_test(rrule(HOURLY,
+ count=3,
+ byminute=(15, 45),
+ bysecond=(15, 45),
+ bysetpos=(3, -3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutely(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyInterval(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ interval=2,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyIntervalLarge(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ interval=1501,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByMonth(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ bymonth=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByMonthDay(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ bymonthday=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByMonthAndMonthDay(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(5, 7),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByWeekDay(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByNWeekDay(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByMonthAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByMonthAndNWeekDay(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByMonthDayAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByMonthAndMonthDayAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByYearDay(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=4,
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByYearDayNeg(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=4,
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByMonthAndYearDay(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByMonthAndYearDayNeg(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByWeekNo(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ byweekno=20,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByWeekNoAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ byweekno=1,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByWeekNoAndWeekDayLarge(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ byweekno=52,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByWeekNoAndWeekDayLast(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ byweekno=-1,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByWeekNoAndWeekDay53(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ byweekno=53,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByEaster(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ byeaster=0,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByEasterPos(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ byeaster=1,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByEasterNeg(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ byeaster=-1,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByHour(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ byhour=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByMinute(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyBySecond(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByHourAndMinute(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByHourAndSecond(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ byhour=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByMinuteAndSecond(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyByHourAndMinuteAndSecond(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrMinutelyBySetPos(self):
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=3,
+ bysecond=(15, 30, 45),
+ bysetpos=(3, -3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondly(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyInterval(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ interval=2,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyIntervalLarge(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ interval=90061,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByMonth(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ bymonth=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByMonthDay(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ bymonthday=(1, 3),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByMonthAndMonthDay(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(5, 7),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByWeekDay(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByNWeekDay(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByMonthAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByMonthAndNWeekDay(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ bymonth=(1, 3),
+ byweekday=(TU(1), TH(-1)),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByMonthDayAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByMonthAndMonthDayAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ bymonth=(1, 3),
+ bymonthday=(1, 3),
+ byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByYearDay(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=4,
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByYearDayNeg(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=4,
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByMonthAndYearDay(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(1, 100, 200, 365),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByMonthAndYearDayNeg(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=4,
+ bymonth=(4, 7),
+ byyearday=(-365, -266, -166, -1),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByWeekNo(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ byweekno=20,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByWeekNoAndWeekDay(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ byweekno=1,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByWeekNoAndWeekDayLarge(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ byweekno=52,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByWeekNoAndWeekDayLast(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ byweekno=-1,
+ byweekday=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByWeekNoAndWeekDay53(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ byweekno=53,
+ byweekday=MO,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByEaster(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ byeaster=0,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByEasterPos(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ byeaster=1,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByEasterNeg(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ byeaster=-1,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByHour(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ byhour=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByMinute(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyBySecond(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByHourAndMinute(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByHourAndSecond(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ byhour=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByMinuteAndSecond(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByHourAndMinuteAndSecond(self):
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ byhour=(6, 18),
+ byminute=(6, 18),
+ bysecond=(6, 18),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrSecondlyByHourAndMinuteAndSecondBug(self):
+ # This explores a bug found by Mathieu Bridon.
+ self._rrulestr_reverse_test(rrule(SECONDLY,
+ count=3,
+ bysecond=(0,),
+ byminute=(1,),
+ dtstart=datetime(2010, 3, 22, 12, 1)))
+
+ def testToStrWithWkSt(self):
+ self._rrulestr_reverse_test(rrule(WEEKLY,
+ count=3,
+ wkst=SU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testToStrLongIntegers(self):
+ if not PY3: # There is no longs in python3
+ self._rrulestr_reverse_test(rrule(MINUTELY,
+ count=long(2),
+ interval=long(2),
+ bymonth=long(2),
+ byweekday=long(3),
+ byhour=long(6),
+ byminute=long(6),
+ bysecond=long(6),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ self._rrulestr_reverse_test(rrule(YEARLY,
+ count=long(2),
+ bymonthday=long(5),
+ byweekno=long(2),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+
+ def testReplaceIfSet(self):
+ rr = rrule(YEARLY,
+ count=1,
+ bymonthday=5,
+ dtstart=datetime(1997, 1, 1))
+ newrr = rr.replace(bymonthday=6)
+ self.assertEqual(list(rr), [datetime(1997, 1, 5)])
+ self.assertEqual(list(newrr),
+ [datetime(1997, 1, 6)])
+
+ def testReplaceIfNotSet(self):
+ rr = rrule(YEARLY,
+ count=1,
+ dtstart=datetime(1997, 1, 1))
+ newrr = rr.replace(bymonthday=6)
+ self.assertEqual(list(rr), [datetime(1997, 1, 1)])
+ self.assertEqual(list(newrr),
+ [datetime(1997, 1, 6)])
+
+
+class RRuleSetTest(unittest.TestCase):
+ def testSet(self):
+ rrset = rruleset()
+ rrset.rrule(rrule(YEARLY, count=2, byweekday=TU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+ rrset.rrule(rrule(YEARLY, count=1, byweekday=TH,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+ self.assertEqual(list(rrset),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 9, 9, 0)])
+
+ def testSetDate(self):
+ rrset = rruleset()
+ rrset.rrule(rrule(YEARLY, count=1, byweekday=TU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+ rrset.rdate(datetime(1997, 9, 4, 9))
+ rrset.rdate(datetime(1997, 9, 9, 9))
+ self.assertEqual(list(rrset),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 9, 9, 0)])
+
+ def testSetExRule(self):
+ rrset = rruleset()
+ rrset.rrule(rrule(YEARLY, count=6, byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+ rrset.exrule(rrule(YEARLY, count=3, byweekday=TH,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+ self.assertEqual(list(rrset),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 9, 9, 0),
+ datetime(1997, 9, 16, 9, 0)])
+
+ def testSetExDate(self):
+ rrset = rruleset()
+ rrset.rrule(rrule(YEARLY, count=6, byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+ rrset.exdate(datetime(1997, 9, 4, 9))
+ rrset.exdate(datetime(1997, 9, 11, 9))
+ rrset.exdate(datetime(1997, 9, 18, 9))
+ self.assertEqual(list(rrset),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 9, 9, 0),
+ datetime(1997, 9, 16, 9, 0)])
+
+ def testSetExDateRevOrder(self):
+ rrset = rruleset()
+ rrset.rrule(rrule(MONTHLY, count=5, bymonthday=10,
+ dtstart=datetime(2004, 1, 1, 9, 0)))
+ rrset.exdate(datetime(2004, 4, 10, 9, 0))
+ rrset.exdate(datetime(2004, 2, 10, 9, 0))
+ self.assertEqual(list(rrset),
+ [datetime(2004, 1, 10, 9, 0),
+ datetime(2004, 3, 10, 9, 0),
+ datetime(2004, 5, 10, 9, 0)])
+
+ def testSetDateAndExDate(self):
+ rrset = rruleset()
+ rrset.rdate(datetime(1997, 9, 2, 9))
+ rrset.rdate(datetime(1997, 9, 4, 9))
+ rrset.rdate(datetime(1997, 9, 9, 9))
+ rrset.rdate(datetime(1997, 9, 11, 9))
+ rrset.rdate(datetime(1997, 9, 16, 9))
+ rrset.rdate(datetime(1997, 9, 18, 9))
+ rrset.exdate(datetime(1997, 9, 4, 9))
+ rrset.exdate(datetime(1997, 9, 11, 9))
+ rrset.exdate(datetime(1997, 9, 18, 9))
+ self.assertEqual(list(rrset),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 9, 9, 0),
+ datetime(1997, 9, 16, 9, 0)])
+
+ def testSetDateAndExRule(self):
+ rrset = rruleset()
+ rrset.rdate(datetime(1997, 9, 2, 9))
+ rrset.rdate(datetime(1997, 9, 4, 9))
+ rrset.rdate(datetime(1997, 9, 9, 9))
+ rrset.rdate(datetime(1997, 9, 11, 9))
+ rrset.rdate(datetime(1997, 9, 16, 9))
+ rrset.rdate(datetime(1997, 9, 18, 9))
+ rrset.exrule(rrule(YEARLY, count=3, byweekday=TH,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+ self.assertEqual(list(rrset),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 9, 9, 0),
+ datetime(1997, 9, 16, 9, 0)])
+
+ def testSetCount(self):
+ rrset = rruleset()
+ rrset.rrule(rrule(YEARLY, count=6, byweekday=(TU, TH),
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+ rrset.exrule(rrule(YEARLY, count=3, byweekday=TH,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+ self.assertEqual(rrset.count(), 3)
+
+ def testSetCachePre(self):
+ rrset = rruleset()
+ rrset.rrule(rrule(YEARLY, count=2, byweekday=TU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+ rrset.rrule(rrule(YEARLY, count=1, byweekday=TH,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+ self.assertEqual(list(rrset),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 9, 9, 0)])
+
+ def testSetCachePost(self):
+ rrset = rruleset(cache=True)
+ rrset.rrule(rrule(YEARLY, count=2, byweekday=TU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+ rrset.rrule(rrule(YEARLY, count=1, byweekday=TH,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+ for x in rrset: pass
+ self.assertEqual(list(rrset),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 9, 9, 0)])
+
+ def testSetCachePostInternal(self):
+ rrset = rruleset(cache=True)
+ rrset.rrule(rrule(YEARLY, count=2, byweekday=TU,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+ rrset.rrule(rrule(YEARLY, count=1, byweekday=TH,
+ dtstart=datetime(1997, 9, 2, 9, 0)))
+ for x in rrset: pass
+ self.assertEqual(list(rrset._cache),
+ [datetime(1997, 9, 2, 9, 0),
+ datetime(1997, 9, 4, 9, 0),
+ datetime(1997, 9, 9, 9, 0)])
+
+ def testSetRRuleCount(self):
+ # Test that the count is updated when an rrule is added
+ rrset = rruleset(cache=False)
+ for cache in (True, False):
+ rrset = rruleset(cache=cache)
+ rrset.rrule(rrule(YEARLY, count=2, byweekday=TH,
+ dtstart=datetime(1983, 4, 1)))
+ rrset.rrule(rrule(WEEKLY, count=4, byweekday=FR,
+ dtstart=datetime(1991, 6, 3)))
+
+ # Check the length twice - first one sets a cache, second reads it
+ self.assertEqual(rrset.count(), 6)
+ self.assertEqual(rrset.count(), 6)
+
+ # This should invalidate the cache and force an update
+ rrset.rrule(rrule(MONTHLY, count=3, dtstart=datetime(1994, 1, 3)))
+
+ self.assertEqual(rrset.count(), 9)
+ self.assertEqual(rrset.count(), 9)
+
+ def testSetRDateCount(self):
+ # Test that the count is updated when an rdate is added
+ rrset = rruleset(cache=False)
+ for cache in (True, False):
+ rrset = rruleset(cache=cache)
+ rrset.rrule(rrule(YEARLY, count=2, byweekday=TH,
+ dtstart=datetime(1983, 4, 1)))
+ rrset.rrule(rrule(WEEKLY, count=4, byweekday=FR,
+ dtstart=datetime(1991, 6, 3)))
+
+ # Check the length twice - first one sets a cache, second reads it
+ self.assertEqual(rrset.count(), 6)
+ self.assertEqual(rrset.count(), 6)
+
+ # This should invalidate the cache and force an update
+ rrset.rdate(datetime(1993, 2, 14))
+
+ self.assertEqual(rrset.count(), 7)
+ self.assertEqual(rrset.count(), 7)
+
+ def testSetExRuleCount(self):
+ # Test that the count is updated when an exrule is added
+ rrset = rruleset(cache=False)
+ for cache in (True, False):
+ rrset = rruleset(cache=cache)
+ rrset.rrule(rrule(YEARLY, count=2, byweekday=TH,
+ dtstart=datetime(1983, 4, 1)))
+ rrset.rrule(rrule(WEEKLY, count=4, byweekday=FR,
+ dtstart=datetime(1991, 6, 3)))
+
+ # Check the length twice - first one sets a cache, second reads it
+ self.assertEqual(rrset.count(), 6)
+ self.assertEqual(rrset.count(), 6)
+
+ # This should invalidate the cache and force an update
+ rrset.exrule(rrule(WEEKLY, count=2, interval=2,
+ dtstart=datetime(1991, 6, 14)))
+
+ self.assertEqual(rrset.count(), 4)
+ self.assertEqual(rrset.count(), 4)
+
+ def testSetExDateCount(self):
+ # Test that the count is updated when an rdate is added
+ for cache in (True, False):
+ rrset = rruleset(cache=cache)
+ rrset.rrule(rrule(YEARLY, count=2, byweekday=TH,
+ dtstart=datetime(1983, 4, 1)))
+ rrset.rrule(rrule(WEEKLY, count=4, byweekday=FR,
+ dtstart=datetime(1991, 6, 3)))
+
+ # Check the length twice - first one sets a cache, second reads it
+ self.assertEqual(rrset.count(), 6)
+ self.assertEqual(rrset.count(), 6)
+
+ # This should invalidate the cache and force an update
+ rrset.exdate(datetime(1991, 6, 28))
+
+ self.assertEqual(rrset.count(), 5)
+ self.assertEqual(rrset.count(), 5)
+
+
+class WeekdayTest(unittest.TestCase):
+ def testInvalidNthWeekday(self):
+ with self.assertRaises(ValueError):
+ zeroth_friday = FR(0)
+
+ def testWeekdayCallable(self):
+ # Calling a weekday instance generates a new weekday instance with the
+ # value of n changed.
+ from dateutil.rrule import weekday
+ self.assertEqual(MO(1), weekday(0, 1))
+
+ # Calling a weekday instance with the identical n returns the original
+ # object
+ FR_3 = weekday(4, 3)
+ self.assertIs(FR_3(3), FR_3)
+
+ def testWeekdayEquality(self):
+ # Two weekday objects are not equal if they have different values for n
+ self.assertNotEqual(TH, TH(-1))
+ self.assertNotEqual(SA(3), SA(2))
+
+ def testWeekdayEqualitySubclass(self):
+ # Two weekday objects equal if their "weekday" and "n" attributes are
+ # available and the same
+ class BasicWeekday(object):
+ def __init__(self, weekday):
+ self.weekday = weekday
+
+ class BasicNWeekday(BasicWeekday):
+ def __init__(self, weekday, n=None):
+ super(BasicNWeekday, self).__init__(weekday)
+ self.n = n
+
+ MO_Basic = BasicWeekday(0)
+
+ self.assertNotEqual(MO, MO_Basic)
+ self.assertNotEqual(MO(1), MO_Basic)
+
+ TU_BasicN = BasicNWeekday(1)
+
+ self.assertEqual(TU, TU_BasicN)
+ self.assertNotEqual(TU(3), TU_BasicN)
+
+ WE_Basic3 = BasicNWeekday(2, 3)
+ self.assertEqual(WE(3), WE_Basic3)
+ self.assertNotEqual(WE(2), WE_Basic3)
+
+ def testWeekdayReprNoN(self):
+ no_n_reprs = ('MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU')
+ no_n_wdays = (MO, TU, WE, TH, FR, SA, SU)
+
+ for repstr, wday in zip(no_n_reprs, no_n_wdays):
+ self.assertEqual(repr(wday), repstr)
+
+ def testWeekdayReprWithN(self):
+ with_n_reprs = ('WE(+1)', 'TH(-2)', 'SU(+3)')
+ with_n_wdays = (WE(1), TH(-2), SU(+3))
+
+ for repstr, wday in zip(with_n_reprs, with_n_wdays):
+ self.assertEqual(repr(wday), repstr)
+
diff --git a/libs/dateutil/test/test_tz.py b/libs/dateutil/test/test_tz.py
new file mode 100644
index 000000000..4ca203661
--- /dev/null
+++ b/libs/dateutil/test/test_tz.py
@@ -0,0 +1,2114 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+from ._common import unittest, PicklableMixin
+from ._common import total_seconds
+from ._common import TZEnvContext, TZWinContext
+from ._common import WarningTestMixin
+from ._common import ComparesEqual
+
+from datetime import datetime, timedelta
+from datetime import time as dt_time
+from datetime import tzinfo
+from six import BytesIO, StringIO
+
+import os
+import subprocess
+import sys
+import base64
+import copy
+import itertools
+
+from functools import partial
+
+IS_WIN = sys.platform.startswith('win')
+
+# dateutil imports
+from dateutil.relativedelta import relativedelta, SU
+from dateutil.parser import parse
+from dateutil import tz as tz
+from dateutil import zoneinfo
+
+try:
+ from dateutil import tzwin
+except ImportError as e:
+ if IS_WIN:
+ raise e
+ else:
+ pass
+
+MISSING_TARBALL = ("This test fails if you don't have the dateutil "
+ "timezone file installed. Please read the README")
+
+TZFILE_EST5EDT = b"""
+VFppZgAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABAAAAAAAAADrAAAABAAAABCeph5wn7rrYKCGAHCh
+ms1gomXicKOD6eCkaq5wpTWnYKZTyvCnFYlgqDOs8Kj+peCqE47wqt6H4KvzcPCsvmngrdNS8K6e
+S+CvszTwsH4t4LGcUXCyZ0pgs3wzcLRHLGC1XBVwticOYLc793C4BvBguRvZcLnm0mC7BPXwu8a0
+YLzk1/C9r9DgvsS58L+PsuDApJvwwW+U4MKEffDDT3bgxGRf8MUvWODGTXxwxw864MgtXnDI+Fdg
+yg1AcMrYOWDLiPBw0iP0cNJg++DTdeTw1EDd4NVVxvDWIL/g1zWo8NgAoeDZFYrw2eCD4Nr+p3Db
+wGXg3N6JcN2pgmDevmtw34lkYOCeTXDhaUZg4n4vcONJKGDkXhFw5Vcu4OZHLfDnNxDg6CcP8OkW
+8uDqBvHw6vbU4Ovm0/Ds1rbg7ca18O6/02Dvr9Jw8J+1YPGPtHDyf5dg82+WcPRfeWD1T3hw9j9b
+YPcvWnD4KHfg+Q88cPoIWeD6+Fjw++g74PzYOvD9yB3g/rgc8P+n/+AAl/7wAYfh4AJ34PADcP5g
+BGD9cAVQ4GAGQN9wBzDCYAeNGXAJEKRgCa2U8ArwhmAL4IVwDNmi4A3AZ3AOuYTgD6mD8BCZZuAR
+iWXwEnlI4BNpR/AUWSrgFUkp8BY5DOAXKQvwGCIpYBkI7fAaAgtgGvIKcBvh7WAc0exwHcHPYB6x
+znAfobFgIHYA8CGBk2AiVeLwI2qv4CQ1xPAlSpHgJhWm8Ccqc+An/sNwKQpV4CnepXAq6jfgK76H
+cCzTVGAtnmlwLrM2YC9+S3AwkxhgMWdn8DJy+mAzR0nwNFLcYDUnK/A2Mr5gNwcN8Dgb2uA45u/w
+Ofu84DrG0fA7257gPK/ucD27gOA+j9BwP5ti4EBvsnBBhH9gQk+UcENkYWBEL3ZwRURDYEYPWHBH
+JCVgR/h08EkEB2BJ2FbwSuPpYEu4OPBMzQXgTZga8E6s5+BPd/zwUIzJ4FFhGXBSbKvgU0D7cFRM
+jeBVIN1wVixv4FcAv3BYFYxgWOChcFn1bmBawINwW9VQYFypn/BdtTJgXomB8F+VFGBgaWPwYX4w
+4GJJRfBjXhLgZCkn8GU99OBmEkRwZx3W4GfyJnBo/bjgadIIcGrdmuBrsepwbMa3YG2RzHBupplg
+b3GucHCGe2BxWsrwcmZdYHM6rPB0Rj9gdRqO8HYvW+B2+nDweA894HjaUvB57x/gero08HvPAeB8
+o1Fwfa7j4H6DM3B/jsXgAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQAB
+AAEAAQABAgMBAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQAB
+AAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEA
+AQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQAB
+AAEAAQABAAEAAQABAAEAAQABAAEAAf//x8ABAP//ubAABP//x8ABCP//x8ABDEVEVABFU1QARVdU
+AEVQVAAAAAABAAAAAQ==
+"""
+
+EUROPE_HELSINKI = b"""
+VFppZgAAAAAAAAAAAAAAAAAAAAAAAAAFAAAABQAAAAAAAAB1AAAABQAAAA2kc28Yy85RYMy/hdAV
+I+uQFhPckBcDzZAX876QGOOvkBnToJAaw5GQG7y9EBysrhAdnJ8QHoyQEB98gRAgbHIQIVxjECJM
+VBAjPEUQJCw2ECUcJxAmDBgQJwVDkCf1NJAo5SWQKdUWkCrFB5ArtPiQLKTpkC2U2pAuhMuQL3S8
+kDBkrZAxXdkQMnK0EDM9uxA0UpYQNR2dEDYyeBA2/X8QOBuUkDjdYRA5+3aQOr1DEDvbWJA8pl+Q
+Pbs6kD6GQZA/mxyQQGYjkEGEORBCRgWQQ2QbEEQl55BFQ/0QRgXJkEcj3xBH7uYQSQPBEEnOyBBK
+46MQS66qEEzMv5BNjowQTqyhkE9ubhBQjIOQUVeKkFJsZZBTN2yQVExHkFUXTpBWLCmQVvcwkFgV
+RhBY1xKQWfUoEFq29JBb1QoQXKAREF207BBef/MQX5TOEGBf1RBhfeqQYj+3EGNdzJBkH5kQZT2u
+kGYItZBnHZCQZ+iXkGj9cpBpyHmQat1UkGuoW5BsxnEQbYg9kG6mUxBvaB+QcIY1EHFRPBByZhcQ
+czEeEHRF+RB1EQAQdi8VkHbw4hB4DveQeNDEEHnu2ZB6sKYQe867kHyZwpB9rp2QfnmkkH+Of5AC
+AQIDBAMEAwQDBAMEAwQDBAMEAwQDBAMEAwQDBAMEAwQDBAMEAwQDBAMEAwQDBAMEAwQDBAMEAwQD
+BAMEAwQDBAMEAwQDBAMEAwQDBAMEAwQDBAMEAwQDBAMEAwQDBAMEAwQDBAMEAwQDBAMEAwQDBAME
+AwQAABdoAAAAACowAQQAABwgAAkAACowAQQAABwgAAlITVQARUVTVABFRVQAAAAAAQEAAAABAQ==
+"""
+
+NEW_YORK = b"""
+VFppZgAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABAAAABcAAADrAAAABAAAABCeph5wn7rrYKCGAHCh
+ms1gomXicKOD6eCkaq5wpTWnYKZTyvCnFYlgqDOs8Kj+peCqE47wqt6H4KvzcPCsvmngrdNS8K6e
+S+CvszTwsH4t4LGcUXCyZ0pgs3wzcLRHLGC1XBVwticOYLc793C4BvBguRvZcLnm0mC7BPXwu8a0
+YLzk1/C9r9DgvsS58L+PsuDApJvwwW+U4MKEffDDT3bgxGRf8MUvWODGTXxwxw864MgtXnDI+Fdg
+yg1AcMrYOWDLiPBw0iP0cNJg++DTdeTw1EDd4NVVxvDWIL/g1zWo8NgAoeDZFYrw2eCD4Nr+p3Db
+wGXg3N6JcN2pgmDevmtw34lkYOCeTXDhaUZg4n4vcONJKGDkXhFw5Vcu4OZHLfDnNxDg6CcP8OkW
+8uDqBvHw6vbU4Ovm0/Ds1rbg7ca18O6/02Dvr9Jw8J+1YPGPtHDyf5dg82+WcPRfeWD1T3hw9j9b
+YPcvWnD4KHfg+Q88cPoIWeD6+Fjw++g74PzYOvD9yB3g/rgc8P+n/+AAl/7wAYfh4AJ34PADcP5g
+BGD9cAVQ4GEGQN9yBzDCYgeNGXMJEKRjCa2U9ArwhmQL4IV1DNmi5Q3AZ3YOuYTmD6mD9xCZZucR
+iWX4EnlI6BNpR/kUWSrpFUkp+RY5DOoXKQv6GCIpaxkI7fsaAgtsGvIKfBvh7Wwc0ex8HcHPbR6x
+zn0fobFtIHYA/SGBk20iVeL+I2qv7iQ1xP4lSpHuJhWm/ycqc+8n/sOAKQpV8CnepYAq6jfxK76H
+gSzTVHItnmmCLrM2cy9+S4MwkxhzMWdoBDJy+nQzR0oENFLcdTUnLAU2Mr51NwcOBjgb2vY45vAG
+Ofu89jrG0gY72572PK/uhj27gPY+j9CGP5ti9kBvsoZBhH92Qk+UhkNkYXZEL3aHRURDd0XzqQdH
+LV/3R9OLB0kNQfdJs20HSu0j90uciYdM1kB3TXxrh062IndPXE2HUJYEd1E8L4dSdeZ3UxwRh1RV
+yHdU+/OHVjWqd1blEAdYHsb3WMTyB1n+qPdapNQHW96K91yEtgddvmz3XmSYB1+eTvdgTbSHYYdr
+d2ItlodjZ013ZA14h2VHL3dl7VqHZycRd2fNPIdpBvN3aa0eh2rm1XdrljsHbM/x9212HQdur9P3
+b1X/B3CPtfdxNeEHcm+X93MVwwd0T3n3dP7fh3Y4lnd23sGHeBh4d3i+o4d5+Fp3ep6Fh3vYPHd8
+fmeHfbged35eSYd/mAB3AAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQAB
+AAEAAQABAgMBAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQAB
+AAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEA
+AQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQAB
+AAEAAQABAAEAAQABAAEAAQABAAEAAf//x8ABAP//ubAABP//x8ABCP//x8ABDEVEVABFU1QARVdU
+AEVQVAAEslgAAAAAAQWk7AEAAAACB4YfggAAAAMJZ1MDAAAABAtIhoQAAAAFDSsLhQAAAAYPDD8G
+AAAABxDtcocAAAAIEs6mCAAAAAkVn8qJAAAACheA/goAAAALGWIxiwAAAAwdJeoMAAAADSHa5Q0A
+AAAOJZ6djgAAAA8nf9EPAAAAECpQ9ZAAAAARLDIpEQAAABIuE1ySAAAAEzDnJBMAAAAUM7hIlAAA
+ABU2jBAVAAAAFkO3G5YAAAAXAAAAAQAAAAE=
+"""
+
+TZICAL_EST5EDT = """
+BEGIN:VTIMEZONE
+TZID:US-Eastern
+LAST-MODIFIED:19870101T000000Z
+TZURL:http://zones.stds_r_us.net/tz/US-Eastern
+BEGIN:STANDARD
+DTSTART:19671029T020000
+RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
+TZOFFSETFROM:-0400
+TZOFFSETTO:-0500
+TZNAME:EST
+END:STANDARD
+BEGIN:DAYLIGHT
+DTSTART:19870405T020000
+RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4
+TZOFFSETFROM:-0500
+TZOFFSETTO:-0400
+TZNAME:EDT
+END:DAYLIGHT
+END:VTIMEZONE
+"""
+
+TZICAL_PST8PDT = """
+BEGIN:VTIMEZONE
+TZID:US-Pacific
+LAST-MODIFIED:19870101T000000Z
+BEGIN:STANDARD
+DTSTART:19671029T020000
+RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
+TZOFFSETFROM:-0700
+TZOFFSETTO:-0800
+TZNAME:PST
+END:STANDARD
+BEGIN:DAYLIGHT
+DTSTART:19870405T020000
+RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4
+TZOFFSETFROM:-0800
+TZOFFSETTO:-0700
+TZNAME:PDT
+END:DAYLIGHT
+END:VTIMEZONE
+"""
+
+###
+# Mix-ins
+class context_passthrough(object):
+ def __init__(*args, **kwargs):
+ pass
+
+ def __enter__(*args, **kwargs):
+ pass
+
+ def __exit__(*args, **kwargs):
+ pass
+
+
+class TzFoldMixin(object):
+ """ Mix-in class for testing ambiguous times """
+ def gettz(self, tzname):
+ raise NotImplementedError
+
+ def _get_tzname(self, tzname):
+ return tzname
+
+ def _gettz_context(self, tzname):
+ return context_passthrough()
+
+ def testFoldPositiveUTCOffset(self):
+ # Test that we can resolve ambiguous times
+ tzname = self._get_tzname('Australia/Sydney')
+
+ with self._gettz_context(tzname):
+ SYD0 = self.gettz(tzname)
+ SYD1 = self.gettz(tzname)
+
+ t0_u = datetime(2012, 3, 31, 15, 30, tzinfo=tz.tzutc()) # AEST
+ t1_u = datetime(2012, 3, 31, 16, 30, tzinfo=tz.tzutc()) # AEDT
+
+ # Using fresh tzfiles
+ t0_syd0 = t0_u.astimezone(SYD0)
+ t1_syd1 = t1_u.astimezone(SYD1)
+
+ self.assertEqual(t0_syd0.replace(tzinfo=None),
+ datetime(2012, 4, 1, 2, 30))
+
+ self.assertEqual(t1_syd1.replace(tzinfo=None),
+ datetime(2012, 4, 1, 2, 30))
+
+ self.assertEqual(t0_syd0.utcoffset(), timedelta(hours=11))
+ self.assertEqual(t1_syd1.utcoffset(), timedelta(hours=10))
+
+
+ def testGapPositiveUTCOffset(self):
+ # Test that we don't have a problem around gaps.
+ tzname = self._get_tzname('Australia/Sydney')
+
+ with self._gettz_context(tzname):
+ SYD0 = self.gettz(tzname)
+ SYD1 = self.gettz(tzname)
+
+ t0_u = datetime(2012, 10, 6, 15, 30, tzinfo=tz.tzutc()) # AEST
+ t1_u = datetime(2012, 10, 6, 16, 30, tzinfo=tz.tzutc()) # AEDT
+
+ # Using fresh tzfiles
+ t0 = t0_u.astimezone(SYD0)
+ t1 = t1_u.astimezone(SYD1)
+
+ self.assertEqual(t0.replace(tzinfo=None),
+ datetime(2012, 10, 7, 1, 30))
+
+ self.assertEqual(t1.replace(tzinfo=None),
+ datetime(2012, 10, 7, 3, 30))
+
+ self.assertEqual(t0.utcoffset(), timedelta(hours=10))
+ self.assertEqual(t1.utcoffset(), timedelta(hours=11))
+
+ def testFoldNegativeUTCOffset(self):
+ # Test that we can resolve ambiguous times
+ tzname = self._get_tzname('America/Toronto')
+
+ with self._gettz_context(tzname):
+ # Calling fromutc() alters the tzfile object
+ TOR0 = self.gettz(tzname)
+ TOR1 = self.gettz(tzname)
+
+ t0_u = datetime(2011, 11, 6, 5, 30, tzinfo=tz.tzutc())
+ t1_u = datetime(2011, 11, 6, 6, 30, tzinfo=tz.tzutc())
+
+ # Using fresh tzfiles
+ t0_tor0 = t0_u.astimezone(TOR0)
+ t1_tor1 = t1_u.astimezone(TOR1)
+
+ self.assertEqual(t0_tor0.replace(tzinfo=None),
+ datetime(2011, 11, 6, 1, 30))
+
+ self.assertEqual(t1_tor1.replace(tzinfo=None),
+ datetime(2011, 11, 6, 1, 30))
+
+ self.assertEqual(t0_tor0.utcoffset(), timedelta(hours=-4.0))
+ self.assertEqual(t1_tor1.utcoffset(), timedelta(hours=-5.0))
+
+ def testGapNegativeUTCOffset(self):
+ # Test that we don't have a problem around gaps.
+ tzname = self._get_tzname('America/Toronto')
+
+ with self._gettz_context(tzname):
+ # Calling fromutc() alters the tzfile object
+ TOR0 = self.gettz(tzname)
+ TOR1 = self.gettz(tzname)
+
+ t0_u = datetime(2011, 3, 13, 6, 30, tzinfo=tz.tzutc())
+ t1_u = datetime(2011, 3, 13, 7, 30, tzinfo=tz.tzutc())
+
+ # Using fresh tzfiles
+ t0 = t0_u.astimezone(TOR0)
+ t1 = t1_u.astimezone(TOR1)
+
+ self.assertEqual(t0.replace(tzinfo=None),
+ datetime(2011, 3, 13, 1, 30))
+
+ self.assertEqual(t1.replace(tzinfo=None),
+ datetime(2011, 3, 13, 3, 30))
+
+ self.assertNotEqual(t0, t1)
+ self.assertEqual(t0.utcoffset(), timedelta(hours=-5.0))
+ self.assertEqual(t1.utcoffset(), timedelta(hours=-4.0))
+
+ def testFoldIndependence(self):
+ tzname = self._get_tzname('America/New_York')
+
+ with self._gettz_context(tzname):
+ NYC = self.gettz(tzname)
+ UTC = tz.tzutc()
+ hour = timedelta(hours=1)
+
+ # Firmly 2015-11-01 0:30 EDT-4
+ pre_dst = datetime(2015, 11, 1, 0, 30, tzinfo=NYC)
+
+ # Currently, there's no way around the fact that this resolves to an
+ # ambiguous date, which defaults to EST. I'm not hard-coding in the
+ # answer, though, because the preferred behavior would be that this
+ # results in a time on the EDT side.
+
+ # Ambiguous between 2015-11-01 1:30 EDT-4 and 2015-11-01 1:30 EST-5
+ in_dst = pre_dst + hour
+ in_dst_tzname_0 = in_dst.tzname() # Stash the tzname - EDT
+
+ # Doing the arithmetic in UTC creates a date that is unambiguously
+ # 2015-11-01 1:30 EDT-5
+ in_dst_via_utc = (pre_dst.astimezone(UTC) + 2*hour).astimezone(NYC)
+
+ # Make sure the dates are actually ambiguous
+ self.assertEqual(in_dst, in_dst_via_utc)
+
+ # Make sure we got the right folding behavior
+ self.assertNotEqual(in_dst_via_utc.tzname(), in_dst_tzname_0)
+
+ # Now check to make sure in_dst's tzname hasn't changed
+ self.assertEqual(in_dst_tzname_0, in_dst.tzname())
+
+ def _test_ambiguous_time(self, dt, tzid, ambiguous):
+ # This is a test to check that the individual is_ambiguous values
+ # on the _tzinfo subclasses work.
+ tzname = self._get_tzname(tzid)
+
+ with self._gettz_context(tzname):
+ tzi = self.gettz(tzname)
+
+ self.assertEqual(tz.datetime_ambiguous(dt, tz=tzi), ambiguous)
+
+ def testAmbiguousNegativeUTCOffset(self):
+ self._test_ambiguous_time(datetime(2015, 11, 1, 1, 30),
+ 'America/New_York', True)
+
+ def testAmbiguousPositiveUTCOffset(self):
+ self._test_ambiguous_time(datetime(2012, 4, 1, 2, 30),
+ 'Australia/Sydney', True)
+
+ def testUnambiguousNegativeUTCOffset(self):
+ self._test_ambiguous_time(datetime(2015, 11, 1, 2, 30),
+ 'America/New_York', False)
+
+ def testUnambiguousPositiveUTCOffset(self):
+ self._test_ambiguous_time(datetime(2012, 4, 1, 3, 30),
+ 'Australia/Sydney', False)
+
+ def testUnambiguousGapNegativeUTCOffset(self):
+ # Imaginary time
+ self._test_ambiguous_time(datetime(2011, 3, 13, 2, 30),
+ 'America/New_York', False)
+
+ def testUnambiguousGapPositiveUTCOffset(self):
+ # Imaginary time
+ self._test_ambiguous_time(datetime(2012, 10, 7, 2, 30),
+ 'Australia/Sydney', False)
+
+ def _test_imaginary_time(self, dt, tzid, exists):
+ tzname = self._get_tzname(tzid)
+ with self._gettz_context(tzname):
+ tzi = self.gettz(tzname)
+
+ self.assertEqual(tz.datetime_exists(dt, tz=tzi), exists)
+
+ def testImaginaryNegativeUTCOffset(self):
+ self._test_imaginary_time(datetime(2011, 3, 13, 2, 30),
+ 'America/New_York', False)
+
+ def testNotImaginaryNegativeUTCOffset(self):
+ self._test_imaginary_time(datetime(2011, 3, 13, 1, 30),
+ 'America/New_York', True)
+
+ def testImaginaryPositiveUTCOffset(self):
+ self._test_imaginary_time(datetime(2012, 10, 7, 2, 30),
+ 'Australia/Sydney', False)
+
+ def testNotImaginaryPositiveUTCOffset(self):
+ self._test_imaginary_time(datetime(2012, 10, 7, 1, 30),
+ 'Australia/Sydney', True)
+
+ def testNotImaginaryFoldNegativeUTCOffset(self):
+ self._test_imaginary_time(datetime(2015, 11, 1, 1, 30),
+ 'America/New_York', True)
+
+ def testNotImaginaryFoldPositiveUTCOffset(self):
+ self._test_imaginary_time(datetime(2012, 4, 1, 3, 30),
+ 'Australia/Sydney', True)
+
+ @unittest.skip("Known failure in Python 3.6.")
+ def testEqualAmbiguousComparison(self):
+ tzname = self._get_tzname('Australia/Sydney')
+
+ with self._gettz_context(tzname):
+ SYD0 = self.gettz(tzname)
+ SYD1 = self.gettz(tzname)
+
+ t0_u = datetime(2012, 3, 31, 14, 30, tzinfo=tz.tzutc()) # AEST
+ t1_u = datetime(2012, 3, 31, 16, 30, tzinfo=tz.tzutc()) # AEDT
+
+ t0_syd0 = t0_u.astimezone(SYD0)
+ t0_syd1 = t0_u.astimezone(SYD1)
+
+ # This is considered an "inter-zone comparison" because it's an
+ # ambiguous datetime.
+ self.assertEqual(t0_syd0, t0_syd1)
+
+
+class TzWinFoldMixin(object):
+ def get_args(self, tzname):
+ return (tzname, )
+
+ class context(object):
+ def __init__(*args, **kwargs):
+ pass
+
+ def __enter__(*args, **kwargs):
+ pass
+
+ def __exit__(*args, **kwargs):
+ pass
+
+ def get_utc_transitions(self, tzi, year, gap):
+ dston, dstoff = tzi.transitions(year)
+ if gap:
+ t_n = dston - timedelta(minutes=30)
+
+ t0_u = t_n.replace(tzinfo=tzi).astimezone(tz.tzutc())
+ t1_u = t0_u + timedelta(hours=1)
+ else:
+ # Get 1 hour before the first ambiguous date
+ t_n = dstoff - timedelta(minutes=30)
+
+ t0_u = t_n.replace(tzinfo=tzi).astimezone(tz.tzutc())
+ t_n += timedelta(hours=1) # Naive ambiguous date
+ t0_u = t0_u + timedelta(hours=1) # First ambiguous date
+ t1_u = t0_u + timedelta(hours=1) # Second ambiguous date
+
+ return t_n, t0_u, t1_u
+
+ def testFoldPositiveUTCOffset(self):
+ # Test that we can resolve ambiguous times
+ tzname = 'AUS Eastern Standard Time'
+ args = self.get_args(tzname)
+
+ with self.context(tzname):
+ # Calling fromutc() alters the tzfile object
+ SYD = self.tzclass(*args)
+ SYD0 = self.tzclass(*args)
+ SYD1 = self.tzclass(*args)
+
+ self.assertIsNot(SYD0, SYD1)
+
+ # Get the transition time in UTC from the object, because
+ # Windows doesn't store historical info
+ t_n, t0_u, t1_u = self.get_utc_transitions(SYD0, 2012, False)
+
+ # Using fresh tzfiles
+ t0_syd0 = t0_u.astimezone(SYD0)
+ t1_syd1 = t1_u.astimezone(SYD1)
+
+ self.assertEqual(t0_syd0.replace(tzinfo=None), t_n)
+
+ self.assertEqual(t1_syd1.replace(tzinfo=None), t_n)
+
+ self.assertNotEqual(t0_syd0, t1_syd1)
+ self.assertEqual(t0_syd0.utcoffset(), timedelta(hours=11))
+ self.assertEqual(t1_syd1.utcoffset(), timedelta(hours=10))
+
+ # Re-using them across (make sure there's no cache problem)
+ t0_syd1 = t0_u.astimezone(SYD1)
+ t1_syd0 = t1_u.astimezone(SYD0)
+
+ self.assertEqual(t0_syd0, t0_syd1)
+ self.assertEqual(t1_syd1, t1_syd0)
+
+ def testGapPositiveUTCOffset(self):
+ # Test that we don't have a problem around gaps.
+ tzname = 'AUS Eastern Standard Time'
+ args = self.get_args(tzname)
+
+ with self.context(tzname):
+ # Calling fromutc() alters the tzfile object
+ SYD = self.tzclass(*args)
+ SYD0 = self.tzclass(*args)
+ SYD1 = self.tzclass(*args)
+
+ self.assertIsNot(SYD0, SYD1)
+
+ t_n, t0_u, t1_u = self.get_utc_transitions(SYD, 2012, True)
+
+ # Using fresh tzfiles
+ t0 = t0_u.astimezone(SYD0)
+ t1 = t1_u.astimezone(SYD1)
+
+ self.assertEqual(t0.replace(tzinfo=None), t_n)
+
+ self.assertEqual(t1.replace(tzinfo=None), t_n + timedelta(hours=2))
+
+ self.assertEqual(t0.utcoffset(), timedelta(hours=10))
+ self.assertEqual(t1.utcoffset(), timedelta(hours=11))
+
+ def testFoldNegativeUTCOffset(self):
+ # Test that we can resolve ambiguous times
+ tzname = 'Eastern Standard Time'
+ args = self.get_args(tzname)
+
+ # Calling fromutc() alters the tzfile object
+ with self.context(tzname):
+ TOR = self.tzclass(*args)
+ TOR0 = self.tzclass(*args)
+ TOR1 = self.tzclass(*args)
+
+ t_n, t0_u, t1_u = self.get_utc_transitions(TOR, 2011, False)
+
+ # Using fresh tzfiles
+ t0_tor0 = t0_u.astimezone(TOR0)
+ t1_tor1 = t1_u.astimezone(TOR1)
+
+ self.assertEqual(t0_tor0.replace(tzinfo=None), t_n)
+ self.assertEqual(t1_tor1.replace(tzinfo=None), t_n)
+
+ self.assertNotEqual(t0_tor0.tzname(), t1_tor1.tzname())
+ self.assertEqual(t0_tor0.utcoffset(), timedelta(hours=-4.0))
+ self.assertEqual(t1_tor1.utcoffset(), timedelta(hours=-5.0))
+
+ # Re-using them across (make sure there's no cache problem)
+ t0_tor1 = t0_u.astimezone(TOR1)
+ t1_tor0 = t1_u.astimezone(TOR0)
+
+ self.assertEqual(t0_tor0, t0_tor1)
+ self.assertEqual(t1_tor1, t1_tor0)
+
+ def testGapNegativeUTCOffset(self):
+ # Test that we don't have a problem around gaps.
+ tzname = 'Eastern Standard Time'
+ args = self.get_args(tzname)
+
+ # Calling fromutc() alters the tzfile object
+ with self.context(tzname):
+ TOR = self.tzclass(*args)
+ TOR0 = self.tzclass(*args)
+ TOR1 = self.tzclass(*args)
+
+ t_n, t0_u, t1_u = self.get_utc_transitions(TOR, 2011, True)
+
+ # Using fresh tzfiles
+ t0 = t0_u.astimezone(TOR0)
+ t1 = t1_u.astimezone(TOR1)
+
+ self.assertEqual(t0.replace(tzinfo=None),
+ t_n)
+
+ self.assertEqual(t1.replace(tzinfo=None),
+ t_n + timedelta(hours=2))
+
+ self.assertNotEqual(t0, t1)
+ self.assertEqual(t0.utcoffset(), timedelta(hours=-5.0))
+ self.assertEqual(t1.utcoffset(), timedelta(hours=-4.0))
+
+ def testFoldIndependence(self):
+ tzname = 'Eastern Standard Time'
+ args = self.get_args(tzname)
+
+ with self.context(tzname):
+ NYC = self.tzclass(*args)
+ UTC = tz.tzutc()
+ hour = timedelta(hours=1)
+
+ # Firmly 2015-11-01 0:30 EDT-4
+ t_n, t0_u, t1_u = self.get_utc_transitions(NYC, 2015, False)
+
+ pre_dst = (t_n - hour).replace(tzinfo=NYC)
+
+ # Currently, there's no way around the fact that this resolves to an
+ # ambiguous date, which defaults to EST. I'm not hard-coding in the
+ # answer, though, because the preferred behavior would be that this
+ # results in a time on the EDT side.
+
+ # Ambiguous between 2015-11-01 1:30 EDT-4 and 2015-11-01 1:30 EST-5
+ in_dst = pre_dst + hour
+ in_dst_tzname_0 = in_dst.tzname() # Stash the tzname - EDT
+
+ # Doing the arithmetic in UTC creates a date that is unambiguously
+ # 2015-11-01 1:30 EDT-5
+ in_dst_via_utc = (pre_dst.astimezone(UTC) + 2*hour).astimezone(NYC)
+
+ # Make sure we got the right folding behavior
+ self.assertNotEqual(in_dst_via_utc.tzname(), in_dst_tzname_0)
+
+ # Now check to make sure in_dst's tzname hasn't changed
+ self.assertEqual(in_dst_tzname_0, in_dst.tzname())
+
+
+###
+# Test Cases
+class TzUTCTest(unittest.TestCase):
+ def testOffset(self):
+ ct = datetime(2009, 4, 1, 12, 11, 13, tzinfo=tz.tzutc())
+
+ self.assertEqual(ct.utcoffset(), timedelta(seconds=0))
+
+ def testDst(self):
+ ct = datetime(2009, 4, 1, 12, 11, 13, tzinfo=tz.tzutc())
+
+ self.assertEqual(ct.dst(), timedelta(seconds=0))
+
+ def testTzName(self):
+ ct = datetime(2009, 4, 1, 12, 11, 13, tzinfo=tz.tzutc())
+ self.assertEqual(ct.tzname(), 'UTC')
+
+ def testEquality(self):
+ UTC0 = tz.tzutc()
+ UTC1 = tz.tzutc()
+
+ self.assertIsNot(UTC0, UTC1)
+ self.assertEqual(UTC0, UTC1)
+
+ def testInequality(self):
+ UTC = tz.tzutc()
+ UTCp4 = tz.tzoffset('UTC+4', 14400)
+
+ self.assertNotEqual(UTC, UTCp4)
+
+ def testInequalityInteger(self):
+ self.assertFalse(tz.tzutc() == 7)
+ self.assertNotEqual(tz.tzutc(), 7)
+
+ def testInequalityUnsupported(self):
+ self.assertEqual(tz.tzutc(), ComparesEqual)
+
+ def testRepr(self):
+ UTC = tz.tzutc()
+ self.assertEqual(repr(UTC), 'tzutc()')
+
+ def testTimeOnlyUTC(self):
+ # https://github.com/dateutil/dateutil/issues/132
+ # tzutc doesn't care
+ tz_utc = tz.tzutc()
+ self.assertEqual(dt_time(13, 20, tzinfo=tz_utc).utcoffset(),
+ timedelta(0))
+
+ def testAmbiguity(self):
+ # Pick an arbitrary datetime, this should always return False.
+ dt = datetime(2011, 9, 1, 2, 30, tzinfo=tz.tzutc())
+
+ self.assertFalse(tz.datetime_ambiguous(dt))
+
+
+class TzOffsetTest(unittest.TestCase):
+ def testTimedeltaOffset(self):
+ est = tz.tzoffset('EST', timedelta(hours=-5))
+ est_s = tz.tzoffset('EST', -18000)
+
+ self.assertEqual(est, est_s)
+
+ def testTzNameNone(self):
+ gmt5 = tz.tzoffset(None, -18000) # -5:00
+ self.assertIs(datetime(2003, 10, 26, 0, 0, tzinfo=gmt5).tzname(),
+ None)
+
+ def testTimeOnlyOffset(self):
+ # tzoffset doesn't care
+ tz_offset = tz.tzoffset('+3', 3600)
+ self.assertEqual(dt_time(13, 20, tzinfo=tz_offset).utcoffset(),
+ timedelta(seconds=3600))
+
+ def testTzOffsetRepr(self):
+ tname = 'EST'
+ tzo = tz.tzoffset(tname, -5 * 3600)
+ self.assertEqual(repr(tzo), "tzoffset(" + repr(tname) + ", -18000)")
+
+
+ def testEquality(self):
+ utc = tz.tzoffset('UTC', 0)
+ gmt = tz.tzoffset('GMT', 0)
+
+ self.assertEqual(utc, gmt)
+
+ def testUTCEquality(self):
+ utc = tz.tzutc()
+ o_utc = tz.tzoffset('UTC', 0)
+
+ self.assertEqual(utc, o_utc)
+ self.assertEqual(o_utc, utc)
+
+ def testInequalityInvalid(self):
+ tzo = tz.tzoffset('-3', -3 * 3600)
+ self.assertFalse(tzo == -3)
+ self.assertNotEqual(tzo, -3)
+
+ def testInequalityUnsupported(self):
+ tzo = tz.tzoffset('-5', -5 * 3600)
+
+ self.assertTrue(tzo == ComparesEqual)
+ self.assertFalse(tzo != ComparesEqual)
+ self.assertEqual(tzo, ComparesEqual)
+
+ def testAmbiguity(self):
+ # Pick an arbitrary datetime, this should always return False.
+ dt = datetime(2011, 9, 1, 2, 30, tzinfo=tz.tzoffset("EST", -5 * 3600))
+
+ self.assertFalse(tz.datetime_ambiguous(dt))
+
+
+class TzLocalTest(unittest.TestCase):
+ def testEquality(self):
+ tz1 = tz.tzlocal()
+ tz2 = tz.tzlocal()
+
+ # Explicitly calling == and != here to ensure the operators work
+ self.assertTrue(tz1 == tz2)
+ self.assertFalse(tz1 != tz2)
+
+ def testInequalityFixedOffset(self):
+ tzl = tz.tzlocal()
+ tzos = tz.tzoffset('LST', total_seconds(tzl._std_offset))
+ tzod = tz.tzoffset('LDT', total_seconds(tzl._std_offset))
+
+ self.assertFalse(tzl == tzos)
+ self.assertFalse(tzl == tzod)
+ self.assertTrue(tzl != tzos)
+ self.assertTrue(tzl != tzod)
+
+ def testInequalityInvalid(self):
+ tzl = tz.tzlocal()
+ UTC = tz.tzutc()
+
+ self.assertTrue(tzl != 1)
+ self.assertTrue(tzl != tz.tzutc())
+ self.assertFalse(tzl == 1)
+ self.assertFalse(tzl == UTC)
+
+ def testInequalityUnsupported(self):
+ tzl = tz.tzlocal()
+
+ self.assertTrue(tzl == ComparesEqual)
+ self.assertFalse(tzl != ComparesEqual)
+
+ def testRepr(self):
+ tzl = tz.tzlocal()
+
+ self.assertEqual(repr(tzl), 'tzlocal()')
+
+
[email protected](IS_WIN, "requires Unix")
[email protected](TZEnvContext.tz_change_allowed(),
+ TZEnvContext.tz_change_disallowed_message())
+class TzLocalNixTest(unittest.TestCase, TzFoldMixin):
+ # This is a set of tests for `tzlocal()` on *nix systems
+
+ # POSIX string indicating change to summer time on the 2nd Sunday in March
+ # at 2AM, and ending the 1st Sunday in November at 2AM. (valid >= 2007)
+ TZ_EST = 'EST+5EDT,M3.2.0/2,M11.1.0/2'
+
+ # POSIX string for AEST/AEDT (valid >= 2008)
+ TZ_AEST = 'AEST-10AEDT,M10.1.0/2,M4.1.0/3'
+
+ # POSIX string for UTC
+ UTC = 'UTC'
+
+ def gettz(self, tzname):
+ # Actual time zone changes are handled by the _gettz_context function
+ return tz.tzlocal()
+
+ def _gettz_context(self, tzname):
+ tzname_map = {'Australia/Sydney': self.TZ_AEST,
+ 'America/Toronto': self.TZ_EST,
+ 'America/New_York': self.TZ_EST}
+
+ return TZEnvContext(tzname_map.get(tzname, tzname))
+
+ def _testTzFunc(self, tzval, func, std_val, dst_val):
+ """
+ This generates tests about how the behavior of a function ``func``
+ changes between STD and DST (e.g. utcoffset, tzname, dst).
+
+ It assume that DST starts the 2nd Sunday in March and ends the 1st
+ Sunday in November
+ """
+ with TZEnvContext(tzval):
+ dt1 = datetime(2015, 2, 1, 12, 0, tzinfo=tz.tzlocal()) # STD
+ dt2 = datetime(2015, 5, 1, 12, 0, tzinfo=tz.tzlocal()) # DST
+
+ self.assertEqual(func(dt1), std_val)
+ self.assertEqual(func(dt2), dst_val)
+
+ def _testTzName(self, tzval, std_name, dst_name):
+ func = datetime.tzname
+
+ self._testTzFunc(tzval, func, std_name, dst_name)
+
+ def testTzNameDST(self):
+ # Test tzname in a zone with DST
+ self._testTzName(self.TZ_EST, 'EST', 'EDT')
+
+ def testTzNameUTC(self):
+ # Test tzname in a zone without DST
+ self._testTzName(self.UTC, 'UTC', 'UTC')
+
+ def _testOffset(self, tzval, std_off, dst_off):
+ func = datetime.utcoffset
+
+ self._testTzFunc(tzval, func, std_off, dst_off)
+
+ def testOffsetDST(self):
+ self._testOffset(self.TZ_EST, timedelta(hours=-5), timedelta(hours=-4))
+
+ def testOffsetUTC(self):
+ self._testOffset(self.UTC, timedelta(0), timedelta(0))
+
+ def _testDST(self, tzval, dst_dst):
+ func = datetime.dst
+ std_dst = timedelta(0)
+
+ self._testTzFunc(tzval, func, std_dst, dst_dst)
+
+ def testDSTDST(self):
+ self._testDST(self.TZ_EST, timedelta(hours=1))
+
+ def testDSTUTC(self):
+ self._testDST(self.UTC, timedelta(0))
+
+ def testTimeOnlyOffsetLocalUTC(self):
+ with TZEnvContext(self.UTC):
+ self.assertEqual(dt_time(13, 20, tzinfo=tz.tzlocal()).utcoffset(),
+ timedelta(0))
+
+ def testTimeOnlyOffsetLocalDST(self):
+ with TZEnvContext(self.TZ_EST):
+ self.assertIs(dt_time(13, 20, tzinfo=tz.tzlocal()).utcoffset(),
+ None)
+
+ def testTimeOnlyDSTLocalUTC(self):
+ with TZEnvContext(self.UTC):
+ self.assertEqual(dt_time(13, 20, tzinfo=tz.tzlocal()).dst(),
+ timedelta(0))
+
+ def testTimeOnlyDSTLocalDST(self):
+ with TZEnvContext(self.TZ_EST):
+ self.assertIs(dt_time(13, 20, tzinfo=tz.tzlocal()).dst(),
+ None)
+
+
+class GettzTest(unittest.TestCase, TzFoldMixin):
+ gettz = staticmethod(tz.gettz)
+
+ def testGettz(self):
+ # bug 892569
+ str(self.gettz('UTC'))
+
+ def testGetTzEquality(self):
+ self.assertEqual(self.gettz('UTC'), self.gettz('UTC'))
+
+ def testTimeOnlyGettz(self):
+ # gettz returns None
+ tz_get = self.gettz('Europe/Minsk')
+ self.assertIs(dt_time(13, 20, tzinfo=tz_get).utcoffset(), None)
+
+ def testTimeOnlyGettzDST(self):
+ # gettz returns None
+ tz_get = self.gettz('Europe/Minsk')
+ self.assertIs(dt_time(13, 20, tzinfo=tz_get).dst(), None)
+
+ def testTimeOnlyGettzTzName(self):
+ tz_get = self.gettz('Europe/Minsk')
+ self.assertIs(dt_time(13, 20, tzinfo=tz_get).tzname(), None)
+
+ def testTimeOnlyFormatZ(self):
+ tz_get = self.gettz('Europe/Minsk')
+ t = dt_time(13, 20, tzinfo=tz_get)
+
+ self.assertEqual(t.strftime('%H%M%Z'), '1320')
+
+ def testPortugalDST(self):
+ # In 1996, Portugal changed from CET to WET
+ PORTUGAL = self.gettz('Portugal')
+
+ t_cet = datetime(1996, 3, 31, 1, 59, tzinfo=PORTUGAL)
+
+ self.assertEqual(t_cet.tzname(), 'CET')
+ self.assertEqual(t_cet.utcoffset(), timedelta(hours=1))
+ self.assertEqual(t_cet.dst(), timedelta(0))
+
+ t_west = datetime(1996, 3, 31, 2, 1, tzinfo=PORTUGAL)
+
+ self.assertEqual(t_west.tzname(), 'WEST')
+ self.assertEqual(t_west.utcoffset(), timedelta(hours=1))
+ self.assertEqual(t_west.dst(), timedelta(hours=1))
+
+
+class ZoneInfoGettzTest(GettzTest, WarningTestMixin):
+ def gettz(self, name):
+ zoneinfo_file = zoneinfo.get_zonefile_instance()
+ return zoneinfo_file.get(name)
+
+ def testZoneInfoFileStart1(self):
+ tz = self.gettz("EST5EDT")
+ self.assertEqual(datetime(2003, 4, 6, 1, 59, tzinfo=tz).tzname(), "EST",
+ MISSING_TARBALL)
+ self.assertEqual(datetime(2003, 4, 6, 2, 00, tzinfo=tz).tzname(), "EDT")
+
+ def testZoneInfoFileEnd1(self):
+ tzc = self.gettz("EST5EDT")
+ self.assertEqual(datetime(2003, 10, 26, 0, 59, tzinfo=tzc).tzname(),
+ "EDT", MISSING_TARBALL)
+
+ end_est = tz.enfold(datetime(2003, 10, 26, 1, 00, tzinfo=tzc), fold=1)
+ self.assertEqual(end_est.tzname(), "EST")
+
+ def testZoneInfoOffsetSignal(self):
+ utc = self.gettz("UTC")
+ nyc = self.gettz("America/New_York")
+ self.assertNotEqual(utc, None, MISSING_TARBALL)
+ self.assertNotEqual(nyc, None)
+ t0 = datetime(2007, 11, 4, 0, 30, tzinfo=nyc)
+ t1 = t0.astimezone(utc)
+ t2 = t1.astimezone(nyc)
+ self.assertEqual(t0, t2)
+ self.assertEqual(nyc.dst(t0), timedelta(hours=1))
+
+ def testZoneInfoCopy(self):
+ # copy.copy() called on a ZoneInfo file was returning the same instance
+ CHI = self.gettz('America/Chicago')
+ CHI_COPY = copy.copy(CHI)
+
+ self.assertIsNot(CHI, CHI_COPY)
+ self.assertEqual(CHI, CHI_COPY)
+
+ def testZoneInfoDeepCopy(self):
+ CHI = self.gettz('America/Chicago')
+ CHI_COPY = copy.deepcopy(CHI)
+
+ self.assertIsNot(CHI, CHI_COPY)
+ self.assertEqual(CHI, CHI_COPY)
+
+ def testZoneInfoInstanceCaching(self):
+ zif_0 = zoneinfo.get_zonefile_instance()
+ zif_1 = zoneinfo.get_zonefile_instance()
+
+ self.assertIs(zif_0, zif_1)
+
+ def testZoneInfoNewInstance(self):
+ zif_0 = zoneinfo.get_zonefile_instance()
+ zif_1 = zoneinfo.get_zonefile_instance(new_instance=True)
+ zif_2 = zoneinfo.get_zonefile_instance()
+
+ self.assertIsNot(zif_0, zif_1)
+ self.assertIs(zif_1, zif_2)
+
+ def testZoneInfoDeprecated(self):
+ with self.assertWarns(DeprecationWarning):
+ tzi = zoneinfo.gettz('US/Eastern')
+
+ def testZoneInfoMetadataDeprecated(self):
+ with self.assertWarns(DeprecationWarning):
+ tzdb_md = zoneinfo.gettz_db_metadata()
+
+
+class TZRangeTest(unittest.TestCase, TzFoldMixin):
+ TZ_EST = tz.tzrange('EST', timedelta(hours=-5),
+ 'EDT', timedelta(hours=-4),
+ start=relativedelta(month=3, day=1, hour=2,
+ weekday=SU(+2)),
+ end=relativedelta(month=11, day=1, hour=1,
+ weekday=SU(+1)))
+
+ TZ_AEST = tz.tzrange('AEST', timedelta(hours=10),
+ 'AEDT', timedelta(hours=11),
+ start=relativedelta(month=10, day=1, hour=2,
+ weekday=SU(+1)),
+ end=relativedelta(month=4, day=1, hour=2,
+ weekday=SU(+1)))
+ # POSIX string for UTC
+ UTC = 'UTC'
+
+ def gettz(self, tzname):
+ tzname_map = {'Australia/Sydney': self.TZ_AEST,
+ 'America/Toronto': self.TZ_EST,
+ 'America/New_York': self.TZ_EST}
+
+ return tzname_map[tzname]
+
+ def testRangeCmp1(self):
+ self.assertEqual(tz.tzstr("EST5EDT"),
+ tz.tzrange("EST", -18000, "EDT", -14400,
+ relativedelta(hours=+2,
+ month=4, day=1,
+ weekday=SU(+1)),
+ relativedelta(hours=+1,
+ month=10, day=31,
+ weekday=SU(-1))))
+
+ def testRangeCmp2(self):
+ self.assertEqual(tz.tzstr("EST5EDT"),
+ tz.tzrange("EST", -18000, "EDT"))
+
+ def testRangeOffsets(self):
+ TZR = tz.tzrange('EST', -18000, 'EDT', -14400,
+ start=relativedelta(hours=2, month=4, day=1,
+ weekday=SU(+2)),
+ end=relativedelta(hours=1, month=10, day=31,
+ weekday=SU(-1)))
+
+ dt_std = datetime(2014, 4, 11, 12, 0, tzinfo=TZR) # STD
+ dt_dst = datetime(2016, 4, 11, 12, 0, tzinfo=TZR) # DST
+
+ dst_zero = timedelta(0)
+ dst_hour = timedelta(hours=1)
+
+ std_offset = timedelta(hours=-5)
+ dst_offset = timedelta(hours=-4)
+
+ # Check dst()
+ self.assertEqual(dt_std.dst(), dst_zero)
+ self.assertEqual(dt_dst.dst(), dst_hour)
+
+ # Check utcoffset()
+ self.assertEqual(dt_std.utcoffset(), std_offset)
+ self.assertEqual(dt_dst.utcoffset(), dst_offset)
+
+ # Check tzname
+ self.assertEqual(dt_std.tzname(), 'EST')
+ self.assertEqual(dt_dst.tzname(), 'EDT')
+
+ def testTimeOnlyRangeFixed(self):
+ # This is a fixed-offset zone, so tzrange allows this
+ tz_range = tz.tzrange('dflt', stdoffset=timedelta(hours=-3))
+ self.assertEqual(dt_time(13, 20, tzinfo=tz_range).utcoffset(),
+ timedelta(hours=-3))
+
+ def testTimeOnlyRange(self):
+ # tzrange returns None because this zone has DST
+ tz_range = tz.tzrange('EST', timedelta(hours=-5),
+ 'EDT', timedelta(hours=-4))
+ self.assertIs(dt_time(13, 20, tzinfo=tz_range).utcoffset(), None)
+
+ def testBrokenIsDstHandling(self):
+ # tzrange._isdst() was using a date() rather than a datetime().
+ # Issue reported by Lennart Regebro.
+ dt = datetime(2007, 8, 6, 4, 10, tzinfo=tz.tzutc())
+ self.assertEqual(dt.astimezone(tz=tz.gettz("GMT+2")),
+ datetime(2007, 8, 6, 6, 10, tzinfo=tz.tzstr("GMT+2")))
+
+ def testRangeTimeDelta(self):
+ # Test that tzrange can be specified with a timedelta instead of an int.
+ EST5EDT_td = tz.tzrange('EST', timedelta(hours=-5),
+ 'EDT', timedelta(hours=-4))
+
+ EST5EDT_sec = tz.tzrange('EST', -18000,
+ 'EDT', -14400)
+
+ self.assertEqual(EST5EDT_td, EST5EDT_sec)
+
+ def testRangeEquality(self):
+ TZR1 = tz.tzrange('EST', -18000, 'EDT', -14400)
+
+ # Standard abbreviation different
+ TZR2 = tz.tzrange('ET', -18000, 'EDT', -14400)
+ self.assertNotEqual(TZR1, TZR2)
+
+ # DST abbreviation different
+ TZR3 = tz.tzrange('EST', -18000, 'EMT', -14400)
+ self.assertNotEqual(TZR1, TZR3)
+
+ # STD offset different
+ TZR4 = tz.tzrange('EST', -14000, 'EDT', -14400)
+ self.assertNotEqual(TZR1, TZR4)
+
+ # DST offset different
+ TZR5 = tz.tzrange('EST', -18000, 'EDT', -18000)
+ self.assertNotEqual(TZR1, TZR5)
+
+ # Start delta different
+ TZR6 = tz.tzrange('EST', -18000, 'EDT', -14400,
+ start=relativedelta(hours=+1, month=3,
+ day=1, weekday=SU(+2)))
+ self.assertNotEqual(TZR1, TZR6)
+
+ # End delta different
+ TZR7 = tz.tzrange('EST', -18000, 'EDT', -14400,
+ end=relativedelta(hours=+1, month=11,
+ day=1, weekday=SU(+2)))
+ self.assertNotEqual(TZR1, TZR7)
+
+ def testRangeInequalityUnsupported(self):
+ TZR = tz.tzrange('EST', -18000, 'EDT', -14400)
+
+ self.assertFalse(TZR == 4)
+ self.assertTrue(TZR == ComparesEqual)
+ self.assertFalse(TZR != ComparesEqual)
+
+
+class TZStrTest(unittest.TestCase, TzFoldMixin):
+ # POSIX string indicating change to summer time on the 2nd Sunday in March
+ # at 2AM, and ending the 1st Sunday in November at 2AM. (valid >= 2007)
+ TZ_EST = 'EST+5EDT,M3.2.0/2,M11.1.0/2'
+
+ # POSIX string for AEST/AEDT (valid >= 2008)
+ TZ_AEST = 'AEST-10AEDT,M10.1.0/2,M4.1.0/3'
+
+ def gettz(self, tzname):
+ # Actual time zone changes are handled by the _gettz_context function
+ tzname_map = {'Australia/Sydney': self.TZ_AEST,
+ 'America/Toronto': self.TZ_EST,
+ 'America/New_York': self.TZ_EST}
+
+ return tz.tzstr(tzname_map[tzname])
+
+ def testStrStart1(self):
+ self.assertEqual(datetime(2003, 4, 6, 1, 59,
+ tzinfo=tz.tzstr("EST5EDT")).tzname(), "EST")
+ self.assertEqual(datetime(2003, 4, 6, 2, 00,
+ tzinfo=tz.tzstr("EST5EDT")).tzname(), "EDT")
+
+ def testStrEnd1(self):
+ self.assertEqual(datetime(2003, 10, 26, 0, 59,
+ tzinfo=tz.tzstr("EST5EDT")).tzname(), "EDT")
+
+ end = tz.enfold(datetime(2003, 10, 26, 1, 00,
+ tzinfo=tz.tzstr("EST5EDT")), fold=1)
+ self.assertEqual(end.tzname(), "EST")
+
+ def testStrStart2(self):
+ s = "EST5EDT,4,0,6,7200,10,0,26,7200,3600"
+ self.assertEqual(datetime(2003, 4, 6, 1, 59,
+ tzinfo=tz.tzstr(s)).tzname(), "EST")
+ self.assertEqual(datetime(2003, 4, 6, 2, 00,
+ tzinfo=tz.tzstr(s)).tzname(), "EDT")
+
+ def testStrEnd2(self):
+ s = "EST5EDT,4,0,6,7200,10,0,26,7200,3600"
+ self.assertEqual(datetime(2003, 10, 26, 0, 59,
+ tzinfo=tz.tzstr(s)).tzname(), "EDT")
+
+ end = tz.enfold(datetime(2003, 10, 26, 1, 00,
+ tzinfo=tz.tzstr(s)), fold=1)
+ self.assertEqual(end.tzname(), "EST")
+
+ def testStrStart3(self):
+ s = "EST5EDT,4,1,0,7200,10,-1,0,7200,3600"
+ self.assertEqual(datetime(2003, 4, 6, 1, 59,
+ tzinfo=tz.tzstr(s)).tzname(), "EST")
+ self.assertEqual(datetime(2003, 4, 6, 2, 00,
+ tzinfo=tz.tzstr(s)).tzname(), "EDT")
+
+ def testStrEnd3(self):
+ s = "EST5EDT,4,1,0,7200,10,-1,0,7200,3600"
+ self.assertEqual(datetime(2003, 10, 26, 0, 59,
+ tzinfo=tz.tzstr(s)).tzname(), "EDT")
+
+ end = tz.enfold(datetime(2003, 10, 26, 1, 00,
+ tzinfo=tz.tzstr(s)), fold=1)
+ self.assertEqual(end.tzname(), "EST")
+
+ def testStrStart4(self):
+ s = "EST5EDT4,M4.1.0/02:00:00,M10-5-0/02:00"
+ self.assertEqual(datetime(2003, 4, 6, 1, 59,
+ tzinfo=tz.tzstr(s)).tzname(), "EST")
+ self.assertEqual(datetime(2003, 4, 6, 2, 00,
+ tzinfo=tz.tzstr(s)).tzname(), "EDT")
+
+ def testStrEnd4(self):
+ s = "EST5EDT4,M4.1.0/02:00:00,M10-5-0/02:00"
+ self.assertEqual(datetime(2003, 10, 26, 0, 59,
+ tzinfo=tz.tzstr(s)).tzname(), "EDT")
+ end = tz.enfold(datetime(2003, 10, 26, 1, 00, tzinfo=tz.tzstr(s)),
+ fold=1)
+ self.assertEqual(end.tzname(), "EST")
+
+ def testStrStart5(self):
+ s = "EST5EDT4,95/02:00:00,298/02:00"
+ self.assertEqual(datetime(2003, 4, 6, 1, 59,
+ tzinfo=tz.tzstr(s)).tzname(), "EST")
+ self.assertEqual(datetime(2003, 4, 6, 2, 00,
+ tzinfo=tz.tzstr(s)).tzname(), "EDT")
+
+ def testStrEnd5(self):
+ s = "EST5EDT4,95/02:00:00,298/02"
+ self.assertEqual(datetime(2003, 10, 26, 0, 59,
+ tzinfo=tz.tzstr(s)).tzname(), "EDT")
+ end = tz.enfold(datetime(2003, 10, 26, 1, 00,
+ tzinfo=tz.tzstr(s)), fold=1)
+ self.assertEqual(end.tzname(), "EST")
+
+ def testStrStart6(self):
+ s = "EST5EDT4,J96/02:00:00,J299/02:00"
+ self.assertEqual(datetime(2003, 4, 6, 1, 59,
+ tzinfo=tz.tzstr(s)).tzname(), "EST")
+ self.assertEqual(datetime(2003, 4, 6, 2, 00,
+ tzinfo=tz.tzstr(s)).tzname(), "EDT")
+
+ def testStrEnd6(self):
+ s = "EST5EDT4,J96/02:00:00,J299/02"
+ self.assertEqual(datetime(2003, 10, 26, 0, 59,
+ tzinfo=tz.tzstr(s)).tzname(), "EDT")
+
+ end = tz.enfold(datetime(2003, 10, 26, 1, 00,
+ tzinfo=tz.tzstr(s)), fold=1)
+ self.assertEqual(end.tzname(), "EST")
+
+ def testStrStr(self):
+ # Test that tz.tzstr() won't throw an error if given a str instead
+ # of a unicode literal.
+ self.assertEqual(datetime(2003, 4, 6, 1, 59,
+ tzinfo=tz.tzstr(str("EST5EDT"))).tzname(), "EST")
+ self.assertEqual(datetime(2003, 4, 6, 2, 00,
+ tzinfo=tz.tzstr(str("EST5EDT"))).tzname(), "EDT")
+
+ def testStrCmp1(self):
+ self.assertEqual(tz.tzstr("EST5EDT"),
+ tz.tzstr("EST5EDT4,M4.1.0/02:00:00,M10-5-0/02:00"))
+
+ def testStrCmp2(self):
+ # TODO: This is parsing the default arguments.
+ self.assertEqual(tz.tzstr("EST5EDT"),
+ tz.tzstr("EST5EDT,4,1,0,7200,10,-1,0,7200,3600"))
+
+ def testStrInequality(self):
+ TZS1 = tz.tzstr('EST5EDT4')
+
+ # Standard abbreviation different
+ TZS2 = tz.tzstr('ET5EDT4')
+ self.assertNotEqual(TZS1, TZS2)
+
+ # DST abbreviation different
+ TZS3 = tz.tzstr('EST5EMT')
+ self.assertNotEqual(TZS1, TZS3)
+
+ # STD offset different
+ TZS4 = tz.tzstr('EST4EDT4')
+ self.assertNotEqual(TZS1, TZS4)
+
+ # DST offset different
+ TZS5 = tz.tzstr('EST5EDT3')
+ self.assertNotEqual(TZS1, TZS5)
+
+ def testStrInequalityStartEnd(self):
+ TZS1 = tz.tzstr('EST5EDT4')
+
+ # Start delta different
+ TZS2 = tz.tzstr('EST5EDT4,M4.2.0/02:00:00,M10-5-0/02:00')
+ self.assertNotEqual(TZS1, TZS2)
+
+ # End delta different
+ TZS3 = tz.tzstr('EST5EDT4,M4.2.0/02:00:00,M11-5-0/02:00')
+ self.assertNotEqual(TZS1, TZS3)
+
+ def testPosixOffset(self):
+ TZ1 = tz.tzstr('UTC-3')
+ self.assertEqual(datetime(2015, 1, 1, tzinfo=TZ1).utcoffset(),
+ timedelta(hours=-3))
+
+ TZ2 = tz.tzstr('UTC-3', posix_offset=True)
+ self.assertEqual(datetime(2015, 1, 1, tzinfo=TZ2).utcoffset(),
+ timedelta(hours=+3))
+
+ def testStrInequalityUnsupported(self):
+ TZS = tz.tzstr('EST5EDT')
+
+ self.assertFalse(TZS == 4)
+ self.assertTrue(TZS == ComparesEqual)
+ self.assertFalse(TZS != ComparesEqual)
+
+ def testTzStrRepr(self):
+ TZS1 = tz.tzstr('EST5EDT4')
+ TZS2 = tz.tzstr('EST')
+
+ self.assertEqual(repr(TZS1), "tzstr(" + repr('EST5EDT4') + ")")
+ self.assertEqual(repr(TZS2), "tzstr(" + repr('EST') + ")")
+
+ def testTzStrFailure(self):
+ with self.assertRaises(ValueError):
+ tz.tzstr('InvalidString;439999')
+
+
+class TZICalTest(unittest.TestCase, TzFoldMixin):
+
+ def gettz(self, tzname):
+ TZ_EST = (
+ 'BEGIN:VTIMEZONE',
+ 'TZID:US-Eastern',
+ 'BEGIN:STANDARD',
+ 'DTSTART:19971029T020000',
+ 'RRULE:FREQ=YEARLY;BYDAY=+1SU;BYMONTH=11',
+ 'TZOFFSETFROM:-0400',
+ 'TZOFFSETTO:-0500',
+ 'TZNAME:EST',
+ 'END:STANDARD',
+ 'BEGIN:DAYLIGHT',
+ 'DTSTART:19980301T020000',
+ 'RRULE:FREQ=YEARLY;BYDAY=+2SU;BYMONTH=03',
+ 'TZOFFSETFROM:-0500',
+ 'TZOFFSETTO:-0400',
+ 'TZNAME:EDT',
+ 'END:DAYLIGHT',
+ 'END:VTIMEZONE'
+ )
+
+ TZ_AEST = (
+ 'BEGIN:VTIMEZONE',
+ 'TZID:Australia-Sydney',
+ 'BEGIN:STANDARD',
+ 'DTSTART:19980301T030000',
+ 'RRULE:FREQ=YEARLY;BYDAY=+1SU;BYMONTH=04',
+ 'TZOFFSETFROM:+1100',
+ 'TZOFFSETTO:+1000',
+ 'TZNAME:AEST',
+ 'END:STANDARD',
+ 'BEGIN:DAYLIGHT',
+ 'DTSTART:19971029T020000',
+ 'RRULE:FREQ=YEARLY;BYDAY=+1SU;BYMONTH=10',
+ 'TZOFFSETFROM:+1000',
+ 'TZOFFSETTO:+1100',
+ 'TZNAME:AEDT',
+ 'END:DAYLIGHT',
+ 'END:VTIMEZONE'
+ )
+
+ tzname_map = {'Australia/Sydney': TZ_AEST,
+ 'America/Toronto': TZ_EST,
+ 'America/New_York': TZ_EST}
+
+ tzc = tz.tzical(StringIO('\n'.join(tzname_map[tzname]))).get()
+
+ return tzc
+
+ def testRepr(self):
+ instr = StringIO(TZICAL_PST8PDT)
+ instr.name = 'StringIO(PST8PDT)'
+ tzc = tz.tzical(instr)
+
+ self.assertEqual(repr(tzc), "tzical(" + repr(instr.name) + ")")
+
+
+ # Test performance
+ def _test_us_zone(self, tzc, func, values, start):
+ if start:
+ dt1 = datetime(2003, 4, 6, 1, 59)
+ dt2 = datetime(2003, 4, 6, 2, 00)
+ fold = [0, 0]
+ else:
+ dt1 = datetime(2003, 10, 26, 0, 59)
+ dt2 = datetime(2003, 10, 26, 1, 00)
+ fold = [0, 1]
+
+ dts = (tz.enfold(dt.replace(tzinfo=tzc), fold=f)
+ for dt, f in zip((dt1, dt2), fold))
+
+ for value, dt in zip(values, dts):
+ self.assertEqual(func(dt), value)
+
+ def _test_multi_zones(self, tzstrs, tzids, func, values, start):
+ tzic = tz.tzical(StringIO(''.join(tzstrs)))
+ for tzid, vals in zip(tzids, values):
+ tzc = tzic.get(tzid)
+
+ self._test_us_zone(tzc, func, vals, start)
+
+ def _prepare_EST(self):
+ return tz.tzical(StringIO(TZICAL_EST5EDT)).get()
+
+ def _testEST(self, start, test_type):
+ tzc = self._prepare_EST()
+ argdict = {
+ 'name': (datetime.tzname, ('EST', 'EDT')),
+ 'offset': (datetime.utcoffset, (timedelta(hours=-5),
+ timedelta(hours=-4))),
+ 'dst': (datetime.dst, (timedelta(hours=0),
+ timedelta(hours=1)))
+ }
+
+ func, values = argdict[test_type]
+
+ if not start:
+ values = reversed(values)
+
+ self._test_us_zone(tzc, func, values, start=start)
+
+ def testESTStartName(self):
+ self._testEST(start=True, test_type='name')
+
+ def testESTEndName(self):
+ self._testEST(start=False, test_type='name')
+
+ def testESTStartOffset(self):
+ self._testEST(start=True, test_type='offset')
+
+ def testESTEndOffset(self):
+ self._testEST(start=False, test_type='offset')
+
+ def testESTStartDST(self):
+ self._testEST(start=True, test_type='dst')
+
+ def testESTEndDST(self):
+ self._testEST(start=False, test_type='dst')
+
+ def _testMultizone(self, start, test_type):
+ tzstrs = (TZICAL_EST5EDT, TZICAL_PST8PDT)
+ tzids = ('US-Eastern', 'US-Pacific')
+
+ argdict = {
+ 'name': (datetime.tzname, (('EST', 'EDT'),
+ ('PST', 'PDT'))),
+ 'offset': (datetime.utcoffset, ((timedelta(hours=-5),
+ timedelta(hours=-4)),
+ (timedelta(hours=-8),
+ timedelta(hours=-7)))),
+ 'dst': (datetime.dst, ((timedelta(hours=0),
+ timedelta(hours=1)),
+ (timedelta(hours=0),
+ timedelta(hours=1))))
+ }
+
+ func, values = argdict[test_type]
+
+ if not start:
+ values = map(reversed, values)
+
+ self._test_multi_zones(tzstrs, tzids, func, values, start)
+
+ def testMultiZoneStartName(self):
+ self._testMultizone(start=True, test_type='name')
+
+ def testMultiZoneEndName(self):
+ self._testMultizone(start=False, test_type='name')
+
+ def testMultiZoneStartOffset(self):
+ self._testMultizone(start=True, test_type='offset')
+
+ def testMultiZoneEndOffset(self):
+ self._testMultizone(start=False, test_type='offset')
+
+ def testMultiZoneStartDST(self):
+ self._testMultizone(start=True, test_type='dst')
+
+ def testMultiZoneEndDST(self):
+ self._testMultizone(start=False, test_type='dst')
+
+ def testMultiZoneKeys(self):
+ tzic = tz.tzical(StringIO(''.join((TZICAL_EST5EDT, TZICAL_PST8PDT))))
+
+ # Sort keys because they are in a random order, being dictionary keys
+ keys = sorted(tzic.keys())
+
+ self.assertEqual(keys, ['US-Eastern', 'US-Pacific'])
+
+ # Test error conditions
+ def testEmptyString(self):
+ with self.assertRaises(ValueError):
+ tz.tzical(StringIO(""))
+
+ def testMultiZoneGet(self):
+ tzic = tz.tzical(StringIO(TZICAL_EST5EDT + TZICAL_PST8PDT))
+
+ with self.assertRaises(ValueError):
+ tzic.get()
+
+ # Test Parsing
+ def testGap(self):
+ tzic = tz.tzical(StringIO('\n'.join((TZICAL_EST5EDT, TZICAL_PST8PDT))))
+
+ keys = sorted(tzic.keys())
+ self.assertEqual(keys, ['US-Eastern', 'US-Pacific'])
+
+
+class TZTest(unittest.TestCase):
+ def testFileStart1(self):
+ tzc = tz.tzfile(BytesIO(base64.b64decode(TZFILE_EST5EDT)))
+ self.assertEqual(datetime(2003, 4, 6, 1, 59, tzinfo=tzc).tzname(), "EST")
+ self.assertEqual(datetime(2003, 4, 6, 2, 00, tzinfo=tzc).tzname(), "EDT")
+
+ def testFileEnd1(self):
+ tzc = tz.tzfile(BytesIO(base64.b64decode(TZFILE_EST5EDT)))
+ self.assertEqual(datetime(2003, 10, 26, 0, 59, tzinfo=tzc).tzname(),
+ "EDT")
+ end_est = tz.enfold(datetime(2003, 10, 26, 1, 00, tzinfo=tzc))
+ self.assertEqual(end_est.tzname(), "EST")
+
+ def testFileLastTransition(self):
+ # After the last transition, it goes to standard time in perpetuity
+ tzc = tz.tzfile(BytesIO(base64.b64decode(TZFILE_EST5EDT)))
+ self.assertEqual(datetime(2037, 10, 25, 0, 59, tzinfo=tzc).tzname(),
+ "EDT")
+
+ last_date = tz.enfold(datetime(2037, 10, 25, 1, 00, tzinfo=tzc), fold=1)
+ self.assertEqual(last_date.tzname(),
+ "EST")
+
+ self.assertEqual(datetime(2038, 5, 25, 12, 0, tzinfo=tzc).tzname(),
+ "EST")
+
+ def testInvalidFile(self):
+ # Should throw a ValueError if an invalid file is passed
+ with self.assertRaises(ValueError):
+ tz.tzfile(BytesIO(b'BadFile'))
+
+ def testRoundNonFullMinutes(self):
+ # This timezone has an offset of 5992 seconds in 1900-01-01.
+ tzc = tz.tzfile(BytesIO(base64.b64decode(EUROPE_HELSINKI)))
+ self.assertEqual(str(datetime(1900, 1, 1, 0, 0, tzinfo=tzc)),
+ "1900-01-01 00:00:00+01:40")
+
+ def testLeapCountDecodesProperly(self):
+ # This timezone has leapcnt, and failed to decode until
+ # Eugene Oden notified about the issue.
+
+ # As leap information is currently unused (and unstored) by tzfile() we
+ # can only indirectly test this: Take advantage of tzfile() not closing
+ # the input file if handed in as an opened file and assert that the
+ # full file content has been read by tzfile(). Note: For this test to
+ # work NEW_YORK must be in TZif version 1 format i.e. no more data
+ # after TZif v1 header + data has been read
+ fileobj = BytesIO(base64.b64decode(NEW_YORK))
+ tzc = tz.tzfile(fileobj)
+ # we expect no remaining file content now, i.e. zero-length; if there's
+ # still data we haven't read the file format correctly
+ remaining_tzfile_content = fileobj.read()
+ self.assertEqual(len(remaining_tzfile_content), 0)
+
+ def testIsStd(self):
+ # NEW_YORK tzfile contains this isstd information:
+ isstd_expected = (0, 0, 0, 1)
+ tzc = tz.tzfile(BytesIO(base64.b64decode(NEW_YORK)))
+ # gather the actual information as parsed by the tzfile class
+ isstd = []
+ for ttinfo in tzc._ttinfo_list:
+ # ttinfo objects contain boolean values
+ isstd.append(int(ttinfo.isstd))
+ # ttinfo list may contain more entries than isstd file content
+ isstd = tuple(isstd[:len(isstd_expected)])
+ self.assertEqual(
+ isstd_expected, isstd,
+ "isstd UTC/local indicators parsed: %s != tzfile contents: %s"
+ % (isstd, isstd_expected))
+
+ def testGMTHasNoDaylight(self):
+ # tz.tzstr("GMT+2") improperly considered daylight saving time.
+ # Issue reported by Lennart Regebro.
+ dt = datetime(2007, 8, 6, 4, 10)
+ self.assertEqual(tz.gettz("GMT+2").dst(dt), timedelta(0))
+
+ def testGMTOffset(self):
+ # GMT and UTC offsets have inverted signal when compared to the
+ # usual TZ variable handling.
+ dt = datetime(2007, 8, 6, 4, 10, tzinfo=tz.tzutc())
+ self.assertEqual(dt.astimezone(tz=tz.tzstr("GMT+2")),
+ datetime(2007, 8, 6, 6, 10, tzinfo=tz.tzstr("GMT+2")))
+ self.assertEqual(dt.astimezone(tz=tz.gettz("UTC-2")),
+ datetime(2007, 8, 6, 2, 10, tzinfo=tz.tzstr("UTC-2")))
+
+ @unittest.skipIf(IS_WIN, "requires Unix")
+ @unittest.skipUnless(TZEnvContext.tz_change_allowed(),
+ TZEnvContext.tz_change_disallowed_message())
+ def testTZSetDoesntCorrupt(self):
+ # if we start in non-UTC then tzset UTC make sure parse doesn't get
+ # confused
+ with TZEnvContext('UTC'):
+ # this should parse to UTC timezone not the original timezone
+ dt = parse('2014-07-20T12:34:56+00:00')
+ self.assertEqual(str(dt), '2014-07-20 12:34:56+00:00')
+
+
[email protected](IS_WIN, "Requires Windows")
+class TzWinTest(unittest.TestCase, TzWinFoldMixin):
+ def setUp(self):
+ self.tzclass = tzwin.tzwin
+
+ def testTzResLoadName(self):
+ # This may not work right on non-US locales.
+ tzr = tzwin.tzres()
+ self.assertEqual(tzr.load_name(112), "Eastern Standard Time")
+
+ def testTzResNameFromString(self):
+ tzr = tzwin.tzres()
+ self.assertEqual(tzr.name_from_string('@tzres.dll,-221'),
+ 'Alaskan Daylight Time')
+
+ self.assertEqual(tzr.name_from_string('Samoa Daylight Time'),
+ 'Samoa Daylight Time')
+
+ with self.assertRaises(ValueError):
+ tzr.name_from_string('@tzres.dll,100')
+
+ def testIsdstZoneWithNoDaylightSaving(self):
+ tz = tzwin.tzwin("UTC")
+ dt = parse("2013-03-06 19:08:15")
+ self.assertFalse(tz._isdst(dt))
+
+ def testOffset(self):
+ tz = tzwin.tzwin("Cape Verde Standard Time")
+ self.assertEqual(tz.utcoffset(datetime(1995, 5, 21, 12, 9, 13)),
+ timedelta(-1, 82800))
+
+ def testTzwinName(self):
+ # https://github.com/dateutil/dateutil/issues/143
+ tw = tz.tzwin('Eastern Standard Time')
+
+ # Cover the transitions for at least two years.
+ ESTs = 'Eastern Standard Time'
+ EDTs = 'Eastern Daylight Time'
+ transition_dates = [(datetime(2015, 3, 8, 0, 59), ESTs),
+ (datetime(2015, 3, 8, 3, 1), EDTs),
+ (datetime(2015, 11, 1, 0, 59), EDTs),
+ (datetime(2015, 11, 1, 3, 1), ESTs),
+ (datetime(2016, 3, 13, 0, 59), ESTs),
+ (datetime(2016, 3, 13, 3, 1), EDTs),
+ (datetime(2016, 11, 6, 0, 59), EDTs),
+ (datetime(2016, 11, 6, 3, 1), ESTs)]
+
+ for t_date, expected in transition_dates:
+ self.assertEqual(t_date.replace(tzinfo=tw).tzname(), expected)
+
+ def testTzwinRepr(self):
+ tw = tz.tzwin('Yakutsk Standard Time')
+ self.assertEqual(repr(tw), 'tzwin(' +
+ repr('Yakutsk Standard Time') + ')')
+
+ def testTzWinEquality(self):
+ # https://github.com/dateutil/dateutil/issues/151
+ tzwin_names = ('Eastern Standard Time',
+ 'West Pacific Standard Time',
+ 'Yakutsk Standard Time',
+ 'Iran Standard Time',
+ 'UTC')
+
+ for tzwin_name in tzwin_names:
+ # Get two different instances to compare
+ tw1 = tz.tzwin(tzwin_name)
+ tw2 = tz.tzwin(tzwin_name)
+
+ self.assertEqual(tw1, tw2)
+
+ def testTzWinInequality(self):
+ # https://github.com/dateutil/dateutil/issues/151
+ # Note these last two currently differ only in their name.
+ tzwin_names = (('Eastern Standard Time', 'Yakutsk Standard Time'),
+ ('Greenwich Standard Time', 'GMT Standard Time'),
+ ('GMT Standard Time', 'UTC'),
+ ('E. South America Standard Time',
+ 'Argentina Standard Time'))
+
+ for tzwn1, tzwn2 in tzwin_names:
+ # Get two different instances to compare
+ tw1 = tz.tzwin(tzwn1)
+ tw2 = tz.tzwin(tzwn2)
+
+ self.assertNotEqual(tw1, tw2)
+
+ def testTzWinEqualityInvalid(self):
+ # Compare to objects that do not implement comparison with this
+ # (should default to False)
+ UTC = tz.tzutc()
+ EST = tz.tzwin('Eastern Standard Time')
+
+ self.assertFalse(EST == UTC)
+ self.assertFalse(EST == 1)
+ self.assertFalse(UTC == EST)
+
+ self.assertTrue(EST != UTC)
+ self.assertTrue(EST != 1)
+
+ def testTzWinInequalityUnsupported(self):
+ # Compare it to an object that is promiscuous about equality, but for
+ # which tzwin does not implement an equality operator.
+ EST = tz.tzwin('Eastern Standard Time')
+ self.assertTrue(EST == ComparesEqual)
+ self.assertFalse(EST != ComparesEqual)
+
+ def testTzwinTimeOnlyDST(self):
+ # For zones with DST, .dst() should return None
+ tw_est = tz.tzwin('Eastern Standard Time')
+ self.assertIs(dt_time(14, 10, tzinfo=tw_est).dst(), None)
+
+ # This zone has no DST, so .dst() can return 0
+ tw_sast = tz.tzwin('South Africa Standard Time')
+ self.assertEqual(dt_time(14, 10, tzinfo=tw_sast).dst(),
+ timedelta(0))
+
+ def testTzwinTimeOnlyUTCOffset(self):
+ # For zones with DST, .utcoffset() should return None
+ tw_est = tz.tzwin('Eastern Standard Time')
+ self.assertIs(dt_time(14, 10, tzinfo=tw_est).utcoffset(), None)
+
+ # This zone has no DST, so .utcoffset() returns standard offset
+ tw_sast = tz.tzwin('South Africa Standard Time')
+ self.assertEqual(dt_time(14, 10, tzinfo=tw_sast).utcoffset(),
+ timedelta(hours=2))
+
+ def testTzwinTimeOnlyTZName(self):
+ # For zones with DST, the name defaults to standard time
+ tw_est = tz.tzwin('Eastern Standard Time')
+ self.assertEqual(dt_time(14, 10, tzinfo=tw_est).tzname(),
+ 'Eastern Standard Time')
+
+ # For zones with no DST, this should work normally.
+ tw_sast = tz.tzwin('South Africa Standard Time')
+ self.assertEqual(dt_time(14, 10, tzinfo=tw_sast).tzname(),
+ 'South Africa Standard Time')
+
+
[email protected](IS_WIN, "Requires Windows")
[email protected](TZWinContext.tz_change_allowed(),
+ TZWinContext.tz_change_disallowed_message())
+class TzWinLocalTest(unittest.TestCase, TzWinFoldMixin):
+
+ def setUp(self):
+ self.tzclass = tzwin.tzwinlocal
+ self.context = TZWinContext
+
+ def get_args(self, tzname):
+ return tuple()
+
+ def testLocal(self):
+ # Not sure how to pin a local time zone, so for now we're just going
+ # to run this and make sure it doesn't raise an error
+ # See Github Issue #135: https://github.com/dateutil/dateutil/issues/135
+ datetime.now(tzwin.tzwinlocal())
+
+ def testTzwinLocalUTCOffset(self):
+ with TZWinContext('Eastern Standard Time'):
+ tzwl = tzwin.tzwinlocal()
+ self.assertEqual(datetime(2014, 3, 11, tzinfo=tzwl).utcoffset(),
+ timedelta(hours=-4))
+
+ def testTzwinLocalName(self):
+ # https://github.com/dateutil/dateutil/issues/143
+ ESTs = 'Eastern Standard Time'
+ EDTs = 'Eastern Daylight Time'
+ transition_dates = [(datetime(2015, 3, 8, 0, 59), ESTs),
+ (datetime(2015, 3, 8, 3, 1), EDTs),
+ (datetime(2015, 11, 1, 0, 59), EDTs),
+ (datetime(2015, 11, 1, 3, 1), ESTs),
+ (datetime(2016, 3, 13, 0, 59), ESTs),
+ (datetime(2016, 3, 13, 3, 1), EDTs),
+ (datetime(2016, 11, 6, 0, 59), EDTs),
+ (datetime(2016, 11, 6, 3, 1), ESTs)]
+
+ with TZWinContext('Eastern Standard Time'):
+ tw = tz.tzwinlocal()
+
+ for t_date, expected in transition_dates:
+ self.assertEqual(t_date.replace(tzinfo=tw).tzname(), expected)
+
+ def testTzWinLocalRepr(self):
+ tw = tz.tzwinlocal()
+ self.assertEqual(repr(tw), 'tzwinlocal()')
+
+ def testTzwinLocalRepr(self):
+ # https://github.com/dateutil/dateutil/issues/143
+ with TZWinContext('Eastern Standard Time'):
+ tw = tz.tzwinlocal()
+
+ self.assertEqual(str(tw), 'tzwinlocal(' +
+ repr('Eastern Standard Time') + ')')
+
+ with TZWinContext('Pacific Standard Time'):
+ tw = tz.tzwinlocal()
+
+ self.assertEqual(str(tw), 'tzwinlocal(' +
+ repr('Pacific Standard Time') + ')')
+
+ def testTzwinLocalEquality(self):
+ tw_est = tz.tzwin('Eastern Standard Time')
+ tw_pst = tz.tzwin('Pacific Standard Time')
+
+ with TZWinContext('Eastern Standard Time'):
+ twl1 = tz.tzwinlocal()
+ twl2 = tz.tzwinlocal()
+
+ self.assertEqual(twl1, twl2)
+ self.assertEqual(twl1, tw_est)
+ self.assertNotEqual(twl1, tw_pst)
+
+ with TZWinContext('Pacific Standard Time'):
+ twl1 = tz.tzwinlocal()
+ twl2 = tz.tzwinlocal()
+ tw = tz.tzwin('Pacific Standard Time')
+
+ self.assertEqual(twl1, twl2)
+ self.assertEqual(twl1, tw)
+ self.assertEqual(twl1, tw_pst)
+ self.assertNotEqual(twl1, tw_est)
+
+ def testTzwinLocalTimeOnlyDST(self):
+ # For zones with DST, .dst() should return None
+ with TZWinContext('Eastern Standard Time'):
+ twl = tz.tzwinlocal()
+ self.assertIs(dt_time(14, 10, tzinfo=twl).dst(), None)
+
+ # This zone has no DST, so .dst() can return 0
+ with TZWinContext('South Africa Standard Time'):
+ twl = tz.tzwinlocal()
+ self.assertEqual(dt_time(14, 10, tzinfo=twl).dst(), timedelta(0))
+
+ def testTzwinLocalTimeOnlyUTCOffset(self):
+ # For zones with DST, .utcoffset() should return None
+ with TZWinContext('Eastern Standard Time'):
+ twl = tz.tzwinlocal()
+ self.assertIs(dt_time(14, 10, tzinfo=twl).utcoffset(), None)
+
+ # This zone has no DST, so .utcoffset() returns standard offset
+ with TZWinContext('South Africa Standard Time'):
+ twl = tz.tzwinlocal()
+ self.assertEqual(dt_time(14, 10, tzinfo=twl).utcoffset(),
+ timedelta(hours=2))
+
+ def testTzwinLocalTimeOnlyTZName(self):
+ # For zones with DST, the name defaults to standard time
+ with TZWinContext('Eastern Standard Time'):
+ twl = tz.tzwinlocal()
+ self.assertEqual(dt_time(14, 10, tzinfo=twl).tzname(),
+ 'Eastern Standard Time')
+
+ # For zones with no DST, this should work normally.
+ with TZWinContext('South Africa Standard Time'):
+ twl = tz.tzwinlocal()
+ self.assertEqual(dt_time(14, 10, tzinfo=twl).tzname(),
+ 'South Africa Standard Time')
+
+
+class TzPickleTest(PicklableMixin, unittest.TestCase):
+ _asfile = False
+
+ def setUp(self):
+ self.assertPicklable = partial(self.assertPicklable,
+ asfile=self._asfile)
+
+ def testPickleTzUTC(self):
+ self.assertPicklable(tz.tzutc())
+
+ def testPickleTzOffsetZero(self):
+ self.assertPicklable(tz.tzoffset('UTC', 0))
+
+ def testPickleTzOffsetPos(self):
+ self.assertPicklable(tz.tzoffset('UTC+1', 3600))
+
+ def testPickleTzOffsetNeg(self):
+ self.assertPicklable(tz.tzoffset('UTC-1', -3600))
+
+ def testPickleTzLocal(self):
+ self.assertPicklable(tz.tzlocal())
+
+ def testPickleTzFileEST5EDT(self):
+ tzc = tz.tzfile(BytesIO(base64.b64decode(TZFILE_EST5EDT)))
+ self.assertPicklable(tzc)
+
+ def testPickleTzFileEurope_Helsinki(self):
+ tzc = tz.tzfile(BytesIO(base64.b64decode(EUROPE_HELSINKI)))
+ self.assertPicklable(tzc)
+
+ def testPickleTzFileNew_York(self):
+ tzc = tz.tzfile(BytesIO(base64.b64decode(NEW_YORK)))
+ self.assertPicklable(tzc)
+
+ @unittest.skip("Known failure")
+ def testPickleTzICal(self):
+ tzc = tz.tzical(StringIO(TZICAL_EST5EDT)).get()
+ self.assertPicklable(tzc)
+
+ def testPickleTzGettz(self):
+ self.assertPicklable(tz.gettz('America/New_York'))
+
+ def testPickleZoneFileGettz(self):
+ zoneinfo_file = zoneinfo.get_zonefile_instance()
+ tzi = zoneinfo_file.get('America/New_York')
+ self.assertIsNot(tzi, None)
+ self.assertPicklable(tzi)
+
+
+class TzPickleFileTest(TzPickleTest):
+ """ Run all the TzPickleTest tests, using a temporary file """
+ _asfile = True
+
+
+class DatetimeAmbiguousTest(unittest.TestCase):
+ """ Test the datetime_exists / datetime_ambiguous functions """
+
+ def testNoTzSpecified(self):
+ with self.assertRaises(ValueError):
+ tz.datetime_ambiguous(datetime(2016, 4, 1, 2, 9))
+
+ def _get_no_support_tzinfo_class(self, dt_start, dt_end, dst_only=False):
+ # Generates a class of tzinfo with no support for is_ambiguous
+ # where dates between dt_start and dt_end are ambiguous.
+
+ class FoldingTzInfo(tzinfo):
+ def utcoffset(self, dt):
+ if not dst_only:
+ dt_n = dt.replace(tzinfo=None)
+
+ if dt_start <= dt_n < dt_end and getattr(dt_n, 'fold', 0):
+ return timedelta(hours=-1)
+
+ return timedelta(hours=0)
+
+ def dst(self, dt):
+ dt_n = dt.replace(tzinfo=None)
+
+ if dt_start <= dt_n < dt_end and getattr(dt_n, 'fold', 0):
+ return timedelta(hours=1)
+ else:
+ return timedelta(0)
+
+ return FoldingTzInfo
+
+ def _get_no_support_tzinfo(self, dt_start, dt_end, dst_only=False):
+ return self._get_no_support_tzinfo_class(dt_start, dt_end, dst_only)()
+
+ def testNoSupportAmbiguityFoldNaive(self):
+ dt_start = datetime(2018, 9, 1, 1, 0)
+ dt_end = datetime(2018, 9, 1, 2, 0)
+
+ tzi = self._get_no_support_tzinfo(dt_start, dt_end)
+
+ self.assertTrue(tz.datetime_ambiguous(datetime(2018, 9, 1, 1, 30),
+ tz=tzi))
+
+ def testNoSupportAmbiguityFoldAware(self):
+ dt_start = datetime(2018, 9, 1, 1, 0)
+ dt_end = datetime(2018, 9, 1, 2, 0)
+
+ tzi = self._get_no_support_tzinfo(dt_start, dt_end)
+
+ self.assertTrue(tz.datetime_ambiguous(datetime(2018, 9, 1, 1, 30,
+ tzinfo=tzi)))
+
+ def testNoSupportAmbiguityUnambiguousNaive(self):
+ dt_start = datetime(2018, 9, 1, 1, 0)
+ dt_end = datetime(2018, 9, 1, 2, 0)
+
+ tzi = self._get_no_support_tzinfo(dt_start, dt_end)
+
+ self.assertFalse(tz.datetime_ambiguous(datetime(2018, 10, 1, 12, 30),
+ tz=tzi))
+
+ def testNoSupportAmbiguityUnambiguousAware(self):
+ dt_start = datetime(2018, 9, 1, 1, 0)
+ dt_end = datetime(2018, 9, 1, 2, 0)
+
+ tzi = self._get_no_support_tzinfo(dt_start, dt_end)
+
+ self.assertFalse(tz.datetime_ambiguous(datetime(2018, 10, 1, 12, 30,
+ tzinfo=tzi)))
+
+ def testNoSupportAmbiguityFoldDSTOnly(self):
+ dt_start = datetime(2018, 9, 1, 1, 0)
+ dt_end = datetime(2018, 9, 1, 2, 0)
+
+ tzi = self._get_no_support_tzinfo(dt_start, dt_end, dst_only=True)
+
+ self.assertTrue(tz.datetime_ambiguous(datetime(2018, 9, 1, 1, 30),
+ tz=tzi))
+
+ def testNoSupportAmbiguityUnambiguousDSTOnly(self):
+ dt_start = datetime(2018, 9, 1, 1, 0)
+ dt_end = datetime(2018, 9, 1, 2, 0)
+
+ tzi = self._get_no_support_tzinfo(dt_start, dt_end, dst_only=True)
+
+ self.assertFalse(tz.datetime_ambiguous(datetime(2018, 10, 1, 12, 30),
+ tz=tzi))
+
+ def testSupportAmbiguityFoldNaive(self):
+ tzi = tz.gettz('US/Eastern')
+
+ dt = datetime(2011, 11, 6, 1, 30)
+
+ self.assertTrue(tz.datetime_ambiguous(dt, tz=tzi))
+
+ def testSupportAmbiguityFoldAware(self):
+ tzi = tz.gettz('US/Eastern')
+
+ dt = datetime(2011, 11, 6, 1, 30, tzinfo=tzi)
+
+ self.assertTrue(tz.datetime_ambiguous(dt))
+
+ def testSupportAmbiguityUnambiguousAware(self):
+ tzi = tz.gettz('US/Eastern')
+
+ dt = datetime(2011, 11, 6, 4, 30)
+
+ self.assertFalse(tz.datetime_ambiguous(dt, tz=tzi))
+
+ def testSupportAmbiguityUnambiguousNaive(self):
+ tzi = tz.gettz('US/Eastern')
+
+ dt = datetime(2011, 11, 6, 4, 30, tzinfo=tzi)
+
+ self.assertFalse(tz.datetime_ambiguous(dt))
+
+ def _get_ambig_error_tzinfo(self, dt_start, dt_end, dst_only=False):
+ cTzInfo = self._get_no_support_tzinfo_class(dt_start, dt_end, dst_only)
+
+ # Takes the wrong number of arguments and raises an error anyway.
+ class FoldTzInfoRaises(cTzInfo):
+ def is_ambiguous(self, dt, other_arg):
+ raise NotImplementedError('This is not implemented')
+
+ return FoldTzInfoRaises()
+
+ def testIncompatibleAmbiguityFoldNaive(self):
+ dt_start = datetime(2018, 9, 1, 1, 0)
+ dt_end = datetime(2018, 9, 1, 2, 0)
+
+ tzi = self._get_ambig_error_tzinfo(dt_start, dt_end)
+
+ self.assertTrue(tz.datetime_ambiguous(datetime(2018, 9, 1, 1, 30),
+ tz=tzi))
+
+ def testIncompatibleAmbiguityFoldAware(self):
+ dt_start = datetime(2018, 9, 1, 1, 0)
+ dt_end = datetime(2018, 9, 1, 2, 0)
+
+ tzi = self._get_ambig_error_tzinfo(dt_start, dt_end)
+
+ self.assertTrue(tz.datetime_ambiguous(datetime(2018, 9, 1, 1, 30,
+ tzinfo=tzi)))
+
+ def testIncompatibleAmbiguityUnambiguousNaive(self):
+ dt_start = datetime(2018, 9, 1, 1, 0)
+ dt_end = datetime(2018, 9, 1, 2, 0)
+
+ tzi = self._get_ambig_error_tzinfo(dt_start, dt_end)
+
+ self.assertFalse(tz.datetime_ambiguous(datetime(2018, 10, 1, 12, 30),
+ tz=tzi))
+
+ def testIncompatibleAmbiguityUnambiguousAware(self):
+ dt_start = datetime(2018, 9, 1, 1, 0)
+ dt_end = datetime(2018, 9, 1, 2, 0)
+
+ tzi = self._get_ambig_error_tzinfo(dt_start, dt_end)
+
+ self.assertFalse(tz.datetime_ambiguous(datetime(2018, 10, 1, 12, 30,
+ tzinfo=tzi)))
+
+ def testIncompatibleAmbiguityFoldDSTOnly(self):
+ dt_start = datetime(2018, 9, 1, 1, 0)
+ dt_end = datetime(2018, 9, 1, 2, 0)
+
+ tzi = self._get_ambig_error_tzinfo(dt_start, dt_end, dst_only=True)
+
+ self.assertTrue(tz.datetime_ambiguous(datetime(2018, 9, 1, 1, 30),
+ tz=tzi))
+
+ def testIncompatibleAmbiguityUnambiguousDSTOnly(self):
+ dt_start = datetime(2018, 9, 1, 1, 0)
+ dt_end = datetime(2018, 9, 1, 2, 0)
+
+ tzi = self._get_ambig_error_tzinfo(dt_start, dt_end, dst_only=True)
+
+ self.assertFalse(tz.datetime_ambiguous(datetime(2018, 10, 1, 12, 30),
+ tz=tzi))
+
+ def testSpecifiedTzOverridesAttached(self):
+ # If a tz is specified, the datetime will be treated as naive.
+
+ # This is not ambiguous in the local zone
+ dt = datetime(2011, 11, 6, 1, 30, tzinfo=tz.gettz('Australia/Sydney'))
+
+ self.assertFalse(tz.datetime_ambiguous(dt))
+
+ tzi = tz.gettz('US/Eastern')
+ self.assertTrue(tz.datetime_ambiguous(dt, tz=tzi))
+
+
+class DatetimeExistsTest(unittest.TestCase):
+ def testNoTzSpecified(self):
+ with self.assertRaises(ValueError):
+ tz.datetime_exists(datetime(2016, 4, 1, 2, 9))
+
+ def testInGapNaive(self):
+ tzi = tz.gettz('Australia/Sydney')
+
+ dt = datetime(2012, 10, 7, 2, 30)
+
+ self.assertFalse(tz.datetime_exists(dt, tz=tzi))
+
+ def testInGapAware(self):
+ tzi = tz.gettz('Australia/Sydney')
+
+ dt = datetime(2012, 10, 7, 2, 30, tzinfo=tzi)
+
+ self.assertFalse(tz.datetime_exists(dt))
+
+ def testExistsNaive(self):
+ tzi = tz.gettz('Australia/Sydney')
+
+ dt = datetime(2012, 10, 7, 10, 30)
+
+ self.assertTrue(tz.datetime_exists(dt, tz=tzi))
+
+ def testExistsAware(self):
+ tzi = tz.gettz('Australia/Sydney')
+
+ dt = datetime(2012, 10, 7, 10, 30, tzinfo=tzi)
+
+ self.assertTrue(tz.datetime_exists(dt))
+
+ def testSpecifiedTzOverridesAttached(self):
+ EST = tz.gettz('US/Eastern')
+ AEST = tz.gettz('Australia/Sydney')
+
+ dt = datetime(2012, 10, 7, 2, 30, tzinfo=EST) # This time exists
+
+ self.assertFalse(tz.datetime_exists(dt, tz=AEST))
+
+
+class EnfoldTest(unittest.TestCase):
+ def testEnterFoldDefault(self):
+ dt = tz.enfold(datetime(2020, 1, 19, 3, 32))
+
+ self.assertEqual(dt.fold, 1)
+
+ def testEnterFold(self):
+ dt = tz.enfold(datetime(2020, 1, 19, 3, 32), fold=1)
+
+ self.assertEqual(dt.fold, 1)
+
+ def testExitFold(self):
+ dt = tz.enfold(datetime(2020, 1, 19, 3, 32), fold=0)
+
+ # Before Python 3.6, dt.fold won't exist if fold is 0.
+ self.assertEqual(getattr(dt, 'fold', 0), 0)
diff --git a/libs/dateutil/tz/__init__.py b/libs/dateutil/tz/__init__.py
index 5a2d9cd6e..1cba7b9e9 100644
--- a/libs/dateutil/tz/__init__.py
+++ b/libs/dateutil/tz/__init__.py
@@ -1,17 +1,4 @@
-# -*- coding: utf-8 -*-
from .tz import *
-from .tz import __doc__
-
-#: Convenience constant providing a :class:`tzutc()` instance
-#:
-#: .. versionadded:: 2.7.0
-UTC = tzutc()
__all__ = ["tzutc", "tzoffset", "tzlocal", "tzfile", "tzrange",
- "tzstr", "tzical", "tzwin", "tzwinlocal", "gettz",
- "enfold", "datetime_ambiguous", "datetime_exists",
- "resolve_imaginary", "UTC", "DeprecatedTzFormatWarning"]
-
-
-class DeprecatedTzFormatWarning(Warning):
- """Warning raised when time zones are parsed from deprecated formats."""
+ "tzstr", "tzical", "tzwin", "tzwinlocal", "gettz"]
diff --git a/libs/dateutil/tz/_common.py b/libs/dateutil/tz/_common.py
index ccabb7da6..212e8ce95 100644
--- a/libs/dateutil/tz/_common.py
+++ b/libs/dateutil/tz/_common.py
@@ -1,9 +1,8 @@
from six import PY3
-
-from functools import wraps
+from six.moves import _thread
from datetime import datetime, timedelta, tzinfo
-
+import copy
ZERO = timedelta(0)
@@ -46,7 +45,7 @@ if hasattr(datetime, 'fold'):
subclass of :py:class:`datetime.datetime` with the ``fold``
attribute added, if ``fold`` is 1.
- .. versionadded:: 2.6.0
+ ..versionadded:: 2.6.0
"""
return dt.replace(fold=fold)
@@ -57,40 +56,10 @@ else:
Python versions before 3.6. It is used only for dates in a fold, so
the ``fold`` attribute is fixed at ``1``.
- .. versionadded:: 2.6.0
+ ..versionadded:: 2.6.0
"""
__slots__ = ()
- def replace(self, *args, **kwargs):
- """
- Return a datetime with the same attributes, except for those
- attributes given new values by whichever keyword arguments are
- specified. Note that tzinfo=None can be specified to create a naive
- datetime from an aware datetime with no conversion of date and time
- data.
-
- This is reimplemented in ``_DatetimeWithFold`` because pypy3 will
- return a ``datetime.datetime`` even if ``fold`` is unchanged.
- """
- argnames = (
- 'year', 'month', 'day', 'hour', 'minute', 'second',
- 'microsecond', 'tzinfo'
- )
-
- for arg, argname in zip(args, argnames):
- if argname in kwargs:
- raise TypeError('Duplicate argument: {}'.format(argname))
-
- kwargs[argname] = arg
-
- for argname in argnames:
- if argname not in kwargs:
- kwargs[argname] = getattr(self, argname)
-
- dt_class = self.__class__ if kwargs.get('fold', 1) else datetime
-
- return dt_class(**kwargs)
-
@property
def fold(self):
return 1
@@ -111,7 +80,7 @@ else:
subclass of :py:class:`datetime.datetime` with the ``fold``
attribute added, if ``fold`` is 1.
- .. versionadded:: 2.6.0
+ ..versionadded:: 2.6.0
"""
if getattr(dt, 'fold', 0) == fold:
return dt
@@ -125,23 +94,6 @@ else:
return datetime(*args)
-def _validate_fromutc_inputs(f):
- """
- The CPython version of ``fromutc`` checks that the input is a ``datetime``
- object and that ``self`` is attached as its ``tzinfo``.
- """
- @wraps(f)
- def fromutc(self, dt):
- if not isinstance(dt, datetime):
- raise TypeError("fromutc() requires a datetime argument")
- if dt.tzinfo is not self:
- raise ValueError("dt.tzinfo is not self")
-
- return f(self, dt)
-
- return fromutc
-
-
class _tzinfo(tzinfo):
"""
Base class for all ``dateutil`` ``tzinfo`` objects.
@@ -159,7 +111,7 @@ class _tzinfo(tzinfo):
:return:
Returns ``True`` if ambiguous, ``False`` otherwise.
- .. versionadded:: 2.6.0
+ ..versionadded:: 2.6.0
"""
dt = dt.replace(tzinfo=self)
@@ -169,7 +121,7 @@ class _tzinfo(tzinfo):
same_offset = wall_0.utcoffset() == wall_1.utcoffset()
same_dt = wall_0.replace(tzinfo=None) == wall_1.replace(tzinfo=None)
-
+
return same_dt and not same_offset
def _fold_status(self, dt_utc, dt_wall):
@@ -211,10 +163,15 @@ class _tzinfo(tzinfo):
occurence, chronologically, of the ambiguous datetime).
:param dt:
- A timezone-aware :class:`datetime.datetime` object.
+ A timezone-aware :class:`datetime.dateime` object.
"""
# Re-implement the algorithm from Python's datetime.py
+ if not isinstance(dt, datetime):
+ raise TypeError("fromutc() requires a datetime argument")
+ if dt.tzinfo is not self:
+ raise ValueError("dt.tzinfo is not self")
+
dtoff = dt.utcoffset()
if dtoff is None:
raise ValueError("fromutc() requires a non-None utcoffset() "
@@ -227,17 +184,16 @@ class _tzinfo(tzinfo):
if dtdst is None:
raise ValueError("fromutc() requires a non-None dst() result")
delta = dtoff - dtdst
-
- dt += delta
- # Set fold=1 so we can default to being in the fold for
- # ambiguous dates.
- dtdst = enfold(dt, fold=1).dst()
- if dtdst is None:
- raise ValueError("fromutc(): dt.dst gave inconsistent "
- "results; cannot convert")
+ if delta:
+ dt += delta
+ # Set fold=1 so we can default to being in the fold for
+ # ambiguous dates.
+ dtdst = enfold(dt, fold=1).dst()
+ if dtdst is None:
+ raise ValueError("fromutc(): dt.dst gave inconsistent "
+ "results; cannot convert")
return dt + dtdst
- @_validate_fromutc_inputs
def fromutc(self, dt):
"""
Given a timezone-aware datetime in a given timezone, calculates a
@@ -249,7 +205,7 @@ class _tzinfo(tzinfo):
occurance, chronologically, of the ambiguous datetime).
:param dt:
- A timezone-aware :class:`datetime.datetime` object.
+ A timezone-aware :class:`datetime.dateime` object.
"""
dt_wall = self._fromutc(dt)
@@ -280,7 +236,7 @@ class tzrangebase(_tzinfo):
abbreviations in DST and STD, respectively.
* ``_hasdst``: Whether or not the zone has DST.
- .. versionadded:: 2.6.0
+ ..versionadded:: 2.6.0
"""
def __init__(self):
raise NotImplementedError('tzrangebase is an abstract base class')
@@ -334,6 +290,7 @@ class tzrangebase(_tzinfo):
utc_transitions = (dston, dstoff)
dt_utc = dt.replace(tzinfo=None)
+
isdst = self._naive_isdst(dt_utc, utc_transitions)
if isdst:
@@ -403,7 +360,7 @@ class tzrangebase(_tzinfo):
@property
def _dst_base_offset(self):
return self._dst_offset - self._std_offset
-
+
__hash__ = None
def __ne__(self, other):
@@ -413,3 +370,11 @@ class tzrangebase(_tzinfo):
return "%s(...)" % self.__class__.__name__
__reduce__ = object.__reduce__
+
+
+def _total_seconds(td):
+ # Python 2.6 doesn't have a total_seconds() method on timedelta objects
+ return ((td.seconds + td.days * 86400) * 1000000 +
+ td.microseconds) // 1000000
+
+_total_seconds = getattr(timedelta, 'total_seconds', _total_seconds)
diff --git a/libs/dateutil/tz/tz.py b/libs/dateutil/tz/tz.py
index ac82b9c83..6bee29168 100644
--- a/libs/dateutil/tz/tz.py
+++ b/libs/dateutil/tz/tz.py
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
"""
This module offers timezone implementations subclassing the abstract
-:py:class:`datetime.tzinfo` type. There are classes to handle tzfile format
-files (usually are in :file:`/etc/localtime`, :file:`/usr/share/zoneinfo`,
-etc), TZ environment string (in all known formats), given ranges (with help
-from relative deltas), local machine timezone, fixed offset timezone, and UTC
+:py:`datetime.tzinfo` type. There are classes to handle tzfile format files
+(usually are in :file:`/etc/localtime`, :file:`/usr/share/zoneinfo`, etc), TZ
+environment string (in all known formats), given ranges (with help from
+relative deltas), local machine timezone, fixed offset timezone, and UTC
timezone.
"""
import datetime
@@ -13,16 +13,16 @@ import time
import sys
import os
import bisect
+import copy
-import six
-from six import string_types
-from six.moves import _thread
-from ._common import tzname_in_python2, _tzinfo
+from operator import itemgetter
+
+from contextlib import contextmanager
+
+from six import string_types, PY3
+from ._common import tzname_in_python2, _tzinfo, _total_seconds
from ._common import tzrangebase, enfold
-from ._common import _validate_fromutc_inputs
-from ._factories import _TzSingleton, _TzOffsetFactory
-from ._factories import _TzStrFactory
try:
from .win import tzwin, tzwinlocal
except ImportError:
@@ -32,39 +32,9 @@ ZERO = datetime.timedelta(0)
EPOCH = datetime.datetime.utcfromtimestamp(0)
EPOCHORDINAL = EPOCH.toordinal()
-
[email protected]_metaclass(_TzSingleton)
class tzutc(datetime.tzinfo):
"""
This is a tzinfo object that represents the UTC time zone.
-
- **Examples:**
-
- .. doctest::
-
- >>> from datetime import *
- >>> from dateutil.tz import *
-
- >>> datetime.now()
- datetime.datetime(2003, 9, 27, 9, 40, 1, 521290)
-
- >>> datetime.now(tzutc())
- datetime.datetime(2003, 9, 27, 12, 40, 12, 156379, tzinfo=tzutc())
-
- >>> datetime.now(tzutc()).tzname()
- 'UTC'
-
- .. versionchanged:: 2.7.0
- ``tzutc()`` is now a singleton, so the result of ``tzutc()`` will
- always return the same object.
-
- .. doctest::
-
- >>> from dateutil.tz import tzutc, UTC
- >>> tzutc() is tzutc()
- True
- >>> tzutc() is UTC
- True
"""
def utcoffset(self, dt):
return ZERO
@@ -92,14 +62,6 @@ class tzutc(datetime.tzinfo):
"""
return False
- @_validate_fromutc_inputs
- def fromutc(self, dt):
- """
- Fast track version of fromutc() returns the original ``dt`` object for
- any valid :py:class:`datetime.datetime` object.
- """
- return dt
-
def __eq__(self, other):
if not isinstance(other, (tzutc, tzoffset)):
return NotImplemented
@@ -118,23 +80,23 @@ class tzutc(datetime.tzinfo):
__reduce__ = object.__reduce__
[email protected]_metaclass(_TzOffsetFactory)
class tzoffset(datetime.tzinfo):
"""
A simple class for representing a fixed offset from UTC.
:param name:
The timezone name, to be returned when ``tzname()`` is called.
+
:param offset:
The time zone offset in seconds, or (since version 2.6.0, represented
- as a :py:class:`datetime.timedelta` object).
+ as a :py:class:`datetime.timedelta` object.
"""
def __init__(self, name, offset):
self._name = name
-
+
try:
# Allow a timedelta
- offset = offset.total_seconds()
+ offset = _total_seconds(offset)
except (TypeError, AttributeError):
pass
self._offset = datetime.timedelta(seconds=offset)
@@ -145,14 +107,6 @@ class tzoffset(datetime.tzinfo):
def dst(self, dt):
return ZERO
- @tzname_in_python2
- def tzname(self, dt):
- return self._name
-
- @_validate_fromutc_inputs
- def fromutc(self, dt):
- return dt + self._offset
-
def is_ambiguous(self, dt):
"""
Whether or not the "wall time" of a given datetime is ambiguous in this
@@ -160,6 +114,8 @@ class tzoffset(datetime.tzinfo):
:param dt:
A :py:class:`datetime.datetime`, naive or time zone aware.
+
+
:return:
Returns ``True`` if ambiguous, ``False`` otherwise.
@@ -167,6 +123,10 @@ class tzoffset(datetime.tzinfo):
"""
return False
+ @tzname_in_python2
+ def tzname(self, dt):
+ return self._name
+
def __eq__(self, other):
if not isinstance(other, tzoffset):
return NotImplemented
@@ -181,7 +141,7 @@ class tzoffset(datetime.tzinfo):
def __repr__(self):
return "%s(%s, %s)" % (self.__class__.__name__,
repr(self._name),
- int(self._offset.total_seconds()))
+ int(_total_seconds(self._offset)))
__reduce__ = object.__reduce__
@@ -201,7 +161,6 @@ class tzlocal(_tzinfo):
self._dst_saved = self._dst_offset - self._std_offset
self._hasdst = bool(self._dst_saved)
- self._tznames = tuple(time.tzname)
def utcoffset(self, dt):
if dt is None and self._hasdst:
@@ -223,7 +182,7 @@ class tzlocal(_tzinfo):
@tzname_in_python2
def tzname(self, dt):
- return self._tznames[self._isdst(dt)]
+ return time.tzname[self._isdst(dt)]
def is_ambiguous(self, dt):
"""
@@ -288,20 +247,12 @@ class tzlocal(_tzinfo):
return dstval
def __eq__(self, other):
- if isinstance(other, tzlocal):
- return (self._std_offset == other._std_offset and
- self._dst_offset == other._dst_offset)
- elif isinstance(other, tzutc):
- return (not self._hasdst and
- self._tznames[0] in {'UTC', 'GMT'} and
- self._std_offset == ZERO)
- elif isinstance(other, tzoffset):
- return (not self._hasdst and
- self._tznames[0] == other._name and
- self._std_offset == other._offset)
- else:
+ if not isinstance(other, tzlocal):
return NotImplemented
+ return (self._std_offset == other._std_offset and
+ self._dst_offset == other._dst_offset)
+
__hash__ = None
def __ne__(self, other):
@@ -363,7 +314,7 @@ class _tzfile(object):
Lightweight class for holding the relevant transition and time zone
information read from binary tzfiles.
"""
- attrs = ['trans_list', 'trans_list_utc', 'trans_idx', 'ttinfo_list',
+ attrs = ['trans_list', 'trans_idx', 'ttinfo_list',
'ttinfo_std', 'ttinfo_dst', 'ttinfo_before', 'ttinfo_first']
def __init__(self, **kwargs):
@@ -373,7 +324,7 @@ class _tzfile(object):
class tzfile(_tzinfo):
"""
- This is a ``tzinfo`` subclass thant allows one to use the ``tzfile(5)``
+ This is a ``tzinfo`` subclass that allows one to use the ``tzfile(5)``
format timezone files to extract current and historical zone information.
:param fileobj:
@@ -386,61 +337,11 @@ class tzfile(_tzinfo):
and ``fileobj`` is a file stream, this parameter will be set either to
``fileobj``'s ``name`` attribute or to ``repr(fileobj)``.
- See `Sources for Time Zone and Daylight Saving Time Data
- <https://data.iana.org/time-zones/tz-link.html>`_ for more information.
- Time zone files can be compiled from the `IANA Time Zone database files
+ See `Sources for Time Zone and Daylight Saving Time Data
+ <http://www.twinsun.com/tz/tz-link.htm>`_ for more information. Time zone
+ files can be compiled from the `IANA Time Zone database files
<https://www.iana.org/time-zones>`_ with the `zic time zone compiler
<https://www.freebsd.org/cgi/man.cgi?query=zic&sektion=8>`_
-
- .. note::
-
- Only construct a ``tzfile`` directly if you have a specific timezone
- file on disk that you want to read into a Python ``tzinfo`` object.
- If you want to get a ``tzfile`` representing a specific IANA zone,
- (e.g. ``'America/New_York'``), you should call
- :func:`dateutil.tz.gettz` with the zone identifier.
-
-
- **Examples:**
-
- Using the US Eastern time zone as an example, we can see that a ``tzfile``
- provides time zone information for the standard Daylight Saving offsets:
-
- .. testsetup:: tzfile
-
- from dateutil.tz import gettz
- from datetime import datetime
-
- .. doctest:: tzfile
-
- >>> NYC = gettz('America/New_York')
- >>> NYC
- tzfile('/usr/share/zoneinfo/America/New_York')
-
- >>> print(datetime(2016, 1, 3, tzinfo=NYC)) # EST
- 2016-01-03 00:00:00-05:00
-
- >>> print(datetime(2016, 7, 7, tzinfo=NYC)) # EDT
- 2016-07-07 00:00:00-04:00
-
-
- The ``tzfile`` structure contains a fully history of the time zone,
- so historical dates will also have the right offsets. For example, before
- the adoption of the UTC standards, New York used local solar mean time:
-
- .. doctest:: tzfile
-
- >>> print(datetime(1901, 4, 12, tzinfo=NYC)) # LMT
- 1901-04-12 00:00:00-04:56
-
- And during World War II, New York was on "Eastern War Time", which was a
- state of permanent daylight saving time:
-
- .. doctest:: tzfile
-
- >>> print(datetime(1944, 2, 7, tzinfo=NYC)) # EWT
- 1944-02-07 00:00:00-04:00
-
"""
def __init__(self, fileobj, filename=None):
@@ -523,10 +424,10 @@ class tzfile(_tzinfo):
# change.
if timecnt:
- out.trans_list_utc = list(struct.unpack(">%dl" % timecnt,
- fileobj.read(timecnt*4)))
+ out.trans_list = list(struct.unpack(">%dl" % timecnt,
+ fileobj.read(timecnt*4)))
else:
- out.trans_list_utc = []
+ out.trans_list = []
# Next come tzh_timecnt one-byte values of type unsigned
# char; each one tells which of the different types of
@@ -537,7 +438,7 @@ class tzfile(_tzinfo):
if timecnt:
out.trans_idx = struct.unpack(">%dB" % timecnt,
- fileobj.read(timecnt))
+ fileobj.read(timecnt))
else:
out.trans_idx = []
@@ -568,9 +469,10 @@ class tzfile(_tzinfo):
# The pairs of values are sorted in ascending order
# by time.
- # Not used, for now (but seek for correct file position)
+ # Not used, for now (but read anyway for correct file position)
if leapcnt:
- fileobj.seek(leapcnt * 8, os.SEEK_CUR)
+ leap = struct.unpack(">%dl" % (leapcnt*2),
+ fileobj.read(leapcnt*8))
# Then there are tzh_ttisstdcnt standard/wall
# indicators, each stored as a one-byte value;
@@ -625,7 +527,7 @@ class tzfile(_tzinfo):
out.ttinfo_dst = None
out.ttinfo_before = None
if out.ttinfo_list:
- if not out.trans_list_utc:
+ if not out.trans_list:
out.ttinfo_std = out.ttinfo_first = out.ttinfo_list[0]
else:
for i in range(timecnt-1, -1, -1):
@@ -656,7 +558,6 @@ class tzfile(_tzinfo):
# always in gmt time. Let me know if you have comments
# about this.
laststdoffset = None
- out.trans_list = []
for i, tti in enumerate(out.trans_idx):
if not tti.isdst:
offset = tti.offset
@@ -669,7 +570,7 @@ class tzfile(_tzinfo):
offset = laststdoffset or 0
- out.trans_list.append(out.trans_list_utc[i] + offset)
+ out.trans_list[i] += offset
# In case we missed any DST offsets on the way in for some reason, make
# a second pass over the list, looking for the /next/ DST offset.
@@ -684,16 +585,15 @@ class tzfile(_tzinfo):
if not isinstance(tti.dstoffset, datetime.timedelta):
tti.dstoffset = datetime.timedelta(seconds=tti.dstoffset)
-
+
out.trans_idx[i] = tti
out.trans_idx = tuple(out.trans_idx)
out.trans_list = tuple(out.trans_list)
- out.trans_list_utc = tuple(out.trans_list_utc)
return out
- def _find_last_transition(self, dt, in_utc=False):
+ def _find_last_transition(self, dt):
# If there's no list, there are no transitions to find
if not self._trans_list:
return None
@@ -702,15 +602,14 @@ class tzfile(_tzinfo):
# Find where the timestamp fits in the transition list - if the
# timestamp is a transition time, it's part of the "after" period.
- trans_list = self._trans_list_utc if in_utc else self._trans_list
- idx = bisect.bisect_right(trans_list, timestamp)
+ idx = bisect.bisect_right(self._trans_list, timestamp)
# We want to know when the previous transition was, so subtract off 1
return idx - 1
def _get_ttinfo(self, idx):
# For no list or after the last transition, default to _ttinfo_std
- if idx is None or (idx + 1) >= len(self._trans_list):
+ if idx is None or (idx + 1) == len(self._trans_list):
return self._ttinfo_std
# If there is a list and the time is before it, return _ttinfo_before
@@ -724,42 +623,6 @@ class tzfile(_tzinfo):
return self._get_ttinfo(idx)
- def fromutc(self, dt):
- """
- The ``tzfile`` implementation of :py:func:`datetime.tzinfo.fromutc`.
-
- :param dt:
- A :py:class:`datetime.datetime` object.
-
- :raises TypeError:
- Raised if ``dt`` is not a :py:class:`datetime.datetime` object.
-
- :raises ValueError:
- Raised if this is called with a ``dt`` which does not have this
- ``tzinfo`` attached.
-
- :return:
- Returns a :py:class:`datetime.datetime` object representing the
- wall time in ``self``'s time zone.
- """
- # These isinstance checks are in datetime.tzinfo, so we'll preserve
- # them, even if we don't care about duck typing.
- if not isinstance(dt, datetime.datetime):
- raise TypeError("fromutc() requires a datetime argument")
-
- if dt.tzinfo is not self:
- raise ValueError("dt.tzinfo is not self")
-
- # First treat UTC as wall time and get the transition we're in.
- idx = self._find_last_transition(dt, in_utc=True)
- tti = self._get_ttinfo(idx)
-
- dt_out = dt + datetime.timedelta(seconds=tti.offset)
-
- fold = self.is_ambiguous(dt_out, idx=idx)
-
- return enfold(dt_out, fold=int(fold))
-
def is_ambiguous(self, dt, idx=None):
"""
Whether or not the "wall time" of a given datetime is ambiguous in this
@@ -797,7 +660,7 @@ class tzfile(_tzinfo):
if idx is None or idx == 0:
return idx
- # If it's ambiguous and we're in a fold, shift to a different index.
+ # Get the current datetime as a timestamp
idx_offset = int(not _fold and self.is_ambiguous(dt, idx))
return idx - idx_offset
@@ -817,7 +680,7 @@ class tzfile(_tzinfo):
if not self._ttinfo_dst:
return ZERO
-
+
tti = self._find_ttinfo(dt)
if not tti.isdst:
@@ -890,9 +753,8 @@ class tzrange(tzrangebase):
:param start:
A :class:`relativedelta.relativedelta` object or equivalent specifying
- the time and time of year that daylight savings time starts. To
- specify, for example, that DST starts at 2AM on the 2nd Sunday in
- March, pass:
+ the time and time of year that daylight savings time starts. To specify,
+ for example, that DST starts at 2AM on the 2nd Sunday in March, pass:
``relativedelta(hours=2, month=3, day=1, weekday=SU(+2))``
@@ -900,12 +762,12 @@ class tzrange(tzrangebase):
value is 2 AM on the first Sunday in April.
:param end:
- A :class:`relativedelta.relativedelta` object or equivalent
- representing the time and time of year that daylight savings time
- ends, with the same specification method as in ``start``. One note is
- that this should point to the first time in the *standard* zone, so if
- a transition occurs at 2AM in the DST zone and the clocks are set back
- 1 hour to 1AM, set the ``hours`` parameter to +1.
+ A :class:`relativedelta.relativedelta` object or equivalent representing
+ the time and time of year that daylight savings time ends, with the
+ same specification method as in ``start``. One note is that this should
+ point to the first time in the *standard* zone, so if a transition
+ occurs at 2AM in the DST zone and the clocks are set back 1 hour to 1AM,
+ set the `hours` parameter to +1.
**Examples:**
@@ -941,12 +803,12 @@ class tzrange(tzrangebase):
self._dst_abbr = dstabbr
try:
- stdoffset = stdoffset.total_seconds()
+ stdoffset = _total_seconds(stdoffset)
except (TypeError, AttributeError):
pass
try:
- dstoffset = dstoffset.total_seconds()
+ dstoffset = _total_seconds(dstoffset)
except (TypeError, AttributeError):
pass
@@ -1017,7 +879,6 @@ class tzrange(tzrangebase):
return self._dst_base_offset_
[email protected]_metaclass(_TzStrFactory)
class tzstr(tzrange):
"""
``tzstr`` objects are time zone objects specified by a time-zone string as
@@ -1036,38 +897,25 @@ class tzstr(tzrange):
:param s:
A time zone string in ``TZ`` variable format. This can be a
- :class:`bytes` (2.x: :class:`str`), :class:`str` (2.x:
- :class:`unicode`) or a stream emitting unicode characters
- (e.g. :class:`StringIO`).
+ :class:`bytes` (2.x: :class:`str`), :class:`str` (2.x: :class:`unicode`)
+ or a stream emitting unicode characters (e.g. :class:`StringIO`).
:param posix_offset:
Optional. If set to ``True``, interpret strings such as ``GMT+3`` or
``UTC+3`` as being 3 hours *behind* UTC rather than ahead, per the
POSIX standard.
- .. caution::
-
- Prior to version 2.7.0, this function also supported time zones
- in the format:
-
- * ``EST5EDT,4,0,6,7200,10,0,26,7200,3600``
- * ``EST5EDT,4,1,0,7200,10,-1,0,7200,3600``
-
- This format is non-standard and has been deprecated; this function
- will raise a :class:`DeprecatedTZFormatWarning` until
- support is removed in a future version.
-
.. _`GNU C Library: TZ Variable`:
https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
"""
def __init__(self, s, posix_offset=False):
global parser
- from dateutil.parser import _parser as parser
+ from dateutil import parser
self._s = s
res = parser._parsetz(s)
- if res is None or res.any_unused_tokens:
+ if res is None:
raise ValueError("unknown string format")
# Here we break the compatibility with the TZ variable handling.
@@ -1156,7 +1004,6 @@ class _tzicalvtz(_tzinfo):
self._comps = comps
self._cachedate = []
self._cachecomp = []
- self._cache_lock = _thread.allocate_lock()
def _find_comp(self, dt):
if len(self._comps) == 1:
@@ -1165,12 +1012,11 @@ class _tzicalvtz(_tzinfo):
dt = dt.replace(tzinfo=None)
try:
- with self._cache_lock:
- return self._cachecomp[self._cachedate.index(
- (dt, self._fold(dt)))]
+ return self._cachecomp[self._cachedate.index((dt, self._fold(dt)))]
except ValueError:
pass
+
lastcompdt = None
lastcomp = None
@@ -1193,13 +1039,12 @@ class _tzicalvtz(_tzinfo):
else:
lastcomp = comp[0]
- with self._cache_lock:
- self._cachedate.insert(0, (dt, self._fold(dt)))
- self._cachecomp.insert(0, lastcomp)
+ self._cachedate.insert(0, (dt, self._fold(dt)))
+ self._cachecomp.insert(0, lastcomp)
- if len(self._cachedate) > 10:
- self._cachedate.pop()
- self._cachecomp.pop()
+ if len(self._cachedate) > 10:
+ self._cachedate.pop()
+ self._cachecomp.pop()
return lastcomp
@@ -1237,13 +1082,13 @@ class _tzicalvtz(_tzinfo):
class tzical(object):
"""
This object is designed to parse an iCalendar-style ``VTIMEZONE`` structure
- as set out in `RFC 5545`_ Section 4.6.5 into one or more `tzinfo` objects.
+ as set out in `RFC 2445`_ Section 4.6.5 into one or more `tzinfo` objects.
:param `fileobj`:
A file or stream in iCalendar format, which should be UTF-8 encoded
with CRLF endings.
- .. _`RFC 5545`: https://tools.ietf.org/html/rfc5545
+ .. _`RFC 2445`: https://www.ietf.org/rfc/rfc2445.txt
"""
def __init__(self, fileobj):
global rrule
@@ -1253,6 +1098,7 @@ class tzical(object):
self._s = fileobj
# ical should be encoded in UTF-8 with CRLF
fileobj = open(fileobj, 'r')
+ file_opened_here = True
else:
self._s = getattr(fileobj, 'name', repr(fileobj))
fileobj = _ContextWrapper(fileobj)
@@ -1391,13 +1237,6 @@ class tzical(object):
raise ValueError("invalid component end: "+value)
elif comptype:
if name == "DTSTART":
- # DTSTART in VTIMEZONE takes a subset of valid RRULE
- # values under RFC 5545.
- for parm in parms:
- if parm != 'VALUE=DATE-TIME':
- msg = ('Unsupported DTSTART param in ' +
- 'VTIMEZONE: ' + parm)
- raise ValueError(msg)
rrulelines.append(line)
founddtstart = True
elif name in ("RRULE", "RDATE", "EXRULE", "EXDATE"):
@@ -1439,7 +1278,6 @@ class tzical(object):
def __repr__(self):
return "%s(%s)" % (self.__class__.__name__, repr(self._s))
-
if sys.platform != "win32":
TZFILES = ["/etc/localtime", "localtime"]
TZPATHS = ["/usr/share/zoneinfo",
@@ -1451,187 +1289,78 @@ else:
TZPATHS = []
-def __get_gettz():
- tzlocal_classes = (tzlocal,)
- if tzwinlocal is not None:
- tzlocal_classes += (tzwinlocal,)
-
- class GettzFunc(object):
- """
- Retrieve a time zone object from a string representation
-
- This function is intended to retrieve the :py:class:`tzinfo` subclass
- that best represents the time zone that would be used if a POSIX
- `TZ variable`_ were set to the same value.
-
- If no argument or an empty string is passed to ``gettz``, local time
- is returned:
-
- .. code-block:: python3
-
- >>> gettz()
- tzfile('/etc/localtime')
-
- This function is also the preferred way to map IANA tz database keys
- to :class:`tzfile` objects:
-
- .. code-block:: python3
-
- >>> gettz('Pacific/Kiritimati')
- tzfile('/usr/share/zoneinfo/Pacific/Kiritimati')
-
- On Windows, the standard is extended to include the Windows-specific
- zone names provided by the operating system:
-
- .. code-block:: python3
-
- >>> gettz('Egypt Standard Time')
- tzwin('Egypt Standard Time')
-
- Passing a GNU ``TZ`` style string time zone specification returns a
- :class:`tzstr` object:
-
- .. code-block:: python3
-
- >>> gettz('AEST-10AEDT-11,M10.1.0/2,M4.1.0/3')
- tzstr('AEST-10AEDT-11,M10.1.0/2,M4.1.0/3')
-
- :param name:
- A time zone name (IANA, or, on Windows, Windows keys), location of
- a ``tzfile(5)`` zoneinfo file or ``TZ`` variable style time zone
- specifier. An empty string, no argument or ``None`` is interpreted
- as local time.
-
- :return:
- Returns an instance of one of ``dateutil``'s :py:class:`tzinfo`
- subclasses.
-
- .. versionchanged:: 2.7.0
-
- After version 2.7.0, any two calls to ``gettz`` using the same
- input strings will return the same object:
-
- .. code-block:: python3
-
- >>> tz.gettz('America/Chicago') is tz.gettz('America/Chicago')
- True
-
- In addition to improving performance, this ensures that
- `"same zone" semantics`_ are used for datetimes in the same zone.
-
-
- .. _`TZ variable`:
- https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
-
- .. _`"same zone" semantics`:
- https://blog.ganssle.io/articles/2018/02/aware-datetime-arithmetic.html
- """
- def __init__(self):
-
- self.__instances = {}
- self._cache_lock = _thread.allocate_lock()
-
- def __call__(self, name=None):
- with self._cache_lock:
- rv = self.__instances.get(name, None)
-
- if rv is None:
- rv = self.nocache(name=name)
- if not (name is None or isinstance(rv, tzlocal_classes)):
- # tzlocal is slightly more complicated than the other
- # time zone providers because it depends on environment
- # at construction time, so don't cache that.
- self.__instances[name] = rv
-
- return rv
-
- def cache_clear(self):
- with self._cache_lock:
- self.__instances = {}
-
- @staticmethod
- def nocache(name=None):
- """A non-cached version of gettz"""
- tz = None
- if not name:
- try:
- name = os.environ["TZ"]
- except KeyError:
- pass
- if name is None or name == ":":
- for filepath in TZFILES:
- if not os.path.isabs(filepath):
- filename = filepath
- for path in TZPATHS:
- filepath = os.path.join(path, filename)
- if os.path.isfile(filepath):
- break
- else:
- continue
+def gettz(name=None):
+ tz = None
+ if not name:
+ try:
+ name = os.environ["TZ"]
+ except KeyError:
+ pass
+ if name is None or name == ":":
+ for filepath in TZFILES:
+ if not os.path.isabs(filepath):
+ filename = filepath
+ for path in TZPATHS:
+ filepath = os.path.join(path, filename)
if os.path.isfile(filepath):
- try:
- tz = tzfile(filepath)
- break
- except (IOError, OSError, ValueError):
- pass
+ break
else:
- tz = tzlocal()
+ continue
+ if os.path.isfile(filepath):
+ try:
+ tz = tzfile(filepath)
+ break
+ except (IOError, OSError, ValueError):
+ pass
+ else:
+ tz = tzlocal()
+ else:
+ if name.startswith(":"):
+ name = name[:-1]
+ if os.path.isabs(name):
+ if os.path.isfile(name):
+ tz = tzfile(name)
else:
- if name.startswith(":"):
- name = name[1:]
- if os.path.isabs(name):
- if os.path.isfile(name):
- tz = tzfile(name)
- else:
+ tz = None
+ else:
+ for path in TZPATHS:
+ filepath = os.path.join(path, name)
+ if not os.path.isfile(filepath):
+ filepath = filepath.replace(' ', '_')
+ if not os.path.isfile(filepath):
+ continue
+ try:
+ tz = tzfile(filepath)
+ break
+ except (IOError, OSError, ValueError):
+ pass
+ else:
+ tz = None
+ if tzwin is not None:
+ try:
+ tz = tzwin(name)
+ except WindowsError:
tz = None
- else:
- for path in TZPATHS:
- filepath = os.path.join(path, name)
- if not os.path.isfile(filepath):
- filepath = filepath.replace(' ', '_')
- if not os.path.isfile(filepath):
- continue
- try:
- tz = tzfile(filepath)
+
+ if not tz:
+ from dateutil.zoneinfo import get_zonefile_instance
+ tz = get_zonefile_instance().get(name)
+
+ if not tz:
+ for c in name:
+ # name must have at least one offset to be a tzstr
+ if c in "0123456789":
+ try:
+ tz = tzstr(name)
+ except ValueError:
+ pass
break
- except (IOError, OSError, ValueError):
- pass
else:
- tz = None
- if tzwin is not None:
- try:
- tz = tzwin(name)
- except WindowsError:
- tz = None
-
- if not tz:
- from dateutil.zoneinfo import get_zonefile_instance
- tz = get_zonefile_instance().get(name)
-
- if not tz:
- for c in name:
- # name is not a tzstr unless it has at least
- # one offset. For short values of "name", an
- # explicit for loop seems to be the fastest way
- # To determine if a string contains a digit
- if c in "0123456789":
- try:
- tz = tzstr(name)
- except ValueError:
- pass
- break
- else:
- if name in ("GMT", "UTC"):
- tz = tzutc()
- elif name in time.tzname:
- tz = tzlocal()
- return tz
-
- return GettzFunc()
-
-
-gettz = __get_gettz()
-del __get_gettz
+ if name in ("GMT", "UTC"):
+ tz = tzutc()
+ elif name in time.tzname:
+ tz = tzlocal()
+ return tz
def datetime_exists(dt, tz=None):
@@ -1646,12 +1375,9 @@ def datetime_exists(dt, tz=None):
:param tz:
A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If
``None`` or not provided, the datetime's own time zone will be used.
-
+
:return:
- Returns a boolean value whether or not the "wall time" exists in
- ``tz``.
-
- .. versionadded:: 2.7.0
+ Returns a boolean value whether or not the "wall time" exists in ``tz``.
"""
if tz is None:
if dt.tzinfo is None:
@@ -1681,7 +1407,7 @@ def datetime_ambiguous(dt, tz=None):
:param tz:
A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If
``None`` or not provided, the datetime's own time zone will be used.
-
+
:return:
Returns a boolean value whether or not the "wall time" is ambiguous in
``tz``.
@@ -1699,7 +1425,7 @@ def datetime_ambiguous(dt, tz=None):
if is_ambiguous_fn is not None:
try:
return tz.is_ambiguous(dt)
- except Exception:
+ except:
pass
# If it doesn't come out and tell us it's ambiguous, we'll just check if
@@ -1714,63 +1440,16 @@ def datetime_ambiguous(dt, tz=None):
return not (same_offset and same_dst)
-def resolve_imaginary(dt):
- """
- Given a datetime that may be imaginary, return an existing datetime.
-
- This function assumes that an imaginary datetime represents what the
- wall time would be in a zone had the offset transition not occurred, so
- it will always fall forward by the transition's change in offset.
-
- .. doctest::
-
- >>> from dateutil import tz
- >>> from datetime import datetime
- >>> NYC = tz.gettz('America/New_York')
- >>> print(tz.resolve_imaginary(datetime(2017, 3, 12, 2, 30, tzinfo=NYC)))
- 2017-03-12 03:30:00-04:00
-
- >>> KIR = tz.gettz('Pacific/Kiritimati')
- >>> print(tz.resolve_imaginary(datetime(1995, 1, 1, 12, 30, tzinfo=KIR)))
- 1995-01-02 12:30:00+14:00
-
- As a note, :func:`datetime.astimezone` is guaranteed to produce a valid,
- existing datetime, so a round-trip to and from UTC is sufficient to get
- an extant datetime, however, this generally "falls back" to an earlier time
- rather than falling forward to the STD side (though no guarantees are made
- about this behavior).
-
- :param dt:
- A :class:`datetime.datetime` which may or may not exist.
-
- :return:
- Returns an existing :class:`datetime.datetime`. If ``dt`` was not
- imaginary, the datetime returned is guaranteed to be the same object
- passed to the function.
-
- .. versionadded:: 2.7.0
- """
- if dt.tzinfo is not None and not datetime_exists(dt):
-
- curr_offset = (dt + datetime.timedelta(hours=24)).utcoffset()
- old_offset = (dt - datetime.timedelta(hours=24)).utcoffset()
-
- dt += curr_offset - old_offset
-
- return dt
-
-
def _datetime_to_timestamp(dt):
"""
- Convert a :class:`datetime.datetime` object to an epoch timestamp in
- seconds since January 1, 1970, ignoring the time zone.
+ Convert a :class:`datetime.datetime` object to an epoch timestamp in seconds
+ since January 1, 1970, ignoring the time zone.
"""
- return (dt.replace(tzinfo=None) - EPOCH).total_seconds()
-
+ return _total_seconds((dt.replace(tzinfo=None) - EPOCH))
class _ContextWrapper(object):
"""
- Class for wrapping contexts so that they are passed through in a
+ Class for wrapping contexts so that they are passed through in a
with statement.
"""
def __init__(self, context):
diff --git a/libs/dateutil/tz/win.py b/libs/dateutil/tz/win.py
index def4353a6..9f4e5519f 100644
--- a/libs/dateutil/tz/win.py
+++ b/libs/dateutil/tz/win.py
@@ -12,6 +12,7 @@ except ValueError:
# ValueError is raised on non-Windows systems for some horrible reason.
raise ImportError("Running tzwin on non-Windows system")
+from ._common import tzname_in_python2, _tzinfo
from ._common import tzrangebase
__all__ = ["tzwin", "tzwinlocal", "tzres"]
@@ -33,7 +34,6 @@ def _settzkeyname():
handle.Close()
return TZKEYNAME
-
TZKEYNAME = _settzkeyname()
@@ -49,7 +49,7 @@ class tzres(object):
def __init__(self, tzres_loc='tzres.dll'):
# Load the user32 DLL so we can load strings from tzres
user32 = ctypes.WinDLL('user32')
-
+
# Specify the LoadStringW function
user32.LoadStringW.argtypes = (wintypes.HINSTANCE,
wintypes.UINT,
@@ -63,7 +63,7 @@ class tzres(object):
def load_name(self, offset):
"""
Load a timezone name from a DLL offset (integer).
-
+
>>> from dateutil.tzwin import tzres
>>> tzr = tzres()
>>> print(tzr.load_name(112))
@@ -192,8 +192,9 @@ class tzwin(tzwinbase):
def __init__(self, name):
self._name = name
+ # multiple contexts only possible in 2.7 and 3.1, we still support 2.6
with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as handle:
- tzkeyname = text_type("{kn}\\{name}").format(kn=TZKEYNAME, name=name)
+ tzkeyname = text_type("{kn}\{name}").format(kn=TZKEYNAME, name=name)
with winreg.OpenKey(handle, tzkeyname) as tzkey:
keydict = valuestodict(tzkey)
@@ -243,7 +244,7 @@ class tzwinlocal(tzwinbase):
self._dst_abbr = keydict["DaylightName"]
try:
- tzkeyname = text_type('{kn}\\{sn}').format(kn=TZKEYNAME,
+ tzkeyname = text_type('{kn}\{sn}').format(kn=TZKEYNAME,
sn=self._std_abbr)
with winreg.OpenKey(handle, tzkeyname) as tzkey:
_keydict = valuestodict(tzkey)
@@ -265,7 +266,7 @@ class tzwinlocal(tzwinbase):
self._stdweeknumber, # Last = 5
self._stdhour,
self._stdminute) = tup[1:5]
-
+
self._stddayofweek = tup[7]
tup = struct.unpack("=8h", keydict["DaylightStart"])
diff --git a/libs/dateutil/tzwin.py b/libs/dateutil/tzwin.py
index cebc673e4..55cd91028 100644
--- a/libs/dateutil/tzwin.py
+++ b/libs/dateutil/tzwin.py
@@ -1,2 +1,2 @@
# tzwin has moved to dateutil.tz.win
-from .tz.win import *
+from .tz.win import * \ No newline at end of file
diff --git a/libs/dateutil/zoneinfo/__init__.py b/libs/dateutil/zoneinfo/__init__.py
index 34f11ad66..7145e05cf 100644
--- a/libs/dateutil/zoneinfo/__init__.py
+++ b/libs/dateutil/zoneinfo/__init__.py
@@ -1,20 +1,32 @@
# -*- coding: utf-8 -*-
+import logging
+import os
import warnings
+import tempfile
+import shutil
import json
from tarfile import TarFile
from pkgutil import get_data
from io import BytesIO
+from contextlib import closing
-from dateutil.tz import tzfile as _tzfile
+from dateutil.tz import tzfile
-__all__ = ["get_zonefile_instance", "gettz", "gettz_db_metadata"]
+__all__ = ["get_zonefile_instance", "gettz", "gettz_db_metadata", "rebuild"]
ZONEFILENAME = "dateutil-zoneinfo.tar.gz"
METADATA_FN = 'METADATA'
+# python2.6 compatability. Note that TarFile.__exit__ != TarFile.close, but
+# it's close enough for python2.6
+tar_open = TarFile.open
+if not hasattr(TarFile, '__exit__'):
+ def tar_open(*args, **kwargs):
+ return closing(TarFile.open(*args, **kwargs))
-class tzfile(_tzfile):
+
+class tzfile(tzfile):
def __reduce__(self):
return (gettz, (self._filename,))
@@ -30,15 +42,23 @@ def getzoneinfofile_stream():
class ZoneInfoFile(object):
def __init__(self, zonefile_stream=None):
if zonefile_stream is not None:
- with TarFile.open(fileobj=zonefile_stream) as tf:
- self.zones = {zf.name: tzfile(tf.extractfile(zf), filename=zf.name)
- for zf in tf.getmembers()
- if zf.isfile() and zf.name != METADATA_FN}
+ with tar_open(fileobj=zonefile_stream, mode='r') as tf:
+ # dict comprehension does not work on python2.6
+ # TODO: get back to the nicer syntax when we ditch python2.6
+ # self.zones = {zf.name: tzfile(tf.extractfile(zf),
+ # filename = zf.name)
+ # for zf in tf.getmembers() if zf.isfile()}
+ self.zones = dict((zf.name, tzfile(tf.extractfile(zf),
+ filename=zf.name))
+ for zf in tf.getmembers()
+ if zf.isfile() and zf.name != METADATA_FN)
# deal with links: They'll point to their parent object. Less
# waste of memory
- links = {zl.name: self.zones[zl.linkname]
- for zl in tf.getmembers() if
- zl.islnk() or zl.issym()}
+ # links = {zl.name: self.zones[zl.linkname]
+ # for zl in tf.getmembers() if zl.islnk() or zl.issym()}
+ links = dict((zl.name, self.zones[zl.linkname])
+ for zl in tf.getmembers() if
+ zl.islnk() or zl.issym())
self.zones.update(links)
try:
metadata_json = tf.extractfile(tf.getmember(METADATA_FN))
@@ -48,14 +68,14 @@ class ZoneInfoFile(object):
# no metadata in tar file
self.metadata = None
else:
- self.zones = {}
+ self.zones = dict()
self.metadata = None
def get(self, name, default=None):
"""
Wrapper for :func:`ZoneInfoFile.zones.get`. This is a convenience method
for retrieving zones from the zone dictionary.
-
+
:param name:
The name of the zone to retrieve. (Generally IANA zone names)
@@ -74,8 +94,7 @@ class ZoneInfoFile(object):
# timezone. Ugly, but adheres to the api.
#
# TODO: Remove after deprecation period.
-_CLASS_ZONE_INSTANCE = []
-
+_CLASS_ZONE_INSTANCE = list()
def get_zonefile_instance(new_instance=False):
"""
@@ -105,7 +124,6 @@ def get_zonefile_instance(new_instance=False):
return zif
-
def gettz(name):
"""
This retrieves a time zone from the local zoneinfo tarball that is packaged
@@ -165,3 +183,5 @@ def gettz_db_metadata():
if len(_CLASS_ZONE_INSTANCE) == 0:
_CLASS_ZONE_INSTANCE.append(ZoneInfoFile(getzoneinfofile_stream()))
return _CLASS_ZONE_INSTANCE[0].metadata
+
+
diff --git a/libs/dateutil/zoneinfo/dateutil-zoneinfo.tar.gz b/libs/dateutil/zoneinfo/dateutil-zoneinfo.tar.gz
index e86b54fe2..1d15597b4 100644
--- a/libs/dateutil/zoneinfo/dateutil-zoneinfo.tar.gz
+++ b/libs/dateutil/zoneinfo/dateutil-zoneinfo.tar.gz
Binary files differ
diff --git a/libs/dateutil/zoneinfo/rebuild.py b/libs/dateutil/zoneinfo/rebuild.py
index 78f0d1a0c..a66c1d916 100644
--- a/libs/dateutil/zoneinfo/rebuild.py
+++ b/libs/dateutil/zoneinfo/rebuild.py
@@ -4,22 +4,21 @@ import tempfile
import shutil
import json
from subprocess import check_call
-from tarfile import TarFile
-from dateutil.zoneinfo import METADATA_FN, ZONEFILENAME
+from dateutil.zoneinfo import tar_open, METADATA_FN, ZONEFILENAME
def rebuild(filename, tag=None, format="gz", zonegroups=[], metadata=None):
"""Rebuild the internal timezone info in dateutil/zoneinfo/zoneinfo*tar*
- filename is the timezone tarball from ``ftp.iana.org/tz``.
+ filename is the timezone tarball from ftp.iana.org/tz.
"""
tmpdir = tempfile.mkdtemp()
zonedir = os.path.join(tmpdir, "zoneinfo")
moduledir = os.path.dirname(__file__)
try:
- with TarFile.open(filename) as tf:
+ with tar_open(filename) as tf:
for name in zonegroups:
tf.extract(name, tmpdir)
filepaths = [os.path.join(tmpdir, n) for n in zonegroups]
@@ -32,14 +31,13 @@ def rebuild(filename, tag=None, format="gz", zonegroups=[], metadata=None):
with open(os.path.join(zonedir, METADATA_FN), 'w') as f:
json.dump(metadata, f, indent=4, sort_keys=True)
target = os.path.join(moduledir, ZONEFILENAME)
- with TarFile.open(target, "w:%s" % format) as tf:
+ with tar_open(target, "w:%s" % format) as tf:
for entry in os.listdir(zonedir):
entrypath = os.path.join(zonedir, entry)
tf.add(entrypath, entry)
finally:
shutil.rmtree(tmpdir)
-
def _print_on_nosuchfile(e):
"""Print helpful troubleshooting message