diff -r 9cceb439048b -r 85f796baab10 mercurial/util.py --- a/mercurial/util.py Fri Jun 30 16:16:35 2006 +0200 +++ b/mercurial/util.py Fri Jun 30 18:47:35 2006 +0200 @@ -859,6 +859,37 @@ s += " %+03d%02d" % (-tz / 3600, ((-tz % 3600) / 60)) return s +def strdate(string, format='%a %b %d %H:%M:%S %Y'): + """parse a localized time string and return a (unixtime, offset) tuple. + if the string cannot be parsed, ValueError is raised.""" + def hastimezone(string): + return (string[-4:].isdigit() and + (string[-5] == '+' or string[-5] == '-') and + string[-6].isspace()) + + if hastimezone(string): + date, tz = string.rsplit(None, 1) + tz = int(tz) + offset = - 3600 * (tz / 100) - 60 * (tz % 100) + else: + date, offset = string, 0 + when = int(time.mktime(time.strptime(date, format))) + offset + return when, offset + +def parsedate(string, formats=('%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M')): + """parse a localized time string and return a (unixtime, offset) tuple. + The date may be a "unixtime offset" string or in one of the specified + formats.""" + try: + when, offset = map(int, string.split(' ')) + return when, offset + except ValueError: pass + for format in formats: + try: + return strdate(string, format) + except ValueError: pass + raise ValueError(_('invalid date: %r') % string) + def shortuser(user): """Return a short representation of a user name or email address.""" f = user.find('@')