comparison mercurial/util.py @ 26126:7b625baed995

util: extract function that parses timezone string It will be used to parse a timezone argument passed to a template function.
author Yuya Nishihara <yuya@tcha.org>
date Tue, 01 Sep 2015 19:43:14 +0900
parents ce26928cbe41
children c5b2074ae8c0
comparison
equal deleted inserted replaced
26125:c990afab2243 26126:7b625baed995
1369 1369
1370 def shortdate(date=None): 1370 def shortdate(date=None):
1371 """turn (timestamp, tzoff) tuple into iso 8631 date.""" 1371 """turn (timestamp, tzoff) tuple into iso 8631 date."""
1372 return datestr(date, format='%Y-%m-%d') 1372 return datestr(date, format='%Y-%m-%d')
1373 1373
1374 def parsetimezone(tz):
1375 """parse a timezone string and return an offset integer"""
1376 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit():
1377 sign = (tz[0] == "+") and 1 or -1
1378 hours = int(tz[1:3])
1379 minutes = int(tz[3:5])
1380 return -sign * (hours * 60 + minutes) * 60
1381 if tz == "GMT" or tz == "UTC":
1382 return 0
1383 return None
1384
1374 def strdate(string, format, defaults=[]): 1385 def strdate(string, format, defaults=[]):
1375 """parse a localized time string and return a (unixtime, offset) tuple. 1386 """parse a localized time string and return a (unixtime, offset) tuple.
1376 if the string cannot be parsed, ValueError is raised.""" 1387 if the string cannot be parsed, ValueError is raised."""
1377 def timezone(string):
1378 tz = string.split()[-1]
1379 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit():
1380 sign = (tz[0] == "+") and 1 or -1
1381 hours = int(tz[1:3])
1382 minutes = int(tz[3:5])
1383 return -sign * (hours * 60 + minutes) * 60
1384 if tz == "GMT" or tz == "UTC":
1385 return 0
1386 return None
1387
1388 # NOTE: unixtime = localunixtime + offset 1388 # NOTE: unixtime = localunixtime + offset
1389 offset, date = timezone(string), string 1389 offset, date = parsetimezone(string.split()[-1]), string
1390 if offset is not None: 1390 if offset is not None:
1391 date = " ".join(string.split()[:-1]) 1391 date = " ".join(string.split()[:-1])
1392 1392
1393 # add missing elements from defaults 1393 # add missing elements from defaults
1394 usenow = False # default to using biased defaults 1394 usenow = False # default to using biased defaults