Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tornado_mysql: round to the nearest full microsecond #2

Merged
merged 1 commit into from
Apr 12, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tornado_mysql/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def convert_datetime(obj):
usecs = '0'
if '.' in hms:
hms, usecs = hms.split('.')
usecs = float('0.' + usecs) * 1e6
usecs = round(float('0.' + usecs) * 1e6)
Copy link

@dangnvang dangnvang Apr 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't checked yet, but is usecs a whole number microseconds (before the round bit)? If so, seems like it could be simpler to just use that directly (and/or convert it to int from string)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may be right. I haven't checked either!

I just wanted to leave the smallest footprint that passes for all million possible microsecond test cases.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dangnvang

i think that any of the changes should be fine, i added a new scenario and it looks ok as well 🙂

This test code:

new2_errors = 0

for expected in range(1000000):
    usecs_string2 = round(float('0.' + str(expected).zfill(6)) * 1e6)

    if usecs_string2 != expected:
        new2_errors += 1

print 'new2_errors: ' + str(new2_errors)

Throws this output

new2_errors: 0

So i think both cases are ok, what do you think?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agustinavarela:
I was thinking more like usecs = int(usecs) instead of usecs = round(float('0.' + usecs) * 1e6)

Looking at the rest of the code, it looks like this is the conversion from MySQL datetimes to Python. The MySQL datetime format, when returned with microseconds (microseconds is the only fractional second supported it looks like), always has 6 numbers after the decimal point, so I think we're safe to go with using the more simple int(usec) conversion method.

Regardless of which one we go with, this change should be tested with an actual DB where you write a datetime and read it back, ensuring that the microseconds portion of it is correctly converted in and out of MySQL.

Copy link

@dangnvang dangnvang Apr 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I did some testing and determined that the MySQL documentation is a little misleading in some places.

In actuality, datetimes do have a fractional precision that you can specify for the number of decimal places to use. Thus, with testing actual use of the library, the int(usec) approach will not work because it's not always 6 characters as I previously inferred from other parts of the documentation.

Keeping it as round(float('0.' + usecs) * 1e6) should be ok!

Sorry for the confusion!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dangnvang thanks so much for checking that! :)

return datetime.datetime(*[ int(x) for x in ymd.split('-')+hms.split(':')+[usecs] ])
except ValueError:
return convert_date(obj)
Expand Down