diff options
author | morpheus65535 <[email protected]> | 2022-01-23 23:07:52 -0500 |
---|---|---|
committer | morpheus65535 <[email protected]> | 2022-01-23 23:07:52 -0500 |
commit | 0c3c5a02a75bc61b6bf6e303de20e11741d2afac (patch) | |
tree | 30ae1d524ffe5d54172b7a4a8445d90c3461e659 /libs/dateutil | |
parent | 36bf0d219d0432c20e6314e0ce752b36f4d88e3c (diff) | |
download | bazarr-0c3c5a02a75bc61b6bf6e303de20e11741d2afac.tar.gz bazarr-0c3c5a02a75bc61b6bf6e303de20e11741d2afac.zip |
Upgraded vendored Python dependencies to the latest versions and removed the unused dependencies.v1.0.3-beta.16
Diffstat (limited to 'libs/dateutil')
35 files changed, 4328 insertions, 2998 deletions
diff --git a/libs/dateutil/__init__.py b/libs/dateutil/__init__.py index ba89aa70b..0defb82e2 100644 --- a/libs/dateutil/__init__.py +++ b/libs/dateutil/__init__.py @@ -1,2 +1,8 @@ # -*- coding: utf-8 -*- -__version__ = "2.6.0" +try: + from ._version import version as __version__ +except ImportError: + __version__ = 'unknown' + +__all__ = ['easter', 'parser', 'relativedelta', 'rrule', 'tz', + 'utils', 'zoneinfo'] diff --git a/libs/dateutil/_common.py b/libs/dateutil/_common.py index cd2a33860..4eb2659bd 100644 --- a/libs/dateutil/_common.py +++ b/libs/dateutil/_common.py @@ -2,6 +2,7 @@ Common code used in multiple modules. """ + class weekday(object): __slots__ = ["weekday", "n"] @@ -23,7 +24,14 @@ class weekday(object): return False return True - __hash__ = None + def __hash__(self): + return hash(( + self.weekday, + self.n, + )) + + def __ne__(self, other): + return not (self == other) def __repr__(self): s = ("MO", "TU", "WE", "TH", "FR", "SA", "SU")[self.weekday] @@ -31,3 +39,5 @@ class weekday(object): return s else: return "%s(%+d)" % (s, self.n) + +# vim:ts=4:sw=4:et diff --git a/libs/dateutil/_version.py b/libs/dateutil/_version.py index 713fe0dfe..b723056a7 100644 --- a/libs/dateutil/_version.py +++ b/libs/dateutil/_version.py @@ -1,4 +1,5 @@ # coding: utf-8 # file generated by setuptools_scm # don't change, don't track in version control -version = '2.7.3' +version = '2.8.2' +version_tuple = (2, 8, 2) diff --git a/libs/dateutil/easter.py b/libs/dateutil/easter.py index e4def97f9..f74d1f744 100644 --- a/libs/dateutil/easter.py +++ b/libs/dateutil/easter.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- """ -This module offers a generic easter computing method for any given year, using +This module offers a generic Easter computing method for any given year, using Western, Orthodox or Julian algorithms. """ @@ -21,15 +21,15 @@ def easter(year, method=EASTER_WESTERN): quoted in "Explanatory Supplement to the Astronomical Almanac", P. Kenneth Seidelmann, editor. - This algorithm implements three different easter + This algorithm implements three different Easter calculation methods: - 1 - Original calculation in Julian calendar, valid in - dates after 326 AD - 2 - Original method, with date converted to Gregorian - calendar, valid in years 1583 to 4099 - 3 - Revised method, in Gregorian calendar, valid in - years 1583 to 4099 as well + 1. Original calculation in Julian calendar, valid in + dates after 326 AD + 2. Original method, with date converted to Gregorian + calendar, valid in years 1583 to 4099 + 3. Revised method, in Gregorian calendar, valid in + years 1583 to 4099 as well These methods are represented by the constants: @@ -41,11 +41,11 @@ def easter(year, method=EASTER_WESTERN): More about the algorithm may be found at: - http://users.chariot.net.au/~gmarts/eastalg.htm + `GM Arts: Easter Algorithms <http://www.gmarts.org/index.php?go=415>`_ and - http://www.tondering.dk/claus/calendar.html + `The Calendar FAQ: Easter <https://www.tondering.dk/claus/cal/easter.php>`_ """ diff --git a/libs/dateutil/parser.py b/libs/dateutil/parser.py deleted file mode 100644 index 147b3f2ca..000000000 --- a/libs/dateutil/parser.py +++ /dev/null @@ -1,1360 +0,0 @@ -# -*- 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/parser/__init__.py b/libs/dateutil/parser/__init__.py index 216762c09..d174b0e4d 100644 --- a/libs/dateutil/parser/__init__.py +++ b/libs/dateutil/parser/__init__.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from ._parser import parse, parser, parserinfo +from ._parser import parse, parser, parserinfo, ParserError from ._parser import DEFAULTPARSER, DEFAULTTZPARSER from ._parser import UnknownTimezoneWarning @@ -9,6 +9,7 @@ from .isoparser import isoparser, isoparse __all__ = ['parse', 'parser', 'parserinfo', 'isoparse', 'isoparser', + 'ParserError', 'UnknownTimezoneWarning'] diff --git a/libs/dateutil/parser/_parser.py b/libs/dateutil/parser/_parser.py index 9d2bb795c..37d1663b2 100644 --- a/libs/dateutil/parser/_parser.py +++ b/libs/dateutil/parser/_parser.py @@ -20,11 +20,11 @@ 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>`_ + <https://www.cl.cam.ac.uk/~mgk25/iso-time.html>`_ +- `W3C Date and Time Formats <https://www.w3.org/TR/NOTE-datetime>`_ - `Time Formats (Planetary Rings Node) <https://pds-rings.seti.org:443/tools/time_formats.html>`_ - `CPAN ParseDate module - <http://search.cpan.org/~muir/Time-modules-2013.0912/lib/Time/ParseDate.pm>`_ + <https://metacpan.org/pod/release/MUIR/Time-modules-2013.0912/lib/Time/ParseDate.pm>`_ - `Java SimpleDateFormat Class <https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html>`_ """ @@ -40,7 +40,7 @@ from calendar import monthrange from io import StringIO import six -from six import binary_type, integer_types, text_type +from six import integer_types, text_type from decimal import Decimal @@ -49,7 +49,7 @@ from warnings import warn from .. import relativedelta from .. import tz -__all__ = ["parse", "parserinfo"] +__all__ = ["parse", "parserinfo", "ParserError"] # TODO: pandas.core.tools.datetimes imports this explicitly. Might be worth @@ -60,14 +60,8 @@ class _timelex(object): _split_decimal = re.compile("([.,])") def __init__(self, instream): - if six.PY2: - # In Python 2, we can't duck type properly because unicode has - # a 'decode' function, and we'd be double-decoding - if isinstance(instream, (binary_type, bytearray)): - instream = instream.decode() - else: - if getattr(instream, 'decode', None) is not None: - instream = instream.decode() + if isinstance(instream, (bytes, bytearray)): + instream = instream.decode() if isinstance(instream, text_type): instream = StringIO(instream) @@ -291,7 +285,7 @@ class parserinfo(object): ("s", "second", "seconds")] AMPM = [("am", "a"), ("pm", "p")] - UTCZONE = ["UTC", "GMT", "Z"] + UTCZONE = ["UTC", "GMT", "Z", "z"] PERTAIN = ["of"] TZOFFSET = {} # TODO: ERA = ["AD", "BC", "CE", "BCE", "Stardate", @@ -388,7 +382,8 @@ class parserinfo(object): 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': + if ((res.tzoffset == 0 and not res.tzname) or + (res.tzname == 'Z' or res.tzname == 'z')): res.tzname = "UTC" res.tzoffset = 0 elif res.tzoffset != 0 and res.tzname and self.utczone(res.tzname): @@ -422,7 +417,7 @@ class _ymd(list): elif not self.has_month: return 1 <= value <= 31 elif not self.has_year: - # Be permissive, assume leapyear + # Be permissive, assume leap year month = self[self.mstridx] return 1 <= value <= monthrange(2000, month)[1] else: @@ -538,7 +533,7 @@ class _ymd(list): year, month, day = self else: # 01-Jan-01 - # Give precendence to day-first, since + # Give precedence to day-first, since # two-digit years is usually hand-written. day, month, year = self @@ -625,7 +620,7 @@ class parser(object): first element being a :class:`datetime.datetime` object, the second a tuple containing the fuzzy tokens. - :raises ValueError: + :raises ParserError: 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. @@ -645,12 +640,15 @@ class parser(object): res, skipped_tokens = self._parse(timestr, **kwargs) if res is None: - raise ValueError("Unknown string format:", timestr) + raise ParserError("Unknown string format: %s", timestr) if len(res) == 0: - raise ValueError("String does not contain a date:", timestr) + raise ParserError("String does not contain a date: %s", timestr) - ret = self._build_naive(res, default) + try: + ret = self._build_naive(res, default) + except ValueError as e: + six.raise_from(ParserError(str(e) + ": %s", timestr), e) if not ignoretz: ret = self._build_tzaware(ret, res, tzinfos) @@ -1021,7 +1019,7 @@ class parser(object): hms_idx = idx + 2 elif idx > 0 and info.hms(tokens[idx-1]) is not None: - # There is a "h", "m", or "s" preceeding this token. Since neither + # There is a "h", "m", or "s" preceding this token. Since neither # of the previous cases was hit, there is no label following this # token, so we use the previous label. # e.g. the "04" in "12h04" @@ -1060,7 +1058,8 @@ class parser(object): tzname is None and tzoffset is None and len(token) <= 5 and - all(x in string.ascii_uppercase for x in token)) + (all(x in string.ascii_uppercase for x in token) + or token in self.info.UTCZONE)) def _ampm_valid(self, hour, ampm, fuzzy): """ @@ -1100,7 +1099,7 @@ class parser(object): def _parse_min_sec(self, value): # TODO: Every usage of this function sets res.second to the return # value. Are there any cases where second will be returned as None and - # we *dont* want to set res.second = None? + # we *don't* want to set res.second = None? minute = int(value) second = None @@ -1109,14 +1108,6 @@ class parser(object): second = int(60 * sec_remainder) return (minute, second) - def _parsems(self, 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]) - def _parse_hms(self, idx, tokens, info, hms_idx): # TODO: Is this going to admit a lot of false-positives for when we # just happen to have digits and "h", "m" or "s" characters in non-date @@ -1135,21 +1126,35 @@ class parser(object): return (new_idx, hms) - def _recombine_skipped(self, tokens, skipped_idxs): - """ - >>> tokens = ["foo", " ", "bar", " ", "19June2000", "baz"] - >>> skipped_idxs = [0, 1, 2, 5] - >>> _recombine_skipped(tokens, skipped_idxs) - ["foo bar", "baz"] - """ - skipped_tokens = [] - for i, idx in enumerate(sorted(skipped_idxs)): - if i > 0 and idx - 1 == skipped_idxs[i - 1]: - skipped_tokens[-1] = skipped_tokens[-1] + tokens[idx] - else: - skipped_tokens.append(tokens[idx]) + # ------------------------------------------------------------------ + # Handling for individual tokens. These are kept as methods instead + # of functions for the sake of customizability via subclassing. - return skipped_tokens + def _parsems(self, 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]) + + def _to_decimal(self, val): + try: + decimal_value = Decimal(val) + # See GH 662, edge case, infinite value should not be converted + # via `_to_decimal` + if not decimal_value.is_finite(): + raise ValueError("Converted decimal value is infinite or NaN") + except Exception as e: + msg = "Could not convert %s to decimal" % val + six.raise_from(ValueError(msg), e) + else: + return decimal_value + + # ------------------------------------------------------------------ + # Post-Parsing construction of datetime output. These are kept as + # methods instead of functions for the sake of customizability via + # subclassing. def _build_tzinfo(self, tzinfos, tzname, tzoffset): if callable(tzinfos): @@ -1164,6 +1169,9 @@ class parser(object): tzinfo = tz.tzstr(tzdata) elif isinstance(tzdata, integer_types): tzinfo = tz.tzoffset(tzname, tzdata) + else: + raise TypeError("Offset must be tzinfo subclass, tz string, " + "or int offset.") return tzinfo def _build_tzaware(self, naive, res, tzinfos): @@ -1181,10 +1189,10 @@ class parser(object): # This is mostly relevant for winter GMT zones parsed in the UK if (aware.tzname() != res.tzname and res.tzname in self.info.UTCZONE): - aware = aware.replace(tzinfo=tz.tzutc()) + aware = aware.replace(tzinfo=tz.UTC) elif res.tzoffset == 0: - aware = naive.replace(tzinfo=tz.tzutc()) + aware = naive.replace(tzinfo=tz.UTC) elif res.tzoffset: aware = naive.replace(tzinfo=tz.tzoffset(res.tzname, res.tzoffset)) @@ -1239,17 +1247,21 @@ class parser(object): return dt - def _to_decimal(self, val): - try: - decimal_value = Decimal(val) - # See GH 662, edge case, infinite value should not be converted via `_to_decimal` - if not decimal_value.is_finite(): - raise ValueError("Converted decimal value is infinite or NaN") - except Exception as e: - msg = "Could not convert %s to decimal" % val - six.raise_from(ValueError(msg), e) - else: - return decimal_value + def _recombine_skipped(self, tokens, skipped_idxs): + """ + >>> tokens = ["foo", " ", "bar", " ", "19June2000", "baz"] + >>> skipped_idxs = [0, 1, 2, 5] + >>> _recombine_skipped(tokens, skipped_idxs) + ["foo bar", "baz"] + """ + skipped_tokens = [] + for i, idx in enumerate(sorted(skipped_idxs)): + if i > 0 and idx - 1 == skipped_idxs[i - 1]: + skipped_tokens[-1] = skipped_tokens[-1] + tokens[idx] + else: + skipped_tokens.append(tokens[idx]) + + return skipped_tokens DEFAULTPARSER = parser() @@ -1341,10 +1353,10 @@ def parse(timestr, parserinfo=None, **kwargs): 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 ParserError: + Raised for invalid or unknown string formats, 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 @@ -1573,6 +1585,29 @@ DEFAULTTZPARSER = _tzparser() def _parsetz(tzstr): return DEFAULTTZPARSER.parse(tzstr) + +class ParserError(ValueError): + """Exception subclass used for any failure to parse a datetime string. + + This is a subclass of :py:exc:`ValueError`, and should be raised any time + earlier versions of ``dateutil`` would have raised ``ValueError``. + + .. versionadded:: 2.8.1 + """ + def __str__(self): + try: + return self.args[0] % self.args[1:] + except (TypeError, IndexError): + return super(ParserError, self).__str__() + + def __repr__(self): + args = ", ".join("'%s'" % arg for arg in self.args) + return "%s(%s)" % (self.__class__.__name__, args) + + class UnknownTimezoneWarning(RuntimeWarning): - """Raised when the parser finds a timezone it cannot parse into a tzinfo""" + """Raised when the parser finds a timezone it cannot parse into a tzinfo. + + .. versionadded:: 2.7.0 + """ # vim:ts=4:sw=4:et diff --git a/libs/dateutil/parser/isoparser.py b/libs/dateutil/parser/isoparser.py index cd27f93d9..5d7bee380 100644 --- a/libs/dateutil/parser/isoparser.py +++ b/libs/dateutil/parser/isoparser.py @@ -88,10 +88,12 @@ class isoparser(object): - ``hh`` - ``hh:mm`` or ``hhmm`` - ``hh:mm:ss`` or ``hhmmss`` - - ``hh:mm:ss.sss`` or ``hh:mm:ss.ssssss`` (3-6 sub-second digits) + - ``hh:mm:ss.ssssss`` (Up to 6 sub-second digits) Midnight is a special case for `hh`, as the standard supports both - 00:00 and 24:00 as a representation. + 00:00 and 24:00 as a representation. The decimal separator can be + either a dot or a comma. + .. caution:: @@ -137,6 +139,10 @@ class isoparser(object): else: raise ValueError('String contains unknown ISO components') + if len(components) > 3 and components[3] == 24: + components[3] = 0 + return datetime(*components) + timedelta(days=1) + return datetime(*components) @_takes_ascii @@ -153,7 +159,7 @@ class isoparser(object): components, pos = self._parse_isodate(datestr) if pos < len(datestr): raise ValueError('String contains unknown ISO ' + - 'components: {}'.format(datestr)) + 'components: {!r}'.format(datestr.decode('ascii'))) return date(*components) @_takes_ascii @@ -167,7 +173,10 @@ class isoparser(object): :return: Returns a :class:`datetime.time` object """ - return time(*self._parse_isotime(timestr)) + components = self._parse_isotime(timestr) + if components[0] == 24: + components[0] = 0 + return time(*components) @_takes_ascii def parse_tzstr(self, tzstr, zero_as_utc=True): @@ -190,10 +199,9 @@ class isoparser(object): return self._parse_tzstr(tzstr, zero_as_utc=zero_as_utc) # Constants - _MICROSECOND_END_REGEX = re.compile(b'[-+Z]+') _DATE_SEP = b'-' _TIME_SEP = b':' - _MICRO_SEP = b'.' + _FRACTION_REGEX = re.compile(b'[\\.,]([0-9]+)') def _parse_isodate(self, dt_str): try: @@ -325,39 +333,42 @@ class isoparser(object): pos = 0 comp = -1 - if len(timestr) < 2: + if len_str < 2: raise ValueError('ISO time too short') - has_sep = len_str >= 3 and timestr[2:3] == self._TIME_SEP + has_sep = False while pos < len_str and comp < 5: comp += 1 - if timestr[pos:pos + 1] in b'-+Z': + if timestr[pos:pos + 1] in b'-+Zz': # Detect time zone boundary components[-1] = self._parse_tzstr(timestr[pos:]) pos = len_str break + if comp == 1 and timestr[pos:pos+1] == self._TIME_SEP: + has_sep = True + pos += 1 + elif comp == 2 and has_sep: + if timestr[pos:pos+1] != self._TIME_SEP: + raise ValueError('Inconsistent use of colon separator') + pos += 1 + if comp < 3: # Hour, minute, second components[comp] = int(timestr[pos:pos + 2]) pos += 2 - if (has_sep and pos < len_str and - timestr[pos:pos + 1] == self._TIME_SEP): - pos += 1 if comp == 3: - # Microsecond - if timestr[pos:pos + 1] != self._MICRO_SEP: + # Fraction of a second + frac = self._FRACTION_REGEX.match(timestr[pos:]) + if not frac: continue - pos += 1 - us_str = self._MICROSECOND_END_REGEX.split(timestr[pos:pos + 6], - 1)[0] - + us_str = frac.group(1)[:6] # Truncate to microseconds components[comp] = int(us_str) * 10**(6 - len(us_str)) - pos += len(us_str) + pos += len(frac.group()) if pos < len_str: raise ValueError('Unused components in ISO string') @@ -366,13 +377,12 @@ class isoparser(object): # Standard supports 00:00 and 24:00 as representations of midnight if any(component != 0 for component in components[1:4]): raise ValueError('Hour may only be 24 at 24:00:00.000') - components[0] = 0 return components def _parse_tzstr(self, tzstr, zero_as_utc=True): - if tzstr == b'Z': - return tz.tzutc() + if tzstr == b'Z' or tzstr == b'z': + return tz.UTC if len(tzstr) not in {3, 5, 6}: raise ValueError('Time zone offset must be 1, 3, 5 or 6 characters') @@ -391,7 +401,7 @@ class isoparser(object): minutes = int(tzstr[(4 if tzstr[3:4] == self._TIME_SEP else 3):]) if zero_as_utc and hours == 0 and minutes == 0: - return tz.tzutc() + return tz.UTC else: if minutes > 59: raise ValueError('Invalid minutes in time zone offset') diff --git a/libs/dateutil/relativedelta.py b/libs/dateutil/relativedelta.py index 7e3bd12ac..a9e85f7e6 100644 --- a/libs/dateutil/relativedelta.py +++ b/libs/dateutil/relativedelta.py @@ -10,16 +10,20 @@ 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"] class relativedelta(object): """ - The relativedelta type is based on the specification of the excellent - work done by M.-A. Lemburg in his - `mx.DateTime <http://www.egenix.com/files/python/mxDateTime.html>`_ extension. + The relativedelta type is designed to be applied to an existing datetime and + can replace specific components of that datetime, or represents an interval + of time. + + It 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. 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,22 +38,26 @@ 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 aritmetic + relativedelta with absolute information does not perform an arithmetic operation, but rather REPLACES the corresponding value in the original datetime with the value(s) in relativedelta. years, months, weeks, days, hours, minutes, seconds, microseconds: Relative information, may be negative (argument is plural); adding or subtracting a relativedelta with relative information performs - the corresponding aritmetic operation on the original datetime value + the corresponding arithmetic 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. + weekday: + One of the weekday instances (MO, TU, etc) available in the + relativedelta module. 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. This argument is always + relative e.g. if the calculated date is already Monday, using MO(1) + or MO(-1) won't change the day. To effectively make it absolute, use + it in combination with the day argument (e.g. day=1, MO(1) for first + Monday of the month). leapdays: Will add given days to the date found, if year is a leap @@ -59,33 +67,39 @@ class relativedelta(object): Set the yearday or the non-leap year day (jump leap days). These are converted to day/month/leapdays information. - Here is the behavior of operations with relativedelta: + 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). - 1. Calculate the absolute year, using the 'year' argument, or the - original datetime year, if the argument is not present. + The order of attributes considered when this relativedelta is + added to a datetime is: - 2. Add the relative 'years' argument to the absolute year. + 1. Year + 2. Month + 3. Day + 4. Hours + 5. Minutes + 6. Seconds + 7. Microseconds - 3. Do steps 1 and 2 for month/months. + Finally, weekday is applied, using the rule described above. - 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. + For example - 5. Add the relative 'days' argument to the absolute day. Notice - that the 'weeks' argument is multiplied by 7 and added to - 'days'. + >>> from datetime import datetime + >>> from dateutil.relativedelta import relativedelta, MO + >>> dt = datetime(2018, 4, 9, 13, 37, 0) + >>> delta = relativedelta(hours=25, day=1, weekday=MO(1)) + >>> dt + delta + datetime.datetime(2018, 4, 2, 14, 37) - 6. Do steps 1 and 2 for hour/hours, minute/minutes, second/seconds, - microsecond/microseconds. + 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. - 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, @@ -95,11 +109,6 @@ 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,9 +168,14 @@ 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 = years - self.months = months + self.years = int(years) + self.months = int(months) self.days = days + weeks * 7 self.leapdays = leapdays self.hours = hours @@ -186,7 +200,6 @@ 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: @@ -250,7 +263,8 @@ class relativedelta(object): @property def weeks(self): - return self.days // 7 + return int(self.days / 7.0) + @weeks.setter def weeks(self, value): self.days = self.days - (self.weeks * 7) + value * 7 @@ -271,7 +285,7 @@ class relativedelta(object): values for the relative attributes. >>> relativedelta(days=1.5, hours=2).normalized() - relativedelta(days=1, hours=14) + relativedelta(days=+1, hours=+14) :return: Returns a :class:`dateutil.relativedelta.relativedelta` object. @@ -311,14 +325,22 @@ class relativedelta(object): microseconds=(other.microseconds + self.microseconds), leapdays=other.leapdays or self.leapdays, - 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 + 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 self.microsecond)) if isinstance(other, datetime.timedelta): return self.__class__(years=self.years, @@ -396,14 +418,41 @@ class relativedelta(object): seconds=self.seconds - other.seconds, microseconds=self.microseconds - other.microseconds, leapdays=self.leapdays or other.leapdays, - 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) + 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) def __neg__(self): return self.__class__(years=-self.years, @@ -495,7 +544,25 @@ class relativedelta(object): self.second == other.second and self.microsecond == other.microsecond) - __hash__ = None + 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, + )) def __ne__(self, other): return not self.__eq__(other) @@ -525,6 +592,7 @@ 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 da94351b9..b3203393c 100644 --- a/libs/dateutil/rrule.py +++ b/libs/dateutil/rrule.py @@ -2,27 +2,29 @@ """ The rrule module offers a small, complete, and very fast, implementation of the recurrence rules documented in the -`iCalendar RFC <http://www.ietf.org/rfc/rfc2445.txt>`_, +`iCalendar RFC <https://tools.ietf.org/html/rfc5545>`_, including support for caching of results. """ -import itertools -import datetime import calendar +import datetime +import heapq +import itertools +import re import sys - -try: - from math import gcd -except ImportError: - from fractions import gcd +from functools import wraps +# For warning about deprecation of until and count +from warnings import warn from six import advance_iterator, integer_types + from six.moves import _thread, range -import heapq from ._common import weekday as weekdaybase -# For warning about deprecation of until and count -from warnings import warn +try: + from math import gcd +except ImportError: + from fractions import gcd __all__ = ["rrule", "rruleset", "rrulestr", "YEARLY", "MONTHLY", "WEEKLY", "DAILY", @@ -46,7 +48,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, @@ -60,6 +62,7 @@ FREQNAMES = ['YEARLY','MONTHLY','WEEKLY','DAILY','HOURLY','MINUTELY','SECONDLY'] easter = None parser = None + class weekday(weekdaybase): """ This version of weekday does not allow n = 0. @@ -70,7 +73,8 @@ 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): @@ -78,6 +82,7 @@ def _invalidates_cache(f): Decorator for rruleset methods which may invalidate the cached length. """ + @wraps(f) def inner_func(self, *args, **kwargs): rv = f(self, *args, **kwargs) self._invalidate_cache() @@ -174,7 +179,7 @@ class rrulebase(object): return False return False - # __len__() introduces a large performance penality. + # __len__() introduces a large performance penalty. def count(self): """ Returns the number of recurrences in this set. It will have go trough the whole recurrence, if this hasn't been done before. """ @@ -256,13 +261,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 @@ -333,10 +338,6 @@ 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 @@ -353,20 +354,26 @@ class rrule(rrulebase): from calendar.firstweekday(), and may be modified by calendar.setfirstweekday(). :param count: - How many occurrences will be generated. + If given, this determines how many occurrences will be generated. .. note:: - As of version 2.5.0, the use of the ``until`` keyword together - with the ``count`` keyword is deprecated per RFC-2445 Sec. 4.3.10. + As of version 2.5.0, the use of the keyword ``until`` in conjunction + with ``count`` is deprecated, to make sure ``dateutil`` is fully + compliant with `RFC-5545 Sec. 3.3.10 <https://tools.ietf.org/ + html/rfc5545#section-3.3.10>`_. Therefore, ``until`` and ``count`` + **must not** occur in the same call to ``rrule``. :param until: - If given, this must be a datetime instance, that will specify the + If given, this must be a datetime instance specifying the upper-bound 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-2445 Sec. 4.3.10. + As of version 2.5.0, the use of the keyword ``until`` in conjunction + with ``count`` is deprecated, to make sure ``dateutil`` is fully + compliant with `RFC-5545 Sec. 3.3.10 <https://tools.ietf.org/ + html/rfc5545#section-3.3.10>`_. Therefore, ``until`` and ``count`` + **must not** occur in the same call to ``rrule``. :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,6 +390,11 @@ 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 @@ -408,11 +420,10 @@ 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 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 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. """ def __init__(self, freq, dtstart=None, interval=1, wkst=None, count=None, until=None, bysetpos=None, @@ -423,7 +434,10 @@ class rrule(rrulebase): super(rrule, self).__init__(cache) global easter if not dtstart: - dtstart = datetime.datetime.now().replace(microsecond=0) + if until and until.tzinfo: + dtstart = datetime.datetime.now(tz=until.tzinfo).replace(microsecond=0) + else: + dtstart = datetime.datetime.now().replace(microsecond=0) elif not isinstance(dtstart, datetime.datetime): dtstart = datetime.datetime.fromordinal(dtstart.toordinal()) else: @@ -444,8 +458,22 @@ class rrule(rrulebase): until = datetime.datetime.fromordinal(until.toordinal()) self._until = until - if count and until: - warn("Using both 'count' and 'until' is inconsistent with RFC 2445" + 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" " and has been deprecated in dateutil. Future versions will " "raise an error.", DeprecationWarning) @@ -533,8 +561,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: @@ -582,13 +610,13 @@ class rrule(rrulebase): self._byweekday = tuple(sorted(self._byweekday)) orig_byweekday = [weekday(x) for x in self._byweekday] else: - orig_byweekday = tuple() + orig_byweekday = () if self._bynweekday is not None: self._bynweekday = tuple(sorted(self._bynweekday)) orig_bynweekday = [weekday(*x) for x in self._bynweekday] else: - orig_bynweekday = tuple() + orig_bynweekday = () if 'byweekday' not in self._original_rule: self._original_rule['byweekday'] = tuple(itertools.chain( @@ -597,7 +625,7 @@ class rrule(rrulebase): # byhour if byhour is None: if freq < HOURLY: - self._byhour = set((dtstart.hour,)) + self._byhour = {dtstart.hour} else: self._byhour = None else: @@ -617,7 +645,7 @@ class rrule(rrulebase): # byminute if byminute is None: if freq < MINUTELY: - self._byminute = set((dtstart.minute,)) + self._byminute = {dtstart.minute} else: self._byminute = None else: @@ -672,7 +700,7 @@ class rrule(rrulebase): def __str__(self): """ Output a string that would generate this RRULE if passed to rrulestr. - This is mostly compatible with RFC2445, except for the + This is mostly compatible with RFC5545, except for the dateutil-specific extension BYEASTER. """ @@ -689,7 +717,7 @@ class rrule(rrulebase): if self._wkst: parts.append('WKST=' + repr(weekday(self._wkst))[0:2]) - if self._count: + if self._count is not None: parts.append('COUNT=' + str(self._count)) if self._until: @@ -697,7 +725,7 @@ class rrule(rrulebase): if self._original_rule.get('byweekday') is not None: # The str() method on weekday objects doesn't generate - # RFC2445-compliant strings, so we should modify that. + # RFC5545-compliant strings, so we should modify that. original_rule = dict(self._original_rule) wday_strings = [] for wday in original_rule['byweekday']: @@ -728,7 +756,7 @@ class rrule(rrulebase): parts.append(partfmt.format(name=name, vals=(','.join(str(v) for v in value)))) - output.append(';'.join(parts)) + output.append('RRULE:' + ';'.join(parts)) return '\n'.join(output) def replace(self, **kwargs): @@ -745,7 +773,6 @@ 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() @@ -844,13 +871,13 @@ class rrule(rrulebase): self._len = total return elif res >= self._dtstart: - total += 1 - yield res - if count: + if count is not None: count -= 1 - if not count: + if count < 0: self._len = total return + total += 1 + yield res else: for i in dayset[start:end]: if i is not None: @@ -861,14 +888,15 @@ class rrule(rrulebase): self._len = total return elif res >= self._dtstart: - total += 1 - yield res - if count: + if count is not None: count -= 1 - if not count: + if count < 0: self._len = total return + total += 1 + yield res + # Handle frequency and interval fixday = False if freq == YEARLY: @@ -1385,7 +1413,52 @@ class rruleset(rrulebase): self._len = total + + class _rrulestr(object): + """ Parses a string representation of a recurrence rule or set of + recurrence rules. + + :param s: + Required, a string defining one or more recurrence rules. + + :param dtstart: + If given, used as the default recurrence start if not specified in the + rule string. + + :param cache: + If set ``True`` caching of results will be enabled, improving + performance of multiple queries considerably. + + :param unfold: + If set ``True`` indicates that a rule string is split over more + than one line and should be joined before processing. + + :param forceset: + If set ``True`` forces a :class:`dateutil.rrule.rruleset` to + be returned. + + :param compatible: + If set ``True`` forces ``unfold`` and ``forceset`` to be ``True``. + + :param ignoretz: + If set ``True``, time zones in parsed strings are ignored and a naive + :class:`datetime.datetime` object is returned. + + :param tzids: + If given, a callable or mapping used to retrieve a + :class:`datetime.tzinfo` from a string representation. + Defaults to :func:`dateutil.tz.gettz`. + + :param tzinfos: + Additional time zone names / aliases which may be present in a string + representation. See :func:`dateutil.parser.parse` for more + information. + + :return: + Returns a :class:`dateutil.rrule.rruleset` or + :class:`dateutil.rrule.rrule` + """ _freq_map = {"YEARLY": YEARLY, "MONTHLY": MONTHLY, @@ -1487,6 +1560,58 @@ class _rrulestr(object): raise ValueError("invalid '%s': %s" % (name, value)) return rrule(dtstart=dtstart, cache=cache, **rrkwargs) + def _parse_date_value(self, date_value, parms, rule_tzids, + ignoretz, tzids, tzinfos): + global parser + if not parser: + from dateutil import parser + + datevals = [] + value_found = False + TZID = None + + for parm in parms: + if parm.startswith("TZID="): + try: + tzkey = rule_tzids[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 + + # RFC 5445 3.8.2.4: The VALUE parameter is optional, but may be found + # only once. + if parm not in {"VALUE=DATE-TIME", "VALUE=DATE"}: + raise ValueError("unsupported parm: " + parm) + else: + if value_found: + msg = ("Duplicate value parameter found in: " + parm) + raise ValueError(msg) + value_found = True + + for datestr in date_value.split(','): + date = parser.parse(datestr, ignoretz=ignoretz, tzinfos=tzinfos) + if TZID is not None: + if date.tzinfo is None: + date = date.replace(tzinfo=TZID) + else: + raise ValueError('DTSTART/EXDATE specifies multiple timezone') + datevals.append(date) + + return datevals + def _parse_rfc(self, s, dtstart=None, cache=False, @@ -1494,11 +1619,17 @@ 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") @@ -1553,17 +1684,18 @@ class _rrulestr(object): raise ValueError("unsupported EXRULE parm: "+parm) exrulevals.append(value) elif name == "EXDATE": - for parm in parms: - if parm != "VALUE=DATE-TIME": - raise ValueError("unsupported RDATE parm: "+parm) - exdatevals.append(value) + exdatevals.extend( + self._parse_date_value(value, parms, + TZID_NAMES, ignoretz, + tzids, tzinfos) + ) elif name == "DTSTART": - for parm in parms: - raise ValueError("unsupported DTSTART parm: "+parm) - if not parser: - from dateutil import parser - dtstart = parser.parse(value, ignoretz=ignoretz, - tzinfos=tzinfos) + dtvals = self._parse_date_value(value, parms, TZID_NAMES, + ignoretz, tzids, tzinfos) + if len(dtvals) != 1: + raise ValueError("Multiple DTSTART values specified:" + + value) + dtstart = dtvals[0] else: raise ValueError("unsupported property: "+name) if (forceset or len(rrulevals) > 1 or rdatevals @@ -1585,10 +1717,7 @@ class _rrulestr(object): ignoretz=ignoretz, tzinfos=tzinfos)) for value in exdatevals: - for datestr in value.split(','): - rset.exdate(parser.parse(datestr, - ignoretz=ignoretz, - tzinfos=tzinfos)) + rset.exdate(value) if compatible and dtstart: rset.rdate(dtstart) return rset @@ -1602,6 +1731,7 @@ 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/_common.py b/libs/dateutil/test/_common.py index f77b53e4f..b8d204737 100644 --- a/libs/dateutil/test/_common.py +++ b/libs/dateutil/test/_common.py @@ -1,64 +1,12 @@ 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) +import pytest class PicklableMixin(object): @@ -81,7 +29,7 @@ class PicklableMixin(object): return nobj - def assertPicklable(self, obj, asfile=False, + def assertPicklable(self, obj, singleton=False, asfile=False, dump_kwargs=None, load_kwargs=None): """ Assert that an object can be pickled and unpickled. This assertion @@ -93,7 +41,8 @@ class PicklableMixin(object): load_kwargs = load_kwargs or {} nobj = get_nobj(obj, dump_kwargs, load_kwargs) - self.assertIsNot(obj, nobj) + if not singleton: + self.assertIsNot(obj, nobj) self.assertEqual(obj, nobj) @@ -138,7 +87,11 @@ class TZContextBase(object): def __enter__(self): if not self.tz_change_allowed(): - raise ValueError(self.tz_change_disallowed_message()) + msg = self.tz_change_disallowed_message() + pytest.skip(msg) + + # If this is used outside of a test suite, we still want an error. + raise ValueError(msg) # pragma: no cover self._old_tz = self.get_current_tz() self.set_current_tz(self.tzval) @@ -192,7 +145,7 @@ class TZWinContext(TZContextBase): p = subprocess.Popen(['tzutil', '/g'], stdout=subprocess.PIPE) ctzname, err = p.communicate() - ctzname = ctzname.decode() # Popen returns + ctzname = ctzname.decode() # Popen returns if p.returncode: raise OSError('Failed to get current time zone: ' + err) @@ -210,17 +163,6 @@ class TZWinContext(TZContextBase): ### -# 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): """ @@ -245,6 +187,7 @@ class NotAValueClass(object): __le__ = __rle__ = _op __ge__ = __rge__ = _op + NotAValue = NotAValueClass() @@ -278,11 +221,13 @@ class ComparesEqualClass(object): __rlt__ = __lt__ __rgt__ = __gt__ + ComparesEqual = ComparesEqualClass() + class UnsetTzClass(object): """ Sentinel class for unset time zone variable """ pass -UnsetTz = UnsetTzClass() +UnsetTz = UnsetTzClass() diff --git a/libs/dateutil/test/conftest.py b/libs/dateutil/test/conftest.py new file mode 100644 index 000000000..78ed70acb --- /dev/null +++ b/libs/dateutil/test/conftest.py @@ -0,0 +1,41 @@ +import os +import pytest + + +# Configure pytest to ignore xfailing tests +# See: https://stackoverflow.com/a/53198349/467366 +def pytest_collection_modifyitems(items): + for item in items: + marker_getter = getattr(item, 'get_closest_marker', None) + + # Python 3.3 support + if marker_getter is None: + marker_getter = item.get_marker + + marker = marker_getter('xfail') + + # Need to query the args because conditional xfail tests still have + # the xfail mark even if they are not expected to fail + if marker and (not marker.args or marker.args[0]): + item.add_marker(pytest.mark.no_cover) + + +def set_tzpath(): + """ + Sets the TZPATH variable if it's specified in an environment variable. + """ + tzpath = os.environ.get('DATEUTIL_TZPATH', None) + + if tzpath is None: + return + + path_components = tzpath.split(':') + + print("Setting TZPATH to {}".format(path_components)) + + from dateutil import tz + tz.TZPATHS.clear() + tz.TZPATHS.extend(path_components) + + +set_tzpath() diff --git a/libs/dateutil/test/property/test_isoparse_prop.py b/libs/dateutil/test/property/test_isoparse_prop.py new file mode 100644 index 000000000..f8e288f3d --- /dev/null +++ b/libs/dateutil/test/property/test_isoparse_prop.py @@ -0,0 +1,27 @@ +from hypothesis import given, assume +from hypothesis import strategies as st + +from dateutil import tz +from dateutil.parser import isoparse + +import pytest + +# Strategies +TIME_ZONE_STRATEGY = st.sampled_from([None, tz.UTC] + + [tz.gettz(zname) for zname in ('US/Eastern', 'US/Pacific', + 'Australia/Sydney', 'Europe/London')]) +ASCII_STRATEGY = st.characters(max_codepoint=127) + + +@given(dt=st.datetimes(timezones=TIME_ZONE_STRATEGY), sep=ASCII_STRATEGY) +def test_timespec_auto(dt, sep): + if dt.tzinfo is not None: + # Assume offset has no sub-second components + assume(dt.utcoffset().total_seconds() % 60 == 0) + + sep = str(sep) # Python 2.7 requires bytes + dtstr = dt.isoformat(sep=sep) + dt_rt = isoparse(dtstr) + + assert dt_rt == dt diff --git a/libs/dateutil/test/property/test_parser_prop.py b/libs/dateutil/test/property/test_parser_prop.py new file mode 100644 index 000000000..fdfd171e8 --- /dev/null +++ b/libs/dateutil/test/property/test_parser_prop.py @@ -0,0 +1,22 @@ +from hypothesis.strategies import integers +from hypothesis import given + +import pytest + +from dateutil.parser import parserinfo + + +@given(integers(min_value=100, max_value=9999)) +def test_convertyear(n): + assert n == parserinfo().convertyear(n) + + +@given(integers(min_value=-50, + max_value=49)) +def test_convertyear_no_specified_century(n): + p = parserinfo() + new_year = p._year + n + result = p.convertyear(new_year % 100, century_specified=False) + assert result == new_year diff --git a/libs/dateutil/test/property/test_tz_prop.py b/libs/dateutil/test/property/test_tz_prop.py new file mode 100644 index 000000000..ec6d271dc --- /dev/null +++ b/libs/dateutil/test/property/test_tz_prop.py @@ -0,0 +1,35 @@ +from datetime import datetime, timedelta + +import pytest +import six +from hypothesis import assume, given +from hypothesis import strategies as st + +from dateutil import tz as tz + +EPOCHALYPSE = datetime.fromtimestamp(2147483647) +NEGATIVE_EPOCHALYPSE = datetime.fromtimestamp(0) - timedelta(seconds=2147483648) + + [email protected]("gettz_arg", [None, ""]) +# TODO: Remove bounds when GH #590 is resolved +@given( + dt=st.datetimes( + min_value=NEGATIVE_EPOCHALYPSE, max_value=EPOCHALYPSE, timezones=st.just(tz.UTC), + ) +) +def test_gettz_returns_local(gettz_arg, dt): + act_tz = tz.gettz(gettz_arg) + if isinstance(act_tz, tz.tzlocal): + return + + dt_act = dt.astimezone(tz.gettz(gettz_arg)) + if six.PY2: + dt_exp = dt.astimezone(tz.tzlocal()) + else: + dt_exp = dt.astimezone() + + assert dt_act == dt_exp + assert dt_act.tzname() == dt_exp.tzname() + assert dt_act.utcoffset() == dt_exp.utcoffset() diff --git a/libs/dateutil/test/test_easter.py b/libs/dateutil/test/test_easter.py index b45d7fe89..cf2ec7f28 100644 --- a/libs/dateutil/test/test_easter.py +++ b/libs/dateutil/test/test_easter.py @@ -2,11 +2,7 @@ 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 +import pytest # List of easters between 1990 and 2050 western_easter_dates = [ @@ -77,23 +73,21 @@ julian_easter_dates = [ ] -class EasterTest(unittest.TestCase): - def testEasterWestern(self): - for easter_date in western_easter_dates: - self.assertEqual(easter_date, - easter(easter_date.year, EASTER_WESTERN)) [email protected]("easter_date", western_easter_dates) +def test_easter_western(easter_date): + assert easter_date == easter(easter_date.year, EASTER_WESTERN) + + [email protected]("easter_date", orthodox_easter_dates) +def test_easter_orthodox(easter_date): + assert easter_date == easter(easter_date.year, EASTER_ORTHODOX) + - def testEasterOrthodox(self): - for easter_date in orthodox_easter_dates: - self.assertEqual(easter_date, - easter(easter_date.year, EASTER_ORTHODOX)) [email protected]("easter_date", julian_easter_dates) +def test_easter_julian(easter_date): + assert easter_date == easter(easter_date.year, EASTER_JULIAN) - 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) +def test_easter_bad_method(): + with pytest.raises(ValueError): + easter(1975, 4) diff --git a/libs/dateutil/test/test_import_star.py b/libs/dateutil/test/test_import_star.py new file mode 100644 index 000000000..2fb709812 --- /dev/null +++ b/libs/dateutil/test/test_import_star.py @@ -0,0 +1,33 @@ +"""Test for the "import *" functionality. + +As import * can be only done at module level, it has been added in a separate file +""" +import pytest + +prev_locals = list(locals()) +from dateutil import * +new_locals = {name:value for name,value in locals().items() + if name not in prev_locals} +new_locals.pop('prev_locals') + + [email protected]_star +def test_imported_modules(): + """ Test that `from dateutil import *` adds modules in __all__ locally """ + import dateutil.easter + import dateutil.parser + import dateutil.relativedelta + import dateutil.rrule + import dateutil.tz + import dateutil.utils + import dateutil.zoneinfo + + assert dateutil.easter == new_locals.pop("easter") + assert dateutil.parser == new_locals.pop("parser") + assert dateutil.relativedelta == new_locals.pop("relativedelta") + assert dateutil.rrule == new_locals.pop("rrule") + assert dateutil.tz == new_locals.pop("tz") + assert dateutil.utils == new_locals.pop("utils") + assert dateutil.zoneinfo == new_locals.pop("zoneinfo") + + assert not new_locals diff --git a/libs/dateutil/test/test_imports.py b/libs/dateutil/test/test_imports.py index 1d8ac171e..60b86005c 100644 --- a/libs/dateutil/test/test_imports.py +++ b/libs/dateutil/test/test_imports.py @@ -1,149 +1,176 @@ import sys +import pytest -try: - import unittest2 as unittest -except ImportError: - import unittest +HOST_IS_WINDOWS = sys.platform.startswith('win') -class ImportEasterTest(unittest.TestCase): - """ Test that dateutil.easter-related imports work properly """ - def testEasterDirect(self): - import dateutil.easter +def test_import_version_str(): + """ Test that dateutil.__version__ can be imported""" + from dateutil import __version__ - def testEasterFrom(self): - from dateutil import easter - def testEasterStar(self): - from dateutil.easter import easter +def test_import_version_root(): + import dateutil + assert hasattr(dateutil, '__version__') -class ImportParserTest(unittest.TestCase): - """ Test that dateutil.parser-related imports work properly """ - def testParserDirect(self): - import dateutil.parser +# Test that dateutil.easter-related imports work properly +def test_import_easter_direct(): + import dateutil.easter - def testParserFrom(self): - from dateutil import parser - def testParserAll(self): - # All interface - from dateutil.parser import parse - from dateutil.parser import parserinfo +def test_import_easter_from(): + from dateutil import easter - # Other public classes - from dateutil.parser import parser - for var in (parse, parserinfo, parser): - self.assertIsNot(var, None) +def test_import_easter_start(): + from dateutil.easter import easter -class ImportRelativeDeltaTest(unittest.TestCase): - """ Test that dateutil.relativedelta-related imports work properly """ - def testRelativeDeltaDirect(self): - import dateutil.relativedelta +# Test that dateutil.parser-related imports work properly +def test_import_parser_direct(): + import dateutil.parser - 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 +def test_import_parser_from(): + from dateutil import parser - 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) +def test_import_parser_all(): + # All interface + from dateutil.parser import parse + from dateutil.parser import parserinfo + # Other public classes + from dateutil.parser import parser -class ImportRRuleTest(unittest.TestCase): - """ Test that dateutil.rrule related imports work properly """ - def testRRuleDirect(self): - import dateutil.rrule + for var in (parse, parserinfo, parser): + assert var is not None - 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 +# Test that dateutil.relativedelta-related imports work properly +def test_import_relative_delta_direct(): + import dateutil.relativedelta - 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) +def test_import_relative_delta_from(): + from dateutil import relativedelta - # In the public interface but not in all - from dateutil.rrule import weekday - self.assertIsNot(weekday, None) +def test_import_relative_delta_all(): + 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): + assert var is not None -class ImportTZTest(unittest.TestCase): - """ Test that dateutil.tz related imports work properly """ - def testTzDirect(self): - import dateutil.tz + # In the public interface but not in all + from dateutil.relativedelta import weekday + assert weekday is not None - 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 +# Test that dateutil.rrule related imports work properly +def test_import_rrule_direct(): + import dateutil.rrule - tz_all = ["tzutc", "tzoffset", "tzlocal", "tzfile", "tzrange", - "tzstr", "tzical", "gettz"] - tz_all += ["tzwin", "tzwinlocal"] if sys.platform.startswith("win") else [] - lvars = locals() +def test_import_rrule_from(): + from dateutil import rrule - for var in tz_all: - self.assertIsNot(lvars[var], None) +def test_import_rrule_all(): + 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 [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 + rr_all = (rrule, rruleset, rrulestr, + YEARLY, MONTHLY, WEEKLY, DAILY, + HOURLY, MINUTELY, SECONDLY, + MO, TU, WE, TH, FR, SA, SU) - def testTzwinFrom(self): - from dateutil import tzwin + for var in rr_all: + assert var is not None - def testTzwinStar(self): - tzwin_all = ["tzwin", "tzwinlocal"] + # In the public interface but not in all + from dateutil.rrule import weekday + assert weekday is not None -class ImportZoneInfoTest(unittest.TestCase): - def testZoneinfoDirect(self): - import dateutil.zoneinfo +# Test that dateutil.tz related imports work properly +def test_import_tztest_direct(): + import dateutil.tz - 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 +def test_import_tz_from(): + from dateutil import tz - zi_all = (gettz, gettz_db_metadata, rebuild) - for var in zi_all: - self.assertIsNot(var, None) +def test_import_tz_all(): + 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 + from dateutil.tz import UTC + from dateutil.tz import datetime_ambiguous + from dateutil.tz import datetime_exists + from dateutil.tz import resolve_imaginary + + tz_all = ["tzutc", "tzoffset", "tzlocal", "tzfile", "tzrange", + "tzstr", "tzical", "gettz", "datetime_ambiguous", + "datetime_exists", "resolve_imaginary", "UTC"] + + tz_all += ["tzwin", "tzwinlocal"] if sys.platform.startswith("win") else [] + lvars = locals() + + for var in tz_all: + assert lvars[var] is not None + +# Test that dateutil.tzwin related imports work properly [email protected](not HOST_IS_WINDOWS, reason="Requires Windows") +def test_import_tz_windows_direct(): + import dateutil.tzwin + + [email protected](not HOST_IS_WINDOWS, reason="Requires Windows") +def test_import_tz_windows_from(): + from dateutil import tzwin + + [email protected](not HOST_IS_WINDOWS, reason="Requires Windows") +def test_import_tz_windows_star(): + from dateutil.tzwin import tzwin + from dateutil.tzwin import tzwinlocal + + tzwin_all = [tzwin, tzwinlocal] + + for var in tzwin_all: + assert var is not None + + +# Test imports of Zone Info +def test_import_zone_info_direct(): + import dateutil.zoneinfo + + +def test_import_zone_info_from(): + from dateutil import zoneinfo + + +def test_import_zone_info_star(): + 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: + assert var is not None diff --git a/libs/dateutil/test/test_internals.py b/libs/dateutil/test/test_internals.py new file mode 100644 index 000000000..530813147 --- /dev/null +++ b/libs/dateutil/test/test_internals.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- +""" +Tests for implementation details, not necessarily part of the user-facing +API. + +The motivating case for these tests is #483, where we want to smoke-test +code that may be difficult to reach through the standard API calls. +""" + +import sys +import pytest + +from dateutil.parser._parser import _ymd +from dateutil import tz + +IS_PY32 = sys.version_info[0:2] == (3, 2) + + +def test_YMD_could_be_day(): + ymd = _ymd('foo bar 124 baz') + + ymd.append(2, 'M') + assert ymd.has_month + assert not ymd.has_year + assert ymd.could_be_day(4) + assert not ymd.could_be_day(-6) + assert not ymd.could_be_day(32) + + # Assumes leap year + assert ymd.could_be_day(29) + + ymd.append(1999) + assert ymd.has_year + assert not ymd.could_be_day(29) + + ymd.append(16, 'D') + assert ymd.has_day + assert not ymd.could_be_day(1) + + ymd = _ymd('foo bar 124 baz') + ymd.append(1999) + assert ymd.could_be_day(31) + + +### +# Test that private interfaces in _parser are deprecated properly [email protected](IS_PY32, reason='pytest.warns not supported on Python 3.2') +def test_parser_private_warns(): + from dateutil.parser import _timelex, _tzparser + from dateutil.parser import _parsetz + + with pytest.warns(DeprecationWarning): + _tzparser() + + with pytest.warns(DeprecationWarning): + _timelex('2014-03-03') + + with pytest.warns(DeprecationWarning): + _parsetz('+05:00') + + [email protected](IS_PY32, reason='pytest.warns not supported on Python 3.2') +def test_parser_parser_private_not_warns(): + from dateutil.parser._parser import _timelex, _tzparser + from dateutil.parser._parser import _parsetz + + with pytest.warns(None) as recorder: + _tzparser() + assert len(recorder) == 0 + + with pytest.warns(None) as recorder: + _timelex('2014-03-03') + + assert len(recorder) == 0 + + with pytest.warns(None) as recorder: + _parsetz('+05:00') + assert len(recorder) == 0 + + +def test_tzstr_internal_timedeltas(): + with pytest.warns(tz.DeprecatedTzFormatWarning): + tz1 = tz.tzstr("EST5EDT,5,4,0,7200,11,-3,0,7200") + + with pytest.warns(tz.DeprecatedTzFormatWarning): + tz2 = tz.tzstr("EST5EDT,4,1,0,7200,10,-1,0,7200") + + assert tz1._start_delta != tz2._start_delta + assert tz1._end_delta != tz2._end_delta diff --git a/libs/dateutil/test/test_isoparser.py b/libs/dateutil/test/test_isoparser.py new file mode 100644 index 000000000..35899ab9b --- /dev/null +++ b/libs/dateutil/test/test_isoparser.py @@ -0,0 +1,509 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from datetime import datetime, timedelta, date, time +import itertools as it + +from dateutil import tz +from dateutil.tz import UTC +from dateutil.parser import isoparser, isoparse + +import pytest +import six + + +def _generate_tzoffsets(limited): + def _mkoffset(hmtuple, fmt): + h, m = hmtuple + m_td = (-1 if h < 0 else 1) * m + + tzo = tz.tzoffset(None, timedelta(hours=h, minutes=m_td)) + return tzo, fmt.format(h, m) + + out = [] + if not limited: + # The subset that's just hours + hm_out_h = [(h, 0) for h in (-23, -5, 0, 5, 23)] + out.extend([_mkoffset(hm, '{:+03d}') for hm in hm_out_h]) + + # Ones that have hours and minutes + hm_out = [] + hm_out_h + hm_out += [(-12, 15), (11, 30), (10, 2), (5, 15), (-5, 30)] + else: + hm_out = [(-5, -0)] + + fmts = ['{:+03d}:{:02d}', '{:+03d}{:02d}'] + out += [_mkoffset(hm, fmt) for hm in hm_out for fmt in fmts] + + # Also add in UTC and naive + out.append((UTC, 'Z')) + out.append((None, '')) + + return out + +FULL_TZOFFSETS = _generate_tzoffsets(False) +FULL_TZOFFSETS_AWARE = [x for x in FULL_TZOFFSETS if x[1]] +TZOFFSETS = _generate_tzoffsets(True) + +DATES = [datetime(1996, 1, 1), datetime(2017, 1, 1)] [email protected]('dt', tuple(DATES)) +def test_year_only(dt): + dtstr = dt.strftime('%Y') + + assert isoparse(dtstr) == dt + +DATES += [datetime(2000, 2, 1), datetime(2017, 4, 1)] [email protected]('dt', tuple(DATES)) +def test_year_month(dt): + fmt = '%Y-%m' + dtstr = dt.strftime(fmt) + + assert isoparse(dtstr) == dt + +DATES += [datetime(2016, 2, 29), datetime(2018, 3, 15)] +YMD_FMTS = ('%Y%m%d', '%Y-%m-%d') [email protected]('dt', tuple(DATES)) [email protected]('fmt', YMD_FMTS) +def test_year_month_day(dt, fmt): + dtstr = dt.strftime(fmt) + + assert isoparse(dtstr) == dt + +def _isoparse_date_and_time(dt, date_fmt, time_fmt, tzoffset, + microsecond_precision=None): + tzi, offset_str = tzoffset + fmt = date_fmt + 'T' + time_fmt + dt = dt.replace(tzinfo=tzi) + dtstr = dt.strftime(fmt) + + if microsecond_precision is not None: + if not fmt.endswith('%f'): # pragma: nocover + raise ValueError('Time format has no microseconds!') + + if microsecond_precision != 6: + dtstr = dtstr[:-(6 - microsecond_precision)] + elif microsecond_precision > 6: # pragma: nocover + raise ValueError('Precision must be 1-6') + + dtstr += offset_str + + assert isoparse(dtstr) == dt + +DATETIMES = [datetime(1998, 4, 16, 12), + datetime(2019, 11, 18, 23), + datetime(2014, 12, 16, 4)] [email protected]('dt', tuple(DATETIMES)) [email protected]('date_fmt', YMD_FMTS) [email protected]('tzoffset', TZOFFSETS) +def test_ymd_h(dt, date_fmt, tzoffset): + _isoparse_date_and_time(dt, date_fmt, '%H', tzoffset) + +DATETIMES = [datetime(2012, 1, 6, 9, 37)] [email protected]('dt', tuple(DATETIMES)) [email protected]('date_fmt', YMD_FMTS) [email protected]('time_fmt', ('%H%M', '%H:%M')) [email protected]('tzoffset', TZOFFSETS) +def test_ymd_hm(dt, date_fmt, time_fmt, tzoffset): + _isoparse_date_and_time(dt, date_fmt, time_fmt, tzoffset) + +DATETIMES = [datetime(2003, 9, 2, 22, 14, 2), + datetime(2003, 8, 8, 14, 9, 14), + datetime(2003, 4, 7, 6, 14, 59)] +HMS_FMTS = ('%H%M%S', '%H:%M:%S') [email protected]('dt', tuple(DATETIMES)) [email protected]('date_fmt', YMD_FMTS) [email protected]('time_fmt', HMS_FMTS) [email protected]('tzoffset', TZOFFSETS) +def test_ymd_hms(dt, date_fmt, time_fmt, tzoffset): + _isoparse_date_and_time(dt, date_fmt, time_fmt, tzoffset) + +DATETIMES = [datetime(2017, 11, 27, 6, 14, 30, 123456)] [email protected]('dt', tuple(DATETIMES)) [email protected]('date_fmt', YMD_FMTS) [email protected]('time_fmt', (x + sep + '%f' for x in HMS_FMTS + for sep in '.,')) [email protected]('tzoffset', TZOFFSETS) [email protected]('precision', list(range(3, 7))) +def test_ymd_hms_micro(dt, date_fmt, time_fmt, tzoffset, precision): + # Truncate the microseconds to the desired precision for the representation + dt = dt.replace(microsecond=int(round(dt.microsecond, precision-6))) + + _isoparse_date_and_time(dt, date_fmt, time_fmt, tzoffset, precision) + +### +# Truncation of extra digits beyond microsecond precision [email protected]('dt_str', [ + '2018-07-03T14:07:00.123456000001', + '2018-07-03T14:07:00.123456999999', +]) +def test_extra_subsecond_digits(dt_str): + assert isoparse(dt_str) == datetime(2018, 7, 3, 14, 7, 0, 123456) + [email protected]('tzoffset', FULL_TZOFFSETS) +def test_full_tzoffsets(tzoffset): + dt = datetime(2017, 11, 27, 6, 14, 30, 123456) + date_fmt = '%Y-%m-%d' + time_fmt = '%H:%M:%S.%f' + + _isoparse_date_and_time(dt, date_fmt, time_fmt, tzoffset) + [email protected]('dt_str', [ + '2014-04-11T00', + '2014-04-10T24', + '2014-04-11T00:00', + '2014-04-10T24:00', + '2014-04-11T00:00:00', + '2014-04-10T24:00:00', + '2014-04-11T00:00:00.000', + '2014-04-10T24:00:00.000', + '2014-04-11T00:00:00.000000', + '2014-04-10T24:00:00.000000'] +) +def test_datetime_midnight(dt_str): + assert isoparse(dt_str) == datetime(2014, 4, 11, 0, 0, 0, 0) + [email protected]('datestr', [ + '2014-01-01', + '20140101', +]) [email protected]('sep', [' ', 'a', 'T', '_', '-']) +def test_isoparse_sep_none(datestr, sep): + isostr = datestr + sep + '14:33:09' + assert isoparse(isostr) == datetime(2014, 1, 1, 14, 33, 9) + +## +# Uncommon date formats +TIME_ARGS = ('time_args', + ((None, time(0), None), ) + tuple(('%H:%M:%S.%f', _t, _tz) + for _t, _tz in it.product([time(0), time(9, 30), time(14, 47)], + TZOFFSETS))) + [email protected]('isocal,dt_expected',[ + ((2017, 10), datetime(2017, 3, 6)), + ((2020, 1), datetime(2019, 12, 30)), # ISO year != Cal year + ((2004, 53), datetime(2004, 12, 27)), # Only half the week is in 2014 +]) +def test_isoweek(isocal, dt_expected): + # TODO: Figure out how to parametrize this on formats, too + for fmt in ('{:04d}-W{:02d}', '{:04d}W{:02d}'): + dtstr = fmt.format(*isocal) + assert isoparse(dtstr) == dt_expected + [email protected]('isocal,dt_expected',[ + ((2016, 13, 7), datetime(2016, 4, 3)), + ((2004, 53, 7), datetime(2005, 1, 2)), # ISO year != Cal year + ((2009, 1, 2), datetime(2008, 12, 30)), # ISO year < Cal year + ((2009, 53, 6), datetime(2010, 1, 2)) # ISO year > Cal year +]) +def test_isoweek_day(isocal, dt_expected): + # TODO: Figure out how to parametrize this on formats, too + for fmt in ('{:04d}-W{:02d}-{:d}', '{:04d}W{:02d}{:d}'): + dtstr = fmt.format(*isocal) + assert isoparse(dtstr) == dt_expected + [email protected]('isoord,dt_expected', [ + ((2004, 1), datetime(2004, 1, 1)), + ((2016, 60), datetime(2016, 2, 29)), + ((2017, 60), datetime(2017, 3, 1)), + ((2016, 366), datetime(2016, 12, 31)), + ((2017, 365), datetime(2017, 12, 31)) +]) +def test_iso_ordinal(isoord, dt_expected): + for fmt in ('{:04d}-{:03d}', '{:04d}{:03d}'): + dtstr = fmt.format(*isoord) + + assert isoparse(dtstr) == dt_expected + + +### +# Acceptance of bytes [email protected]('isostr,dt', [ + (b'2014', datetime(2014, 1, 1)), + (b'20140204', datetime(2014, 2, 4)), + (b'2014-02-04', datetime(2014, 2, 4)), + (b'2014-02-04T12', datetime(2014, 2, 4, 12)), + (b'2014-02-04T12:30', datetime(2014, 2, 4, 12, 30)), + (b'2014-02-04T12:30:15', datetime(2014, 2, 4, 12, 30, 15)), + (b'2014-02-04T12:30:15.224', datetime(2014, 2, 4, 12, 30, 15, 224000)), + (b'20140204T123015.224', datetime(2014, 2, 4, 12, 30, 15, 224000)), + (b'2014-02-04T12:30:15.224Z', datetime(2014, 2, 4, 12, 30, 15, 224000, + UTC)), + (b'2014-02-04T12:30:15.224z', datetime(2014, 2, 4, 12, 30, 15, 224000, + UTC)), + (b'2014-02-04T12:30:15.224+05:00', + datetime(2014, 2, 4, 12, 30, 15, 224000, + tzinfo=tz.tzoffset(None, timedelta(hours=5))))]) +def test_bytes(isostr, dt): + assert isoparse(isostr) == dt + + +### +# Invalid ISO strings [email protected]('isostr,exception', [ + ('201', ValueError), # ISO string too short + ('2012-0425', ValueError), # Inconsistent date separators + ('201204-25', ValueError), # Inconsistent date separators + ('20120425T0120:00', ValueError), # Inconsistent time separators + ('20120425T01:2000', ValueError), # Inconsistent time separators + ('14:3015', ValueError), # Inconsistent time separator + ('20120425T012500-334', ValueError), # Wrong microsecond separator + ('2001-1', ValueError), # YYYY-M not valid + ('2012-04-9', ValueError), # YYYY-MM-D not valid + ('201204', ValueError), # YYYYMM not valid + ('20120411T03:30+', ValueError), # Time zone too short + ('20120411T03:30+1234567', ValueError), # Time zone too long + ('20120411T03:30-25:40', ValueError), # Time zone invalid + ('2012-1a', ValueError), # Invalid month + ('20120411T03:30+00:60', ValueError), # Time zone invalid minutes + ('20120411T03:30+00:61', ValueError), # Time zone invalid minutes + ('20120411T033030.123456012:00', # No sign in time zone + ValueError), + ('2012-W00', ValueError), # Invalid ISO week + ('2012-W55', ValueError), # Invalid ISO week + ('2012-W01-0', ValueError), # Invalid ISO week day + ('2012-W01-8', ValueError), # Invalid ISO week day + ('2013-000', ValueError), # Invalid ordinal day + ('2013-366', ValueError), # Invalid ordinal day + ('2013366', ValueError), # Invalid ordinal day + ('2014-03-12Т12:30:14', ValueError), # Cyrillic T + ('2014-04-21T24:00:01', ValueError), # Invalid use of 24 for midnight + ('2014_W01-1', ValueError), # Invalid separator + ('2014W01-1', ValueError), # Inconsistent use of dashes + ('2014-W011', ValueError), # Inconsistent use of dashes + +]) +def test_iso_raises(isostr, exception): + with pytest.raises(exception): + isoparse(isostr) + + [email protected]('sep_act, valid_sep, exception', [ + ('T', 'C', ValueError), + ('C', 'T', ValueError), +]) +def test_iso_with_sep_raises(sep_act, valid_sep, exception): + parser = isoparser(sep=valid_sep) + isostr = '2012-04-25' + sep_act + '01:25:00' + with pytest.raises(exception): + parser.isoparse(isostr) + + +### +# Test ISOParser constructor [email protected]('sep', [' ', '9', '🍛']) +def test_isoparser_invalid_sep(sep): + with pytest.raises(ValueError): + isoparser(sep=sep) + + +# This only fails on Python 3 [email protected](not six.PY2, reason="Fails on Python 3 only") +def test_isoparser_byte_sep(): + dt = datetime(2017, 12, 6, 12, 30, 45) + dt_str = dt.isoformat(sep=str('T')) + + dt_rt = isoparser(sep=b'T').isoparse(dt_str) + + assert dt == dt_rt + + +### +# Test parse_tzstr [email protected]('tzoffset', FULL_TZOFFSETS) +def test_parse_tzstr(tzoffset): + dt = datetime(2017, 11, 27, 6, 14, 30, 123456) + date_fmt = '%Y-%m-%d' + time_fmt = '%H:%M:%S.%f' + + _isoparse_date_and_time(dt, date_fmt, time_fmt, tzoffset) + + [email protected]('tzstr', [ + '-00:00', '+00:00', '+00', '-00', '+0000', '-0000' +]) [email protected]('zero_as_utc', [True, False]) +def test_parse_tzstr_zero_as_utc(tzstr, zero_as_utc): + tzi = isoparser().parse_tzstr(tzstr, zero_as_utc=zero_as_utc) + assert tzi == UTC + assert (type(tzi) == tz.tzutc) == zero_as_utc + + [email protected]('tzstr,exception', [ + ('00:00', ValueError), # No sign + ('05:00', ValueError), # No sign + ('_00:00', ValueError), # Invalid sign + ('+25:00', ValueError), # Offset too large + ('00:0000', ValueError), # String too long +]) +def test_parse_tzstr_fails(tzstr, exception): + with pytest.raises(exception): + isoparser().parse_tzstr(tzstr) + +### +# Test parse_isodate +def __make_date_examples(): + dates_no_day = [ + date(1999, 12, 1), + date(2016, 2, 1) + ] + + if not six.PY2: + # strftime does not support dates before 1900 in Python 2 + dates_no_day.append(date(1000, 11, 1)) + + # Only one supported format for dates with no day + o = zip(dates_no_day, it.repeat('%Y-%m')) + + dates_w_day = [ + date(1969, 12, 31), + date(1900, 1, 1), + date(2016, 2, 29), + date(2017, 11, 14) + ] + + dates_w_day_fmts = ('%Y%m%d', '%Y-%m-%d') + o = it.chain(o, it.product(dates_w_day, dates_w_day_fmts)) + + return list(o) + + [email protected]('d,dt_fmt', __make_date_examples()) [email protected]('as_bytes', [True, False]) +def test_parse_isodate(d, dt_fmt, as_bytes): + d_str = d.strftime(dt_fmt) + if isinstance(d_str, six.text_type) and as_bytes: + d_str = d_str.encode('ascii') + elif isinstance(d_str, bytes) and not as_bytes: + d_str = d_str.decode('ascii') + + iparser = isoparser() + assert iparser.parse_isodate(d_str) == d + + [email protected]('isostr,exception', [ + ('243', ValueError), # ISO string too short + ('2014-0423', ValueError), # Inconsistent date separators + ('201404-23', ValueError), # Inconsistent date separators + ('2014日03月14', ValueError), # Not ASCII + ('2013-02-29', ValueError), # Not a leap year + ('2014/12/03', ValueError), # Wrong separators + ('2014-04-19T', ValueError), # Unknown components + ('201202', ValueError), # Invalid format +]) +def test_isodate_raises(isostr, exception): + with pytest.raises(exception): + isoparser().parse_isodate(isostr) + + +def test_parse_isodate_error_text(): + with pytest.raises(ValueError) as excinfo: + isoparser().parse_isodate('2014-0423') + + # ensure the error message does not contain b' prefixes + if six.PY2: + expected_error = "String contains unknown ISO components: u'2014-0423'" + else: + expected_error = "String contains unknown ISO components: '2014-0423'" + assert expected_error == str(excinfo.value) + + +### +# Test parse_isotime +def __make_time_examples(): + outputs = [] + + # HH + time_h = [time(0), time(8), time(22)] + time_h_fmts = ['%H'] + + outputs.append(it.product(time_h, time_h_fmts)) + + # HHMM / HH:MM + time_hm = [time(0, 0), time(0, 30), time(8, 47), time(16, 1)] + time_hm_fmts = ['%H%M', '%H:%M'] + + outputs.append(it.product(time_hm, time_hm_fmts)) + + # HHMMSS / HH:MM:SS + time_hms = [time(0, 0, 0), time(0, 15, 30), + time(8, 2, 16), time(12, 0), time(16, 2), time(20, 45)] + + time_hms_fmts = ['%H%M%S', '%H:%M:%S'] + + outputs.append(it.product(time_hms, time_hms_fmts)) + + # HHMMSS.ffffff / HH:MM:SS.ffffff + time_hmsu = [time(0, 0, 0, 0), time(4, 15, 3, 247993), + time(14, 21, 59, 948730), + time(23, 59, 59, 999999)] + + time_hmsu_fmts = ['%H%M%S.%f', '%H:%M:%S.%f'] + + outputs.append(it.product(time_hmsu, time_hmsu_fmts)) + + outputs = list(map(list, outputs)) + + # Time zones + ex_naive = list(it.chain.from_iterable(x[0:2] for x in outputs)) + o = it.product(ex_naive, TZOFFSETS) # ((time, fmt), (tzinfo, offsetstr)) + o = ((t.replace(tzinfo=tzi), fmt + off_str) + for (t, fmt), (tzi, off_str) in o) + + outputs.append(o) + + return list(it.chain.from_iterable(outputs)) + + [email protected]('time_val,time_fmt', __make_time_examples()) [email protected]('as_bytes', [True, False]) +def test_isotime(time_val, time_fmt, as_bytes): + tstr = time_val.strftime(time_fmt) + if isinstance(tstr, six.text_type) and as_bytes: + tstr = tstr.encode('ascii') + elif isinstance(tstr, bytes) and not as_bytes: + tstr = tstr.decode('ascii') + + iparser = isoparser() + + assert iparser.parse_isotime(tstr) == time_val + + [email protected]('isostr', [ + '24:00', + '2400', + '24:00:00', + '240000', + '24:00:00.000', + '24:00:00,000', + '24:00:00.000000', + '24:00:00,000000', +]) +def test_isotime_midnight(isostr): + iparser = isoparser() + assert iparser.parse_isotime(isostr) == time(0, 0, 0, 0) + + [email protected]('isostr,exception', [ + ('3', ValueError), # ISO string too short + ('14時30分15秒', ValueError), # Not ASCII + ('14_30_15', ValueError), # Invalid separators + ('1430:15', ValueError), # Inconsistent separator use + ('25', ValueError), # Invalid hours + ('25:15', ValueError), # Invalid hours + ('14:60', ValueError), # Invalid minutes + ('14:59:61', ValueError), # Invalid seconds + ('14:30:15.34468305:00', ValueError), # No sign in time zone + ('14:30:15+', ValueError), # Time zone too short + ('14:30:15+1234567', ValueError), # Time zone invalid + ('14:59:59+25:00', ValueError), # Invalid tz hours + ('14:59:59+12:62', ValueError), # Invalid tz minutes + ('14:59:30_344583', ValueError), # Invalid microsecond separator + ('24:01', ValueError), # 24 used for non-midnight time + ('24:00:01', ValueError), # 24 used for non-midnight time + ('24:00:00.001', ValueError), # 24 used for non-midnight time + ('24:00:00.000001', ValueError), # 24 used for non-midnight time +]) +def test_isotime_raises(isostr, exception): + iparser = isoparser() + with pytest.raises(exception): + iparser.parse_isotime(isostr) diff --git a/libs/dateutil/test/test_parser.py b/libs/dateutil/test/test_parser.py index 1115bbf65..08a34dafb 100644 --- a/libs/dateutil/test/test_parser.py +++ b/libs/dateutil/test/test_parser.py @@ -1,46 +1,310 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -from ._common import unittest -from datetime import datetime, timedelta, date +import itertools +from datetime import datetime, timedelta +import unittest +import sys +from dateutil import tz 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): +from dateutil.parser import parse, parserinfo +from dateutil.parser import ParserError +from dateutil.parser import UnknownTimezoneWarning + +from ._common import TZEnvContext + +from six import assertRaisesRegex, PY2 +from io import StringIO + +import pytest + +# Platform info +IS_WIN = sys.platform.startswith('win') + +PLATFORM_HAS_DASH_D = False +try: + if datetime.now().strftime('%-d'): + PLATFORM_HAS_DASH_D = True +except ValueError: + pass + + [email protected](params=[True, False]) +def fuzzy(request): + """Fixture to pass fuzzy=True or fuzzy=False to parse""" + return request.param + + +# Parser test cases using no keyword arguments. Format: (parsable_text, expected_datetime, assertion_message) +PARSER_TEST_CASES = [ + ("Thu Sep 25 10:36:28 2003", datetime(2003, 9, 25, 10, 36, 28), "date command format strip"), + ("Thu Sep 25 2003", datetime(2003, 9, 25), "date command format strip"), + ("2003-09-25T10:49:41", datetime(2003, 9, 25, 10, 49, 41), "iso format strip"), + ("2003-09-25T10:49", datetime(2003, 9, 25, 10, 49), "iso format strip"), + ("2003-09-25T10", datetime(2003, 9, 25, 10), "iso format strip"), + ("2003-09-25", datetime(2003, 9, 25), "iso format strip"), + ("20030925T104941", datetime(2003, 9, 25, 10, 49, 41), "iso stripped format strip"), + ("20030925T1049", datetime(2003, 9, 25, 10, 49, 0), "iso stripped format strip"), + ("20030925T10", datetime(2003, 9, 25, 10), "iso stripped format strip"), + ("20030925", datetime(2003, 9, 25), "iso stripped format strip"), + ("2003-09-25 10:49:41,502", datetime(2003, 9, 25, 10, 49, 41, 502000), "python logger format"), + ("199709020908", datetime(1997, 9, 2, 9, 8), "no separator"), + ("19970902090807", datetime(1997, 9, 2, 9, 8, 7), "no separator"), + ("09-25-2003", datetime(2003, 9, 25), "date with dash"), + ("25-09-2003", datetime(2003, 9, 25), "date with dash"), + ("10-09-2003", datetime(2003, 10, 9), "date with dash"), + ("10-09-03", datetime(2003, 10, 9), "date with dash"), + ("2003.09.25", datetime(2003, 9, 25), "date with dot"), + ("09.25.2003", datetime(2003, 9, 25), "date with dot"), + ("25.09.2003", datetime(2003, 9, 25), "date with dot"), + ("10.09.2003", datetime(2003, 10, 9), "date with dot"), + ("10.09.03", datetime(2003, 10, 9), "date with dot"), + ("2003/09/25", datetime(2003, 9, 25), "date with slash"), + ("09/25/2003", datetime(2003, 9, 25), "date with slash"), + ("25/09/2003", datetime(2003, 9, 25), "date with slash"), + ("10/09/2003", datetime(2003, 10, 9), "date with slash"), + ("10/09/03", datetime(2003, 10, 9), "date with slash"), + ("2003 09 25", datetime(2003, 9, 25), "date with space"), + ("09 25 2003", datetime(2003, 9, 25), "date with space"), + ("25 09 2003", datetime(2003, 9, 25), "date with space"), + ("10 09 2003", datetime(2003, 10, 9), "date with space"), + ("10 09 03", datetime(2003, 10, 9), "date with space"), + ("25 09 03", datetime(2003, 9, 25), "date with space"), + ("03 25 Sep", datetime(2003, 9, 25), "strangely ordered date"), + ("25 03 Sep", datetime(2025, 9, 3), "strangely ordered date"), + (" July 4 , 1976 12:01:02 am ", datetime(1976, 7, 4, 0, 1, 2), "extra space"), + ("Wed, July 10, '96", datetime(1996, 7, 10, 0, 0), "random format"), + ("1996.July.10 AD 12:08 PM", datetime(1996, 7, 10, 12, 8), "random format"), + ("July 4, 1976", datetime(1976, 7, 4), "random format"), + ("7 4 1976", datetime(1976, 7, 4), "random format"), + ("4 jul 1976", datetime(1976, 7, 4), "random format"), + ("4 Jul 1976", datetime(1976, 7, 4), "'%-d %b %Y' format"), + ("7-4-76", datetime(1976, 7, 4), "random format"), + ("19760704", datetime(1976, 7, 4), "random format"), + ("0:01:02 on July 4, 1976", datetime(1976, 7, 4, 0, 1, 2), "random format"), + ("July 4, 1976 12:01:02 am", datetime(1976, 7, 4, 0, 1, 2), "random format"), + ("Mon Jan 2 04:24:27 1995", datetime(1995, 1, 2, 4, 24, 27), "random format"), + ("04.04.95 00:22", datetime(1995, 4, 4, 0, 22), "random format"), + ("Jan 1 1999 11:23:34.578", datetime(1999, 1, 1, 11, 23, 34, 578000), "random format"), + ("950404 122212", datetime(1995, 4, 4, 12, 22, 12), "random format"), + ("3rd of May 2001", datetime(2001, 5, 3), "random format"), + ("5th of March 2001", datetime(2001, 3, 5), "random format"), + ("1st of May 2003", datetime(2003, 5, 1), "random format"), + ('0099-01-01T00:00:00', datetime(99, 1, 1, 0, 0), "99 ad"), + ('0031-01-01T00:00:00', datetime(31, 1, 1, 0, 0), "31 ad"), + ("20080227T21:26:01.123456789", datetime(2008, 2, 27, 21, 26, 1, 123456), "high precision seconds"), + ('13NOV2017', datetime(2017, 11, 13), "dBY (See GH360)"), + ('0003-03-04', datetime(3, 3, 4), "pre 12 year same month (See GH PR #293)"), + ('December.0031.30', datetime(31, 12, 30), "BYd corner case (GH#687)"), + + # Cases with legacy h/m/s format, candidates for deprecation (GH#886) + ("2016-12-21 04.2h", datetime(2016, 12, 21, 4, 12), "Fractional Hours"), +] +# Check that we don't have any duplicates +assert len(set([x[0] for x in PARSER_TEST_CASES])) == len(PARSER_TEST_CASES) + + [email protected]("parsable_text,expected_datetime,assertion_message", PARSER_TEST_CASES) +def test_parser(parsable_text, expected_datetime, assertion_message): + assert parse(parsable_text) == expected_datetime, assertion_message + + +# Parser test cases using datetime(2003, 9, 25) as a default. +# Format: (parsable_text, expected_datetime, assertion_message) +PARSER_DEFAULT_TEST_CASES = [ + ("Thu Sep 25 10:36:28", datetime(2003, 9, 25, 10, 36, 28), "date command format strip"), + ("Thu Sep 10:36:28", datetime(2003, 9, 25, 10, 36, 28), "date command format strip"), + ("Thu 10:36:28", datetime(2003, 9, 25, 10, 36, 28), "date command format strip"), + ("Sep 10:36:28", datetime(2003, 9, 25, 10, 36, 28), "date command format strip"), + ("10:36:28", datetime(2003, 9, 25, 10, 36, 28), "date command format strip"), + ("10:36", datetime(2003, 9, 25, 10, 36), "date command format strip"), + ("Sep 2003", datetime(2003, 9, 25), "date command format strip"), + ("Sep", datetime(2003, 9, 25), "date command format strip"), + ("2003", datetime(2003, 9, 25), "date command format strip"), + ("10h36m28.5s", datetime(2003, 9, 25, 10, 36, 28, 500000), "hour with letters"), + ("10h36m28s", datetime(2003, 9, 25, 10, 36, 28), "hour with letters strip"), + ("10h36m", datetime(2003, 9, 25, 10, 36), "hour with letters strip"), + ("10h", datetime(2003, 9, 25, 10), "hour with letters strip"), + ("10 h 36", datetime(2003, 9, 25, 10, 36), "hour with letters strip"), + ("10 h 36.5", datetime(2003, 9, 25, 10, 36, 30), "hour with letter strip"), + ("36 m 5", datetime(2003, 9, 25, 0, 36, 5), "hour with letters spaces"), + ("36 m 5 s", datetime(2003, 9, 25, 0, 36, 5), "minute with letters spaces"), + ("36 m 05", datetime(2003, 9, 25, 0, 36, 5), "minute with letters spaces"), + ("36 m 05 s", datetime(2003, 9, 25, 0, 36, 5), "minutes with letters spaces"), + ("10h am", datetime(2003, 9, 25, 10), "hour am pm"), + ("10h pm", datetime(2003, 9, 25, 22), "hour am pm"), + ("10am", datetime(2003, 9, 25, 10), "hour am pm"), + ("10pm", datetime(2003, 9, 25, 22), "hour am pm"), + ("10:00 am", datetime(2003, 9, 25, 10), "hour am pm"), + ("10:00 pm", datetime(2003, 9, 25, 22), "hour am pm"), + ("10:00am", datetime(2003, 9, 25, 10), "hour am pm"), + ("10:00pm", datetime(2003, 9, 25, 22), "hour am pm"), + ("10:00a.m", datetime(2003, 9, 25, 10), "hour am pm"), + ("10:00p.m", datetime(2003, 9, 25, 22), "hour am pm"), + ("10:00a.m.", datetime(2003, 9, 25, 10), "hour am pm"), + ("10:00p.m.", datetime(2003, 9, 25, 22), "hour am pm"), + ("Wed", datetime(2003, 10, 1), "weekday alone"), + ("Wednesday", datetime(2003, 10, 1), "long weekday"), + ("October", datetime(2003, 10, 25), "long month"), + ("31-Dec-00", datetime(2000, 12, 31), "zero year"), + ("0:01:02", datetime(2003, 9, 25, 0, 1, 2), "random format"), + ("12h 01m02s am", datetime(2003, 9, 25, 0, 1, 2), "random format"), + ("12:08 PM", datetime(2003, 9, 25, 12, 8), "random format"), + ("01h02m03", datetime(2003, 9, 25, 1, 2, 3), "random format"), + ("01h02", datetime(2003, 9, 25, 1, 2), "random format"), + ("01h02s", datetime(2003, 9, 25, 1, 0, 2), "random format"), + ("01m02", datetime(2003, 9, 25, 0, 1, 2), "random format"), + ("01m02h", datetime(2003, 9, 25, 2, 1), "random format"), + ("2004 10 Apr 11h30m", datetime(2004, 4, 10, 11, 30), "random format") +] +# Check that we don't have any duplicates +assert len(set([x[0] for x in PARSER_DEFAULT_TEST_CASES])) == len(PARSER_DEFAULT_TEST_CASES) + + [email protected]("parsable_text,expected_datetime,assertion_message", PARSER_DEFAULT_TEST_CASES) +def test_parser_default(parsable_text, expected_datetime, assertion_message): + assert parse(parsable_text, default=datetime(2003, 9, 25)) == expected_datetime, assertion_message + + [email protected]('sep', ['-', '.', '/', ' ']) +def test_parse_dayfirst(sep): + expected = datetime(2003, 9, 10) + fmt = sep.join(['%d', '%m', '%Y']) + dstr = expected.strftime(fmt) + result = parse(dstr, dayfirst=True) + assert result == expected + + [email protected]('sep', ['-', '.', '/', ' ']) +def test_parse_yearfirst(sep): + expected = datetime(2010, 9, 3) + fmt = sep.join(['%Y', '%m', '%d']) + dstr = expected.strftime(fmt) + result = parse(dstr, yearfirst=True) + assert result == expected + + [email protected]('dstr,expected', [ + ("Thu Sep 25 10:36:28 BRST 2003", datetime(2003, 9, 25, 10, 36, 28)), + ("1996.07.10 AD at 15:08:56 PDT", datetime(1996, 7, 10, 15, 8, 56)), + ("Tuesday, April 12, 1952 AD 3:30:42pm PST", + datetime(1952, 4, 12, 15, 30, 42)), + ("November 5, 1994, 8:15:30 am EST", datetime(1994, 11, 5, 8, 15, 30)), + ("1994-11-05T08:15:30-05:00", datetime(1994, 11, 5, 8, 15, 30)), + ("1994-11-05T08:15:30Z", datetime(1994, 11, 5, 8, 15, 30)), + ("1976-07-04T00:01:02Z", datetime(1976, 7, 4, 0, 1, 2)), + ("1986-07-05T08:15:30z", datetime(1986, 7, 5, 8, 15, 30)), + ("Tue Apr 4 00:22:12 PDT 1995", datetime(1995, 4, 4, 0, 22, 12)), +]) +def test_parse_ignoretz(dstr, expected): + result = parse(dstr, ignoretz=True) + assert result == expected + + +_brsttz = tzoffset("BRST", -10800) + + [email protected]('dstr,expected', [ + ("20030925T104941-0300", + datetime(2003, 9, 25, 10, 49, 41, tzinfo=_brsttz)), + ("Thu, 25 Sep 2003 10:49:41 -0300", + datetime(2003, 9, 25, 10, 49, 41, tzinfo=_brsttz)), + ("2003-09-25T10:49:41.5-03:00", + datetime(2003, 9, 25, 10, 49, 41, 500000, tzinfo=_brsttz)), + ("2003-09-25T10:49:41-03:00", + datetime(2003, 9, 25, 10, 49, 41, tzinfo=_brsttz)), + ("20030925T104941.5-0300", + datetime(2003, 9, 25, 10, 49, 41, 500000, tzinfo=_brsttz)), +]) +def test_parse_with_tzoffset(dstr, expected): + # In these cases, we are _not_ passing a tzinfos arg + result = parse(dstr) + assert result == expected + + +class TestFormat(object): + + def test_ybd(self): + # If we have a 4-digit year, a non-numeric month (abbreviated or not), + # and a day (1 or 2 digits), then there is no ambiguity as to which + # token is a year/month/day. This holds regardless of what order the + # terms are in and for each of the separators below. + + seps = ['-', ' ', '/', '.'] + + year_tokens = ['%Y'] + month_tokens = ['%b', '%B'] + day_tokens = ['%d'] + if PLATFORM_HAS_DASH_D: + day_tokens.append('%-d') + + prods = itertools.product(year_tokens, month_tokens, day_tokens) + perms = [y for x in prods for y in itertools.permutations(x)] + unambig_fmts = [sep.join(perm) for sep in seps for perm in perms] + + actual = datetime(2003, 9, 25) + + for fmt in unambig_fmts: + dstr = actual.strftime(fmt) + res = parse(dstr) + assert res == actual + + # TODO: some redundancy with PARSER_TEST_CASES cases + @pytest.mark.parametrize("fmt,dstr", [ + ("%a %b %d %Y", "Thu Sep 25 2003"), + ("%b %d %Y", "Sep 25 2003"), + ("%Y-%m-%d", "2003-09-25"), + ("%Y%m%d", "20030925"), + ("%Y-%b-%d", "2003-Sep-25"), + ("%d-%b-%Y", "25-Sep-2003"), + ("%b-%d-%Y", "Sep-25-2003"), + ("%m-%d-%Y", "09-25-2003"), + ("%d-%m-%Y", "25-09-2003"), + ("%Y.%m.%d", "2003.09.25"), + ("%Y.%b.%d", "2003.Sep.25"), + ("%d.%b.%Y", "25.Sep.2003"), + ("%b.%d.%Y", "Sep.25.2003"), + ("%m.%d.%Y", "09.25.2003"), + ("%d.%m.%Y", "25.09.2003"), + ("%Y/%m/%d", "2003/09/25"), + ("%Y/%b/%d", "2003/Sep/25"), + ("%d/%b/%Y", "25/Sep/2003"), + ("%b/%d/%Y", "Sep/25/2003"), + ("%m/%d/%Y", "09/25/2003"), + ("%d/%m/%Y", "25/09/2003"), + ("%Y %m %d", "2003 09 25"), + ("%Y %b %d", "2003 Sep 25"), + ("%d %b %Y", "25 Sep 2003"), + ("%m %d %Y", "09 25 2003"), + ("%d %m %Y", "25 09 2003"), + ("%y %d %b", "03 25 Sep",), + ]) + def test_strftime_formats_2003Sep25(self, fmt, dstr): + expected = datetime(2003, 9, 25) + + # First check that the format strings behave as expected + # (not strictly necessary, but nice to have) + assert expected.strftime(fmt) == dstr + + res = parse(dstr) + assert res == expected + + +class TestInputTypes(object): + def test_empty_string_invalid(self): + with pytest.raises(ParserError): parse('') - def testNone(self): - with self.assertRaises(TypeError): + def test_none_invalid(self): + with pytest.raises(TypeError): parse(None) - def testInvalidType(self): - with self.assertRaises(TypeError): + def test_int_invalid(self): + with pytest.raises(TypeError): parse(13) - def testDuckTyping(self): + def test_duck_typing(self): # We want to support arbitrary classes that implement the stream # interface. @@ -51,25 +315,112 @@ class ParserTest(unittest.TestCase): 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)) + res = parse(dstr) + expected = datetime(2014, 1, 19) + assert res == expected - def testParseStream(self): + def test_parse_stream(self): dstr = StringIO('2014 January 19') - self.assertEqual(parse(dstr), datetime(2014, 1, 19)) + res = parse(dstr) + expected = datetime(2014, 1, 19) + assert res == expected + + def test_parse_str(self): + # Parser should be able to handle bytestring and unicode + uni_str = '2014-05-01 08:00:00' + bytes_str = uni_str.encode() + + res = parse(bytes_str) + expected = parse(uni_str) + assert res == expected + + def test_parse_bytes(self): + res = parse(b'2014 January 19') + expected = datetime(2014, 1, 19) + assert res == expected + + def test_parse_bytearray(self): + # GH#417 + res = parse(bytearray(b'2014 January 19')) + expected = datetime(2014, 1, 19) + assert res == expected + + +class TestTzinfoInputTypes(object): + def assert_equal_same_tz(self, dt1, dt2): + assert dt1 == dt2 + assert dt1.tzinfo is dt2.tzinfo + + def test_tzinfo_dict_could_return_none(self): + dstr = "2017-02-03 12:40 BRST" + result = parse(dstr, tzinfos={"BRST": None}) + expected = datetime(2017, 2, 3, 12, 40) + self.assert_equal_same_tz(result, expected) + + def test_tzinfos_callable_could_return_none(self): + dstr = "2017-02-03 12:40 BRST" + result = parse(dstr, tzinfos=lambda *args: None) + expected = datetime(2017, 2, 3, 12, 40) + self.assert_equal_same_tz(result, expected) + + def test_invalid_tzinfo_input(self): + dstr = "2014 January 19 09:00 UTC" + # Pass an absurd tzinfos object + tzinfos = {"UTC": ValueError} + with pytest.raises(TypeError): + parse(dstr, tzinfos=tzinfos) + + def test_valid_tzinfo_tzinfo_input(self): + dstr = "2014 January 19 09:00 UTC" + tzinfos = {"UTC": tz.UTC} + expected = datetime(2014, 1, 19, 9, tzinfo=tz.UTC) + res = parse(dstr, tzinfos=tzinfos) + self.assert_equal_same_tz(res, expected) + + def test_valid_tzinfo_unicode_input(self): + dstr = "2014 January 19 09:00 UTC" + tzinfos = {u"UTC": u"UTC+0"} + expected = datetime(2014, 1, 19, 9, tzinfo=tz.tzstr("UTC+0")) + res = parse(dstr, tzinfos=tzinfos) + self.assert_equal_same_tz(res, expected) + + def test_valid_tzinfo_callable_input(self): + dstr = "2014 January 19 09:00 UTC" + + def tzinfos(*args, **kwargs): + return u"UTC+0" + + expected = datetime(2014, 1, 19, 9, tzinfo=tz.tzstr("UTC+0")) + res = parse(dstr, tzinfos=tzinfos) + self.assert_equal_same_tz(res, expected) + + def test_valid_tzinfo_int_input(self): + dstr = "2014 January 19 09:00 UTC" + tzinfos = {u"UTC": -28800} + expected = datetime(2014, 1, 19, 9, tzinfo=tz.tzoffset(u"UTC", -28800)) + res = parse(dstr, tzinfos=tzinfos) + self.assert_equal_same_tz(res, expected) + + +class ParserTest(unittest.TestCase): + + @classmethod + def setup_class(cls): + cls.tzinfos = {"BRST": -10800} + cls.brsttz = tzoffset("BRST", -10800) + cls.default = datetime(2003, 9, 25) - def testParseStr(self): - self.assertEqual(parse(self.str_str), - parse(self.uni_str)) + # Parser should be able to handle bytestring and unicode + cls.uni_str = '2014-05-01 08:00:00' + cls.str_str = cls.uni_str.encode() def testParserParseStr(self): from dateutil.parser import parser - self.assertEqual(parser().parse(self.str_str), - parser().parse(self.uni_str)) + assert parser().parse(self.str_str) == parser().parse(self.uni_str) def testParseUnicodeWords(self): @@ -87,9 +438,9 @@ class ParserTest(unittest.TestCase): ("ноя", "Ноябрь"), ("дек", "Декабрь")] - self.assertEqual(parse('10 Сентябрь 2015 10:20', - parserinfo=rus_parserinfo()), - datetime(2015, 9, 10, 10, 20)) + expected = datetime(2015, 9, 10, 10, 20) + res = parse('10 Сентябрь 2015 10:20', parserinfo=rus_parserinfo()) + assert res == expected def testParseWithNulls(self): # This relies on the from __future__ import unicode_literals, because @@ -97,8 +448,7 @@ class ParserTest(unittest.TestCase): # 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)) + assert parse(pstring) == datetime(1924, 8, 29) def testDateCommandFormat(self): self.assertEqual(parse("Thu Sep 25 10:36:28 BRST 2003", @@ -106,13 +456,6 @@ class ParserTest(unittest.TestCase): 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), @@ -120,405 +463,34 @@ class ParserTest(unittest.TestCase): tzinfo=self.brsttz)) def testDateCommandFormatWithLong(self): - if not PY3: + if PY2: 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"), + self.assertEqual(parse("2003-09-25T10:49:41+03:00"), datetime(2003, 9, 25, 10, 49, 41, - tzinfo=self.brsttz)) + tzinfo=tzoffset(None, 10800))) 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)) + self.assertEqual(parse("20030925T104941+0300"), + datetime(2003, 9, 25, 10, 49, 41, + tzinfo=tzoffset(None, 10800))) def testAMPMNoHour(self): - with self.assertRaises(ValueError): + with pytest.raises(ParserError): parse("AM") - with self.assertRaises(ValueError): + with pytest.raises(ParserError): 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): + with pytest.raises(ParserError): parse("13:44 AM") - with self.assertRaises(ValueError): + with pytest.raises(ParserError): parse("January 25, 1921 23:13 PM") def testPertain(self): @@ -527,22 +499,6 @@ class ParserTest(unittest.TestCase): 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." @@ -551,14 +507,19 @@ class ParserTest(unittest.TestCase): tzinfo=self.brsttz)) def testFuzzyWithTokens(self): - s = "Today is 25 of September of 2003, exactly " \ + s1 = "Today is 25 of September of 2003, exactly " \ "at 10:49:41 with timezone -03:00." - self.assertEqual(parse(s, fuzzy_with_tokens=True), + self.assertEqual(parse(s1, fuzzy_with_tokens=True), (datetime(2003, 9, 25, 10, 49, 41, tzinfo=self.brsttz), ('Today is ', 'of ', ', exactly at ', ' with timezone ', '.'))) + s2 = "http://biz.yahoo.com/ipo/p/600221.html" + self.assertEqual(parse(s2, fuzzy_with_tokens=True), + (datetime(2060, 2, 21, 0, 0, 0), + ('http://biz.yahoo.com/ipo/p/', '.html'))) + def testFuzzyAMPMProblem(self): # Sometimes fuzzy parsing results in AM/PM flag being set without # hours - if it's fuzzy it should ignore that. @@ -576,186 +537,44 @@ class ParserTest(unittest.TestCase): 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)) + with pytest.warns(UnknownTimezoneWarning): + res = parse(s1, fuzzy=True) + self.assertEqual(res, datetime(1945, 1, 29, 14, 45)) 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)) + with pytest.warns(UnknownTimezoneWarning): + res = parse("5:50 A.M. on June 13, 1990") - 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") + self.assertEqual(res, datetime(1990, 6, 13, 5, 50)) 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): + def testUnspecifiedDayFallbackFebNoLeapYear(self): self.assertEqual(parse("Feb 2007", default=datetime(2010, 1, 31)), datetime(2007, 2, 28)) - def testUnspecifiedDayFallbackFebLeapYear(self): + 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') + with pytest.raises(ParserError): + parse('shouldfail') def testCorrectErrorOnFuzzyWithTokens(self): - assertRaisesRegex(self, ValueError, 'Unknown string format', + assertRaisesRegex(self, ParserError, 'Unknown string format', parse, '04/04/32/423', fuzzy_with_tokens=True) - assertRaisesRegex(self, ValueError, 'Unknown string format', + assertRaisesRegex(self, ParserError, 'Unknown string format', parse, '04/04/04 +32423', fuzzy_with_tokens=True) - assertRaisesRegex(self, ValueError, 'Unknown string format', + assertRaisesRegex(self, ParserError, 'Unknown string format', parse, '04/04/0d4', fuzzy_with_tokens=True) def testIncreasingCTime(self): @@ -766,22 +585,22 @@ class ParserTest(unittest.TestCase): 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) + assert 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) + assert 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) + assert dt1.microsecond == 10000 + assert dt2.microsecond == 10000 def testMicrosecondPrecisionErrorReturns(self): # One more precision issue, discovered by Eric Brown. This should @@ -791,11 +610,7 @@ class ParserTest(unittest.TestCase): 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)) + assert parse(dt.isoformat()) == dt def testCustomParserInfo(self): # Custom parser info wasn't working, as Michael Elsdörfer discovered. @@ -806,7 +621,26 @@ class ParserTest(unittest.TestCase): MONTHS[0] = ("Foo", "Foo") myparser = parser(myparserinfo()) dt = myparser.parse("01/Foo/2007") - self.assertEqual(dt, datetime(2007, 1, 1)) + assert dt == datetime(2007, 1, 1) + + def testCustomParserShortDaynames(self): + # Horacio Hoyos discovered that day names shorter than 3 characters, + # for example two letter German day name abbreviations, don't work: + # https://github.com/dateutil/dateutil/issues/343 + from dateutil.parser import parserinfo, parser + + class GermanParserInfo(parserinfo): + WEEKDAYS = [("Mo", "Montag"), + ("Di", "Dienstag"), + ("Mi", "Mittwoch"), + ("Do", "Donnerstag"), + ("Fr", "Freitag"), + ("Sa", "Samstag"), + ("So", "Sonntag")] + + myparser = parser(GermanParserInfo()) + dt = myparser.parse("Sa 21. Jan 2017") + self.assertEqual(dt, datetime(2017, 1, 21)) def testNoYearFirstNoDayFirst(self): dtstr = '090107' @@ -851,7 +685,7 @@ class ParserTest(unittest.TestCase): def testUnambiguousDayFirst(self): dtstr = '2015 09 25' - self.assertEqual(parse(dtstr, dayfirst=True), + self.assertEqual(parse(dtstr, dayfirst=True), datetime(2015, 9, 25)) def testUnambiguousDayFirstYearFirst(self): @@ -859,3 +693,272 @@ class ParserTest(unittest.TestCase): self.assertEqual(parse(dtstr, dayfirst=True, yearfirst=True), datetime(2015, 9, 25)) + def test_mstridx(self): + # See GH408 + dtstr = '2015-15-May' + self.assertEqual(parse(dtstr), + datetime(2015, 5, 15)) + + def test_idx_check(self): + dtstr = '2017-07-17 06:15:' + # Pre-PR, the trailing colon will cause an IndexError at 824-825 + # when checking `i < len_l` and then accessing `l[i+1]` + res = parse(dtstr, fuzzy=True) + assert res == datetime(2017, 7, 17, 6, 15) + + def test_hmBY(self): + # See GH#483 + dtstr = '02:17NOV2017' + res = parse(dtstr, default=self.default) + assert res == datetime(2017, 11, self.default.day, 2, 17) + + def test_validate_hour(self): + # See GH353 + invalid = "201A-01-01T23:58:39.239769+03:00" + with pytest.raises(ParserError): + parse(invalid) + + def test_era_trailing_year(self): + dstr = 'AD2001' + res = parse(dstr) + assert res.year == 2001, res + + def test_includes_timestr(self): + timestr = "2020-13-97T44:61:83" + + try: + parse(timestr) + except ParserError as e: + assert e.args[1] == timestr + else: + pytest.fail("Failed to raise ParserError") + + +class TestOutOfBounds(object): + + def test_no_year_zero(self): + with pytest.raises(ParserError): + parse("0000 Jun 20") + + def test_out_of_bound_day(self): + with pytest.raises(ParserError): + parse("Feb 30, 2007") + + def test_illegal_month_error(self): + with pytest.raises(ParserError): + parse("0-100") + + def test_day_sanity(self, fuzzy): + dstr = "2014-15-25" + with pytest.raises(ParserError): + parse(dstr, fuzzy=fuzzy) + + def test_minute_sanity(self, fuzzy): + dstr = "2014-02-28 22:64" + with pytest.raises(ParserError): + parse(dstr, fuzzy=fuzzy) + + def test_hour_sanity(self, fuzzy): + dstr = "2014-02-28 25:16 PM" + with pytest.raises(ParserError): + parse(dstr, fuzzy=fuzzy) + + def test_second_sanity(self, fuzzy): + dstr = "2014-02-28 22:14:64" + with pytest.raises(ParserError): + parse(dstr, fuzzy=fuzzy) + + +class TestParseUnimplementedCases(object): + @pytest.mark.xfail + def test_somewhat_ambiguous_string(self): + # Ref: github issue #487 + # The parser is choosing the wrong part for hour + # causing datetime to raise an exception. + dtstr = '1237 PM BRST Mon Oct 30 2017' + res = parse(dtstr, tzinfo=self.tzinfos) + assert res == datetime(2017, 10, 30, 12, 37, tzinfo=self.tzinfos) + + @pytest.mark.xfail + def test_YmdH_M_S(self): + # found in nasdaq's ftp data + dstr = '1991041310:19:24' + expected = datetime(1991, 4, 13, 10, 19, 24) + res = parse(dstr) + assert res == expected, (res, expected) + + @pytest.mark.xfail + def test_first_century(self): + dstr = '0031 Nov 03' + expected = datetime(31, 11, 3) + res = parse(dstr) + assert res == expected, res + + @pytest.mark.xfail + def test_era_trailing_year_with_dots(self): + dstr = 'A.D.2001' + res = parse(dstr) + assert res.year == 2001, res + + @pytest.mark.xfail + def test_ad_nospace(self): + expected = datetime(6, 5, 19) + for dstr in [' 6AD May 19', ' 06AD May 19', + ' 006AD May 19', ' 0006AD May 19']: + res = parse(dstr) + assert res == expected, (dstr, res) + + @pytest.mark.xfail + def test_four_letter_day(self): + dstr = 'Frid Dec 30, 2016' + expected = datetime(2016, 12, 30) + res = parse(dstr) + assert res == expected + + @pytest.mark.xfail + def test_non_date_number(self): + dstr = '1,700' + with pytest.raises(ParserError): + parse(dstr) + + @pytest.mark.xfail + def test_on_era(self): + # This could be classified as an "eras" test, but the relevant part + # about this is the ` on ` + dstr = '2:15 PM on January 2nd 1973 A.D.' + expected = datetime(1973, 1, 2, 14, 15) + res = parse(dstr) + assert res == expected + + @pytest.mark.xfail + def test_extraneous_year(self): + # This was found in the wild at insidertrading.org + dstr = "2011 MARTIN CHILDREN'S IRREVOCABLE TRUST u/a/d NOVEMBER 7, 2012" + res = parse(dstr, fuzzy_with_tokens=True) + expected = datetime(2012, 11, 7) + assert res == expected + + @pytest.mark.xfail + def test_extraneous_year_tokens(self): + # This was found in the wild at insidertrading.org + # Unlike in the case above, identifying the first "2012" as the year + # would not be a problem, but inferring that the latter 2012 is hhmm + # is a problem. + dstr = "2012 MARTIN CHILDREN'S IRREVOCABLE TRUST u/a/d NOVEMBER 7, 2012" + expected = datetime(2012, 11, 7) + (res, tokens) = parse(dstr, fuzzy_with_tokens=True) + assert res == expected + assert tokens == ("2012 MARTIN CHILDREN'S IRREVOCABLE TRUST u/a/d ",) + + @pytest.mark.xfail + def test_extraneous_year2(self): + # This was found in the wild at insidertrading.org + dstr = ("Berylson Amy Smith 1998 Grantor Retained Annuity Trust " + "u/d/t November 2, 1998 f/b/o Jennifer L Berylson") + res = parse(dstr, fuzzy_with_tokens=True) + expected = datetime(1998, 11, 2) + assert res == expected + + @pytest.mark.xfail + def test_extraneous_year3(self): + # This was found in the wild at insidertrading.org + dstr = "SMITH R & WEISS D 94 CHILD TR FBO M W SMITH UDT 12/1/1994" + res = parse(dstr, fuzzy_with_tokens=True) + expected = datetime(1994, 12, 1) + assert res == expected + + @pytest.mark.xfail + def test_unambiguous_YYYYMM(self): + # 171206 can be parsed as YYMMDD. However, 201712 cannot be parsed + # as instance of YYMMDD and parser could fallback to YYYYMM format. + dstr = "201712" + res = parse(dstr) + expected = datetime(2017, 12, 1) + assert res == expected + + @pytest.mark.xfail + def test_extraneous_numerical_content(self): + # ref: https://github.com/dateutil/dateutil/issues/1029 + # parser interprets price and percentage as parts of the date + dstr = "£14.99 (25% off, until April 20)" + res = parse(dstr, fuzzy=True, default=datetime(2000, 1, 1)) + expected = datetime(2000, 4, 20) + assert res == expected + + [email protected](IS_WIN, reason="Windows does not use TZ var") +class TestTZVar(object): + def test_parse_unambiguous_nonexistent_local(self): + # When dates are specified "EST" even when they should be "EDT" in the + # local time zone, we should still assign the local time zone + with TZEnvContext('EST+5EDT,M3.2.0/2,M11.1.0/2'): + dt_exp = datetime(2011, 8, 1, 12, 30, tzinfo=tz.tzlocal()) + dt = parse('2011-08-01T12:30 EST') + + assert dt.tzname() == 'EDT' + assert dt == dt_exp + + def test_tzlocal_in_gmt(self): + # GH #318 + with TZEnvContext('GMT0BST,M3.5.0,M10.5.0'): + # This is an imaginary datetime in tz.tzlocal() but should still + # parse using the GMT-as-alias-for-UTC rule + dt = parse('2004-05-01T12:00 GMT') + dt_exp = datetime(2004, 5, 1, 12, tzinfo=tz.UTC) + + assert dt == dt_exp + + def test_tzlocal_parse_fold(self): + # One manifestion of GH #318 + with TZEnvContext('EST+5EDT,M3.2.0/2,M11.1.0/2'): + dt_exp = datetime(2011, 11, 6, 1, 30, tzinfo=tz.tzlocal()) + dt_exp = tz.enfold(dt_exp, fold=1) + dt = parse('2011-11-06T01:30 EST') + + # Because this is ambiguous, until `tz.tzlocal() is tz.tzlocal()` + # we'll just check the attributes we care about rather than + # dt == dt_exp + assert dt.tzname() == dt_exp.tzname() + assert dt.replace(tzinfo=None) == dt_exp.replace(tzinfo=None) + assert getattr(dt, 'fold') == getattr(dt_exp, 'fold') + assert dt.astimezone(tz.UTC) == dt_exp.astimezone(tz.UTC) + + +def test_parse_tzinfos_fold(): + NYC = tz.gettz('America/New_York') + tzinfos = {'EST': NYC, 'EDT': NYC} + + dt_exp = tz.enfold(datetime(2011, 11, 6, 1, 30, tzinfo=NYC), fold=1) + dt = parse('2011-11-06T01:30 EST', tzinfos=tzinfos) + + assert dt == dt_exp + assert dt.tzinfo is dt_exp.tzinfo + assert getattr(dt, 'fold') == getattr(dt_exp, 'fold') + assert dt.astimezone(tz.UTC) == dt_exp.astimezone(tz.UTC) + + [email protected]('dtstr,dt', [ + ('5.6h', datetime(2003, 9, 25, 5, 36)), + ('5.6m', datetime(2003, 9, 25, 0, 5, 36)), + # '5.6s' never had a rounding problem, test added for completeness + ('5.6s', datetime(2003, 9, 25, 0, 0, 5, 600000)) +]) +def test_rounding_floatlike_strings(dtstr, dt): + assert parse(dtstr, default=datetime(2003, 9, 25)) == dt + + [email protected]('value', ['1: test', 'Nan']) +def test_decimal_error(value): + # GH 632, GH 662 - decimal.Decimal raises some non-ParserError exception + # when constructed with an invalid value + with pytest.raises(ParserError): + parse(value) + +def test_parsererror_repr(): + # GH 991 — the __repr__ was not properly indented and so was never defined. + # This tests the current behavior of the ParserError __repr__, but the + # precise format is not guaranteed to be stable and may change even in + # minor versions. This test exists to avoid regressions. + s = repr(ParserError("Problem with string: %s", "2019-01-01")) + + assert s == "ParserError('Problem with string: %s', '2019-01-01')" diff --git a/libs/dateutil/test/test_relativedelta.py b/libs/dateutil/test/test_relativedelta.py index 9e1ca7c1a..1e5d17044 100644 --- a/libs/dateutil/test/test_relativedelta.py +++ b/libs/dateutil/test/test_relativedelta.py @@ -1,14 +1,17 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -from ._common import unittest, WarningTestMixin, NotAValue +from ._common import NotAValue import calendar from datetime import datetime, date, timedelta +import unittest -from dateutil.relativedelta import * +import pytest +from dateutil.relativedelta import relativedelta, MO, TU, WE, FR, SU -class RelativeDeltaTest(WarningTestMixin, unittest.TestCase): + +class RelativeDeltaTest(unittest.TestCase): now = datetime(2003, 9, 17, 20, 54, 47, 282310) today = date(2003, 9, 17) @@ -116,14 +119,30 @@ class RelativeDeltaTest(WarningTestMixin, unittest.TestCase): self.assertEqual(self.today+relativedelta(day=31, weekday=FR(-1)), date(2003, 9, 26)) + def testLastDayOfFebruary(self): + self.assertEqual(date(2021, 2, 1) + relativedelta(day=31), + date(2021, 2, 28)) + + def testLastDayOfFebruaryLeapYear(self): + self.assertEqual(date(2020, 2, 1) + relativedelta(day=31), + date(2020, 2, 29)) + def testNextWednesdayIsToday(self): self.assertEqual(self.today+relativedelta(weekday=WE), date(2003, 9, 17)) - def testNextWenesdayNotToday(self): + def testNextWednesdayNotToday(self): self.assertEqual(self.today+relativedelta(days=+1, weekday=WE), date(2003, 9, 24)) + def testAddMoreThan12Months(self): + self.assertEqual(date(2003, 12, 1) + relativedelta(months=+13), + date(2005, 1, 1)) + + def testAddNegativeMonths(self): + self.assertEqual(date(2003, 1, 1) + relativedelta(months=-2), + date(2002, 11, 1)) + def test15thISOYearWeek(self): self.assertEqual(date(2003, 1, 1) + relativedelta(day=4, weeks=+14, weekday=MO(-1)), @@ -180,6 +199,12 @@ class RelativeDeltaTest(WarningTestMixin, unittest.TestCase): relativedelta(years=1, months=2, days=13, hours=4, minutes=5, microseconds=6)) + def testAbsoluteAddition(self): + self.assertEqual(relativedelta() + relativedelta(day=0, hour=0), + relativedelta(day=0, hour=0)) + self.assertEqual(relativedelta(day=0, hour=0) + relativedelta(), + relativedelta(day=0, hour=0)) + def testAdditionToDatetime(self): self.assertEqual(datetime(2000, 1, 1) + relativedelta(days=1), datetime(2000, 1, 2)) @@ -196,6 +221,31 @@ class RelativeDeltaTest(WarningTestMixin, unittest.TestCase): # For unsupported types that define their own comparators, etc. self.assertIs(relativedelta(days=1) + NotAValue, NotAValue) + def testAdditionFloatValue(self): + self.assertEqual(datetime(2000, 1, 1) + relativedelta(days=float(1)), + datetime(2000, 1, 2)) + self.assertEqual(datetime(2000, 1, 1) + relativedelta(months=float(1)), + datetime(2000, 2, 1)) + self.assertEqual(datetime(2000, 1, 1) + relativedelta(years=float(1)), + datetime(2001, 1, 1)) + + def testAdditionFloatFractionals(self): + self.assertEqual(datetime(2000, 1, 1, 0) + + relativedelta(days=float(0.5)), + datetime(2000, 1, 1, 12)) + self.assertEqual(datetime(2000, 1, 1, 0, 0) + + relativedelta(hours=float(0.5)), + datetime(2000, 1, 1, 0, 30)) + self.assertEqual(datetime(2000, 1, 1, 0, 0, 0) + + relativedelta(minutes=float(0.5)), + datetime(2000, 1, 1, 0, 0, 30)) + self.assertEqual(datetime(2000, 1, 1, 0, 0, 0, 0) + + relativedelta(seconds=float(0.5)), + datetime(2000, 1, 1, 0, 0, 0, 500000)) + self.assertEqual(datetime(2000, 1, 1, 0, 0, 0, 0) + + relativedelta(microseconds=float(500000.25)), + datetime(2000, 1, 1, 0, 0, 0, 500000)) + def testSubtraction(self): self.assertEqual(relativedelta(days=10) - relativedelta(years=1, months=2, days=3, hours=4, @@ -238,6 +288,20 @@ class RelativeDeltaTest(WarningTestMixin, unittest.TestCase): self.assertFalse(relativedelta(days=0)) self.assertTrue(relativedelta(days=1)) + def testAbsoluteValueNegative(self): + rd_base = relativedelta(years=-1, months=-5, days=-2, hours=-3, + minutes=-5, seconds=-2, microseconds=-12) + rd_expected = relativedelta(years=1, months=5, days=2, hours=3, + minutes=5, seconds=2, microseconds=12) + self.assertEqual(abs(rd_base), rd_expected) + + def testAbsoluteValuePositive(self): + rd_base = relativedelta(years=1, months=5, days=2, hours=3, + minutes=5, seconds=2, microseconds=12) + rd_expected = rd_base + + self.assertEqual(abs(rd_base), rd_expected) + def testComparison(self): d1 = relativedelta(years=1, months=1, days=1, leapdays=0, hours=1, minutes=1, seconds=1, microseconds=1) @@ -304,28 +368,38 @@ class RelativeDeltaTest(WarningTestMixin, unittest.TestCase): with self.assertRaises(ValueError): relativedelta(months=1.5) + def testRelativeDeltaInvalidDatetimeObject(self): + with self.assertRaises(TypeError): + relativedelta(dt1='2018-01-01', dt2='2018-01-02') + + with self.assertRaises(TypeError): + relativedelta(dt1=datetime(2018, 1, 1), dt2='2018-01-02') + + with self.assertRaises(TypeError): + relativedelta(dt1='2018-01-01', dt2=datetime(2018, 1, 2)) + def testRelativeDeltaFractionalAbsolutes(self): # Fractional absolute values will soon be unsupported, # check for the deprecation warning. - with self.assertWarns(DeprecationWarning): + with pytest.warns(DeprecationWarning): relativedelta(year=2.86) - with self.assertWarns(DeprecationWarning): + with pytest.warns(DeprecationWarning): relativedelta(month=1.29) - with self.assertWarns(DeprecationWarning): + with pytest.warns(DeprecationWarning): relativedelta(day=0.44) - with self.assertWarns(DeprecationWarning): + with pytest.warns(DeprecationWarning): relativedelta(hour=23.98) - with self.assertWarns(DeprecationWarning): + with pytest.warns(DeprecationWarning): relativedelta(minute=45.21) - with self.assertWarns(DeprecationWarning): + with pytest.warns(DeprecationWarning): relativedelta(second=13.2) - with self.assertWarns(DeprecationWarning): + with pytest.warns(DeprecationWarning): relativedelta(microsecond=157221.93) def testRelativeDeltaFractionalRepr(self): @@ -410,13 +484,13 @@ class RelativeDeltaTest(WarningTestMixin, unittest.TestCase): self.assertEqual(rd1.normalized(), relativedelta(days=2, hours=18)) - # Equvalent to (days=1, hours=11, minutes=31, seconds=12) + # Equivalent 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): + def testRelativeDeltaNormalizeFractionalDays2(self): # Equivalent to (hours=1, minutes=30) rd1 = relativedelta(hours=1.5) @@ -447,7 +521,7 @@ class RelativeDeltaTest(WarningTestMixin, unittest.TestCase): self.assertEqual(rd1.normalized(), relativedelta(seconds=45, microseconds=25000)) - def testRelativeDeltaFractionalPositiveOverflow(self): + def testRelativeDeltaFractionalPositiveOverflow2(self): # Equivalent to (days=1, hours=14) rd1 = relativedelta(days=1.5, hours=2) self.assertEqual(rd1.normalized(), @@ -569,3 +643,64 @@ class RelativeDeltaTest(WarningTestMixin, unittest.TestCase): ) self.assertEqual(expected, rd + td) + + def testHashable(self): + try: + {relativedelta(minute=1): 'test'} + except: + self.fail("relativedelta() failed to hash!") + + +class RelativeDeltaWeeksPropertyGetterTest(unittest.TestCase): + """Test the weeks property getter""" + + def test_one_day(self): + rd = relativedelta(days=1) + self.assertEqual(rd.days, 1) + self.assertEqual(rd.weeks, 0) + + def test_minus_one_day(self): + rd = relativedelta(days=-1) + self.assertEqual(rd.days, -1) + self.assertEqual(rd.weeks, 0) + + def test_height_days(self): + rd = relativedelta(days=8) + self.assertEqual(rd.days, 8) + self.assertEqual(rd.weeks, 1) + + def test_minus_height_days(self): + rd = relativedelta(days=-8) + self.assertEqual(rd.days, -8) + self.assertEqual(rd.weeks, -1) + + +class RelativeDeltaWeeksPropertySetterTest(unittest.TestCase): + """Test the weeks setter which makes a "smart" update of the days attribute""" + + def test_one_day_set_one_week(self): + rd = relativedelta(days=1) + rd.weeks = 1 # add 7 days + self.assertEqual(rd.days, 8) + self.assertEqual(rd.weeks, 1) + + def test_minus_one_day_set_one_week(self): + rd = relativedelta(days=-1) + rd.weeks = 1 # add 7 days + self.assertEqual(rd.days, 6) + self.assertEqual(rd.weeks, 0) + + def test_height_days_set_minus_one_week(self): + rd = relativedelta(days=8) + rd.weeks = -1 # change from 1 week, 1 day to -1 week, 1 day + self.assertEqual(rd.days, -6) + self.assertEqual(rd.weeks, 0) + + def test_minus_height_days_set_minus_one_week(self): + rd = relativedelta(days=-8) + rd.weeks = -1 # does not change anything + self.assertEqual(rd.days, -8) + self.assertEqual(rd.weeks, -1) + + +# vim:ts=4:sw=4:et diff --git a/libs/dateutil/test/test_rrule.py b/libs/dateutil/test/test_rrule.py index 2a1e6e8b4..52673ecc2 100644 --- a/libs/dateutil/test/test_rrule.py +++ b/libs/dateutil/test/test_rrule.py @@ -1,15 +1,25 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -from ._common import WarningTestMixin, unittest -import calendar from datetime import datetime, date -from six import PY3 +import unittest +from six import PY2 -from dateutil.rrule import * +from dateutil import tz +from dateutil.rrule import ( + rrule, rruleset, rrulestr, + YEARLY, MONTHLY, WEEKLY, DAILY, + HOURLY, MINUTELY, SECONDLY, + MO, TU, WE, TH, FR, SA, SU +) +from freezegun import freeze_time -class RRuleTest(WarningTestMixin, unittest.TestCase): +import pytest + + +class RRuleTest(unittest.TestCase): def _rrulestr_reverse_test(self, rule): """ Call with an `rrule` and it will test that `str(rrule)` generates a @@ -21,6 +31,20 @@ class RRuleTest(WarningTestMixin, unittest.TestCase): self.assertEqual(list(rule), list(rrulestr_rrule)) + def testStrAppendRRULEToken(self): + # `_rrulestr_reverse_test` does not check if the "RRULE:" prefix + # property is appended properly, so give it a dedicated test + self.assertEqual(str(rrule(YEARLY, + count=5, + dtstart=datetime(1997, 9, 2, 9, 0))), + "DTSTART:19970902T090000\n" + "RRULE:FREQ=YEARLY;COUNT=5") + + rr_str = ( + 'DTSTART:19970105T083000\nRRULE:FREQ=YEARLY;INTERVAL=2' + ) + self.assertEqual(str(rrulestr(rr_str)), rr_str) + def testYearly(self): self.assertEqual(list(rrule(YEARLY, count=3, @@ -2259,7 +2283,7 @@ class RRuleTest(WarningTestMixin, unittest.TestCase): datetime(2010, 3, 22, 14, 1)]) def testLongIntegers(self): - if not PY3: # There is no longs in python3 + if PY2: # There are no longs in python3 self.assertEqual(list(rrule(MINUTELY, count=long(2), interval=long(2), @@ -2344,10 +2368,10 @@ class RRuleTest(WarningTestMixin, unittest.TestCase): def testBadUntilCountRRule(self): """ - See rfc-2445 4.3.10 - This checks for the deprecation warning, and will + See rfc-5545 3.3.10 - This checks for the deprecation warning, and will eventually check for an error. """ - with self.assertWarns(DeprecationWarning): + with pytest.warns(DeprecationWarning): rrule(DAILY, dtstart=datetime(1997, 9, 2, 9, 0), count=3, until=datetime(1997, 9, 4, 9, 0)) @@ -2472,6 +2496,12 @@ class RRuleTest(WarningTestMixin, unittest.TestCase): dtstart=datetime(1997, 9, 2, 9, 0)).count(), 3) + def testCountZero(self): + self.assertEqual(rrule(YEARLY, + count=0, + dtstart=datetime(1997, 9, 2, 9, 0)).count(), + 0) + 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) @@ -2644,6 +2674,70 @@ class RRuleTest(WarningTestMixin, unittest.TestCase): datetime(1998, 9, 2, 9, 0), datetime(1999, 9, 2, 9, 0)]) + def testStrWithTZID(self): + NYC = tz.gettz('America/New_York') + self.assertEqual(list(rrulestr( + "DTSTART;TZID=America/New_York:19970902T090000\n" + "RRULE:FREQ=YEARLY;COUNT=3\n" + )), + [datetime(1997, 9, 2, 9, 0, tzinfo=NYC), + datetime(1998, 9, 2, 9, 0, tzinfo=NYC), + datetime(1999, 9, 2, 9, 0, tzinfo=NYC)]) + + def testStrWithTZIDMapping(self): + rrstr = ("DTSTART;TZID=Eastern:19970902T090000\n" + + "RRULE:FREQ=YEARLY;COUNT=3") + + NYC = tz.gettz('America/New_York') + rr = rrulestr(rrstr, tzids={'Eastern': NYC}) + exp = [datetime(1997, 9, 2, 9, 0, tzinfo=NYC), + datetime(1998, 9, 2, 9, 0, tzinfo=NYC), + datetime(1999, 9, 2, 9, 0, tzinfo=NYC)] + + self.assertEqual(list(rr), exp) + + def testStrWithTZIDCallable(self): + rrstr = ('DTSTART;TZID=UTC+04:19970902T090000\n' + + 'RRULE:FREQ=YEARLY;COUNT=3') + + TZ = tz.tzstr('UTC+04') + def parse_tzstr(tzstr): + if tzstr is None: + raise ValueError('Invalid tzstr') + + return tz.tzstr(tzstr) + + rr = rrulestr(rrstr, tzids=parse_tzstr) + + exp = [datetime(1997, 9, 2, 9, 0, tzinfo=TZ), + datetime(1998, 9, 2, 9, 0, tzinfo=TZ), + datetime(1999, 9, 2, 9, 0, tzinfo=TZ),] + + self.assertEqual(list(rr), exp) + + def testStrWithTZIDCallableFailure(self): + rrstr = ('DTSTART;TZID=America/New_York:19970902T090000\n' + + 'RRULE:FREQ=YEARLY;COUNT=3') + + class TzInfoError(Exception): + pass + + def tzinfos(tzstr): + if tzstr == 'America/New_York': + raise TzInfoError('Invalid!') + return None + + with self.assertRaises(TzInfoError): + rrulestr(rrstr, tzids=tzinfos) + + def testStrWithConflictingTZID(self): + # RFC 5545 Section 3.3.5, FORM #2: DATE WITH UTC TIME + # https://tools.ietf.org/html/rfc5545#section-3.3.5 + # The "TZID" property parameter MUST NOT be applied to DATE-TIME + with self.assertRaises(ValueError): + rrulestr("DTSTART;TZID=America/New_York:19970902T090000Z\n"+ + "RRULE:FREQ=YEARLY;COUNT=3\n") + def testStrType(self): self.assertEqual(isinstance(rrulestr( "DTSTART:19970902T090000\n" @@ -2758,6 +2852,74 @@ class RRuleTest(WarningTestMixin, unittest.TestCase): datetime(1997, 9, 9, 9, 0), datetime(1997, 9, 16, 9, 0)]) + def testStrSetExDateMultiple(self): + rrstr = ("DTSTART:19970902T090000\n" + "RRULE:FREQ=YEARLY;COUNT=6;BYDAY=TU,TH\n" + "EXDATE:19970904T090000,19970911T090000,19970918T090000\n") + + rr = rrulestr(rrstr) + assert list(rr) == [datetime(1997, 9, 2, 9, 0), + datetime(1997, 9, 9, 9, 0), + datetime(1997, 9, 16, 9, 0)] + + def testStrSetExDateWithTZID(self): + BXL = tz.gettz('Europe/Brussels') + rr = rrulestr("DTSTART;TZID=Europe/Brussels:19970902T090000\n" + "RRULE:FREQ=YEARLY;COUNT=6;BYDAY=TU,TH\n" + "EXDATE;TZID=Europe/Brussels:19970904T090000\n" + "EXDATE;TZID=Europe/Brussels:19970911T090000\n" + "EXDATE;TZID=Europe/Brussels:19970918T090000\n") + + assert list(rr) == [datetime(1997, 9, 2, 9, 0, tzinfo=BXL), + datetime(1997, 9, 9, 9, 0, tzinfo=BXL), + datetime(1997, 9, 16, 9, 0, tzinfo=BXL)] + + def testStrSetExDateValueDateTimeNoTZID(self): + rrstr = '\n'.join([ + "DTSTART:19970902T090000", + "RRULE:FREQ=YEARLY;COUNT=4;BYDAY=TU,TH", + "EXDATE;VALUE=DATE-TIME:19970902T090000", + "EXDATE;VALUE=DATE-TIME:19970909T090000", + ]) + + rr = rrulestr(rrstr) + assert list(rr) == [datetime(1997, 9, 4, 9), datetime(1997, 9, 11, 9)] + + def testStrSetExDateValueMixDateTimeNoTZID(self): + rrstr = '\n'.join([ + "DTSTART:19970902T090000", + "RRULE:FREQ=YEARLY;COUNT=4;BYDAY=TU,TH", + "EXDATE;VALUE=DATE-TIME:19970902T090000", + "EXDATE:19970909T090000", + ]) + + rr = rrulestr(rrstr) + assert list(rr) == [datetime(1997, 9, 4, 9), datetime(1997, 9, 11, 9)] + + def testStrSetExDateValueDateTimeWithTZID(self): + BXL = tz.gettz('Europe/Brussels') + rrstr = '\n'.join([ + "DTSTART;VALUE=DATE-TIME;TZID=Europe/Brussels:19970902T090000", + "RRULE:FREQ=YEARLY;COUNT=4;BYDAY=TU,TH", + "EXDATE;VALUE=DATE-TIME;TZID=Europe/Brussels:19970902T090000", + "EXDATE;VALUE=DATE-TIME;TZID=Europe/Brussels:19970909T090000", + ]) + + rr = rrulestr(rrstr) + assert list(rr) == [datetime(1997, 9, 4, 9, tzinfo=BXL), + datetime(1997, 9, 11, 9, tzinfo=BXL)] + + def testStrSetExDateValueDate(self): + rrstr = '\n'.join([ + "DTSTART;VALUE=DATE:19970902", + "RRULE:FREQ=YEARLY;COUNT=4;BYDAY=TU,TH", + "EXDATE;VALUE=DATE:19970902", + "EXDATE;VALUE=DATE:19970909", + ]) + + rr = rrulestr(rrstr) + assert list(rr) == [datetime(1997, 9, 4), datetime(1997, 9, 11)] + def testStrSetDateAndExDate(self): self.assertEqual(list(rrulestr( "DTSTART:19970902T090000\n" @@ -2812,7 +2974,7 @@ class RRuleTest(WarningTestMixin, unittest.TestCase): def testStrUntil(self): self.assertEqual(list(rrulestr( - "DTSTART:19970902T090000\n" + "DTSTART:19970902T090000\n" "RRULE:FREQ=YEARLY;" "UNTIL=19990101T000000;BYDAY=1TU,-1TH\n" )), @@ -2820,12 +2982,45 @@ class RRuleTest(WarningTestMixin, unittest.TestCase): datetime(1998, 1, 6, 9, 0), datetime(1998, 12, 31, 9, 0)]) + def testStrValueDatetime(self): + rr = rrulestr("DTSTART;VALUE=DATE-TIME:19970902T090000\n" + "RRULE:FREQ=YEARLY;COUNT=2") + + self.assertEqual(list(rr), [datetime(1997, 9, 2, 9, 0, 0), + datetime(1998, 9, 2, 9, 0, 0)]) + + def testStrValueDate(self): + rr = rrulestr("DTSTART;VALUE=DATE:19970902\n" + "RRULE:FREQ=YEARLY;COUNT=2") + + self.assertEqual(list(rr), [datetime(1997, 9, 2, 0, 0, 0), + datetime(1998, 9, 2, 0, 0, 0)]) + + def testStrMultipleDTStartComma(self): + with pytest.raises(ValueError): + rr = rrulestr("DTSTART:19970101T000000,19970202T000000\n" + "RRULE:FREQ=YEARLY;COUNT=1") + def testStrInvalidUntil(self): with self.assertRaises(ValueError): list(rrulestr("DTSTART:19970902T090000\n" "RRULE:FREQ=YEARLY;" "UNTIL=TheCowsComeHome;BYDAY=1TU,-1TH\n")) + def testStrUntilMustBeUTC(self): + with self.assertRaises(ValueError): + list(rrulestr("DTSTART;TZID=America/New_York:19970902T090000\n" + "RRULE:FREQ=YEARLY;" + "UNTIL=19990101T000000;BYDAY=1TU,-1TH\n")) + + def testStrUntilWithTZ(self): + NYC = tz.gettz('America/New_York') + rr = list(rrulestr("DTSTART;TZID=America/New_York:19970101T000000\n" + "RRULE:FREQ=YEARLY;" + "UNTIL=19990101T000000Z\n")) + self.assertEqual(list(rr), [datetime(1997, 1, 1, 0, 0, 0, tzinfo=NYC), + datetime(1998, 1, 1, 0, 0, 0, tzinfo=NYC)]) + def testStrEmptyByDay(self): with self.assertRaises(ValueError): list(rrulestr("DTSTART:19970902T090000\n" @@ -2865,12 +3060,6 @@ class RRuleTest(WarningTestMixin, unittest.TestCase): 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), @@ -4389,7 +4578,7 @@ class RRuleTest(WarningTestMixin, unittest.TestCase): dtstart=datetime(1997, 9, 2, 9, 0))) def testToStrLongIntegers(self): - if not PY3: # There is no longs in python3 + if PY2: # There are no longs in python3 self._rrulestr_reverse_test(rrule(MINUTELY, count=long(2), interval=long(2), @@ -4399,7 +4588,7 @@ class RRuleTest(WarningTestMixin, unittest.TestCase): 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), @@ -4426,6 +4615,31 @@ class RRuleTest(WarningTestMixin, unittest.TestCase): [datetime(1997, 1, 6)]) +@freeze_time(datetime(2018, 3, 6, 5, 36, tzinfo=tz.UTC)) +def test_generated_aware_dtstart(): + dtstart_exp = datetime(2018, 3, 6, 5, 36, tzinfo=tz.UTC) + UNTIL = datetime(2018, 3, 6, 8, 0, tzinfo=tz.UTC) + + rule_without_dtstart = rrule(freq=HOURLY, until=UNTIL) + rule_with_dtstart = rrule(freq=HOURLY, dtstart=dtstart_exp, until=UNTIL) + assert list(rule_without_dtstart) == list(rule_with_dtstart) + + [email protected](reason="rrulestr loses time zone, gh issue #637") +@freeze_time(datetime(2018, 3, 6, 5, 36, tzinfo=tz.UTC)) +def test_generated_aware_dtstart_rrulestr(): + rrule_without_dtstart = rrule(freq=HOURLY, + until=datetime(2018, 3, 6, 8, 0, + tzinfo=tz.UTC)) + rrule_r = rrulestr(str(rrule_without_dtstart)) + + assert list(rrule_r) == list(rrule_without_dtstart) + + class RRuleSetTest(unittest.TestCase): def testSet(self): rrset = rruleset() @@ -4641,7 +4855,7 @@ class RRuleSetTest(unittest.TestCase): class WeekdayTest(unittest.TestCase): def testInvalidNthWeekday(self): with self.assertRaises(ValueError): - zeroth_friday = FR(0) + FR(0) def testWeekdayCallable(self): # Calling a weekday instance generates a new weekday instance with the @@ -4672,7 +4886,7 @@ class WeekdayTest(unittest.TestCase): self.n = n MO_Basic = BasicWeekday(0) - + self.assertNotEqual(MO, MO_Basic) self.assertNotEqual(MO(1), MO_Basic) @@ -4698,4 +4912,3 @@ class WeekdayTest(unittest.TestCase): 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 index 4ca203661..e5e4772d9 100644 --- a/libs/dateutil/test/test_tz.py +++ b/libs/dateutil/test/test_tz.py @@ -1,29 +1,30 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -from ._common import unittest, PicklableMixin -from ._common import total_seconds +from ._common import PicklableMixin 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 +from six import PY2 +from io import BytesIO, StringIO +import unittest -import os -import subprocess import sys import base64 import copy -import itertools +import gc +import weakref from functools import partial IS_WIN = sys.platform.startswith('win') +import pytest + # dateutil imports -from dateutil.relativedelta import relativedelta, SU +from dateutil.relativedelta import relativedelta, SU, TH from dateutil.parser import parse from dateutil import tz as tz from dateutil import zoneinfo @@ -152,6 +153,19 @@ END:DAYLIGHT END:VTIMEZONE """ +EST_TUPLE = ('EST', timedelta(hours=-5), timedelta(hours=0)) +EDT_TUPLE = ('EDT', timedelta(hours=-4), timedelta(hours=1)) + +SUPPORTS_SUB_MINUTE_OFFSETS = sys.version_info >= (3, 6) + + +### +# Helper functions +def get_timezone_tuple(dt): + """Retrieve a (tzname, utcoffset, dst) tuple for a given DST""" + return dt.tzname(), dt.utcoffset(), dt.dst() + + ### # Mix-ins class context_passthrough(object): @@ -181,15 +195,13 @@ class TzFoldMixin(object): tzname = self._get_tzname('Australia/Sydney') with self._gettz_context(tzname): - SYD0 = self.gettz(tzname) - SYD1 = self.gettz(tzname) + SYD = 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 + t0_u = datetime(2012, 3, 31, 15, 30, tzinfo=tz.UTC) # AEST + t1_u = datetime(2012, 3, 31, 16, 30, tzinfo=tz.UTC) # AEDT - # Using fresh tzfiles - t0_syd0 = t0_u.astimezone(SYD0) - t1_syd1 = t1_u.astimezone(SYD1) + t0_syd0 = t0_u.astimezone(SYD) + t1_syd1 = t1_u.astimezone(SYD) self.assertEqual(t0_syd0.replace(tzinfo=None), datetime(2012, 4, 1, 2, 30)) @@ -200,21 +212,18 @@ class TzFoldMixin(object): 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) + SYD = 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 + t0_u = datetime(2012, 10, 6, 15, 30, tzinfo=tz.UTC) # AEST + t1_u = datetime(2012, 10, 6, 16, 30, tzinfo=tz.UTC) # AEDT - # Using fresh tzfiles - t0 = t0_u.astimezone(SYD0) - t1 = t1_u.astimezone(SYD1) + t0 = t0_u.astimezone(SYD) + t1 = t1_u.astimezone(SYD) self.assertEqual(t0.replace(tzinfo=None), datetime(2012, 10, 7, 1, 30)) @@ -230,41 +239,36 @@ class TzFoldMixin(object): 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) + TOR = 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()) + t0_u = datetime(2011, 11, 6, 5, 30, tzinfo=tz.UTC) + t1_u = datetime(2011, 11, 6, 6, 30, tzinfo=tz.UTC) - # Using fresh tzfiles - t0_tor0 = t0_u.astimezone(TOR0) - t1_tor1 = t1_u.astimezone(TOR1) + t0_tor = t0_u.astimezone(TOR) + t1_tor = t1_u.astimezone(TOR) - self.assertEqual(t0_tor0.replace(tzinfo=None), + self.assertEqual(t0_tor.replace(tzinfo=None), datetime(2011, 11, 6, 1, 30)) - self.assertEqual(t1_tor1.replace(tzinfo=None), + self.assertEqual(t1_tor.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)) + self.assertNotEqual(t0_tor.tzname(), t1_tor.tzname()) + self.assertEqual(t0_tor.utcoffset(), timedelta(hours=-4.0)) + self.assertEqual(t1_tor.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) + TOR = 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()) + t0_u = datetime(2011, 3, 13, 6, 30, tzinfo=tz.UTC) + t1_u = datetime(2011, 3, 13, 7, 30, tzinfo=tz.UTC) - # Using fresh tzfiles - t0 = t0_u.astimezone(TOR0) - t1 = t1_u.astimezone(TOR1) + t0 = t0_u.astimezone(TOR) + t1 = t1_u.astimezone(TOR) self.assertEqual(t0.replace(tzinfo=None), datetime(2011, 3, 13, 1, 30)) @@ -276,22 +280,39 @@ class TzFoldMixin(object): self.assertEqual(t0.utcoffset(), timedelta(hours=-5.0)) self.assertEqual(t1.utcoffset(), timedelta(hours=-4.0)) + def testFoldLondon(self): + tzname = self._get_tzname('Europe/London') + + with self._gettz_context(tzname): + LON = self.gettz(tzname) + UTC = tz.UTC + + t0_u = datetime(2013, 10, 27, 0, 30, tzinfo=UTC) # BST + t1_u = datetime(2013, 10, 27, 1, 30, tzinfo=UTC) # GMT + + t0 = t0_u.astimezone(LON) + t1 = t1_u.astimezone(LON) + + self.assertEqual(t0.replace(tzinfo=None), + datetime(2013, 10, 27, 1, 30)) + + self.assertEqual(t1.replace(tzinfo=None), + datetime(2013, 10, 27, 1, 30)) + + self.assertEqual(t0.utcoffset(), timedelta(hours=1)) + self.assertEqual(t1.utcoffset(), timedelta(hours=0)) + def testFoldIndependence(self): tzname = self._get_tzname('America/New_York') with self._gettz_context(tzname): NYC = self.gettz(tzname) - UTC = tz.tzutc() + UTC = tz.UTC 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 @@ -309,6 +330,25 @@ class TzFoldMixin(object): # Now check to make sure in_dst's tzname hasn't changed self.assertEqual(in_dst_tzname_0, in_dst.tzname()) + def testInZoneFoldEquality(self): + # Two datetimes in the same zone are considered to be equal if their + # wall times are equal, even if they have different absolute times. + + tzname = self._get_tzname('America/New_York') + + with self._gettz_context(tzname): + NYC = self.gettz(tzname) + UTC = tz.UTC + + dt0 = datetime(2011, 11, 6, 1, 30, tzinfo=NYC) + dt1 = tz.enfold(dt0, fold=1) + + # Make sure these actually represent different times + self.assertNotEqual(dt0.astimezone(UTC), dt1.astimezone(UTC)) + + # Test that they compare equal + self.assertEqual(dt0, dt1) + 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. @@ -384,8 +424,7 @@ class TzFoldMixin(object): 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_u = datetime(2012, 3, 31, 14, 30, tzinfo=tz.UTC) # AEST t0_syd0 = t0_u.astimezone(SYD0) t0_syd1 = t0_u.astimezone(SYD1) @@ -414,13 +453,13 @@ class TzWinFoldMixin(object): if gap: t_n = dston - timedelta(minutes=30) - t0_u = t_n.replace(tzinfo=tzi).astimezone(tz.tzutc()) + t0_u = t_n.replace(tzinfo=tzi).astimezone(tz.UTC) 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()) + t0_u = t_n.replace(tzinfo=tzi).astimezone(tz.UTC) 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 @@ -435,33 +474,22 @@ class TzWinFoldMixin(object): 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) + t_n, t0_u, t1_u = self.get_utc_transitions(SYD, 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) + t0_syd = t0_u.astimezone(SYD) + t1_syd = t1_u.astimezone(SYD) - self.assertNotEqual(t0_syd0, t1_syd1) - self.assertEqual(t0_syd0.utcoffset(), timedelta(hours=11)) - self.assertEqual(t1_syd1.utcoffset(), timedelta(hours=10)) + self.assertEqual(t0_syd.replace(tzinfo=None), t_n) - # 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(t1_syd.replace(tzinfo=None), t_n) - self.assertEqual(t0_syd0, t0_syd1) - self.assertEqual(t1_syd1, t1_syd0) + self.assertEqual(t0_syd.utcoffset(), timedelta(hours=11)) + self.assertEqual(t1_syd.utcoffset(), timedelta(hours=10)) + self.assertNotEqual(t0_syd.tzname(), t1_syd.tzname()) def testGapPositiveUTCOffset(self): # Test that we don't have a problem around gaps. @@ -469,18 +497,12 @@ class TzWinFoldMixin(object): 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) + t0 = t0_u.astimezone(SYD) + t1 = t1_u.astimezone(SYD) self.assertEqual(t0.replace(tzinfo=None), t_n) @@ -494,48 +516,33 @@ class TzWinFoldMixin(object): 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) + t0_tor = t0_u.astimezone(TOR) + t1_tor = t1_u.astimezone(TOR) - 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)) + self.assertEqual(t0_tor.replace(tzinfo=None), t_n) + self.assertEqual(t1_tor.replace(tzinfo=None), t_n) - # 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) + self.assertNotEqual(t0_tor.tzname(), t1_tor.tzname()) + self.assertEqual(t0_tor.utcoffset(), timedelta(hours=-4.0)) + self.assertEqual(t1_tor.utcoffset(), timedelta(hours=-5.0)) 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) + t0 = t0_u.astimezone(TOR) + t1 = t1_u.astimezone(TOR) self.assertEqual(t0.replace(tzinfo=None), t_n) @@ -543,7 +550,7 @@ class TzWinFoldMixin(object): self.assertEqual(t1.replace(tzinfo=None), t_n + timedelta(hours=2)) - self.assertNotEqual(t0, t1) + self.assertNotEqual(t0.tzname(), t1.tzname()) self.assertEqual(t0.utcoffset(), timedelta(hours=-5.0)) self.assertEqual(t1.utcoffset(), timedelta(hours=-4.0)) @@ -553,7 +560,7 @@ class TzWinFoldMixin(object): with self.context(tzname): NYC = self.tzclass(*args) - UTC = tz.tzutc() + UTC = tz.UTC hour = timedelta(hours=1) # Firmly 2015-11-01 0:30 EDT-4 @@ -580,10 +587,36 @@ class TzWinFoldMixin(object): # Now check to make sure in_dst's tzname hasn't changed self.assertEqual(in_dst_tzname_0, in_dst.tzname()) + def testInZoneFoldEquality(self): + # Two datetimes in the same zone are considered to be equal if their + # wall times are equal, even if they have different absolute times. + tzname = 'Eastern Standard Time' + args = self.get_args(tzname) + + with self.context(tzname): + NYC = self.tzclass(*args) + UTC = tz.UTC + + t_n, t0_u, t1_u = self.get_utc_transitions(NYC, 2011, False) + + dt0 = t_n.replace(tzinfo=NYC) + dt1 = tz.enfold(dt0, fold=1) + + # Make sure these actually represent different times + self.assertNotEqual(dt0.astimezone(UTC), dt1.astimezone(UTC)) + + # Test that they compare equal + self.assertEqual(dt0, dt1) ### # Test Cases class TzUTCTest(unittest.TestCase): + def testSingleton(self): + UTC_0 = tz.tzutc() + UTC_1 = tz.tzutc() + + self.assertIs(UTC_0, UTC_1) + def testOffset(self): ct = datetime(2009, 4, 1, 12, 11, 13, tzinfo=tz.tzutc()) @@ -602,7 +635,6 @@ class TzUTCTest(unittest.TestCase): UTC0 = tz.tzutc() UTC1 = tz.tzutc() - self.assertIsNot(UTC0, UTC1) self.assertEqual(UTC0, UTC1) def testInequality(self): @@ -636,6 +668,7 @@ class TzUTCTest(unittest.TestCase): self.assertFalse(tz.datetime_ambiguous(dt)) class TzOffsetTest(unittest.TestCase): def testTimedeltaOffset(self): est = tz.tzoffset('EST', timedelta(hours=-5)) @@ -659,7 +692,6 @@ class TzOffsetTest(unittest.TestCase): 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) @@ -667,7 +699,7 @@ class TzOffsetTest(unittest.TestCase): self.assertEqual(utc, gmt) def testUTCEquality(self): - utc = tz.tzutc() + utc = tz.UTC o_utc = tz.tzoffset('UTC', 0) self.assertEqual(utc, o_utc) @@ -691,7 +723,73 @@ class TzOffsetTest(unittest.TestCase): self.assertFalse(tz.datetime_ambiguous(dt)) + def testTzOffsetInstance(self): + tz1 = tz.tzoffset.instance('EST', timedelta(hours=-5)) + tz2 = tz.tzoffset.instance('EST', timedelta(hours=-5)) + + assert tz1 is not tz2 + + def testTzOffsetSingletonDifferent(self): + tz1 = tz.tzoffset('EST', timedelta(hours=-5)) + tz2 = tz.tzoffset('EST', -18000) + + assert tz1 is tz2 + +def test_tzoffset_weakref(): + UTC1 = tz.tzoffset('UTC', 0) + UTC_ref = weakref.ref(tz.tzoffset('UTC', 0)) + UTC1 is UTC_ref() + del UTC1 + gc.collect() + + assert UTC_ref() is not None # Should be in the strong cache + assert UTC_ref() is tz.tzoffset('UTC', 0) + + # Fill the strong cache with other items + for offset in range(5,15): + tz.tzoffset('RandomZone', offset) + + gc.collect() + assert UTC_ref() is None + assert UTC_ref() is not tz.tzoffset('UTC', 0) + + [email protected]('args', [ + ('UTC', 0), + ('EST', -18000), + ('EST', timedelta(hours=-5)), + (None, timedelta(hours=3)), +]) +def test_tzoffset_singleton(args): + tz1 = tz.tzoffset(*args) + tz2 = tz.tzoffset(*args) + + assert tz1 is tz2 + + [email protected](not SUPPORTS_SUB_MINUTE_OFFSETS, + reason='Sub-minute offsets not supported') +def test_tzoffset_sub_minute(): + delta = timedelta(hours=12, seconds=30) + test_datetime = datetime(2000, 1, 1, tzinfo=tz.tzoffset(None, delta)) + assert test_datetime.utcoffset() == delta + + [email protected](SUPPORTS_SUB_MINUTE_OFFSETS, + reason='Sub-minute offsets supported') +def test_tzoffset_sub_minute_rounding(): + delta = timedelta(hours=12, seconds=30) + test_date = datetime(2000, 1, 1, tzinfo=tz.tzoffset(None, delta)) + assert test_date.utcoffset() == timedelta(hours=12, minutes=1) + + class TzLocalTest(unittest.TestCase): def testEquality(self): tz1 = tz.tzlocal() @@ -703,8 +801,8 @@ class TzLocalTest(unittest.TestCase): def testInequalityFixedOffset(self): tzl = tz.tzlocal() - tzos = tz.tzoffset('LST', total_seconds(tzl._std_offset)) - tzod = tz.tzoffset('LDT', total_seconds(tzl._std_offset)) + tzos = tz.tzoffset('LST', tzl._std_offset.total_seconds()) + tzod = tz.tzoffset('LDT', tzl._std_offset.total_seconds()) self.assertFalse(tzl == tzos) self.assertFalse(tzl == tzod) @@ -713,12 +811,15 @@ class TzLocalTest(unittest.TestCase): 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) + + # TODO: Use some sort of universal local mocking so that it's clear + # that we're expecting tzlocal to *not* be Pacific/Kiritimati + LINT = tz.gettz('Pacific/Kiritimati') + self.assertTrue(tzl != LINT) + self.assertFalse(tzl == LINT) def testInequalityUnsupported(self): tzl = tz.tzlocal() @@ -732,9 +833,24 @@ class TzLocalTest(unittest.TestCase): self.assertEqual(repr(tzl), 'tzlocal()') [email protected]('args,kwargs', [ + (('EST', -18000), {}), + (('EST', timedelta(hours=-5)), {}), + (('EST',), {'offset': -18000}), + (('EST',), {'offset': timedelta(hours=-5)}), + (tuple(), {'name': 'EST', 'offset': -18000}) +]) +def test_tzoffset_is(args, kwargs): + tz_ref = tz.tzoffset('EST', -18000) + assert tz.tzoffset(*args, **kwargs) is tz_ref + + +def test_tzoffset_is_not(): + assert tz.tzoffset('EDT', -14400) is not tz.tzoffset('EST', -18000) + + @unittest.skipIf(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 @@ -745,6 +861,9 @@ class TzLocalNixTest(unittest.TestCase, TzFoldMixin): # POSIX string for AEST/AEDT (valid >= 2008) TZ_AEST = 'AEST-10AEDT,M10.1.0/2,M4.1.0/3' + # POSIX string for BST/GMT + TZ_LON = 'GMT0BST,M3.5.0,M10.5.0' + # POSIX string for UTC UTC = 'UTC' @@ -755,7 +874,8 @@ class TzLocalNixTest(unittest.TestCase, TzFoldMixin): def _gettz_context(self, tzname): tzname_map = {'Australia/Sydney': self.TZ_AEST, 'America/Toronto': self.TZ_EST, - 'America/New_York': self.TZ_EST} + 'America/New_York': self.TZ_EST, + 'Europe/London': self.TZ_LON} return TZEnvContext(tzname_map.get(tzname, tzname)) @@ -830,7 +950,76 @@ class TzLocalNixTest(unittest.TestCase, TzFoldMixin): self.assertIs(dt_time(13, 20, tzinfo=tz.tzlocal()).dst(), None) - + def testUTCEquality(self): + with TZEnvContext(self.UTC): + assert tz.tzlocal() == tz.UTC + + +# TODO: Maybe a better hack than this? +def mark_tzlocal_nix(f): + marks = [ + pytest.mark.tzlocal, + pytest.mark.skipif(IS_WIN, reason='requires Unix'), + ] + + for mark in reversed(marks): + f = mark(f) + + return f + + +@mark_tzlocal_nix [email protected]('tzvar', ['UTC', 'GMT0', 'UTC0']) +def test_tzlocal_utc_equal(tzvar): + with TZEnvContext(tzvar): + assert tz.tzlocal() == tz.UTC + + +@mark_tzlocal_nix [email protected]('tzvar', [ + 'Europe/London', 'America/New_York', + 'GMT0BST', 'EST5EDT']) +def test_tzlocal_utc_unequal(tzvar): + with TZEnvContext(tzvar): + assert tz.tzlocal() != tz.UTC + + +@mark_tzlocal_nix +def test_tzlocal_local_time_trim_colon(): + with TZEnvContext(':/etc/localtime'): + assert tz.gettz() is not None + + +@mark_tzlocal_nix [email protected]('tzvar, tzoff', [ + ('EST5', tz.tzoffset('EST', -18000)), + ('GMT0', tz.tzoffset('GMT', 0)), + ('YAKT-9', tz.tzoffset('YAKT', timedelta(hours=9))), + ('JST-9', tz.tzoffset('JST', timedelta(hours=9))), +]) +def test_tzlocal_offset_equal(tzvar, tzoff): + with TZEnvContext(tzvar): + # Including both to test both __eq__ and __ne__ + assert tz.tzlocal() == tzoff + assert not (tz.tzlocal() != tzoff) + + +@mark_tzlocal_nix [email protected]('tzvar, tzoff', [ + ('EST5EDT', tz.tzoffset('EST', -18000)), + ('GMT0BST', tz.tzoffset('GMT', 0)), + ('EST5', tz.tzoffset('EST', -14400)), + ('YAKT-9', tz.tzoffset('JST', timedelta(hours=9))), + ('JST-9', tz.tzoffset('YAKT', timedelta(hours=9))), +]) +def test_tzlocal_offset_unequal(tzvar, tzoff): + with TZEnvContext(tzvar): + # Including both to test both __eq__ and __ne__ + assert tz.tzlocal() != tzoff + assert not (tz.tzlocal() == tzoff) + + class GettzTest(unittest.TestCase, TzFoldMixin): gettz = staticmethod(tz.gettz) @@ -877,8 +1066,134 @@ class GettzTest(unittest.TestCase, TzFoldMixin): self.assertEqual(t_west.utcoffset(), timedelta(hours=1)) self.assertEqual(t_west.dst(), timedelta(hours=1)) - -class ZoneInfoGettzTest(GettzTest, WarningTestMixin): + def testGettzCacheTzFile(self): + NYC1 = tz.gettz('America/New_York') + NYC2 = tz.gettz('America/New_York') + + assert NYC1 is NYC2 + + def testGettzCacheTzLocal(self): + local1 = tz.gettz() + local2 = tz.gettz() + + assert local1 is not local2 + + +def test_gettz_same_result_for_none_and_empty_string(): + local_from_none = tz.gettz() + local_from_empty_string = tz.gettz("") + assert local_from_none is not None + assert local_from_empty_string is not None + assert local_from_none == local_from_empty_string + + [email protected]('badzone', [ + 'Fake.Region/Abcdefghijklmnop', # Violates several tz project name rules +]) +def test_gettz_badzone(badzone): + # Make sure passing a bad TZ string to gettz returns None (GH #800) + tzi = tz.gettz(badzone) + assert tzi is None + + +def test_gettz_badzone_unicode(): + # Make sure a unicode string can be passed to TZ (GH #802) + # When fixed, combine this with test_gettz_badzone + tzi = tz.gettz('🐼') + assert tzi is None + + + "badzone,exc_reason", + [ + pytest.param( + b"America/New_York", + ".*should be str, not bytes.*", + id="bytes on Python 3", + marks=[ + pytest.mark.skipif( + PY2, reason="bytes arguments accepted in Python 2" + ) + ], + ), + pytest.param( + object(), + None, + id="no startswith()", + marks=[ + pytest.mark.xfail(reason="AttributeError instead of TypeError", + raises=AttributeError), + ], + ), + ], +) +def test_gettz_zone_wrong_type(badzone, exc_reason): + with pytest.raises(TypeError, match=exc_reason): + tz.gettz(badzone) + + [email protected](IS_WIN, reason='zoneinfo separately cached') +def test_gettz_cache_clear(): + NYC1 = tz.gettz('America/New_York') + tz.gettz.cache_clear() + + NYC2 = tz.gettz('America/New_York') + + assert NYC1 is not NYC2 + [email protected](IS_WIN, reason='zoneinfo separately cached') +def test_gettz_set_cache_size(): + tz.gettz.cache_clear() + tz.gettz.set_cache_size(3) + + MONACO_ref = weakref.ref(tz.gettz('Europe/Monaco')) + EASTER_ref = weakref.ref(tz.gettz('Pacific/Easter')) + CURRIE_ref = weakref.ref(tz.gettz('Australia/Currie')) + + gc.collect() + + assert MONACO_ref() is not None + assert EASTER_ref() is not None + assert CURRIE_ref() is not None + + tz.gettz.set_cache_size(2) + gc.collect() + + assert MONACO_ref() is None + [email protected](IS_WIN, reason="Windows does not use system zoneinfo") +def test_gettz_weakref(): + tz.gettz.cache_clear() + tz.gettz.set_cache_size(2) + NYC1 = tz.gettz('America/New_York') + NYC_ref = weakref.ref(tz.gettz('America/New_York')) + + assert NYC1 is NYC_ref() + + del NYC1 + gc.collect() + + assert NYC_ref() is not None # Should still be in the strong cache + assert tz.gettz('America/New_York') is NYC_ref() + + # Populate strong cache with other timezones + tz.gettz('Europe/Monaco') + tz.gettz('Pacific/Easter') + tz.gettz('Australia/Currie') + + gc.collect() + assert NYC_ref() is None # Should have been pushed out + assert tz.gettz('America/New_York') is not NYC_ref() + +class ZoneInfoGettzTest(GettzTest): def gettz(self, name): zoneinfo_file = zoneinfo.get_zonefile_instance() return zoneinfo_file.get(name) @@ -938,12 +1253,12 @@ class ZoneInfoGettzTest(GettzTest, WarningTestMixin): self.assertIs(zif_1, zif_2) def testZoneInfoDeprecated(self): - with self.assertWarns(DeprecationWarning): - tzi = zoneinfo.gettz('US/Eastern') + with pytest.warns(DeprecationWarning): + zoneinfo.gettz('US/Eastern') def testZoneInfoMetadataDeprecated(self): - with self.assertWarns(DeprecationWarning): - tzdb_md = zoneinfo.gettz_db_metadata() + with pytest.warns(DeprecationWarning): + zoneinfo.gettz_db_metadata() class TZRangeTest(unittest.TestCase, TzFoldMixin): @@ -960,13 +1275,21 @@ class TZRangeTest(unittest.TestCase, TzFoldMixin): weekday=SU(+1)), end=relativedelta(month=4, day=1, hour=2, weekday=SU(+1))) + + TZ_LON = tz.tzrange('GMT', timedelta(hours=0), + 'BST', timedelta(hours=1), + start=relativedelta(month=3, day=31, weekday=SU(-1), + hours=2), + end=relativedelta(month=10, day=31, weekday=SU(-1), + hours=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} + 'America/New_York': self.TZ_EST, + 'Europe/London': self.TZ_LON} return tzname_map[tzname] @@ -1027,7 +1350,7 @@ class TZRangeTest(unittest.TestCase, TzFoldMixin): 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()) + dt = datetime(2007, 8, 6, 4, 10, tzinfo=tz.UTC) self.assertEqual(dt.astimezone(tz=tz.gettz("GMT+2")), datetime(2007, 8, 6, 6, 10, tzinfo=tz.tzstr("GMT+2"))) @@ -1080,6 +1403,7 @@ class TZRangeTest(unittest.TestCase, TzFoldMixin): 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) @@ -1088,106 +1412,18 @@ class TZStrTest(unittest.TestCase, TzFoldMixin): # POSIX string for AEST/AEDT (valid >= 2008) TZ_AEST = 'AEST-10AEDT,M10.1.0/2,M4.1.0/3' + # POSIX string for GMT/BST + TZ_LON = 'GMT0BST,M3.5.0,M10.5.0' + 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} + 'America/New_York': self.TZ_EST, + 'Europe/London': self.TZ_LON} 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. @@ -1196,15 +1432,6 @@ class TZStrTest(unittest.TestCase, TzFoldMixin): 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') @@ -1262,10 +1489,217 @@ class TZStrTest(unittest.TestCase, TzFoldMixin): with self.assertRaises(ValueError): tz.tzstr('InvalidString;439999') + def testTzStrSingleton(self): + tz1 = tz.tzstr('EST5EDT') + tz2 = tz.tzstr('CST4CST') + tz3 = tz.tzstr('EST5EDT') + + self.assertIsNot(tz1, tz2) + self.assertIs(tz1, tz3) + + def testTzStrSingletonPosix(self): + tz_t1 = tz.tzstr('GMT+3', posix_offset=True) + tz_f1 = tz.tzstr('GMT+3', posix_offset=False) + + tz_t2 = tz.tzstr('GMT+3', posix_offset=True) + tz_f2 = tz.tzstr('GMT+3', posix_offset=False) + + self.assertIs(tz_t1, tz_t2) + self.assertIsNot(tz_t1, tz_f1) + + self.assertIs(tz_f1, tz_f2) + + def testTzStrInstance(self): + tz1 = tz.tzstr('EST5EDT') + tz2 = tz.tzstr.instance('EST5EDT') + tz3 = tz.tzstr.instance('EST5EDT') + + assert tz1 is not tz2 + assert tz2 is not tz3 + + # Ensure that these still are all the same zone + assert tz1 == tz2 == tz3 + + +def test_tzstr_weakref(): + tz_t1 = tz.tzstr('EST5EDT') + tz_t2_ref = weakref.ref(tz.tzstr('EST5EDT')) + assert tz_t1 is tz_t2_ref() + + del tz_t1 + gc.collect() + + assert tz_t2_ref() is not None + assert tz.tzstr('EST5EDT') is tz_t2_ref() + + for offset in range(5,15): + tz.tzstr('GMT+{}'.format(offset)) + gc.collect() + + assert tz_t2_ref() is None + assert tz.tzstr('EST5EDT') is not tz_t2_ref() + + [email protected]('tz_str,expected', [ + # From https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html + ('', tz.tzrange(None)), # TODO: Should change this so tz.tzrange('') works + ('EST+5EDT,M3.2.0/2,M11.1.0/12', + tz.tzrange('EST', -18000, 'EDT', -14400, + start=relativedelta(month=3, day=1, weekday=SU(2), hours=2), + end=relativedelta(month=11, day=1, weekday=SU(1), hours=11))), + ('WART4WARST,J1/0,J365/25', # This is DST all year, Western Argentina Summer Time + tz.tzrange('WART', timedelta(hours=-4), 'WARST', + start=relativedelta(month=1, day=1, hours=0), + end=relativedelta(month=12, day=31, days=1))), + ('IST-2IDT,M3.4.4/26,M10.5.0', # Israel Standard / Daylight Time + tz.tzrange('IST', timedelta(hours=2), 'IDT', + start=relativedelta(month=3, day=1, weekday=TH(4), days=1, hours=2), + end=relativedelta(month=10, day=31, weekday=SU(-1), hours=1))), + ('WGT3WGST,M3.5.0/2,M10.5.0/1', + tz.tzrange('WGT', timedelta(hours=-3), 'WGST', + start=relativedelta(month=3, day=31, weekday=SU(-1), hours=2), + end=relativedelta(month=10, day=31, weekday=SU(-1), hours=0))), + + # Different offset specifications + ('WGT0300WGST', + tz.tzrange('WGT', timedelta(hours=-3), 'WGST')), + ('WGT03:00WGST', + tz.tzrange('WGT', timedelta(hours=-3), 'WGST')), + ('AEST-1100AEDT', + tz.tzrange('AEST', timedelta(hours=11), 'AEDT')), + ('AEST-11:00AEDT', + tz.tzrange('AEST', timedelta(hours=11), 'AEDT')), + + # Different time formats + ('EST5EDT,M3.2.0/4:00,M11.1.0/3:00', + tz.tzrange('EST', timedelta(hours=-5), 'EDT', + start=relativedelta(month=3, day=1, weekday=SU(2), hours=4), + end=relativedelta(month=11, day=1, weekday=SU(1), hours=2))), + ('EST5EDT,M3.2.0/04:00,M11.1.0/03:00', + tz.tzrange('EST', timedelta(hours=-5), 'EDT', + start=relativedelta(month=3, day=1, weekday=SU(2), hours=4), + end=relativedelta(month=11, day=1, weekday=SU(1), hours=2))), + ('EST5EDT,M3.2.0/0400,M11.1.0/0300', + tz.tzrange('EST', timedelta(hours=-5), 'EDT', + start=relativedelta(month=3, day=1, weekday=SU(2), hours=4), + end=relativedelta(month=11, day=1, weekday=SU(1), hours=2))), +]) +def test_valid_GNU_tzstr(tz_str, expected): + tzi = tz.tzstr(tz_str) + + assert tzi == expected + + [email protected]('tz_str, expected', [ + ('EST5EDT,5,4,0,7200,11,3,0,7200', + tz.tzrange('EST', timedelta(hours=-5), 'EDT', + start=relativedelta(month=5, day=1, weekday=SU(+4), hours=+2), + end=relativedelta(month=11, day=1, weekday=SU(+3), hours=+1))), + ('EST5EDT,5,-4,0,7200,11,3,0,7200', + tz.tzrange('EST', timedelta(hours=-5), 'EDT', + start=relativedelta(hours=+2, month=5, day=31, weekday=SU(-4)), + end=relativedelta(hours=+1, month=11, day=1, weekday=SU(+3)))), + ('EST5EDT,5,4,0,7200,11,-3,0,7200', + tz.tzrange('EST', timedelta(hours=-5), 'EDT', + start=relativedelta(hours=+2, month=5, day=1, weekday=SU(+4)), + end=relativedelta(hours=+1, month=11, day=31, weekday=SU(-3)))), + ('EST5EDT,5,4,0,7200,11,-3,0,7200,3600', + tz.tzrange('EST', timedelta(hours=-5), 'EDT', + start=relativedelta(hours=+2, month=5, day=1, weekday=SU(+4)), + end=relativedelta(hours=+1, month=11, day=31, weekday=SU(-3)))), + ('EST5EDT,5,4,0,7200,11,-3,0,7200,3600', + tz.tzrange('EST', timedelta(hours=-5), 'EDT', + start=relativedelta(hours=+2, month=5, day=1, weekday=SU(+4)), + end=relativedelta(hours=+1, month=11, day=31, weekday=SU(-3)))), + ('EST5EDT,5,4,0,7200,11,-3,0,7200,-3600', + tz.tzrange('EST', timedelta(hours=-5), 'EDT', timedelta(hours=-6), + start=relativedelta(hours=+2, month=5, day=1, weekday=SU(+4)), + end=relativedelta(hours=+3, month=11, day=31, weekday=SU(-3)))), + ('EST5EDT,5,4,0,7200,11,-3,0,7200,+7200', + tz.tzrange('EST', timedelta(hours=-5), 'EDT', timedelta(hours=-3), + start=relativedelta(hours=+2, month=5, day=1, weekday=SU(+4)), + end=relativedelta(hours=0, month=11, day=31, weekday=SU(-3)))), + ('EST5EDT,5,4,0,7200,11,-3,0,7200,+3600', + tz.tzrange('EST', timedelta(hours=-5), 'EDT', + start=relativedelta(hours=+2, month=5, day=1, weekday=SU(+4)), + end=relativedelta(hours=+1, month=11, day=31, weekday=SU(-3)))), +]) +def test_valid_dateutil_format(tz_str, expected): + # This tests the dateutil-specific format that is used widely in the tests + # and examples. It is unclear where this format originated from. + with pytest.warns(tz.DeprecatedTzFormatWarning): + tzi = tz.tzstr.instance(tz_str) + + assert tzi == expected + + [email protected]('tz_str', [ + 'hdfiughdfuig,dfughdfuigpu87ñ::', + ',dfughdfuigpu87ñ::', + '-1:WART4WARST,J1,J365/25', + 'WART4WARST,J1,J365/-25', + 'IST-2IDT,M3.4.-1/26,M10.5.0', + 'IST-2IDT,M3,2000,1/26,M10,5,0' +]) +def test_invalid_GNU_tzstr(tz_str): + with pytest.raises(ValueError): + tz.tzstr(tz_str) + + +# Different representations of the same default rule set +DEFAULT_TZSTR_RULES_EQUIV_2003 = [ + 'EST5EDT', + 'EST5EDT4,M4.1.0/02:00:00,M10-5-0/02:00', + 'EST5EDT4,95/02:00:00,298/02:00', + 'EST5EDT4,J96/02:00:00,J299/02:00', + 'EST5EDT4,J96/02:00:00,J299/02' +] + + [email protected]('tz_str', DEFAULT_TZSTR_RULES_EQUIV_2003) +def test_tzstr_default_start(tz_str): + tzi = tz.tzstr(tz_str) + dt_std = datetime(2003, 4, 6, 1, 59, tzinfo=tzi) + dt_dst = datetime(2003, 4, 6, 2, 00, tzinfo=tzi) + + assert get_timezone_tuple(dt_std) == EST_TUPLE + assert get_timezone_tuple(dt_dst) == EDT_TUPLE + + [email protected]('tz_str', DEFAULT_TZSTR_RULES_EQUIV_2003) +def test_tzstr_default_end(tz_str): + tzi = tz.tzstr(tz_str) + dt_dst = datetime(2003, 10, 26, 0, 59, tzinfo=tzi) + dt_dst_ambig = datetime(2003, 10, 26, 1, 00, tzinfo=tzi) + dt_std_ambig = tz.enfold(dt_dst_ambig, fold=1) + dt_std = datetime(2003, 10, 26, 2, 00, tzinfo=tzi) + + assert get_timezone_tuple(dt_dst) == EDT_TUPLE + assert get_timezone_tuple(dt_dst_ambig) == EDT_TUPLE + assert get_timezone_tuple(dt_std_ambig) == EST_TUPLE + assert get_timezone_tuple(dt_std) == EST_TUPLE + + [email protected]('tzstr_1', ['EST5EDT', + 'EST5EDT4,M4.1.0/02:00:00,M10-5-0/02:00']) [email protected]('tzstr_2', ['EST5EDT', + 'EST5EDT4,M4.1.0/02:00:00,M10-5-0/02:00']) +def test_tzstr_default_cmp(tzstr_1, tzstr_2): + tz1 = tz.tzstr(tzstr_1) + tz2 = tz.tzstr(tzstr_2) + + assert tz1 == tz2 class TZICalTest(unittest.TestCase, TzFoldMixin): - - def gettz(self, tzname): + def _gettz_str_tuple(self, tzname): TZ_EST = ( 'BEGIN:VTIMEZONE', 'TZID:US-Eastern', @@ -1284,7 +1718,27 @@ class TZICalTest(unittest.TestCase, TzFoldMixin): 'TZNAME:EDT', 'END:DAYLIGHT', 'END:VTIMEZONE' - ) + ) + + TZ_PST = ( + 'BEGIN:VTIMEZONE', + 'TZID:US-Pacific', + 'BEGIN:STANDARD', + 'DTSTART:19971029T020000', + 'RRULE:FREQ=YEARLY;BYDAY=+1SU;BYMONTH=11', + 'TZOFFSETFROM:-0700', + 'TZOFFSETTO:-0800', + 'TZNAME:PST', + 'END:STANDARD', + 'BEGIN:DAYLIGHT', + 'DTSTART:19980301T020000', + 'RRULE:FREQ=YEARLY;BYDAY=+2SU;BYMONTH=03', + 'TZOFFSETFROM:-0800', + 'TZOFFSETTO:-0700', + 'TZNAME:PDT', + 'END:DAYLIGHT', + 'END:VTIMEZONE' + ) TZ_AEST = ( 'BEGIN:VTIMEZONE', @@ -1304,13 +1758,57 @@ class TZICalTest(unittest.TestCase, TzFoldMixin): 'TZNAME:AEDT', 'END:DAYLIGHT', 'END:VTIMEZONE' - ) + ) + + TZ_LON = ( + 'BEGIN:VTIMEZONE', + 'TZID:Europe-London', + 'BEGIN:STANDARD', + 'DTSTART:19810301T030000', + 'RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10;BYHOUR=02', + 'TZOFFSETFROM:+0100', + 'TZOFFSETTO:+0000', + 'TZNAME:GMT', + 'END:STANDARD', + 'BEGIN:DAYLIGHT', + 'DTSTART:19961001T030000', + 'RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=03;BYHOUR=01', + 'TZOFFSETFROM:+0000', + 'TZOFFSETTO:+0100', + 'TZNAME:BST', + 'END:DAYLIGHT', + 'END:VTIMEZONE' + ) tzname_map = {'Australia/Sydney': TZ_AEST, 'America/Toronto': TZ_EST, - 'America/New_York': TZ_EST} + 'America/New_York': TZ_EST, + 'America/Los_Angeles': TZ_PST, + 'Europe/London': TZ_LON} + + return tzname_map[tzname] + + def _gettz_str(self, tzname): + return '\n'.join(self._gettz_str_tuple(tzname)) + + def _tzstr_dtstart_with_params(self, tzname, param_str): + # Adds parameters to the DTSTART values of a given tzstr + tz_str_tuple = self._gettz_str_tuple(tzname) - tzc = tz.tzical(StringIO('\n'.join(tzname_map[tzname]))).get() + out_tz = [] + for line in tz_str_tuple: + if line.startswith('DTSTART'): + name, value = line.split(':', 1) + line = name + ';' + param_str + ':' + value + + out_tz.append(line) + + return '\n'.join(out_tz) + + def gettz(self, tzname): + tz_str = self._gettz_str(tzname) + + tzc = tz.tzical(StringIO(tz_str)).get() return tzc @@ -1321,16 +1819,15 @@ class TZICalTest(unittest.TestCase, TzFoldMixin): 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) + dt1 = datetime(2003, 3, 9, 1, 59) + dt2 = datetime(2003, 3, 9, 2, 00) fold = [0, 0] else: - dt1 = datetime(2003, 10, 26, 0, 59) - dt2 = datetime(2003, 10, 26, 1, 00) + dt1 = datetime(2003, 11, 2, 0, 59) + dt2 = datetime(2003, 11, 2, 1, 00) fold = [0, 1] dts = (tz.enfold(dt.replace(tzinfo=tzc), fold=f) @@ -1340,17 +1837,20 @@ class TZICalTest(unittest.TestCase, TzFoldMixin): self.assertEqual(func(dt), value) def _test_multi_zones(self, tzstrs, tzids, func, values, start): - tzic = tz.tzical(StringIO(''.join(tzstrs))) + tzic = tz.tzical(StringIO('\n'.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() + tz_str = self._gettz_str('America/New_York') + return tz.tzical(StringIO(tz_str)).get() + + def _testEST(self, start, test_type, tzc=None): + if tzc is None: + tzc = self._prepare_EST() - def _testEST(self, start, test_type): - tzc = self._prepare_EST() argdict = { 'name': (datetime.tzname, ('EST', 'EDT')), 'offset': (datetime.utcoffset, (timedelta(hours=-5), @@ -1384,8 +1884,22 @@ class TZICalTest(unittest.TestCase, TzFoldMixin): def testESTEndDST(self): self._testEST(start=False, test_type='dst') + def testESTValueDatetime(self): + # Violating one-test-per-test rule because we're not set up to do + # parameterized tests and the manual proliferation is getting a bit + # out of hand. + tz_str = self._tzstr_dtstart_with_params('America/New_York', + 'VALUE=DATE-TIME') + + tzc = tz.tzical(StringIO(tz_str)).get() + + for start in (True, False): + for test_type in ('name', 'offset', 'dst'): + self._testEST(start=start, test_type=test_type, tzc=tzc) + def _testMultizone(self, start, test_type): - tzstrs = (TZICAL_EST5EDT, TZICAL_PST8PDT) + tzstrs = (self._gettz_str('America/New_York'), + self._gettz_str('America/Los_Angeles')) tzids = ('US-Eastern', 'US-Pacific') argdict = { @@ -1427,7 +1941,9 @@ class TZICalTest(unittest.TestCase, TzFoldMixin): self._testMultizone(start=False, test_type='dst') def testMultiZoneKeys(self): - tzic = tz.tzical(StringIO(''.join((TZICAL_EST5EDT, TZICAL_PST8PDT)))) + est_str = self._gettz_str('America/New_York') + pst_str = self._gettz_str('America/Los_Angeles') + tzic = tz.tzical(StringIO('\n'.join((est_str, pst_str)))) # Sort keys because they are in a random order, being dictionary keys keys = sorted(tzic.keys()) @@ -1445,6 +1961,24 @@ class TZICalTest(unittest.TestCase, TzFoldMixin): with self.assertRaises(ValueError): tzic.get() + def testDtstartDate(self): + tz_str = self._tzstr_dtstart_with_params('America/New_York', + 'VALUE=DATE') + with self.assertRaises(ValueError): + tz.tzical(StringIO(tz_str)) + + def testDtstartTzid(self): + tz_str = self._tzstr_dtstart_with_params('America/New_York', + 'TZID=UTC') + with self.assertRaises(ValueError): + tz.tzical(StringIO(tz_str)) + + def testDtstartBadParam(self): + tz_str = self._tzstr_dtstart_with_params('America/New_York', + 'FOO=BAR') + with self.assertRaises(ValueError): + tz.tzical(StringIO(tz_str)) + # Test Parsing def testGap(self): tzic = tz.tzical(StringIO('\n'.join((TZICAL_EST5EDT, TZICAL_PST8PDT)))) @@ -1484,11 +2018,13 @@ class TZTest(unittest.TestCase): 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 testFilestreamWithNameRepr(self): + # If fileobj is a filestream with a "name" attribute this name should + # be reflected in the tz object's repr + fileobj = BytesIO(base64.b64decode(TZFILE_EST5EDT)) + fileobj.name = 'foo' + tzc = tz.tzfile(fileobj) + self.assertEqual(repr(tzc), 'tzfile(' + repr('foo') + ')') def testLeapCountDecodesProperly(self): # This timezone has leapcnt, and failed to decode until @@ -1501,7 +2037,7 @@ class TZTest(unittest.TestCase): # 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) + 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() @@ -1532,15 +2068,13 @@ class TZTest(unittest.TestCase): 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()) + dt = datetime(2007, 8, 6, 4, 10, tzinfo=tz.UTC) 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 @@ -1550,6 +2084,40 @@ class TZTest(unittest.TestCase): self.assertEqual(str(dt), '2014-07-20 12:34:56+00:00') [email protected](not SUPPORTS_SUB_MINUTE_OFFSETS, + reason='Sub-minute offsets not supported') +def test_tzfile_sub_minute_offset(): + # If user running python 3.6 or newer, exact offset is used + tzc = tz.tzfile(BytesIO(base64.b64decode(EUROPE_HELSINKI))) + offset = timedelta(hours=1, minutes=39, seconds=52) + assert datetime(1900, 1, 1, 0, 0, tzinfo=tzc).utcoffset() == offset + + [email protected](SUPPORTS_SUB_MINUTE_OFFSETS, + reason='Sub-minute offsets supported.') +def test_sub_minute_rounding_tzfile(): + # This timezone has an offset of 5992 seconds in 1900-01-01. + # For python version pre-3.6, this will be rounded + tzc = tz.tzfile(BytesIO(base64.b64decode(EUROPE_HELSINKI))) + offset = timedelta(hours=1, minutes=40) + assert datetime(1900, 1, 1, 0, 0, tzinfo=tzc).utcoffset() == offset + + +def test_samoa_transition(): + # utcoffset() was erroneously returning +14:00 an hour early (GH #812) + APIA = tz.gettz('Pacific/Apia') + dt = datetime(2011, 12, 29, 23, 59, tzinfo=APIA) + assert dt.utcoffset() == timedelta(hours=-10) + + # Make sure the transition actually works, too + dt_after = (dt.astimezone(tz.UTC) + timedelta(minutes=1)).astimezone(APIA) + assert dt_after == datetime(2011, 12, 31, tzinfo=APIA) + assert dt_after.utcoffset() == timedelta(hours=14) + + @unittest.skipUnless(IS_WIN, "Requires Windows") class TzWinTest(unittest.TestCase, TzWinFoldMixin): def setUp(self): @@ -1639,7 +2207,7 @@ class TzWinTest(unittest.TestCase, TzWinFoldMixin): def testTzWinEqualityInvalid(self): # Compare to objects that do not implement comparison with this # (should default to False) - UTC = tz.tzutc() + UTC = tz.UTC EST = tz.tzwin('Eastern Standard Time') self.assertFalse(EST == UTC) @@ -1689,8 +2257,6 @@ class TzWinTest(unittest.TestCase, TzWinFoldMixin): @unittest.skipUnless(IS_WIN, "Requires Windows") [email protected](TZWinContext.tz_change_allowed(), - TZWinContext.tz_change_disallowed_message()) class TzWinLocalTest(unittest.TestCase, TzWinFoldMixin): def setUp(self): @@ -1698,12 +2264,12 @@ class TzWinLocalTest(unittest.TestCase, TzWinFoldMixin): self.context = TZWinContext def get_args(self, tzname): - return tuple() + return () 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 + # See GitHub Issue #135: https://github.com/dateutil/dateutil/issues/135 datetime.now(tzwin.tzwinlocal()) def testTzwinLocalUTCOffset(self): @@ -1816,17 +2382,18 @@ class TzPickleTest(PicklableMixin, unittest.TestCase): asfile=self._asfile) def testPickleTzUTC(self): - self.assertPicklable(tz.tzutc()) + self.assertPicklable(tz.tzutc(), singleton=True) def testPickleTzOffsetZero(self): - self.assertPicklable(tz.tzoffset('UTC', 0)) + self.assertPicklable(tz.tzoffset('UTC', 0), singleton=True) def testPickleTzOffsetPos(self): - self.assertPicklable(tz.tzoffset('UTC+1', 3600)) + self.assertPicklable(tz.tzoffset('UTC+1', 3600), singleton=True) def testPickleTzOffsetNeg(self): - self.assertPicklable(tz.tzoffset('UTC-1', -3600)) + self.assertPicklable(tz.tzoffset('UTC-1', -3600), singleton=True) + @pytest.mark.tzlocal def testPickleTzLocal(self): self.assertPicklable(tz.tzlocal()) @@ -2096,19 +2663,149 @@ class DatetimeExistsTest(unittest.TestCase): self.assertFalse(tz.datetime_exists(dt, tz=AEST)) -class EnfoldTest(unittest.TestCase): - def testEnterFoldDefault(self): +class TestEnfold: + def test_enter_fold_default(self): dt = tz.enfold(datetime(2020, 1, 19, 3, 32)) - self.assertEqual(dt.fold, 1) + assert dt.fold == 1 - def testEnterFold(self): + def test_enter_fold(self): dt = tz.enfold(datetime(2020, 1, 19, 3, 32), fold=1) - self.assertEqual(dt.fold, 1) + assert dt.fold == 1 - def testExitFold(self): + def test_exit_fold(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) + assert getattr(dt, 'fold', 0) == 0 + + def test_defold(self): + dt = tz.enfold(datetime(2020, 1, 19, 3, 32), fold=1) + + dt2 = tz.enfold(dt, fold=0) + + assert getattr(dt2, 'fold', 0) == 0 + + def test_fold_replace_args(self): + # This test can be dropped when Python < 3.6 is dropped, since it + # is mainly to cover the `replace` method on _DatetimeWithFold + dt = tz.enfold(datetime(1950, 1, 2, 12, 30, 15, 8), fold=1) + + dt2 = dt.replace(1952, 2, 3, 13, 31, 16, 9) + assert dt2 == tz.enfold(datetime(1952, 2, 3, 13, 31, 16, 9), fold=1) + assert dt2.fold == 1 + + def test_fold_replace_exception_duplicate_args(self): + dt = tz.enfold(datetime(1999, 1, 3), fold=1) + + with pytest.raises(TypeError): + dt.replace(1950, year=2000) + + [email protected]_resolve_imaginary +class ImaginaryDateTest(unittest.TestCase): + def testCanberraForward(self): + tzi = tz.gettz('Australia/Canberra') + dt = datetime(2018, 10, 7, 2, 30, tzinfo=tzi) + dt_act = tz.resolve_imaginary(dt) + dt_exp = datetime(2018, 10, 7, 3, 30, tzinfo=tzi) + self.assertEqual(dt_act, dt_exp) + + def testLondonForward(self): + tzi = tz.gettz('Europe/London') + dt = datetime(2018, 3, 25, 1, 30, tzinfo=tzi) + dt_act = tz.resolve_imaginary(dt) + dt_exp = datetime(2018, 3, 25, 2, 30, tzinfo=tzi) + self.assertEqual(dt_act, dt_exp) + + def testKeivForward(self): + tzi = tz.gettz('Europe/Kiev') + dt = datetime(2018, 3, 25, 3, 30, tzinfo=tzi) + dt_act = tz.resolve_imaginary(dt) + dt_exp = datetime(2018, 3, 25, 4, 30, tzinfo=tzi) + self.assertEqual(dt_act, dt_exp) + + [email protected]_resolve_imaginary [email protected]('dt', [ + datetime(2017, 11, 5, 1, 30, tzinfo=tz.gettz('America/New_York')), + datetime(2018, 10, 28, 1, 30, tzinfo=tz.gettz('Europe/London')), + datetime(2017, 4, 2, 2, 30, tzinfo=tz.gettz('Australia/Sydney')), +]) +def test_resolve_imaginary_ambiguous(dt): + assert tz.resolve_imaginary(dt) is dt + + dt_f = tz.enfold(dt) + assert dt is not dt_f + assert tz.resolve_imaginary(dt_f) is dt_f + + [email protected]_resolve_imaginary [email protected]('dt', [ + datetime(2017, 6, 2, 12, 30, tzinfo=tz.gettz('America/New_York')), + datetime(2018, 4, 2, 9, 30, tzinfo=tz.gettz('Europe/London')), + datetime(2017, 2, 2, 16, 30, tzinfo=tz.gettz('Australia/Sydney')), + datetime(2017, 12, 2, 12, 30, tzinfo=tz.gettz('America/New_York')), + datetime(2018, 12, 2, 9, 30, tzinfo=tz.gettz('Europe/London')), + datetime(2017, 6, 2, 16, 30, tzinfo=tz.gettz('Australia/Sydney')), + datetime(2025, 9, 25, 1, 17, tzinfo=tz.UTC), + datetime(2025, 9, 25, 1, 17, tzinfo=tz.tzoffset('EST', -18000)), + datetime(2019, 3, 4, tzinfo=None) +]) +def test_resolve_imaginary_existing(dt): + assert tz.resolve_imaginary(dt) is dt + + +def __get_kiritimati_resolve_imaginary_test(): + # In the 2018d release of the IANA database, the Kiritimati "imaginary day" + # data was corrected, so if the system zoneinfo is older than 2018d, the + # Kiritimati test will fail. + + tzi = tz.gettz('Pacific/Kiritimati') + new_version = False + if not tz.datetime_exists(datetime(1995, 1, 1, 12, 30), tzi): + zif = zoneinfo.get_zonefile_instance() + if zif.metadata is not None: + new_version = zif.metadata['tzversion'] >= '2018d' + + if new_version: + tzi = zif.get('Pacific/Kiritimati') + else: + new_version = True + + if new_version: + dates = (datetime(1994, 12, 31, 12, 30), datetime(1995, 1, 1, 12, 30)) + else: + dates = (datetime(1995, 1, 1, 12, 30), datetime(1995, 1, 2, 12, 30)) + + return (tzi, ) + dates + + +resolve_imaginary_tests = [ + (tz.gettz('Europe/London'), + datetime(2018, 3, 25, 1, 30), datetime(2018, 3, 25, 2, 30)), + (tz.gettz('America/New_York'), + datetime(2017, 3, 12, 2, 30), datetime(2017, 3, 12, 3, 30)), + (tz.gettz('Australia/Sydney'), + datetime(2014, 10, 5, 2, 0), datetime(2014, 10, 5, 3, 0)), + __get_kiritimati_resolve_imaginary_test(), +] + + +if SUPPORTS_SUB_MINUTE_OFFSETS: + resolve_imaginary_tests.append( + (tz.gettz('Africa/Monrovia'), + datetime(1972, 1, 7, 0, 30), datetime(1972, 1, 7, 1, 14, 30))) + + [email protected]_resolve_imaginary [email protected]('tzi, dt, dt_exp', resolve_imaginary_tests) +def test_resolve_imaginary(tzi, dt, dt_exp): + dt = dt.replace(tzinfo=tzi) + dt_exp = dt_exp.replace(tzinfo=tzi) + + dt_r = tz.resolve_imaginary(dt) + assert dt_r == dt_exp + assert dt_r.tzname() == dt_exp.tzname() + assert dt_r.utcoffset() == dt_exp.utcoffset() diff --git a/libs/dateutil/test/test_utils.py b/libs/dateutil/test/test_utils.py new file mode 100644 index 000000000..fe1bfdcb8 --- /dev/null +++ b/libs/dateutil/test/test_utils.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +from datetime import timedelta, datetime + +from dateutil import tz +from dateutil import utils +from dateutil.tz import UTC +from dateutil.utils import within_delta + +from freezegun import freeze_time + +NYC = tz.gettz("America/New_York") + + +@freeze_time(datetime(2014, 12, 15, 1, 21, 33, 4003)) +def test_utils_today(): + assert utils.today() == datetime(2014, 12, 15, 0, 0, 0) + + +@freeze_time(datetime(2014, 12, 15, 12), tz_offset=5) +def test_utils_today_tz_info(): + assert utils.today(NYC) == datetime(2014, 12, 15, 0, 0, 0, tzinfo=NYC) + + +@freeze_time(datetime(2014, 12, 15, 23), tz_offset=5) +def test_utils_today_tz_info_different_day(): + assert utils.today(UTC) == datetime(2014, 12, 16, 0, 0, 0, tzinfo=UTC) + + +def test_utils_default_tz_info_naive(): + dt = datetime(2014, 9, 14, 9, 30) + assert utils.default_tzinfo(dt, NYC).tzinfo is NYC + + +def test_utils_default_tz_info_aware(): + dt = datetime(2014, 9, 14, 9, 30, tzinfo=UTC) + assert utils.default_tzinfo(dt, NYC).tzinfo is UTC + + +def test_utils_within_delta(): + d1 = datetime(2016, 1, 1, 12, 14, 1, 9) + d2 = d1.replace(microsecond=15) + + assert within_delta(d1, d2, timedelta(seconds=1)) + assert not within_delta(d1, d2, timedelta(microseconds=1)) + + +def test_utils_within_delta_with_negative_delta(): + d1 = datetime(2016, 1, 1) + d2 = datetime(2015, 12, 31) + + assert within_delta(d2, d1, timedelta(days=-1)) diff --git a/libs/dateutil/tz/__init__.py b/libs/dateutil/tz/__init__.py index 1cba7b9e9..af1352c47 100644 --- a/libs/dateutil/tz/__init__.py +++ b/libs/dateutil/tz/__init__.py @@ -1,4 +1,12 @@ +# -*- coding: utf-8 -*- from .tz import * +from .tz import __doc__ __all__ = ["tzutc", "tzoffset", "tzlocal", "tzfile", "tzrange", - "tzstr", "tzical", "tzwin", "tzwinlocal", "gettz"] + "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.""" diff --git a/libs/dateutil/tz/_common.py b/libs/dateutil/tz/_common.py index 212e8ce95..e6ac11831 100644 --- a/libs/dateutil/tz/_common.py +++ b/libs/dateutil/tz/_common.py @@ -1,8 +1,9 @@ -from six import PY3 -from six.moves import _thread +from six import PY2 + +from functools import wraps from datetime import datetime, timedelta, tzinfo -import copy + ZERO = timedelta(0) @@ -15,14 +16,18 @@ def tzname_in_python2(namefunc): tzname() API changed in Python 3. It used to return bytes, but was changed to unicode strings """ - def adjust_encoding(*args, **kwargs): - name = namefunc(*args, **kwargs) - if name is not None and not PY3: - name = name.encode() + if PY2: + @wraps(namefunc) + def adjust_encoding(*args, **kwargs): + name = namefunc(*args, **kwargs) + if name is not None: + name = name.encode() - return name + return name - return adjust_encoding + return adjust_encoding + else: + return namefunc # The following is adapted from Alexander Belopolsky's tz library @@ -45,7 +50,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) @@ -56,10 +61,40 @@ 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 @@ -80,7 +115,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 @@ -94,6 +129,23 @@ 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. @@ -111,7 +163,7 @@ class _tzinfo(tzinfo): :return: Returns ``True`` if ambiguous, ``False`` otherwise. - ..versionadded:: 2.6.0 + .. versionadded:: 2.6.0 """ dt = dt.replace(tzinfo=self) @@ -121,7 +173,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): @@ -160,18 +212,13 @@ class _tzinfo(tzinfo): Since this is the one time that we *know* we have an unambiguous datetime object, we take this opportunity to determine whether the datetime is ambiguous and in a "fold" state (e.g. if it's the first - occurence, chronologically, of the ambiguous datetime). + occurrence, chronologically, of the ambiguous datetime). :param dt: - A timezone-aware :class:`datetime.dateime` object. + A timezone-aware :class:`datetime.datetime` 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() " @@ -184,16 +231,17 @@ class _tzinfo(tzinfo): if dtdst is None: raise ValueError("fromutc() requires a non-None dst() result") delta = dtoff - dtdst - 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") + + 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 @@ -202,10 +250,10 @@ class _tzinfo(tzinfo): Since this is the one time that we *know* we have an unambiguous datetime object, we take this opportunity to determine whether the datetime is ambiguous and in a "fold" state (e.g. if it's the first - occurance, chronologically, of the ambiguous datetime). + occurrence, chronologically, of the ambiguous datetime). :param dt: - A timezone-aware :class:`datetime.dateime` object. + A timezone-aware :class:`datetime.datetime` object. """ dt_wall = self._fromutc(dt) @@ -236,7 +284,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') @@ -290,7 +338,6 @@ class tzrangebase(_tzinfo): utc_transitions = (dston, dstoff) dt_utc = dt.replace(tzinfo=None) - isdst = self._naive_isdst(dt_utc, utc_transitions) if isdst: @@ -360,7 +407,7 @@ class tzrangebase(_tzinfo): @property def _dst_base_offset(self): return self._dst_offset - self._std_offset - + __hash__ = None def __ne__(self, other): @@ -370,11 +417,3 @@ 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/_factories.py b/libs/dateutil/tz/_factories.py index de2e0c1de..f8a65891a 100644 --- a/libs/dateutil/tz/_factories.py +++ b/libs/dateutil/tz/_factories.py @@ -1,4 +1,8 @@ from datetime import timedelta +import weakref +from collections import OrderedDict + +from six.moves import _thread class _TzSingleton(type): @@ -11,6 +15,7 @@ class _TzSingleton(type): cls.__instance = super(_TzSingleton, cls).__call__() return cls.__instance + class _TzFactory(type): def instance(cls, *args, **kwargs): """Alternate constructor that returns a fresh instance""" @@ -19,7 +24,11 @@ class _TzFactory(type): class _TzOffsetFactory(_TzFactory): def __init__(cls, *args, **kwargs): - cls.__instances = {} + cls.__instances = weakref.WeakValueDictionary() + cls.__strong_cache = OrderedDict() + cls.__strong_cache_size = 8 + + cls._cache_lock = _thread.allocate_lock() def __call__(cls, name, offset): if isinstance(offset, timedelta): @@ -31,12 +40,25 @@ class _TzOffsetFactory(_TzFactory): if instance is None: instance = cls.__instances.setdefault(key, cls.instance(name, offset)) + + # This lock may not be necessary in Python 3. See GH issue #901 + with cls._cache_lock: + cls.__strong_cache[key] = cls.__strong_cache.pop(key, instance) + + # Remove an item if the strong cache is overpopulated + if len(cls.__strong_cache) > cls.__strong_cache_size: + cls.__strong_cache.popitem(last=False) + return instance class _TzStrFactory(_TzFactory): def __init__(cls, *args, **kwargs): - cls.__instances = {} + cls.__instances = weakref.WeakValueDictionary() + cls.__strong_cache = OrderedDict() + cls.__strong_cache_size = 8 + + cls.__cache_lock = _thread.allocate_lock() def __call__(cls, s, posix_offset=False): key = (s, posix_offset) @@ -45,5 +67,14 @@ class _TzStrFactory(_TzFactory): if instance is None: instance = cls.__instances.setdefault(key, cls.instance(s, posix_offset)) + + # This lock may not be necessary in Python 3. See GH issue #901 + with cls.__cache_lock: + cls.__strong_cache[key] = cls.__strong_cache.pop(key, instance) + + # Remove an item if the strong cache is overpopulated + if len(cls.__strong_cache) > cls.__strong_cache_size: + cls.__strong_cache.popitem(last=False) + return instance diff --git a/libs/dateutil/tz/tz.py b/libs/dateutil/tz/tz.py index 6bee29168..c67f56d46 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:`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: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 timezone. """ import datetime @@ -13,28 +13,63 @@ import time import sys import os import bisect -import copy +import weakref +from collections import OrderedDict -from operator import itemgetter - -from contextlib import contextmanager - -from six import string_types, PY3 -from ._common import tzname_in_python2, _tzinfo, _total_seconds +import six +from six import string_types +from six.moves import _thread +from ._common import tzname_in_python2, _tzinfo 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: tzwin = tzwinlocal = None +# For warning about rounding tzinfo +from warnings import warn + 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 @@ -62,6 +97,14 @@ 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 @@ -80,26 +123,33 @@ class tzutc(datetime.tzinfo): __reduce__ = object.__reduce__ +#: Convenience constant providing a :class:`tzutc()` instance +#: +#: .. versionadded:: 2.7.0 +UTC = tzutc() + + [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 = _total_seconds(offset) + offset = offset.total_seconds() except (TypeError, AttributeError): pass - self._offset = datetime.timedelta(seconds=offset) + + self._offset = datetime.timedelta(seconds=_get_supported_offset(offset)) def utcoffset(self, dt): return self._offset @@ -107,6 +157,14 @@ 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 @@ -114,8 +172,6 @@ class tzoffset(datetime.tzinfo): :param dt: A :py:class:`datetime.datetime`, naive or time zone aware. - - :return: Returns ``True`` if ambiguous, ``False`` otherwise. @@ -123,10 +179,6 @@ 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 @@ -141,7 +193,7 @@ class tzoffset(datetime.tzinfo): def __repr__(self): return "%s(%s, %s)" % (self.__class__.__name__, repr(self._name), - int(_total_seconds(self._offset))) + int(self._offset.total_seconds())) __reduce__ = object.__reduce__ @@ -161,6 +213,7 @@ 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: @@ -182,7 +235,7 @@ class tzlocal(_tzinfo): @tzname_in_python2 def tzname(self, dt): - return time.tzname[self._isdst(dt)] + return self._tznames[self._isdst(dt)] def is_ambiguous(self, dt): """ @@ -247,12 +300,20 @@ class tzlocal(_tzinfo): return dstval def __eq__(self, other): - if not isinstance(other, tzlocal): + 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: return NotImplemented - return (self._std_offset == other._std_offset and - self._dst_offset == other._dst_offset) - __hash__ = None def __ne__(self, other): @@ -314,7 +375,7 @@ class _tzfile(object): Lightweight class for holding the relevant transition and time zone information read from binary tzfiles. """ - attrs = ['trans_list', 'trans_idx', 'ttinfo_list', + attrs = ['trans_list', 'trans_list_utc', 'trans_idx', 'ttinfo_list', 'ttinfo_std', 'ttinfo_dst', 'ttinfo_before', 'ttinfo_first'] def __init__(self, **kwargs): @@ -337,11 +398,61 @@ 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 - <http://www.twinsun.com/tz/tz-link.htm>`_ 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 + <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 <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): @@ -361,7 +472,7 @@ class tzfile(_tzinfo): if fileobj is not None: if not file_opened_here: - fileobj = _ContextWrapper(fileobj) + fileobj = _nullcontext(fileobj) with fileobj as file_stream: tzobj = self._read_tzfile(file_stream) @@ -424,10 +535,10 @@ class tzfile(_tzinfo): # change. if timecnt: - out.trans_list = list(struct.unpack(">%dl" % timecnt, - fileobj.read(timecnt*4))) + out.trans_list_utc = list(struct.unpack(">%dl" % timecnt, + fileobj.read(timecnt*4))) else: - out.trans_list = [] + out.trans_list_utc = [] # Next come tzh_timecnt one-byte values of type unsigned # char; each one tells which of the different types of @@ -438,7 +549,7 @@ class tzfile(_tzinfo): if timecnt: out.trans_idx = struct.unpack(">%dB" % timecnt, - fileobj.read(timecnt)) + fileobj.read(timecnt)) else: out.trans_idx = [] @@ -469,10 +580,9 @@ class tzfile(_tzinfo): # The pairs of values are sorted in ascending order # by time. - # Not used, for now (but read anyway for correct file position) + # Not used, for now (but seek for correct file position) if leapcnt: - leap = struct.unpack(">%dl" % (leapcnt*2), - fileobj.read(leapcnt*8)) + fileobj.seek(leapcnt * 8, os.SEEK_CUR) # Then there are tzh_ttisstdcnt standard/wall # indicators, each stored as a one-byte value; @@ -502,10 +612,7 @@ class tzfile(_tzinfo): out.ttinfo_list = [] for i in range(typecnt): gmtoff, isdst, abbrind = ttinfo[i] - # Round to full-minutes if that's not the case. Python's - # datetime doesn't accept sub-minute timezones. Check - # http://python.org/sf/1447945 for some information. - gmtoff = 60 * ((gmtoff + 30) // 60) + gmtoff = _get_supported_offset(gmtoff) tti = _ttinfo() tti.offset = gmtoff tti.dstoffset = datetime.timedelta(0) @@ -527,7 +634,7 @@ class tzfile(_tzinfo): out.ttinfo_dst = None out.ttinfo_before = None if out.ttinfo_list: - if not out.trans_list: + if not out.trans_list_utc: out.ttinfo_std = out.ttinfo_first = out.ttinfo_list[0] else: for i in range(timecnt-1, -1, -1): @@ -557,43 +664,52 @@ class tzfile(_tzinfo): # isgmt are off, so it should be in wall time. OTOH, it's # always in gmt time. Let me know if you have comments # about this. - laststdoffset = None - for i, tti in enumerate(out.trans_idx): - if not tti.isdst: - offset = tti.offset - laststdoffset = offset - else: - if laststdoffset is not None: - # Store the DST offset as well and update it in the list - tti.dstoffset = tti.offset - laststdoffset - out.trans_idx[i] = tti - - offset = laststdoffset or 0 - - 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. - laststdoffset = None - for i in reversed(range(len(out.trans_idx))): - tti = out.trans_idx[i] - if tti.isdst: - if not (tti.dstoffset or laststdoffset is None): - tti.dstoffset = tti.offset - laststdoffset - else: - laststdoffset = tti.offset + lastdst = None + lastoffset = None + lastdstoffset = None + lastbaseoffset = None + out.trans_list = [] - if not isinstance(tti.dstoffset, datetime.timedelta): - tti.dstoffset = datetime.timedelta(seconds=tti.dstoffset) - - out.trans_idx[i] = tti + for i, tti in enumerate(out.trans_idx): + offset = tti.offset + dstoffset = 0 + + if lastdst is not None: + if tti.isdst: + if not lastdst: + dstoffset = offset - lastoffset + + if not dstoffset and lastdstoffset: + dstoffset = lastdstoffset + + tti.dstoffset = datetime.timedelta(seconds=dstoffset) + lastdstoffset = dstoffset + + # If a time zone changes its base offset during a DST transition, + # then you need to adjust by the previous base offset to get the + # transition time in local time. Otherwise you use the current + # base offset. Ideally, I would have some mathematical proof of + # why this is true, but I haven't really thought about it enough. + baseoffset = offset - dstoffset + adjustment = baseoffset + if (lastbaseoffset is not None and baseoffset != lastbaseoffset + and tti.isdst != lastdst): + # The base DST has changed + adjustment = lastbaseoffset + + lastdst = tti.isdst + lastoffset = offset + lastbaseoffset = baseoffset + + out.trans_list.append(out.trans_list_utc[i] + adjustment) 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): + def _find_last_transition(self, dt, in_utc=False): # If there's no list, there are no transitions to find if not self._trans_list: return None @@ -602,14 +718,15 @@ 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. - idx = bisect.bisect_right(self._trans_list, timestamp) + trans_list = self._trans_list_utc if in_utc else self._trans_list + idx = bisect.bisect_right(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 @@ -623,6 +740,42 @@ 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 @@ -660,7 +813,7 @@ class tzfile(_tzinfo): if idx is None or idx == 0: return idx - # Get the current datetime as a timestamp + # If it's ambiguous and we're in a fold, shift to a different index. idx_offset = int(not _fold and self.is_ambiguous(dt, idx)) return idx - idx_offset @@ -680,7 +833,7 @@ class tzfile(_tzinfo): if not self._ttinfo_dst: return ZERO - + tti = self._find_ttinfo(dt) if not tti.isdst: @@ -753,8 +906,9 @@ 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))`` @@ -762,12 +916,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:** @@ -803,12 +957,12 @@ class tzrange(tzrangebase): self._dst_abbr = dstabbr try: - stdoffset = _total_seconds(stdoffset) + stdoffset = stdoffset.total_seconds() except (TypeError, AttributeError): pass try: - dstoffset = _total_seconds(dstoffset) + dstoffset = dstoffset.total_seconds() except (TypeError, AttributeError): pass @@ -879,6 +1033,7 @@ 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 @@ -897,25 +1052,38 @@ 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 import parser + from dateutil.parser import _parser as parser self._s = s res = parser._parsetz(s) - if res is None: + if res is None or res.any_unused_tokens: raise ValueError("unknown string format") # Here we break the compatibility with the TZ variable handling. @@ -1004,6 +1172,7 @@ 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: @@ -1012,11 +1181,12 @@ class _tzicalvtz(_tzinfo): dt = dt.replace(tzinfo=None) try: - return self._cachecomp[self._cachedate.index((dt, self._fold(dt)))] + with self._cache_lock: + return self._cachecomp[self._cachedate.index( + (dt, self._fold(dt)))] except ValueError: pass - lastcompdt = None lastcomp = None @@ -1039,12 +1209,13 @@ class _tzicalvtz(_tzinfo): else: lastcomp = comp[0] - self._cachedate.insert(0, (dt, self._fold(dt))) - self._cachecomp.insert(0, lastcomp) + with self._cache_lock: + 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 @@ -1082,13 +1253,13 @@ class _tzicalvtz(_tzinfo): class tzical(object): """ This object is designed to parse an iCalendar-style ``VTIMEZONE`` structure - as set out in `RFC 2445`_ Section 4.6.5 into one or more `tzinfo` objects. + as set out in `RFC 5545`_ 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 2445`: https://www.ietf.org/rfc/rfc2445.txt + .. _`RFC 5545`: https://tools.ietf.org/html/rfc5545 """ def __init__(self, fileobj): global rrule @@ -1098,10 +1269,9 @@ 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) + fileobj = _nullcontext(fileobj) self._vtz = {} @@ -1237,6 +1407,13 @@ 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"): @@ -1278,6 +1455,7 @@ 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", @@ -1289,78 +1467,217 @@ else: TZPATHS = [] -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): - break - else: - continue - if os.path.isfile(filepath): +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 = weakref.WeakValueDictionary() + self.__strong_cache_size = 8 + self.__strong_cache = OrderedDict() + 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) + or rv is None): + # tzlocal is slightly more complicated than the other + # time zone providers because it depends on environment + # at construction time, so don't cache that. + # + # We also cannot store weak references to None, so we + # will also not store that. + self.__instances[name] = rv + else: + # No need for strong caching, return immediately + return rv + + self.__strong_cache[name] = self.__strong_cache.pop(name, rv) + + if len(self.__strong_cache) > self.__strong_cache_size: + self.__strong_cache.popitem(last=False) + + return rv + + def set_cache_size(self, size): + with self._cache_lock: + self.__strong_cache_size = size + while len(self.__strong_cache) > size: + self.__strong_cache.popitem(last=False) + + def cache_clear(self): + with self._cache_lock: + self.__instances = weakref.WeakValueDictionary() + self.__strong_cache.clear() + + @staticmethod + def nocache(name=None): + """A non-cached version of gettz""" + tz = None + if not name: try: - tz = tzfile(filepath) - break - except (IOError, OSError, ValueError): + name = os.environ["TZ"] + except KeyError: pass - else: - tz = tzlocal() - else: - if name.startswith(":"): - name = name[:-1] - if os.path.isabs(name): - if os.path.isfile(name): - tz = tzfile(name) + if name is None or name in ("", ":"): + 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 + if os.path.isfile(filepath): + try: + tz = tzfile(filepath) + break + except (IOError, OSError, ValueError): + pass + else: + tz = tzlocal() 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: + if name.startswith(":"): + name = name[1:] + except TypeError as e: + if isinstance(name, bytes): + new_msg = "gettz argument should be str, not bytes" + six.raise_from(TypeError(new_msg), e) + else: + raise + if os.path.isabs(name): + if os.path.isfile(name): + tz = tzfile(name) + else: 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 must have at least one offset to be a tzstr - if c in "0123456789": - try: - tz = tzstr(name) - except ValueError: - pass + 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: - if name in ("GMT", "UTC"): - tz = tzutc() - elif name in time.tzname: - tz = tzlocal() - return tz + tz = None + if tzwin is not None: + try: + tz = tzwin(name) + except (WindowsError, UnicodeEncodeError): + # UnicodeEncodeError is for Python 2.7 compat + 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 = UTC + elif name in time.tzname: + tz = tzlocal() + return tz + + return GettzFunc() + + +gettz = __get_gettz() +del __get_gettz def datetime_exists(dt, tz=None): @@ -1375,9 +1692,12 @@ 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``. + Returns a boolean value whether or not the "wall time" exists in + ``tz``. + + .. versionadded:: 2.7.0 """ if tz is None: if dt.tzinfo is None: @@ -1388,7 +1708,7 @@ def datetime_exists(dt, tz=None): # This is essentially a test of whether or not the datetime can survive # a round trip to UTC. - dt_rt = dt.replace(tzinfo=tz).astimezone(tzutc()).astimezone(tz) + dt_rt = dt.replace(tzinfo=tz).astimezone(UTC).astimezone(tz) dt_rt = dt_rt.replace(tzinfo=None) return dt == dt_rt @@ -1407,7 +1727,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``. @@ -1425,7 +1745,7 @@ def datetime_ambiguous(dt, tz=None): if is_ambiguous_fn is not None: try: return tz.is_ambiguous(dt) - except: + except Exception: pass # If it doesn't come out and tell us it's ambiguous, we'll just check if @@ -1440,25 +1760,90 @@ def datetime_ambiguous(dt, tz=None): return not (same_offset and same_dst) -def _datetime_to_timestamp(dt): +def resolve_imaginary(dt): """ - Convert a :class:`datetime.datetime` object to an epoch timestamp in seconds - since January 1, 1970, ignoring the time zone. + 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 """ - return _total_seconds((dt.replace(tzinfo=None) - EPOCH)) + if dt.tzinfo is not None and not datetime_exists(dt): -class _ContextWrapper(object): + 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): """ - Class for wrapping contexts so that they are passed through in a - with statement. + Convert a :class:`datetime.datetime` object to an epoch timestamp in + seconds since January 1, 1970, ignoring the time zone. """ - def __init__(self, context): - self.context = context + return (dt.replace(tzinfo=None) - EPOCH).total_seconds() - def __enter__(self): - return self.context - def __exit__(*args, **kwargs): - pass +if sys.version_info >= (3, 6): + def _get_supported_offset(second_offset): + return second_offset +else: + def _get_supported_offset(second_offset): + # For python pre-3.6, round to full-minutes if that's not the case. + # Python's datetime doesn't accept sub-minute timezones. Check + # http://python.org/sf/1447945 or https://bugs.python.org/issue5288 + # for some information. + old_offset = second_offset + calculated_offset = 60 * ((second_offset + 30) // 60) + return calculated_offset + + +try: + # Python 3.7 feature + from contextlib import nullcontext as _nullcontext +except ImportError: + class _nullcontext(object): + """ + Class for wrapping contexts so that they are passed through in a + with statement. + """ + def __init__(self, context): + self.context = context + + def __enter__(self): + return self.context + + def __exit__(*args, **kwargs): + pass # vim:ts=4:sw=4:et diff --git a/libs/dateutil/tz/win.py b/libs/dateutil/tz/win.py index 9f4e5519f..cde07ba79 100644 --- a/libs/dateutil/tz/win.py +++ b/libs/dateutil/tz/win.py @@ -1,3 +1,11 @@ +# -*- coding: utf-8 -*- +""" +This module provides an interface to the native time zone data on Windows, +including :py:class:`datetime.tzinfo` implementations. + +Attempting to import this module on a non-Windows platform will raise an +:py:obj:`ImportError`. +""" # This code was originally contributed by Jeffrey Harris. import datetime import struct @@ -12,7 +20,6 @@ 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"] @@ -34,12 +41,13 @@ def _settzkeyname(): handle.Close() return TZKEYNAME + TZKEYNAME = _settzkeyname() class tzres(object): """ - Class for accessing `tzres.dll`, which contains timezone name related + Class for accessing ``tzres.dll``, which contains timezone name related resources. .. versionadded:: 2.5.0 @@ -49,7 +57,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 +71,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)) @@ -72,9 +80,10 @@ class tzres(object): :param offset: A positive integer value referring to a string from the tzres dll. - ..note: + .. note:: + Offsets found in the registry are generally of the form - `@tzres.dll,-114`. The offset in this case if 114, not -114. + ``@tzres.dll,-114``. The offset in this case is 114, not -114. """ resource = self.p_wchar() @@ -146,6 +155,9 @@ class tzwinbase(tzrangebase): return result def display(self): + """ + Return the display name of the time zone. + """ return self._display def transitions(self, year): @@ -188,13 +200,23 @@ class tzwinbase(tzrangebase): class tzwin(tzwinbase): + """ + Time zone object created from the zone info in the Windows registry + + These are similar to :py:class:`dateutil.tz.tzrange` objects in that + the time zone data is provided in the format of a single offset rule + for either 0 or 2 time zone transitions per year. + + :param: name + The name of a Windows time zone key, e.g. "Eastern Standard Time". + The full list of keys can be retrieved with :func:`tzwin.list`. + """ 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) @@ -235,6 +257,22 @@ class tzwin(tzwinbase): class tzwinlocal(tzwinbase): + """ + Class representing the local time zone information in the Windows registry + + While :class:`dateutil.tz.tzlocal` makes system calls (via the :mod:`time` + module) to retrieve time zone information, ``tzwinlocal`` retrieves the + rules directly from the Windows registry and creates an object like + :class:`dateutil.tz.tzwin`. + + Because Windows does not have an equivalent of :func:`time.tzset`, on + Windows, :class:`dateutil.tz.tzlocal` instances will always reflect the + time zone settings *at the time that the process was started*, meaning + changes to the machine's time zone settings during the run of a program + on Windows will **not** be reflected by :class:`dateutil.tz.tzlocal`. + Because ``tzwinlocal`` reads the registry directly, it is unaffected by + this issue. + """ def __init__(self): with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as handle: with winreg.OpenKey(handle, TZLOCALKEYNAME) as tzlocalkey: @@ -244,7 +282,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) @@ -266,7 +304,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 55cd91028..cebc673e4 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 *
\ No newline at end of file +from .tz.win import * diff --git a/libs/dateutil/utils.py b/libs/dateutil/utils.py index ebcce6aa2..dd2d245a0 100644 --- a/libs/dateutil/utils.py +++ b/libs/dateutil/utils.py @@ -28,7 +28,7 @@ def today(tzinfo=None): def default_tzinfo(dt, tzinfo): """ - Sets the the ``tzinfo`` parameter on naive datetimes only + Sets the ``tzinfo`` parameter on naive datetimes only This is useful for example when you are provided a datetime that may have either an implicit or explicit time zone, such as when parsing a time zone @@ -63,7 +63,7 @@ def default_tzinfo(dt, tzinfo): def within_delta(dt1, dt2, delta): """ - Useful for comparing two datetimes that may a negilible difference + Useful for comparing two datetimes that may have a negligible difference to be considered equal. """ delta = abs(delta) diff --git a/libs/dateutil/zoneinfo/__init__.py b/libs/dateutil/zoneinfo/__init__.py index 7145e05cf..34f11ad66 100644 --- a/libs/dateutil/zoneinfo/__init__.py +++ b/libs/dateutil/zoneinfo/__init__.py @@ -1,32 +1,20 @@ # -*- 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 +from dateutil.tz import tzfile as _tzfile -__all__ = ["get_zonefile_instance", "gettz", "gettz_db_metadata", "rebuild"] +__all__ = ["get_zonefile_instance", "gettz", "gettz_db_metadata"] 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,)) @@ -42,23 +30,15 @@ def getzoneinfofile_stream(): class ZoneInfoFile(object): def __init__(self, zonefile_stream=None): if zonefile_stream is not None: - 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) + 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} # 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 = dict((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()} self.zones.update(links) try: metadata_json = tf.extractfile(tf.getmember(METADATA_FN)) @@ -68,14 +48,14 @@ class ZoneInfoFile(object): # no metadata in tar file self.metadata = None else: - self.zones = dict() + self.zones = {} 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) @@ -94,7 +74,8 @@ class ZoneInfoFile(object): # timezone. Ugly, but adheres to the api. # # TODO: Remove after deprecation period. -_CLASS_ZONE_INSTANCE = list() +_CLASS_ZONE_INSTANCE = [] + def get_zonefile_instance(new_instance=False): """ @@ -124,6 +105,7 @@ 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 @@ -183,5 +165,3 @@ 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 Binary files differindex 1d15597b4..524c48e12 100644 --- a/libs/dateutil/zoneinfo/dateutil-zoneinfo.tar.gz +++ b/libs/dateutil/zoneinfo/dateutil-zoneinfo.tar.gz diff --git a/libs/dateutil/zoneinfo/rebuild.py b/libs/dateutil/zoneinfo/rebuild.py index a66c1d916..684c6586f 100644 --- a/libs/dateutil/zoneinfo/rebuild.py +++ b/libs/dateutil/zoneinfo/rebuild.py @@ -3,41 +3,65 @@ import os import tempfile import shutil import json -from subprocess import check_call +from subprocess import check_call, check_output +from tarfile import TarFile -from dateutil.zoneinfo import tar_open, METADATA_FN, ZONEFILENAME +from dateutil.zoneinfo import 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 tar_open(filename) as tf: + with TarFile.open(filename) as tf: for name in zonegroups: tf.extract(name, tmpdir) filepaths = [os.path.join(tmpdir, n) for n in zonegroups] - try: - check_call(["zic", "-d", zonedir] + filepaths) - except OSError as e: - _print_on_nosuchfile(e) - raise + + _run_zic(zonedir, filepaths) + # write metadata file 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 tar_open(target, "w:%s" % format) as tf: + with TarFile.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 _run_zic(zonedir, filepaths): + """Calls the ``zic`` compiler in a compatible way to get a "fat" binary. + + Recent versions of ``zic`` default to ``-b slim``, while older versions + don't even have the ``-b`` option (but default to "fat" binaries). The + current version of dateutil does not support Version 2+ TZif files, which + causes problems when used in conjunction with "slim" binaries, so this + function is used to ensure that we always get a "fat" binary. + """ + + try: + help_text = check_output(["zic", "--help"]) + except OSError as e: + _print_on_nosuchfile(e) + raise + + if b"-b " in help_text: + bloat_args = ["-b", "fat"] + else: + bloat_args = [] + + check_call(["zic"] + bloat_args + ["-d", zonedir] + filepaths) + + def _print_on_nosuchfile(e): """Print helpful troubleshooting message |