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 issueHey,
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:
- Created 7 years ago
- Comments:17 (11 by maintainers)
just for the reference setting custom type parsing with mysql is done like:
https://github.com/mysqljs/mysql#type-casting
My mistake instead of
i passed the
timezone
option flat: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.