4

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?

3
  • 4
    mysql's date/time format doesn't include timezones. You haven't included that -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. Commented Sep 10, 2012 at 15:54
  • 1
    In order to keep the timezone information in MySQL, convert all datetime objects to UTC before storing them. The mysql command line client doesn't show warnings directly when they occur, it merely hints them (Query OK, 1 row affected, 1 warning (0.00 sec)). Other clients might hide them completely. Commented Sep 10, 2012 at 18:44
  • and this should remove the time zone info: dt = dt_tz.replace(tzinfo=None) Commented Feb 26, 2013 at 14:57

1 Answer 1

0

Marc B had it right.

When executed directly, you are getting the warnings, but probably not noticing:

mysql> 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);
Empty set, 3 warnings (0.00 sec)

mysql> show warnings;
+---------+------+----------------------------------------------------------------------+
| Level   | Code | Message                                                              |
+---------+------+----------------------------------------------------------------------+
| Warning | 1292 | Truncated incorrect datetime value: '2012-09-10 00:00:00-05:00'      |
| Warning | 1292 | Truncated incorrect datetime value: '2012-09-10 00:00:00-05:00'      |
| Warning | 1292 | Incorrect datetime value: '1347148800' for column 'created' at row 1 |
+---------+------+----------------------------------------------------------------------+
3 rows in set (0.00 sec)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.