Search code examples
javascriptdatabasedatabase-designdst

Daylight saving time abolition - am I saving datetimes correctly?


I'm currently working on a PHP/mySQL web app where we store dates as unix timestamps in UNSIGNED INT(10) columns. Whenever we need to display dates in the web view we take that number and parse it with moment.js.

While one of my colleagues had doubts about this way of solving the task (he preferred storing dates as "YYYY-MM-DD hh:mm:ss" VARCHARs), so far we had zero problems.

I recently read that the european union is moving forward about abolishing daylight saving times. Will this affect my webapp in any way based on the particular way I'm storing dates?


Solution

  • I'm gonna come out and say no it shouldn't matter. I'm no expert, but I store dates as UNIX timestamps as well, and if you're generating your 10-digit timestamps something like:

    getSecondsSinceEpoch = ((date) => Math.floor(date.getTime()/1000));
    

    (or anything relying on Date.prototype.getTime()), then you're storing a universal date that doesn't care about timezones, which is good because if the user logs in from a different timezone, you're not returning varchars that are based on where they used to be. The same applies to when Daylight Savings Time changes the clock in their timezone (or fails to do so after being abolished.)

    To get the correct human-readable string for the user's current location, Javascript relies on a timezoneOffset, which is not part of the date object but instead answers the question "how many timezones away from London is this browser right now?" (except in minutes instead of hours, and after accounting for Daylight Savings Time.) Presumably, the user's device will have become aware of the change, as will their browser, and your script will be able to act accordingly. (If their device is using the wrong timezone, your app won't be the only one that misbehaves until they get it sorted out.)

    As I said, I'm not an expert on this stuff, so it's possible that I've convinced myself of something that's not true about the way that Javascript handles dates, but as far as my current understanding goes, I think your solution should be fine. I hope if I'm mistaken, some other nerd will chime in forthwith and let us both know.