I pass '2012-09-10 00:00:00-05:00' to the 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)
MySQL query is, after todaystart been replaced by:
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 request in code, it will give this error: Warning: Truncated incorrect datetime value: '2012-09-09 00:00:00-05:00'
The code I use is this (in 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 do not see the format offset in the MySQL docs for str_to_date. I would prefer to keep this offset, since the data is returned for a third-party system and, keeping it in place, I do not need to do any logic between this return and the execution of the query with the returned date. However, I donโt think this is due to the bias, since it works if I run it directly.
What did I do wrong to cause this Warning appear when Python launches a query to MySQL?
Andy source share