MySQL doesn't accept timezone offsets in datetime literals

Issue #3136 closed
Will Ross created an issue

Datetime literals with a timezone offset ("+0000" or "-0500") are invalid in MySQL. The issue is more visible with MySQL 5.6 because STRICT_TRANS_TABLES was added to the default configuration, causing statements that worked in older versions to fail. If no tzinfo is given, the datetime works fine. I'd have submitted a patch for this, but I'm not sure where to look in the code. That and I'm not sure it's even an actual bug, or intended behavior for backends that don't support timezones being passed timezone aware objects.

Comments (2)

  1. Mike Bayer repo owner

    yeah sorry, this is kind of out of our wheelhouse. MySQL doesn't have a timezone-aware type in any case: http://dev.mysql.com/doc/refman/5.5/en/datetime.html

    note our timezone docs:

    DateTime

    If True, and supported by the backend, will produce ‘TIMESTAMP WITH TIMEZONE’. For backends that don’t support timezone aware timestamps, has no effect.

    below is a script illustrating the plain MySQLdb equivalent, the call with the timezone fails:

    import MySQLdb
    import datetime as dt
    
    conn = MySQLdb.connect(
        user="scott", passwd="tiger",
        host="localhost", db="test")
    
    cursor = conn.cursor()
    
    try:
        cursor.execute("drop table timestamps")
    except:
        pass
    
    
    class UTC(dt.tzinfo):
    
        def utcoffset(self, obj):
            return dt.timedelta()
    
        def dst(self, obj):
            return dt.timedelta()
    
        def tzname(self, obj):
            return u'UTC+00:00'
    
    utc = UTC()
    cursor.execute("""
        create table timestamps (
            id integer primary key AUTO_INCREMENT,
            data DATETIME
        )
    """)
    
    
    # works
    cursor.execute("insert into timestamps (data) values (%s)", dt.datetime.utcnow())
    
    
    # fails
    cursor.execute("insert into timestamps (data) values (%s)", dt.datetime.now(utc))
    
  2. Log in to comment