fixed formatting and added a 'test' (sort of)

This commit is contained in:
Marcelo Glezer 2015-03-02 23:22:44 -03:00
parent 91e5a4a4b5
commit 58dba3f490
3 changed files with 13 additions and 17 deletions

View File

@ -40,8 +40,8 @@ class FlowDetailsView(urwid.ListBox):
sc = self.flow.server_conn sc = self.flow.server_conn
parts = [ parts = [
["Address", "%s:%s" % sc.address()], ["Address", "%s:%s" % sc.address()],
["Start time", utils.format_timestamp(sc.timestamp_start, True)], ["Start time", utils.format_timestamp_with_milli(sc.timestamp_start)],
["End time", utils.format_timestamp(sc.timestamp_end, True) if sc.timestamp_end else "active"], ["End time", utils.format_timestamp_with_milli(sc.timestamp_end) if sc.timestamp_end else "active"],
] ]
text.extend(common.format_keyvals(parts, key="key", val="text", indent=4)) text.extend(common.format_keyvals(parts, key="key", val="text", indent=4))
@ -84,9 +84,9 @@ class FlowDetailsView(urwid.ListBox):
cc = self.flow.client_conn cc = self.flow.client_conn
parts = [ parts = [
["Address", "%s:%s" % cc.address()], ["Address", "%s:%s" % cc.address()],
["Start time", utils.format_timestamp(cc.timestamp_start, True)], ["Start time", utils.format_timestamp_with_milli(cc.timestamp_start)],
# ["Requests", "%s"%cc.requestcount], # ["Requests", "%s"%cc.requestcount],
["End time", utils.format_timestamp(cc.timestamp_end, True) if cc.timestamp_end else "active"], ["End time", utils.format_timestamp_with_milli(cc.timestamp_end) if cc.timestamp_end else "active"],
] ]
text.extend(common.format_keyvals(parts, key="key", val="text", indent=4)) text.extend(common.format_keyvals(parts, key="key", val="text", indent=4))

View File

@ -11,20 +11,14 @@ def timestamp():
def format_timestamp(s, include_fractions = False): def format_timestamp(s, include_fractions = False):
if type(s) is not float: s = time.localtime(s)
s = time.localtime(s) d = datetime.datetime.fromtimestamp(time.mktime(s))
d = datetime.datetime.fromtimestamp(time.mktime(s)) return d.strftime("%Y-%m-%d %H:%M:%S")
else:
# s is a timestamp(float) can be converted to a datetime without lossing microsecs
d = datetime.datetime.fromtimestamp(s)
if not include_fractions:
format = "%Y-%m-%d %H:%M:%S"
else:
# this will show 3 miliseconds digits (but will truncate and not round the 4th significant digit
# so 600999 microseconds will be 600ms and not 601ms)
format = "%Y-%m-%d %H:%M:%S."+str(d.microsecond/1000)
return d.strftime(format)
def format_timestamp_with_milli(s):
d = datetime.datetime.fromtimestamp(s)
return d.strftime("%Y-%m-%d %H:%M:%S.")+str(d.microsecond/1000).zfill(3)
def isBin(s): def isBin(s):
""" """
Does this string have any non-ASCII characters? Does this string have any non-ASCII characters?

View File

@ -8,6 +8,8 @@ utils.CERT_SLEEP_TIME = 0
def test_format_timestamp(): def test_format_timestamp():
assert utils.format_timestamp(utils.timestamp()) assert utils.format_timestamp(utils.timestamp())
def test_format_timestamp_with_milli():
assert utils.format_timestamp_with_milli(utils.timestamp())
def test_isBin(): def test_isBin():
assert not utils.isBin("testing\n\r") assert not utils.isBin("testing\n\r")