Add microsecond support to iso8601_from_timestamp

We now have the ability to generate iso8601 dates, with microseconds,
from timestamps.

Change-Id: I2d87fa22148098d428115bfcd0b4ce3879a07c38
Signed-off-by: Paul Belanger <paul.belanger@polybeacon.com>
This commit is contained in:
Paul Belanger 2015-01-16 12:43:45 -05:00
parent ff05ecc7ea
commit d5e2009ddd
2 changed files with 8 additions and 2 deletions

View File

@ -203,6 +203,12 @@ class TimeUtilsTest(test_base.BaseTestCase):
ts = calendar.timegm(utcnow.timetuple())
self.assertEqual(iso, timeutils.iso8601_from_timestamp(ts))
def test_iso8601_from_timestamp_ms(self):
ts = timeutils.utcnow_ts(microsecond=True)
utcnow = datetime.datetime.utcfromtimestamp(ts)
iso = timeutils.isotime(utcnow, subsecond=True)
self.assertEqual(iso, timeutils.iso8601_from_timestamp(ts, True))
def test_is_soon(self):
expires = timeutils.utcnow() + datetime.timedelta(minutes=5)
self.assertFalse(timeutils.is_soon(expires, 120))

View File

@ -130,9 +130,9 @@ def utcnow():
return datetime.datetime.utcnow()
def iso8601_from_timestamp(timestamp):
def iso8601_from_timestamp(timestamp, microsecond=False):
"""Returns an iso8601 formatted date from timestamp."""
return isotime(datetime.datetime.utcfromtimestamp(timestamp))
return isotime(datetime.datetime.utcfromtimestamp(timestamp), microsecond)
utcnow.override_time = None