Skip to content

Commit

Permalink
Add seconds-to-timestamp function to help format timestamps when give…
Browse files Browse the repository at this point in the history
…n seconds
  • Loading branch information
cyli committed Nov 17, 2014
1 parent 2132def commit c9bc809
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
25 changes: 25 additions & 0 deletions mimic/test/test_util.py
Expand Up @@ -43,3 +43,28 @@ def test_attribute_names(self):
self.assertEqual(['ima_string', 'ima_name'],
helper.attribute_names(['ima_string',
Attribute('ima_name')]))

def test_seconds_to_timestamp_default_timestamp(self):
"""
:func:`helper.seconds_to_timestamp` returns a timestamp matching
the seconds since the epoch given. The timestamp conforms to with the
default format string of ``%Y-%m-%dT%H:%M:%S.%fZ`` if no format
string is provided.
"""
matches = [(0, "1970-01-01T00:00:00.000000Z"),
(1.5, "1970-01-01T00:00:01.500000Z"),
(121.4005, "1970-01-01T00:02:01.400500Z")]
for match in matches:
self.assertEqual(match[1], helper.seconds_to_timestamp(match[0]))

def test_seconds_to_timestamp_provided_timestamp(self):
"""
:func:`helper.seconds_to_timestamp` uses the provided timestamp format
to format the seconds.
"""
matches = [("%m-%d-%Y %H:%M:%S", "01-01-1970 00:00:00"),
("%Y-%m-%d", "1970-01-01"),
("%H %M %S (%f)", "00 00 00 (000000)")]
for match in matches:
self.assertEqual(match[1],
helper.seconds_to_timestamp(0, match[0]))
7 changes: 7 additions & 0 deletions mimic/util/helper.py
Expand Up @@ -35,6 +35,13 @@ def attribute_names(attribute_list):
for attr in attribute_list]


def seconds_to_timestamp(seconds, format=fmt):
"""
Return an ISO8601 Zulu timestamp given seconds since the epoch.
"""
return datetime.utcfromtimestamp(seconds).strftime(format)


def not_found_response(resource='servers'):
"""
Return a 404 response body for Nova, depending on the resource. Expects
Expand Down

0 comments on commit c9bc809

Please sign in to comment.