comparison mercurial/util.py @ 32462:bb18728ea617

util: raise ParseError when parsing dates (BC) a7dce526c462 refactored util.parsedate in order to raise ValueError instead of Abort for using with ui.configwith. It causes several problems, putting arbitrary bytes in ValueError can cause issues with Python 3. Moreover, we added a function to convert ValueError exceptions back to Abort. A better approach would be to make parsedate raises ParseError, removing the convert function and update configwith to also catch ParseError. The side-effect is that error message when giving an invalid date in CLI change from: abort: invalid date: 'foo bar' to: hg: parse error: invalid date: 'foo bar' I'm not sure if it's an acceptable change, I found personally the error message more clear but more verbose too.
author Boris Feld <boris.feld@octobus.net>
date Wed, 24 May 2017 17:50:17 +0200
parents a7dce526c462
children 47ce079b1afa
comparison
equal deleted inserted replaced
32461:2b5953a49f14 32462:bb18728ea617
1922 """parse a localized date/time and return a (unixtime, offset) tuple. 1922 """parse a localized date/time and return a (unixtime, offset) tuple.
1923 1923
1924 The date may be a "unixtime offset" string or in one of the specified 1924 The date may be a "unixtime offset" string or in one of the specified
1925 formats. If the date already is a (unixtime, offset) tuple, it is returned. 1925 formats. If the date already is a (unixtime, offset) tuple, it is returned.
1926 1926
1927 This function calls rawparsedate and convert ValueError to Abort for
1928 functions that needs higher level exception.
1929
1930 >>> parsedate(' today ') == parsedate(\ 1927 >>> parsedate(' today ') == parsedate(\
1931 datetime.date.today().strftime('%b %d')) 1928 datetime.date.today().strftime('%b %d'))
1932 True 1929 True
1933 >>> parsedate( 'yesterday ') == parsedate((datetime.date.today() -\ 1930 >>> parsedate( 'yesterday ') == parsedate((datetime.date.today() -\
1934 datetime.timedelta(days=1)\ 1931 datetime.timedelta(days=1)\
1939 >>> (strnow - now) < 1 1936 >>> (strnow - now) < 1
1940 True 1937 True
1941 >>> tz == strtz 1938 >>> tz == strtz
1942 True 1939 True
1943 """ 1940 """
1944 try:
1945 return rawparsedate(date, formats=formats, bias=bias)
1946 except ValueError as exception:
1947 raise Abort(str(exception))
1948
1949 def rawparsedate(date, formats=None, bias=None):
1950 """parse a localized date/time and return a (unixtime, offset) tuple.
1951
1952 The date may be a "unixtime offset" string or in one of the specified
1953 formats. If the date already is a (unixtime, offset) tuple, it is returned.
1954
1955 See docstring of parsedate for example.
1956 Raise ValueError for invalid date value.
1957 """
1958 if bias is None: 1941 if bias is None:
1959 bias = {} 1942 bias = {}
1960 if not date: 1943 if not date:
1961 return 0, 0 1944 return 0, 0
1962 if isinstance(date, tuple) and len(date) == 2: 1945 if isinstance(date, tuple) and len(date) == 2:
1999 except (ValueError, OverflowError): 1982 except (ValueError, OverflowError):
2000 pass 1983 pass
2001 else: 1984 else:
2002 break 1985 break
2003 else: 1986 else:
2004 raise ValueError(_('invalid date: %r') % date) 1987 raise error.ParseError(_('invalid date: %r') % date)
2005 # validate explicit (probably user-specified) date and 1988 # validate explicit (probably user-specified) date and
2006 # time zone offset. values must fit in signed 32 bits for 1989 # time zone offset. values must fit in signed 32 bits for
2007 # current 32-bit linux runtimes. timezones go from UTC-12 1990 # current 32-bit linux runtimes. timezones go from UTC-12
2008 # to UTC+14 1991 # to UTC+14
2009 if when < -0x80000000 or when > 0x7fffffff: 1992 if when < -0x80000000 or when > 0x7fffffff:
2010 raise ValueError(_('date exceeds 32 bits: %d') % when) 1993 raise error.ParseError(_('date exceeds 32 bits: %d') % when)
2011 if offset < -50400 or offset > 43200: 1994 if offset < -50400 or offset > 43200:
2012 raise ValueError(_('impossible time zone offset: %d') % offset) 1995 raise error.ParseError(_('impossible time zone offset: %d') % offset)
2013 return when, offset 1996 return when, offset
2014 1997
2015 def matchdate(date): 1998 def matchdate(date):
2016 """Return a function that matches a given date match specifier 1999 """Return a function that matches a given date match specifier
2017 2000