I am passing '2012-09-10 00:00:00-05:00'
to a MySQL query. This value is retrieved using the pytz module for Python.
import pytz
class MyClass():
def __init____(self):
self.tz = pytz.timezone(settings.TIME_ZONE)
todaystart = self.tz.localize(datetime.now(self.tz).replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=None), is_dst=None).astimezone(self.tz)
The MySQL query is this, after todaystart
has been substituted in:
SELECT * FROM mytable WHERE created > UNIX_TIMESTAMP(STR_TO_DATE('2012-09-10 00:00:00-05:00','%Y-%m-%d %k:%i:%s') - INTERVAL 1 DAY);
If I execute this query directly, it returns data as expected. If I put this query into code it gives this error: Warning: Truncated incorrect datetime value: '2012-09-09 00:00:00-05:00'
The code I am using is this (within Django):
query = """SELECT * FROM mytable WHERE created > UNIX_TIMESTAMP(STR_TO_DATE('2012-09-10 00:00:00-05:00','%Y-%m-%d %k:%i:%s') - INTERVAL 1 DAY);"""
myCursor = connections[system_db].cursor()
results = myCursor.execute(query) # Dies on this statement
resultcount = results.fetchall()
I didn't see an offset format string in the MySQL docs for str_to_date. I would prefer to keep that offset, as the data is returned for a 3rd party system and by keeping it in place I don't have to do any logic between that return and executing the query with the returned date. However, I don't think it has to do with the offset since it works if I run it directly.
What have I done incorrectly that would cause this Warning
to appear when Python runs the query against MySQL?
-05:00
in the format string in str_to_date, so you get a warning that the string is truncated. warnings aren't an error, so it should work fine in your clients.Query OK, 1 row affected, 1 warning (0.00 sec)
). Other clients might hide them completely.