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.

Cannot store/retrieve UTC dates in sqlite

See original GitHub issue

Version: 2.1.19

When retrieving a Date previously stored in an SQLite database, the date comes back with an offset tacked on, presumably based on the local timezone:

var now = new Date();
things.create({ id: 1, someDate: new Date() });

things.get(1, function(err, thing) {
  var diff = now.getTime() - thing.someDate.getTime();
  // expected: 0, actual: 25200000 (7 hours)
});

I saw in the docs that some of the database engines accept a timestamp option, but SQlite is not one of those engines. Indeed, adding a ?timezone=Z did not remedy the situation.

Is there a way to treat dates as UTC using SQLite? Better yet, is there any way to treat all dates as UTC (as all dates should always be) for all database engines? I realize that as a workaround I can use string dates or make the column numeric and store just the timestamp, but it would be super convenient to just use Date objects and not worry about it. I would expect the same Date I stored would be the same Date coming back, not a new Date with an offset applied.

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
apococommented, Oct 28, 2014

I think I tracked down the problem to the valueToProperty function. Here’s my findings:

  • The timezone option in the query string is being passed to the driver; I’m using ?timezone=Z.
  • Dates are being stored as strings.
  • The date strings have the correct, expected value, and are expressed in UTC. Example: 2014-10-28T12:11:36.938Z.

valueToProperty then messes up this value. It does the following sequence:

  1. value = new Date(value) = good, gives the expected Date.
  2. value.setTime(value.getTime() - (value.getTimezoneOffset() * 60000)); = wrong; injects an offset into a date that is already correct.
  3. value.setTime(value.getTime() - (tz * 60000)); = does nothing in my case, since my tz is 0.

It looks like the intent for this logic is to be the inverse of the sql-query dateToString method, but it’s not quite right.

It seems to me that this could all be greatly simplified:

  1. When storing a Date, store the result of value.toISOString().
  2. When retrieving a Date, do a new Date(value).

This way, there is no need to worry about local timezones at all. What are your thoughts?

0reactions
erikreppencommented, Jan 28, 2015

@dxg UTC with timezone or without should make no difference to the JS Date object as long as there were no shenanigans like the ones described by apoco and there’s nothing in the server environment causing confusion about local time when the date object set in the first place.I agree though. Why have it there by default. As raw UTC will help more devs learn how to handle timestamps properly by handling them less outside of the client.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to store datetime in SQLite - Stack Overflow
I cannot envision a scenario in which string comparisons would be faster ... Store UTC date time as eight byte 64 bit integer...
Read more >
How to retrieve dates as UTC in SQLite - Thomas Levesque
As you can see, after reading it from the database, the date is no longer in UTC, and doesn't even contain an indication...
Read more >
Make a time-range query faster when "GROUP BY day" - SQLite
The problem is: the query is rather slow because: ... is not) then I would pre-compute the group table of localtime dates and...
Read more >
SQLite Date & Time - How To Handle Date and Time in SQLite
To insert date and time values into the datetime_text table, you use the DATETIME function. For example, to get the current UTC date...
Read more >
flutter sqflite orm - Sql
The only dependency - SQLiteInstead, the built-in Date And Time Functions of ... in SQLite; Part 2: Enable Sqflite in Flutter. mysql select...
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