Skip to content

MSSQL does not respect timezone setting when parsing dates #5403

Closed

Description

@ggrimbert

Hi,

I am experiencing an issue with dates.

I use the following declaration for my table

  var p_usrnot = sequelize.define("usrnot", {
    usrnotincde : {
      type : DataTypes.INTEGER,
      allowNull : true,
      primaryKey : true,
      autoIncrement : true
    },
    usrincde : {
      type : DataTypes.INTEGER,
      allowNull : false,
      defaultValue : "0"
    },
    subjectdsc : {
      type : DataTypes.STRING,
      allowNull : true
    },
    msgdsc : {
      type : DataTypes.STRING,
      allowNull : true
    },
    replydsc : {
      type : DataTypes.STRING,
      allowNull : true
    },
    notnatincde : {
      type : DataTypes.INTEGER,
      allowNull : false,
      defaultValue : "1"
    },
    usrnoticon : {
      type : DataTypes.STRING,
      allowNull : true
    },
    usrnotcolor : {
      type : DataTypes.STRING,
      allowNull : true
    },
    ackpnt:{
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue : true
    },
    ackreadpnt:{
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue : true
    },
    rcdreadhrd : {
      type : DataTypes.DATE,
      allowNull : true
    },
    rcdreceivehrd : {
      type : DataTypes.DATE,
      allowNull : true
    },
    rcdcreatehrd : {
      type : DataTypes.DATE,
      allowNull : true
    },
    operctcincde : {
      type : DataTypes.INTEGER,
      allowNull : false
    }

I have configured the timezone property with +01:00
I tried to set defaultValue: sequelize.Sequelize.NOW to my rcdcreatehrd but at each time I try to insert a date, it is setted like this : 2016-02-11 17:54:37.0930000 +00:00 and so when I try to make a new Date of this, I get :Thu Feb 11 2016 18:54:37 GMT+0100 (CET), so my date is one hour later than the real hour

I tried removing the timezone, send new Date().toJSON(), new Date().toUTCString()...

What can I do ?

I am using mssql with DATETIMEOFFSET (but tried DATETIME2 too without success).

Thanks

Activity

  1. janmeier commented on Feb 11, 2016

    @janmeier
    Member

    2016-02-11 17:54:37.0930000 +00:00 and Thu Feb 11 2016 18:54:37 GMT+0100 (CET) represent the same point in time.

    I'm not quite sure what your question is - can you give a more concrete example - some code that tries the insert, the accompanying SQL, and what you think should happen?

  2. ggrimbert commented on Feb 12, 2016

    @ggrimbert
    Author

    I have the table as defined in my precedent post, I try to insert a new entry into this table by sequelize. For my dates I use, client side new Date() that gives me : Fri Feb 12 2016 08:33:05 GMT+0100 (CET).

    When sequelize inserts with that date, it's transformed to Fri Feb 12 2016 08:33:05 GMT+0000 (CET). So when I recover my date, on client side, using sequelize findAll, the date returned is Fri Feb 12 2016 08:33:05 GMT+0000, but I need to work with that date so, I create a new date object from this, and get Fri Feb 12 2016 09:33:05 GMT+0100, the date is one hour later than the real one.

    To recap : client side date is valid and with the timestamp, when put on DB, the timezone disappear, and when I recover my date from DB, client side adds an hour AND the timezone, so the date is one hour later.

    On client side, i use new Date()
    Here's the sql that sequelize produces : INSERT INTO p_usrnot OUTPUT INSERTED.* VALUES (N'352',N'fdsds',N'sfdf',N'',N'-1',N'taxi',N'red',0,1,N'2016-02-12 08:40:27.911',N'10001062',N'-1');

    As said previously, the timezone parameter of sequelize is setted to +01:00, and I tried without setting this parameter, but the same problem is still appearing.

    I think that sequelize should write the timezone to DB as the mssql timestamp method do.

  3. janmeier commented on Feb 12, 2016

    @janmeier
    Member

    The problem is that datetime in mssql does not support specifying the timezone, see https://msdn.microsoft.com/en-us/library/bb677335.aspx

    The insert looks correct - what sequelize actually inserts is a date without timezone, but sequelize knows that it is in CET, and should parse it correctly when fetching again.

    I just tested this, and it does indeed seem that the mssql dialect does not parse the date with the correct timezone

  4. added
    type: bug
    dialect: mssql
    on Feb 12, 2016
  5. changed the title [-]Insert new dates [/-] [+]MSSQL does not respect timezone setting when parsing dates[/+] on Feb 12, 2016
  6. ggrimbert commented on Feb 12, 2016

    @ggrimbert
    Author

    Not sure of understanding where the problem comes from, wherever I don't use datetime2 but datetimeoffset (https://msdn.microsoft.com/en-us/library/bb630289.aspx), this one know the timezone, and is visible when I query my table.

    select SYSDATETIMEOFFSET() which returns 2016-02-12 09:23:40.7895747 +01:00.

    I guess that there is no support for this type in sequelize. Do you think it could be long to support this ?

  7. janmeier commented on Feb 12, 2016

    @janmeier
    Member

    You could override the Date.stringify method

    DATE.prototype.$stringify = function (date, options) {
    to insert the timezone

  8. ggrimbert commented on Feb 12, 2016

    @ggrimbert
    Author

    I found a solution, I don't use DATETIMEOFFSET but DATETIME2, and I don't specify the timezone to sequelize. By doing this, the date is stored without timezone, and javascript parses the date correctly.

    Do you think this method is viable ?

  9. janmeier commented on Feb 12, 2016

    @janmeier
    Member

    If you are only ever going to have your servers in the same timezone, then yes :)

    But in any case, this is definitely a bug that needs fixing

  10. added this to the 4.0 milestone on Feb 21, 2016
  11. janmeier commented on Feb 21, 2016

    @janmeier
    Member

    We should change mssql to use datetimeoffset by default when creating tables in 4.0

    The parsing issue cannot be solved in sequelize, we need to contact tedious maintainers about that

  12. ggrimbert commented on Feb 22, 2016

    @ggrimbert
    Author

    Not sure that this type should be used by default, it could be a problem with tables that uses DATETIME2.
    Maybe you can create a new datatype in sequelize, and make this available for all database types, not only MSSQL.

  13. janmeier commented on Feb 22, 2016

    @janmeier
    Member

    @ggrimbert What should be special about this new datatype? DATE in sequelize is already supposed to respect the timezone setting, its only mssql which doesn't.

    Changing to datetimeoffset for tables generated by sequelize wil at least fix the problem going forward, and it will still work with datetime2 (albeit without timezone support)

  14. ggrimbert commented on Feb 23, 2016

    @ggrimbert
    Author

    Sorry, I didn't know that's going to work with datetime2.

  15. brunoventura commented on Dec 7, 2016

    @brunoventura

    After some hours debugging sequelize I've found a solution to problem with timezone:

    options.timezone = "America/Sao_Paulo"
    
    db.connectionManager.$refreshTypeParser({
        types: {
            mssql: [111]
        },
        parse: value => moment(value).tz('UTC').format('YYYY-MM-DD HH:mm:ss.SSS')
    });
    

    Any regrets with this solution?

  16. felixfbecker commented on Dec 7, 2016

    @felixfbecker
    Contributor

    It is using private API

  17. brunoventura commented on Dec 7, 2016

    @brunoventura

    ¯\_(ツ)_/¯ I'm using sequelize": "^3.27.0, there's a public API to achieve this behavior? (I tried to find without success)

    https://github.com/sequelize/sequelize/blob/v3.27.0/lib/dialects/mssql/connection-manager.js#L30

  18. felixfbecker commented on Dec 7, 2016

    @felixfbecker
    Contributor

    What is the options object you are using?
    If you have a fix, just provide a PR

  19. brunoventura commented on Dec 7, 2016

    @brunoventura

    the options sent in Sequelize constructor:

    "options" : {
        "dialect": "mssql",
        "timezone": "America/Sao_Paulo",
        "define": {
            "omitNull": false,
            "timestamps": false
        },
        "dialectOptions": {
            "requestTimeout": 20000
        }
    }
    

    The only thing to do here is expose this method to public API, but in v4.0 it's already been done (for some connectors, not all)

  20. felixfbecker commented on Dec 7, 2016

    @felixfbecker
    Contributor

    No, if there is a bug in the type parser, it should be fixed in data-types.js, not by calling $refreshTypeParsers() later on from user land code.

  21. janmeier commented on Dec 13, 2016

    @janmeier
    Member
  22. pibone commented on Jan 24, 2017

    @pibone

    For the sake of fixing this malefic bug, some concerns I have:

    • It's only necessary to add the parse function as in mysql data type?
    • Testing?
  23. zhupengfeivip commented on Apr 23, 2018

    @zhupengfeivip

    We use mariadb10.0.3, without datatime2 and datetimeoffset data type, how to solve it?Is this not a fix for sequelize?I see this property, does it work?
    DialectOptions: {
    UseUTC: true, // ->Add this line. For reading from database.
    },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dialect: mssqltype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions