From 91e5a4a4b5bf1beb083afb0731294cfeaca62944 Mon Sep 17 00:00:00 2001 From: Marcelo Glezer Date: Mon, 2 Mar 2015 18:30:46 -0300 Subject: #487 added microsecond support to format_timestamp and used in FlowDetailView. Still WIP. --- libmproxy/utils.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'libmproxy/utils.py') diff --git a/libmproxy/utils.py b/libmproxy/utils.py index 76e99c34..a50be7fc 100644 --- a/libmproxy/utils.py +++ b/libmproxy/utils.py @@ -10,11 +10,20 @@ def timestamp(): return time.time() -def format_timestamp(s): - s = time.localtime(s) - d = datetime.datetime.fromtimestamp(time.mktime(s)) - return d.strftime("%Y-%m-%d %H:%M:%S") - +def format_timestamp(s, include_fractions = False): + if type(s) is not float: + s = time.localtime(s) + d = datetime.datetime.fromtimestamp(time.mktime(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 isBin(s): """ -- cgit v1.2.3 From 58dba3f49083288e19f6e9edb7cc60e88dd446e3 Mon Sep 17 00:00:00 2001 From: Marcelo Glezer Date: Mon, 2 Mar 2015 23:22:44 -0300 Subject: fixed formatting and added a 'test' (sort of) --- libmproxy/utils.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) (limited to 'libmproxy/utils.py') diff --git a/libmproxy/utils.py b/libmproxy/utils.py index a50be7fc..e608c8a3 100644 --- a/libmproxy/utils.py +++ b/libmproxy/utils.py @@ -11,20 +11,14 @@ def timestamp(): def format_timestamp(s, include_fractions = False): - if type(s) is not float: - s = time.localtime(s) - d = datetime.datetime.fromtimestamp(time.mktime(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) - + s = time.localtime(s) + d = datetime.datetime.fromtimestamp(time.mktime(s)) + return d.strftime("%Y-%m-%d %H:%M:%S") + +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): """ Does this string have any non-ASCII characters? -- cgit v1.2.3 From 485003511b0eaf61cca38a889ec5a888937a142b Mon Sep 17 00:00:00 2001 From: Marcelo Glezer Date: Tue, 3 Mar 2015 17:49:47 -0300 Subject: removed unused parameter --- libmproxy/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libmproxy/utils.py') diff --git a/libmproxy/utils.py b/libmproxy/utils.py index e608c8a3..e2306c11 100644 --- a/libmproxy/utils.py +++ b/libmproxy/utils.py @@ -10,7 +10,7 @@ def timestamp(): return time.time() -def format_timestamp(s, include_fractions = False): +def format_timestamp(s): s = time.localtime(s) d = datetime.datetime.fromtimestamp(time.mktime(s)) return d.strftime("%Y-%m-%d %H:%M:%S") -- cgit v1.2.3 From 48023db59e048da20484ca631d41aa524425f5dd Mon Sep 17 00:00:00 2001 From: Tarashish Mishra Date: Wed, 4 Mar 2015 22:32:01 +0530 Subject: Minor refactor to PR #496 --- libmproxy/utils.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'libmproxy/utils.py') diff --git a/libmproxy/utils.py b/libmproxy/utils.py index e2306c11..51f2dc26 100644 --- a/libmproxy/utils.py +++ b/libmproxy/utils.py @@ -15,10 +15,12 @@ def format_timestamp(s): d = datetime.datetime.fromtimestamp(time.mktime(s)) return d.strftime("%Y-%m-%d %H:%M:%S") + 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) - + return d.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] + + def isBin(s): """ Does this string have any non-ASCII characters? @@ -84,15 +86,15 @@ def pretty_size(size): def pretty_duration(secs): formatters = [ - (100, "{:.0f}s"), - (10, "{:2.1f}s"), - (1, "{:1.2f}s"), + (100, "{:.0f}s"), + (10, "{:2.1f}s"), + (1, "{:1.2f}s"), ] for limit, formatter in formatters: if secs >= limit: return formatter.format(secs) - #less than 1 sec + #less than 1 sec return "{:.0f}ms".format(secs*1000) class Data: -- cgit v1.2.3