Skip to content

Timezone conversion issue #543

Closed
Closed
@wmertens

Description

@wmertens

I adapted @chaoran's code from #538 to show an issue with manual escaping:

var mysql = require('mysql')
  , assert = require('assert');

var conn= mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

conn.connect();

conn.query(
  'CREATE TABLE IF NOT EXISTS datetime_bug (id SERIAL, created_at DATETIME)', 
  function(err) {
    if (err) throw err;

    // Make sure milliseconds aren't an issue
    var date = new Date(Math.floor(Date.now()/1000)*1000);

    conn.query('INSERT INTO datetime_bug SET created_at = '+mysql.escape(date), function(err, result) {
      if (err) throw err;

      var id = result.insertId;

      conn.query('SELECT * FROM datetime_bug WHERE ?', { id: id }, function(err, row) {
        if (err) throw err;
        conn.end();
        assert.equal(date, row[0].created_at);
      });
    });
  }
);

this will throw things like AssertionError: "2013-07-12T07:39:55.000Z" == "2013-07-12T05:39:55.000Z" unless you're living in UTC.

To fix this, you need to call escape(date, true, 'local'). The issue is that the escaping converts to UTC but removes timezone information and mysql thinks that the date is a local date.

Maybe the escape function should always convert dates to a CONVERT_TZ() call so there is never an issue no matter what the local and remote time zones.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions