From caf1e1a63a2538fee519625f9095c3ab72854dbf Mon Sep 17 00:00:00 2001 From: iory Date: Fri, 17 Apr 2020 17:01:56 +0900 Subject: [PATCH] Add timedelta_isoformat --- pysnooper/pycompat.py | 6 ++++++ pysnooper/tracer.py | 12 ++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pysnooper/pycompat.py b/pysnooper/pycompat.py index 5b9a481..c6b1bf7 100644 --- a/pysnooper/pycompat.py +++ b/pysnooper/pycompat.py @@ -80,3 +80,9 @@ else: return result +def timedelta_isoformat(timedelta, timespec='microseconds'): + assert isinstance(timedelta, datetime_module.timedelta) + if timespec != 'microseconds': + raise NotImplementedError + time = (datetime_module.datetime.min + timedelta).time() + return time_isoformat(time, timespec) diff --git a/pysnooper/tracer.py b/pysnooper/tracer.py index e43b5bf..7dbd748 100644 --- a/pysnooper/tracer.py +++ b/pysnooper/tracer.py @@ -310,9 +310,8 @@ class Tracer: self.target_frames.discard(calling_frame) self.frame_to_local_reprs.pop(calling_frame, None) - now = (datetime_module.datetime.min + ( - datetime_module.datetime.now() - self.start_time)).time() - now_string = pycompat.time_isoformat(now, timespec='microseconds') + duration = datetime_module.datetime.now() - self.start_time + now_string = pycompat.timedelta_isoformat(duration, timespec='microseconds') self.write('Total elapsed time: {now_string}'.format(**locals())) def _is_internal_frame(self, frame): @@ -360,11 +359,12 @@ class Tracer: ### Finished checking whether we should trace this line. ############## if self.relative_time: - now = (datetime_module.datetime.min + - (datetime_module.datetime.now() - self.start_time)).time() + duration = datetime_module.datetime.now() - self.start_time + now_string = pycompat.timedelta_isoformat( + duration, timespec='microseconds') if not self.normalize else ' ' * 15 else: now = datetime_module.datetime.now().time() - now_string = pycompat.time_isoformat(now, timespec='microseconds') if not self.normalize else ' ' * 15 + now_string = pycompat.time_isoformat(now, timespec='microseconds') if not self.normalize else ' ' * 15 line_no = frame.f_lineno source_path, source = get_path_and_source_from_frame(frame) source_path = source_path if not self.normalize else os.path.basename(source_path)