From: Brian Warner Date: Mon, 13 Oct 2008 22:52:58 +0000 (-0700) Subject: util/time_format.py: accept space separator, add unit tests X-Git-Url: https://git.rkrishnan.org/specifications/install.html?a=commitdiff_plain;h=b0a00e0f3334525495a4a938cfa60c11a105efda;p=tahoe-lafs%2Ftahoe-lafs.git util/time_format.py: accept space separator, add unit tests --- diff --git a/src/allmydata/test/test_util.py b/src/allmydata/test/test_util.py index 7eb8c120..babaef78 100644 --- a/src/allmydata/test/test_util.py +++ b/src/allmydata/test/test_util.py @@ -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) diff --git a/src/allmydata/util/time_format.py b/src/allmydata/util/time_format.py index 63b5770d..98d32e49 100644 --- a/src/allmydata/util/time_format.py +++ b/src/allmydata/util/time_format.py @@ -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\d{4})-(?P\d{2})-(?P\d{2})[T_](?P\d{2}):(?P\d{2}):(?P\d{2})(?P\.\d+)?")): +def iso_utc_time_to_localseconds(isotime, _conversion_re=re.compile(r"(?P\d{4})-(?P\d{2})-(?P\d{2})[T_ ](?P\d{2}):(?P\d{2}):(?P\d{2})(?P\.\d+)?")): """ The inverse of iso_utc().