comparison mercurial/util.py @ 29636:84ef4517de03 stable

date: refactor timezone parsing We want to be able to accept ISO 8601 style timezones that don't include a space separator, so we change the timezone parsing function to accept a full date string and return both the offset and the non-timezone portion.
author Matt Mackall <mpm@selenic.com>
date Wed, 27 Jul 2016 15:14:19 -0500
parents 616cbcb59e05
children 46b2ccce7fde
comparison
equal deleted inserted replaced
29635:dee24c87dbf0 29636:84ef4517de03
1744 1744
1745 def shortdate(date=None): 1745 def shortdate(date=None):
1746 """turn (timestamp, tzoff) tuple into iso 8631 date.""" 1746 """turn (timestamp, tzoff) tuple into iso 8631 date."""
1747 return datestr(date, format='%Y-%m-%d') 1747 return datestr(date, format='%Y-%m-%d')
1748 1748
1749 def parsetimezone(tz): 1749 def parsetimezone(s):
1750 """parse a timezone string and return an offset integer""" 1750 """find a trailing timezone, if any, in string, and return a
1751 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit(): 1751 (offset, remainder) pair"""
1752 sign = (tz[0] == "+") and 1 or -1 1752
1753 hours = int(tz[1:3]) 1753 if s.endswith("GMT") or s.endswith("UTC"):
1754 minutes = int(tz[3:5]) 1754 return 0, s[:-3].rstrip()
1755 return -sign * (hours * 60 + minutes) * 60 1755
1756 if tz == "GMT" or tz == "UTC": 1756 # Unix-style timezones [+-]hhmm
1757 return 0 1757 if len(s) >= 5 and s[-5] in "+-" and s[-4:].isdigit():
1758 return None 1758 sign = (s[-5] == "+") and 1 or -1
1759 hours = int(s[-4:-2])
1760 minutes = int(s[-2:])
1761 return -sign * (hours * 60 + minutes) * 60, s[:-5].rstrip()
1762
1763 return None, s
1759 1764
1760 def strdate(string, format, defaults=[]): 1765 def strdate(string, format, defaults=[]):
1761 """parse a localized time string and return a (unixtime, offset) tuple. 1766 """parse a localized time string and return a (unixtime, offset) tuple.
1762 if the string cannot be parsed, ValueError is raised.""" 1767 if the string cannot be parsed, ValueError is raised."""
1763 # NOTE: unixtime = localunixtime + offset 1768 # NOTE: unixtime = localunixtime + offset
1764 offset, date = parsetimezone(string.split()[-1]), string 1769 offset, date = parsetimezone(string)
1765 if offset is not None:
1766 date = " ".join(string.split()[:-1])
1767 1770
1768 # add missing elements from defaults 1771 # add missing elements from defaults
1769 usenow = False # default to using biased defaults 1772 usenow = False # default to using biased defaults
1770 for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity 1773 for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity
1771 found = [True for p in part if ("%"+p) in format] 1774 found = [True for p in part if ("%"+p) in format]