Patchwork [3,of,5] util: extract function that parses timezone string

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 1, 2015, 1:25 p.m.
Message ID <ced5f8e9a180e38cd554.1441113913@mimosa>
Download mbox | patch
Permalink /patch/10343/
State Accepted
Headers show

Comments

Yuya Nishihara - Sept. 1, 2015, 1:25 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1441104194 -32400
#      Tue Sep 01 19:43:14 2015 +0900
# Node ID ced5f8e9a180e38cd554d8c15334189e9bced9cd
# Parent  d8a5d9f3ff0f47cb46aef617a23cef29c7701f2d
util: extract function that parses timezone string

It will be used to parse a timezone argument passed to a template function.

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1371,22 +1371,22 @@  def shortdate(date=None):
     """turn (timestamp, tzoff) tuple into iso 8631 date."""
     return datestr(date, format='%Y-%m-%d')
 
+def parsetimezone(tz):
+    """parse a timezone string and return an offset integer"""
+    if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit():
+        sign = (tz[0] == "+") and 1 or -1
+        hours = int(tz[1:3])
+        minutes = int(tz[3:5])
+        return -sign * (hours * 60 + minutes) * 60
+    if tz == "GMT" or tz == "UTC":
+        return 0
+    return None
+
 def strdate(string, format, defaults=[]):
     """parse a localized time string and return a (unixtime, offset) tuple.
     if the string cannot be parsed, ValueError is raised."""
-    def timezone(string):
-        tz = string.split()[-1]
-        if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit():
-            sign = (tz[0] == "+") and 1 or -1
-            hours = int(tz[1:3])
-            minutes = int(tz[3:5])
-            return -sign * (hours * 60 + minutes) * 60
-        if tz == "GMT" or tz == "UTC":
-            return 0
-        return None
-
     # NOTE: unixtime = localunixtime + offset
-    offset, date = timezone(string), string
+    offset, date = parsetimezone(string.split()[-1]), string
     if offset is not None:
         date = " ".join(string.split()[:-1])