jeremydaly / data-api-client

A "DocumentClient" for the Amazon Aurora Serverless Data API
MIT License
446 stars 63 forks source link

datetime(3) fields sometimes parsed UTC, sometimes LocalTime. #110

Closed joshlang closed 2 years ago

joshlang commented 3 years ago

In the database, a table has this column: Blarg datetime(3) default current_timestamp(3)

So, select Blarg from atable yields a result like these:

It's a nasty bug, happening approx 1 in 10 times - because 320 milliseconds is .320, and the trailing insignificant 0 is removed.

The bug in data-api-client is in this code:

// Converts the string value to a Date object.
// If standard TIMESTAMP format (YYYY-MM-DD[ HH:MM:SS[.FFF]]) without TZ + treatAsLocalDate=false then assume UTC Date
// In all other cases convert value to datetime as-is (also values with TZ info)
const formatFromTimeStamp = (value,treatAsLocalDate) =>
  !treatAsLocalDate && /^\d{4}-\d{2}-\d{2}(\s\d{2}:\d{2}:\d{2}(\.\d{3})?)?$/.test(value) ?
    new Date(value + 'Z') :
    new Date(value)

Personally, I'd probably change that last \d{3} to \d+ to fix the problem

Also: datetime(6) would almost always be treated incorrectly, and the above regex change would fix it too.