Mercurial > hg
comparison mercurial/util.py @ 32407:a7dce526c462
util: refactor util.parsedate to raises ValueError
Split most of util.parsedate in util.rawparsedate and make it raises ValueError instead
of error.Abort.
The util.parsedate function is now just a shell function converting ValueError
to error.Abort for existing users.
I need to parse a date from config in a later patch and use util.rawparsedate
with ui.configwith which expect a convert that raises ValueError.
author | Boris Feld <boris.feld@octobus.net> |
---|---|
date | Fri, 19 May 2017 12:07:23 +0200 |
parents | df448de7cf3b |
children | bb18728ea617 |
comparison
equal
deleted
inserted
replaced
32406:952017471f93 | 32407:a7dce526c462 |
---|---|
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 | |
1927 >>> parsedate(' today ') == parsedate(\ | 1930 >>> parsedate(' today ') == parsedate(\ |
1928 datetime.date.today().strftime('%b %d')) | 1931 datetime.date.today().strftime('%b %d')) |
1929 True | 1932 True |
1930 >>> parsedate( 'yesterday ') == parsedate((datetime.date.today() -\ | 1933 >>> parsedate( 'yesterday ') == parsedate((datetime.date.today() -\ |
1931 datetime.timedelta(days=1)\ | 1934 datetime.timedelta(days=1)\ |
1936 >>> (strnow - now) < 1 | 1939 >>> (strnow - now) < 1 |
1937 True | 1940 True |
1938 >>> tz == strtz | 1941 >>> tz == strtz |
1939 True | 1942 True |
1940 """ | 1943 """ |
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 """ | |
1941 if bias is None: | 1958 if bias is None: |
1942 bias = {} | 1959 bias = {} |
1943 if not date: | 1960 if not date: |
1944 return 0, 0 | 1961 return 0, 0 |
1945 if isinstance(date, tuple) and len(date) == 2: | 1962 if isinstance(date, tuple) and len(date) == 2: |
1982 except (ValueError, OverflowError): | 1999 except (ValueError, OverflowError): |
1983 pass | 2000 pass |
1984 else: | 2001 else: |
1985 break | 2002 break |
1986 else: | 2003 else: |
1987 raise Abort(_('invalid date: %r') % date) | 2004 raise ValueError(_('invalid date: %r') % date) |
1988 # validate explicit (probably user-specified) date and | 2005 # validate explicit (probably user-specified) date and |
1989 # time zone offset. values must fit in signed 32 bits for | 2006 # time zone offset. values must fit in signed 32 bits for |
1990 # current 32-bit linux runtimes. timezones go from UTC-12 | 2007 # current 32-bit linux runtimes. timezones go from UTC-12 |
1991 # to UTC+14 | 2008 # to UTC+14 |
1992 if when < -0x80000000 or when > 0x7fffffff: | 2009 if when < -0x80000000 or when > 0x7fffffff: |
1993 raise Abort(_('date exceeds 32 bits: %d') % when) | 2010 raise ValueError(_('date exceeds 32 bits: %d') % when) |
1994 if offset < -50400 or offset > 43200: | 2011 if offset < -50400 or offset > 43200: |
1995 raise Abort(_('impossible time zone offset: %d') % offset) | 2012 raise ValueError(_('impossible time zone offset: %d') % offset) |
1996 return when, offset | 2013 return when, offset |
1997 | 2014 |
1998 def matchdate(date): | 2015 def matchdate(date): |
1999 """Return a function that matches a given date match specifier | 2016 """Return a function that matches a given date match specifier |
2000 | 2017 |