Skip to content

sql_types.keep_datetime Reduces Precision of DateTimeOffset #2584

Closed
@dennismsmith

Description

@dennismsmith

In NHibernate 5.x, when using the sql_types.keep_datetime option for SQL Server in order to use the DATETIME rather than DATETIME2 type, it has the side effect of reducing the precision of any DATETIMEOFFSET type. In particular, you only get two digits of sub-second precision. In NHibernate 4.x, the DATETIMEOFFSET type got the full seven digits of precision.

The documentation for sql_types.keep_datetime doesn't mention DATETIMEOFFSET at all, so I would not have expected it to modify the behaviour of DATETIMEOFFSET:

Since NHibernate v5.0 and if the dialect supports it, DbType.DateTime2 is used instead of DbType.DateTime. This may be disabled by setting sql_types.keep_datetime to true. Defaults to false.

My database has a combination of DATETIME and DATETIMEOFFSET columns in it, which is why I need to use sql_types.keep_datetime.

This can be tested with the following:

configuration.SetProperty(Environment.SqlTypesKeepDateTime, "true");
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
	DateTimeOffsetType type = new DateTimeOffsetType();
	DateTimeOffset value = (DateTimeOffset) type.Seed(s.GetSessionImplementation());

	string stringRepresentation = value.ToString("o");
	string fractionalSeconds = stringRepresentation.Substring(stringRepresentation.IndexOf('.') + 1, 7);
	Assert.That(fractionalSeconds, Does.Not.EndWith("00000"));
}

If you don't have configuration.SetProperty(Environment.SqlTypesKeepDateTime, "true"); set, it will consistently pass.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions