Skip to content

Instantly share code, notes, and snippets.

@clodal
Last active July 23, 2018 07:12
Show Gist options
  • Save clodal/c98419dd09f9054babdc4c133fce85d9 to your computer and use it in GitHub Desktop.
Save clodal/c98419dd09f9054babdc4c133fce85d9 to your computer and use it in GitHub Desktop.
Sequelize + Postgres DateTime UTC Behaviour

Sequelize v4 + Postgres DateTime UTC Behaviour

19 July 2018

  • Sequelize will save datetime with timezone in postgres.
  • Sequelize will always save datetime in UTC.
  • Sequelize will retrieve datetime based on server timezone settings.
  • Zeit Now.sh sfo1 servers are in UTC.
  • Amazon RDS us-west-1 database instances are in UTC.
  • When writing datetime to the database, sequelize will automatically convert the incoming client datetime to UTC, based on the timezone specified in sequelize.options.timezone.
  • When reading datetime from the database, sequelize will return datetime in UTC. The server or client is expected to be responsible for formatting this datetime back to the user's locale.
  • When sending emails from the server, ensure that if any datetime fields are in the email, they should be converted to the email recipient's timezone at email definition time. This is because email clients will not know to detect user locale since javascript is disabled in email spec. There is no way to show time in user timezone in an email without having information about his timezone in backend.

Update

Found a good reference https://blog.theodo.fr/2018/01/tips-tricks-date-handling-moment-js/ confirming some of the pointers in the findings above.

Recommendation

  • Postgres database, Node server, and Sequelize options.timezone should be in UTC
  • Send and return datetime from server to client in UTC, vice versa
  • Client should be timezone aware; set via environment variables. This allows for multiple clients with different timezones to work with a single server. Which is the usual recommended setup for a multi-country site.
  • Server needs to be timezone aware when sending emails. In this case, either deploy multiple servers with different timezone env vars or better, send timezone information to server when posting data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment