##// END OF EJS Templates
date: refactor timezone parsing...
Matt Mackall -
r29636:84ef4517 stable
parent child Browse files
Show More
@@ -670,7 +670,9 b' def localdate(context, mapping, args):'
670 670 tzoffset = None
671 671 tz = evalfuncarg(context, mapping, args[1])
672 672 if isinstance(tz, str):
673 tzoffset = util.parsetimezone(tz)
673 tzoffset, remainder = util.parsetimezone(tz)
674 if remainder:
675 tzoffset = None
674 676 if tzoffset is None:
675 677 try:
676 678 tzoffset = int(tz)
@@ -1746,24 +1746,27 b' def shortdate(date=None):'
1746 1746 """turn (timestamp, tzoff) tuple into iso 8631 date."""
1747 1747 return datestr(date, format='%Y-%m-%d')
1748 1748
1749 def parsetimezone(tz):
1750 """parse a timezone string and return an offset integer"""
1751 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit():
1752 sign = (tz[0] == "+") and 1 or -1
1753 hours = int(tz[1:3])
1754 minutes = int(tz[3:5])
1755 return -sign * (hours * 60 + minutes) * 60
1756 if tz == "GMT" or tz == "UTC":
1757 return 0
1758 return None
1749 def parsetimezone(s):
1750 """find a trailing timezone, if any, in string, and return a
1751 (offset, remainder) pair"""
1752
1753 if s.endswith("GMT") or s.endswith("UTC"):
1754 return 0, s[:-3].rstrip()
1755
1756 # Unix-style timezones [+-]hhmm
1757 if len(s) >= 5 and s[-5] in "+-" and s[-4:].isdigit():
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 1765 def strdate(string, format, defaults=[]):
1761 1766 """parse a localized time string and return a (unixtime, offset) tuple.
1762 1767 if the string cannot be parsed, ValueError is raised."""
1763 1768 # NOTE: unixtime = localunixtime + offset
1764 offset, date = parsetimezone(string.split()[-1]), string
1765 if offset is not None:
1766 date = " ".join(string.split()[:-1])
1769 offset, date = parsetimezone(string)
1767 1770
1768 1771 # add missing elements from defaults
1769 1772 usenow = False # default to using biased defaults
@@ -3253,6 +3253,9 b' Test localdate(date, tz) function:'
3253 3253 1970-01-01 09:00 +0900
3254 3254 $ TZ=JST-09 hg log -r0 -T '{localdate(date, "UTC")|isodate}\n'
3255 3255 1970-01-01 00:00 +0000
3256 $ TZ=JST-09 hg log -r0 -T '{localdate(date, "blahUTC")|isodate}\n'
3257 hg: parse error: localdate expects a timezone
3258 [255]
3256 3259 $ TZ=JST-09 hg log -r0 -T '{localdate(date, "+0200")|isodate}\n'
3257 3260 1970-01-01 02:00 +0200
3258 3261 $ TZ=JST-09 hg log -r0 -T '{localdate(date, "0")|isodate}\n'
General Comments 0
You need to be logged in to leave comments. Login now