question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mysql: store UTC, but when fetch its wrapped into JS local Date (allow user override how values returned by driver are converted to javascript values)

See original GitHub issue

Hey,

i’m using the knex option timezone: UTC and i store UTC dates in the database in the format YYYY-MM-DD HH:mm:ss (this is the only format i can use, because i use type datetime). Everything is fine with storing the values.

My server is running in +02:00. When i fetch the datetime fields from the database, the UTC dates are wrapped into JS native Date object, which is in local TZ.

Example: stored value: 2016-05-05 10:00:00 (which is UTC) fetched value: Tue May 05 2016 10:00:00 GMT+0200 (CEST) (type Date)

But this is wrong representation. For postgres and sqlite i don’t have these problems. Postgres stores the values with offset (+00 or +02) and sqlite returns the dates as UTC strings if i want, which is fine.

This issue is maybe similar to this: tgriesser/knex#524

I saw that some people using knex timestamp type, but i assume it will not help fixing the issue.

Is there an option to tell Knex to not transform the stored values into a native JS Date? Or is there a hack? Thank you!

Knex Version: 0.10.0 Bookshelf Version: 0.9.5 Node Version: v4.4.2

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:17 (11 by maintainers)

github_iconTop GitHub Comments

22reactions
elhigucommented, Jul 24, 2017

just for the reference setting custom type parsing with mysql is done like:

var moment = require('moment');
var connection = require('knex')({
        client: 'mysql',
        connection: {
            host: db.host,
            user: db.user,
            password: db.password,
            database: db.database,
            timezone: 'UTC',
            typeCast: function (field, next) {
              if (field.type == 'DATETIME') {
                return moment(field.string()).format('YYYY-MM-DD HH:mm:ss');
              }
              return next();
            }
        }
   });

https://github.com/mysqljs/mysql#type-casting

7reactions
kirrg001commented, Sep 5, 2016

My mistake instead of

knex({ connection: { timezone: 'UTC'} })

i passed the timezone option flat:

knex({ timezone: 'UTC', connection: { ... } })

So now the fetched value is correct: Tue May 05 2016 12:00:00 GMT+0200 (CEST) But still you can leave it as a feature request. When i configure knex to TZ UTC, it should maybe not return a local JS Date.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NodeJS responded MySQL timezone is different when I fetch ...
Setting this timezone configuration means that every Date (Javascript Date object) will be translated TO utc when sent to database and FROM utc...
Read more >
Support for Date-Time Types in Connector/J 8.0 - MySQL
Incoming values are converted by server from the session time zone to UTC for storage, and outgoing values are converted from UTC to...
Read more >
Using the Data API for Aurora Serverless v1
The API provides a more secure way to use AWS Lambda. It enables you to access your DB cluster without your needing to...
Read more >
IO tools (text, CSV, HDF5, …) — pandas 1.5.2 documentation
Note that the entire file is read into a single DataFrame regardless, use the chunksize or iterator parameter to return the data in...
Read more >
Databases | Django documentation
Django needs the following parameters for its database connections: ... both MySQL and Django will attempt to convert the values from UTC to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found