Skip to content

Commit

Permalink
Fix the seconds_difference() bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mgedmin committed Feb 23, 2019
1 parent d84eb14 commit 50dd591
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 166 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changes
2.0.0 (unreleased)
------------------

- Fix logic bug in seconds_difference() that could introduce error up to nearly
a whole second for any particular event.

- Limit number precision in HTML reports to 3 decimal digits.

- Drop Python 2.6 support.
Expand Down
15 changes: 13 additions & 2 deletions src/zc/zservertracelog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""
__docformat__ = "reStructuredText"

import datetime
import doctest
import os
import re
Expand All @@ -26,7 +27,9 @@
import manuel.testing
import zope.testing.renormalizing

from zc.zservertracelog.fseek import FSeekTest
from zc.zservertracelog.fseek import FSeekTest # noqa
from zc.zservertracelog.tracereport import seconds_difference


here = os.path.dirname(os.path.abspath(__file__))

Expand Down Expand Up @@ -59,6 +62,14 @@ def __call__(self, environ, start_response):
return app(environ, start_response)


class TestHelpers(unittest.TestCase):

def test_seconds_difference(self):
dt1 = datetime.datetime(2019, 2, 23, 14, 5, 54, 451)
dt2 = dt1 + datetime.timedelta(minutes=15, seconds=3, microseconds=42)
self.assertEqual(seconds_difference(dt2, dt1), 15 * 60 + 3 + 0.000042)


def setUp(test):
test.globs['FauxApplication'] = FauxApplication

Expand All @@ -79,5 +90,5 @@ def test_suite():
'tracereport.rst',
checker=checker,
setUp=analysis_setUp),
unittest.makeSuite(FSeekTest),
unittest.defaultTestLoader.loadTestsFromName(__name__),
])
3 changes: 1 addition & 2 deletions src/zc/zservertracelog/tracereport.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ def __add__(self, other):

def seconds_difference(dt1, dt2):
delta = dt1 - dt2
micros = float('0.' + str(delta.microseconds)) # XXX: this is Wrong!
return delta.seconds + micros
return delta.seconds + delta.microseconds * 1e-6


def parse_line(line):
Expand Down
Loading

0 comments on commit 50dd591

Please sign in to comment.