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.

Library is converting my datetime string to UTC

See original GitHub issue

I’m working on a DB that was converted from MySQL to Postgres using a migration script. While converting the database interactions from mysql to node-postgres, I noticed that my test SQL queries were not being stored correctly - all my datetimes that were being input as ISO-8601 formatted strings with an explicit UTC flag (“YYYY-MM-DDThh:mm:ss.sssZ”) were getting treated as though they were local timestamps and being implicitly converted to the “correct” UTC timezone.

There doesn’t appear to be any official way to override this behavior, and as such it is seriously getting in the way of my conversion. I tried changing the datetime strings to signify UTC using the timezone-offset format (“YYYY-MM-DDThh:mm:ss.sss+00:00”), but it made no difference.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
noinklingcommented, Jul 10, 2019

To summarize and hopefully clear up any confusion:

When you supply a JS Date object as an input value, the library converts it to a string literal that Postgres can understand. By default, this literal will be in local time, with the appropriate timezone offset, rather than UTC.

This is fine for timestamp with time zone a.k.a. timestamptz because you end up with the same thing stored either way (barring a rare edge case that has to do with the precision of JS’s .getTimezoneOffset()), and parsing back to a Date object doesn’t require any assumptions.

But the timestamp and date types just ignore the offset part on input, there’s no automatic conversion to UTC first, so the value ends up being in local time. Since this is usually not what you want, you can fix it by doing:

pg.defaults.parseInputDatesAsUTC = true;

// In pg-promise:
pgp.pg.defaults.parseInputDatesAsUTC = true;

// Note that supplying it as part of the connection options won't work,
// even though it probably should

…or by calling .toISOString() like @tim-phillips says (just beware that this method may have issues for dates far in the past/future since the format isn’t 100% compatible with Postgres).

The caveat with this is that the output parsing done by postgres-date also assumes local time for timestamp and date types, so those output parsers need to be overridden (as others have shown) if you value your sanity and want roundtrips to succeed.

As for the default behaviour being changed, #783 seems to indicate that this is unlikely (it may not be worth the breaking change in any case).

4reactions
stasandkcommented, Nov 4, 2018
'use strict'

const { Pool, types } = require('pg')
const moment = require('moment')

// Force conversion to UTC using moment
types.setTypeParser(1114, str => moment.utc(str).format())

// Disable automatic date parsing by node-postgres and parse dates in your application
// types.setTypeParser(1114, str => str)

const pool = new Pool()

pool.on('error', (err) => {
  console.error('Unexpected error on idle client', err)
  process.exit(-1)
})

module.exports = pool

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert local time string to UTC? - Stack Overflow
First, parse the string into a naive datetime object. This is an instance of datetime.datetime with no attached timezone information. See its documentation....
Read more >
datetime — Basic date and time types — Python 3.11.1 ...
These tzinfo objects capture information about the offset from UTC time, the time zone name, and whether daylight saving time is in effect....
Read more >
Date.UTC() - JavaScript - MDN Web Docs
The Date.UTC() method accepts parameters similar to the Date constructor, but treats them as UTC. It returns the number of milliseconds ...
Read more >
Converting Strings to datetime in Python - Stack Abuse
In this tutorial, we'll be converting Strings to datetime in Python ... to store the time in your database as UTC format and...
Read more >
Demystifying DateTime Manipulation in JavaScript - Toptal
Date libraries help in many ways to make your life easier. ... Converting a string to a JavaScript date object is done in...
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