﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
32699	Comparisons using TruncTime are unreliable in MySQL	Alex Hill	Alex Hill	"To reproduce:


{{{
class DTModel(models.Model):
    dt = models.DateTimeField()

obj = DTModel.objects.create(dt=datetime(2021, 4, 30, 10, 30))

DTModel.objects.filter(pk=obj.pk, dt__time=time(10, 30)).count()  # should be 1 but returns 0
}}}

TruncTime is implemented in MySQL using the `TIME()` function. When we pass time values to MySQL, we return `str(value)` from `adapt_timefield_value` which, when the microseconds field of the value is zero, yields a string in ""HH:MM:SS"" format.

And it looks like comparing time values with strings is problematic and its success depends on including or not including the microseconds value.

* `SELECT TIME('10:30:00.000000') = '10:30:00'` returns 0
* `SELECT TIME('10:30:00.000000') = '10:30:00.000000'` returns 1

But if you make a TIME column and insert '10:30:00.000000' into it, then `SELECT ... WHERE TIME(dt) = '10:30:00.000000'` won't find the row. It's a mess.

The problem I'm seeing can be fixed by returning a time string including the microseconds component from `adapt_timefield_value`, but I wouldn't be surprised if that breaks something else given the above. What does appear to work in all cases is passing the value to TIME() or preceding it with the TIME keyword.

This affects all current versions of Django. I'll open a PR once I have a ticket number :)"	Bug	closed	Uncategorized	3.2	Normal	fixed			Ready for checkin	1	0	0	0	0	0
