]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
util/time_format.py: accept space separator, add unit tests
authorBrian Warner <warner@allmydata.com>
Mon, 13 Oct 2008 22:52:58 +0000 (15:52 -0700)
committerBrian Warner <warner@allmydata.com>
Mon, 13 Oct 2008 22:52:58 +0000 (15:52 -0700)
src/allmydata/test/test_util.py
src/allmydata/util/time_format.py

index 7eb8c120b463f077673ed572e86b316f9ea108e3..babaef78aefcbf93ea72647f0b415568b2e5f00c 100644 (file)
@@ -1,14 +1,14 @@
 
 def foo(): pass # keep the line number constant
 
-import os
+import os, time
 from twisted.trial import unittest
 from twisted.internet import defer, reactor
 from twisted.python import failure
 
 from allmydata.util import base32, idlib, humanreadable, mathutil, hashutil
 from allmydata.util import assertutil, fileutil, testutil, deferredutil
-from allmydata.util import limiter
+from allmydata.util import limiter, time_format
 
 class Base32(unittest.TestCase):
     def test_b2a_matches_Pythons(self):
@@ -517,3 +517,27 @@ class Limiter(unittest.TestCase):
                 self.failUnless( (i, str(i)) in self.calls)
         d.addCallback(_all_done)
         return d
+
+class TimeFormat(unittest.TestCase):
+    def test_epoch(self):
+        s = time_format.iso_utc_time_to_localseconds("1970-01-01T00:00:01")
+        self.failUnlessEqual(s, 1.0)
+        s = time_format.iso_utc_time_to_localseconds("1970-01-01_00:00:01")
+        self.failUnlessEqual(s, 1.0)
+        s = time_format.iso_utc_time_to_localseconds("1970-01-01 00:00:01")
+        self.failUnlessEqual(s, 1.0)
+
+        self.failUnlessEqual(time_format.iso_utc(1.0), "1970-01-01_00:00:01")
+        self.failUnlessEqual(time_format.iso_utc(1.0, sep=" "),
+                             "1970-01-01 00:00:01")
+        now = time.time()
+        def my_time():
+            return 1.0
+        self.failUnlessEqual(time_format.iso_utc(t=my_time),
+                             "1970-01-01_00:00:01")
+        e = self.failUnlessRaises(ValueError,
+                                  time_format.iso_utc_time_to_localseconds,
+                                  "invalid timestring")
+        self.failUnless("not a complete ISO8601 timestamp" in str(e))
+        s = time_format.iso_utc_time_to_localseconds("1970-01-01_00:00:01.500")
+        self.failUnlessEqual(s, 1.5)
index 63b5770dc566d146bacaa6c43cf8429cb7f4d7eb..98d32e491a0e4adbaec156a0da0d250655bdbfa0 100644 (file)
@@ -14,7 +14,7 @@ def iso_utc(now=None, sep='_', t=time.time):
         now = t()
     return datetime.datetime.utcfromtimestamp(now).isoformat(sep)
 
-def iso_utc_time_to_localseconds(isotime, _conversion_re=re.compile(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})[T_](?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})(?P<subsecond>\.\d+)?")):
+def iso_utc_time_to_localseconds(isotime, _conversion_re=re.compile(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})[T_ ](?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})(?P<subsecond>\.\d+)?")):
     """
     The inverse of iso_utc().