소스 검색

Import negative timezone fix from hg-git.

Jelmer Vernooij 16 년 전
부모
커밋
49752a5b6f
2개의 변경된 파일33개의 추가작업 그리고 2개의 파일을 삭제
  1. 6 2
      dulwich/objects.py
  2. 27 0
      dulwich/tests/test_objects.py

+ 6 - 2
dulwich/objects.py

@@ -472,15 +472,19 @@ class Tree(ShaFile):
 
 def parse_timezone(text):
     offset = int(text)
+    signum = (offset < 0) and -1 or 1
+    offset = abs(offset)
     hours = int(offset / 100)
     minutes = (offset % 100)
-    return (hours * 3600) + (minutes * 60)
+    return signum * (hours * 3600 + minutes * 60)
 
 
 def format_timezone(offset):
     if offset % 60 != 0:
         raise ValueError("Unable to handle non-minute offset.")
-    return '%+03d%02d' % (offset / 3600, (offset / 60) % 60)
+    sign = (offset < 0) and '-' or '+'
+    offset = abs(offset)
+    return '%c%02d%02d' % (sign, offset / 3600, (offset / 60) % 60)
 
 
 class Commit(ShaFile):

+ 27 - 0
dulwich/tests/test_objects.py

@@ -26,7 +26,9 @@ from dulwich.objects import (
     Tree,
     Commit,
     Tag,
+    format_timezone,
     hex_to_sha,
+    parse_timezone,
     )
 
 a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
@@ -268,3 +270,28 @@ OK2XeQOiEeXtT76rV4t2WR4=
         self.assertEquals("v2.6.22-rc7", x.name)
 
 
+class TimezoneTests(unittest.TestCase):
+
+    def test_parse_timezone_utc(self):
+        self.assertEquals(0, parse_timezone("+0000"))
+
+    def test_generate_timezone_utc(self):
+        self.assertEquals("+0000", format_timezone(0))
+
+    def test_parse_timezone_cet(self):
+        self.assertEquals(60 * 60, parse_timezone("+0100"))
+
+    def test_format_timezone_cet(self):
+        self.assertEquals("+0100", format_timezone(60 * 60))
+
+    def test_format_timezone_pdt(self):
+        self.assertEquals("-0400", format_timezone(-4 * 60 * 60))
+
+    def test_parse_timezone_pdt(self):
+        self.assertEquals(-4 * 60 * 60, parse_timezone("-0400"))
+
+    def test_format_timezone_pdt_half(self):
+        self.assertEquals("-0440", format_timezone(int(((-4 * 60) - 40) * 60)))
+
+    def test_parse_timezone_pdt_half(self):
+        self.assertEquals(((-4 * 60) - 40) * 60, parse_timezone("-0440"))