Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Issue #211: Ensure the DateTime is UTC #214

Merged
merged 2 commits into from

2 participants

@kojoru

This commit should allow EnsureUtc mode in SQLServerOrmLiteDialectProvider which will convert DateTime to UTC if it is DateTimeKind.Local before saving.
The returned DateTimes will get DateTimeKind.Utc so that occasional conversion to UTC will not corrupt it.

@mythz mythz merged commit f86b037 into from
@mythz
Owner

cool, thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 2, 2013
  1. @kojoru

    Issue #211: Ensure the DateTime is UTC

    kojoru authored
    For MS SQL Server
  2. @kojoru
This page is out of date. Refresh to see the latest.
View
40 src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs
@@ -99,6 +99,14 @@ public override object ConvertDbValue(object value, Type type)
return dateTimeValue - timeSpanOffset;
}
+ if (_ensureUtc && type == typeof (DateTime))
+ {
+ var result = base.ConvertDbValue(value, type);
+ if(result is DateTime)
+ return DateTime.SpecifyKind((DateTime)result, DateTimeKind.Utc);
+ return result;
+ }
+
if (type == typeof(byte[]))
return value;
@@ -111,7 +119,7 @@ public override object ConvertDbValue(object value, Type type)
}
- public override string GetQuotedValue(object value, Type fieldType)
+ public override string GetQuotedValue(object value, Type fieldType)
{
if (value == null) return "NULL";
@@ -122,7 +130,9 @@ public override string GetQuotedValue(object value, Type fieldType)
}
if (fieldType == typeof(DateTime))
{
- var dateValue = (DateTime)value;
+ var dateValue = (DateTime)value;
+ if (_ensureUtc && dateValue.Kind == DateTimeKind.Local)
+ dateValue = dateValue.ToUniversalTime();
const string iso8601Format = "yyyyMMdd HH:mm:ss.fff";
return base.GetQuotedValue(dateValue.ToString(iso8601Format), typeof(string));
}
@@ -143,16 +153,22 @@ public override string GetQuotedValue(object value, Type fieldType)
return base.GetQuotedValue(value, fieldType);
- }
-
- protected bool _useDateTime2;
- public void UseDatetime2(bool shouldUseDatetime2)
- {
- _useDateTime2 = shouldUseDatetime2;
- DateTimeColumnDefinition = shouldUseDatetime2 ? "datetime2" : "datetime";
- base.DbTypeMap.Set<DateTime>(shouldUseDatetime2 ? DbType.DateTime2 : DbType.DateTime, DateTimeColumnDefinition);
- base.DbTypeMap.Set<DateTime?>(shouldUseDatetime2 ? DbType.DateTime2 : DbType.DateTime, DateTimeColumnDefinition);
- }
+ }
+
+ protected bool _useDateTime2;
+ public void UseDatetime2(bool shouldUseDatetime2)
+ {
+ _useDateTime2 = shouldUseDatetime2;
+ DateTimeColumnDefinition = shouldUseDatetime2 ? "datetime2" : "datetime";
+ base.DbTypeMap.Set<DateTime>(shouldUseDatetime2 ? DbType.DateTime2 : DbType.DateTime, DateTimeColumnDefinition);
+ base.DbTypeMap.Set<DateTime?>(shouldUseDatetime2 ? DbType.DateTime2 : DbType.DateTime, DateTimeColumnDefinition);
+ }
+
+ protected bool _ensureUtc;
+ public void EnsureUtc(bool shouldEnsureUtc)
+ {
+ _ensureUtc = shouldEnsureUtc;
+ }
public override long GetLastInsertId(IDbCommand dbCmd)
{
View
52 src/ServiceStack.OrmLite.SqlServerTests/EnsureUtcTest.cs
@@ -0,0 +1,52 @@
+using System;
+using System.Data;
+using System.Linq;
+using NUnit.Framework;
+using ServiceStack.OrmLite.SqlServer;
+
+namespace ServiceStack.OrmLite.SqlServerTests
+{
+ internal class EnsureUtcTests : OrmLiteTestBase
+ {
+ [Test]
+ public void SaveDateTimeToDatabase()
+ {
+ var dbFactory = new OrmLiteConnectionFactory(base.ConnectionString, SqlServerOrmLiteDialectProvider.Instance);
+ SqlServerOrmLiteDialectProvider.Instance.EnsureUtc(true);
+
+ using (var db = dbFactory.OpenDbConnection())
+ {
+ var dateTime = new DateTime(2012, 1, 1, 1, 1, 1, DateTimeKind.Local);
+ var x = InsertAndSelectDateTime(db, dateTime);
+ Assert.AreEqual(DateTimeKind.Utc, x.Test.Kind);
+ Assert.AreEqual(x.Test.ToUniversalTime(), dateTime.ToUniversalTime());
+ Assert.AreEqual(x.Test.ToLocalTime(), dateTime.ToLocalTime());
+
+ dateTime = new DateTime(2012, 1, 1, 1, 1, 1, DateTimeKind.Utc);
+ x = InsertAndSelectDateTime(db, dateTime);
+ Assert.AreEqual(DateTimeKind.Utc, x.Test.Kind);
+ Assert.AreEqual(x.Test.ToUniversalTime(), dateTime.ToUniversalTime());
+ Assert.AreEqual(x.Test.ToLocalTime(), dateTime.ToLocalTime());
+
+ dateTime = new DateTime(2012, 1, 1, 1, 1, 1, DateTimeKind.Unspecified);
+ x = InsertAndSelectDateTime(db, dateTime);
+ Assert.AreEqual(DateTimeKind.Utc, x.Test.Kind);
+ Assert.AreEqual(x.Test.ToUniversalTime(), dateTime);
+ Assert.AreEqual(x.Test.ToLocalTime(), dateTime.ToLocalTime());
+ }
+ }
+
+ private static DateTimeObject InsertAndSelectDateTime(IDbConnection db, DateTime dateTime)
+ {
+ db.DropAndCreateTable<DateTimeObject>();
+ db.Insert(new DateTimeObject {Test = dateTime});
+ var x = db.Select<DateTimeObject>().First();
+ return x;
+ }
+
+ private class DateTimeObject
+ {
+ public DateTime Test { get; set; }
+ }
+ }
+}
View
201 src/ServiceStack.OrmLite.SqlServerTests/ServiceStack.OrmLite.SqlServerTests.csproj
@@ -1,107 +1,108 @@
-<?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
- <PropertyGroup>
- <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
- <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
- <ProductVersion>8.0.30703</ProductVersion>
- <SchemaVersion>2.0</SchemaVersion>
- <ProjectGuid>{96793C11-2A99-4217-8946-3E0DB9534A4D}</ProjectGuid>
- <OutputType>Library</OutputType>
- <AppDesignerFolder>Properties</AppDesignerFolder>
- <RootNamespace>ServiceStack.OrmLite.SqlServerTests</RootNamespace>
- <AssemblyName>ServiceStack.OrmLite.SqlServerTests</AssemblyName>
- <FileAlignment>512</FileAlignment>
- </PropertyGroup>
- <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
- <DebugSymbols>true</DebugSymbols>
- <DebugType>full</DebugType>
- <Optimize>false</Optimize>
- <OutputPath>bin\Debug\</OutputPath>
- <DefineConstants>DEBUG;TRACE</DefineConstants>
- <ErrorReport>prompt</ErrorReport>
- <WarningLevel>4</WarningLevel>
- </PropertyGroup>
- <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
- <DebugType>pdbonly</DebugType>
- <Optimize>true</Optimize>
- <OutputPath>bin\Release\</OutputPath>
- <DefineConstants>TRACE</DefineConstants>
- <ErrorReport>prompt</ErrorReport>
- <WarningLevel>4</WarningLevel>
- </PropertyGroup>
- <ItemGroup>
- <Reference Include="nunit.framework">
- <HintPath>..\..\lib\tests\nunit.framework.dll</HintPath>
- </Reference>
- <Reference Include="ServiceStack.Common">
- <HintPath>..\..\lib\ServiceStack.Common.dll</HintPath>
- </Reference>
- <Reference Include="ServiceStack.Interfaces">
- <HintPath>..\..\lib\ServiceStack.Interfaces.dll</HintPath>
- </Reference>
- <Reference Include="ServiceStack.Text">
- <HintPath>..\..\lib\ServiceStack.Text.dll</HintPath>
- </Reference>
- <Reference Include="System" />
- <Reference Include="System.ComponentModel.DataAnnotations" />
- <Reference Include="System.configuration" />
- <Reference Include="System.Core" />
- <Reference Include="System.Xml.Linq" />
- <Reference Include="System.Data.DataSetExtensions" />
- <Reference Include="Microsoft.CSharp" />
- <Reference Include="System.Data" />
- <Reference Include="System.Xml" />
- </ItemGroup>
- <ItemGroup>
- <Compile Include="Datetime2Tests.cs" />
- <Compile Include="InsertParam_GetLastInsertId.cs" />
- <Compile Include="NestedTransactions.cs" />
- <Compile Include="EnumTests.cs" />
- <Compile Include="Expressions\AdditiveExpressionsTest.cs" />
- <Compile Include="Expressions\Author.cs" />
- <Compile Include="Expressions\AuthorUseCase.cs" />
- <Compile Include="Expressions\ConditionalExpressionTest.cs" />
- <Compile Include="Expressions\EqualityExpressionsTest.cs" />
- <Compile Include="Expressions\ExpressionsTestBase.cs" />
- <Compile Include="Expressions\LogicalExpressionsTest.cs" />
- <Compile Include="Expressions\MultiplicativeExpressionsTest.cs" />
- <Compile Include="Expressions\OrmLiteCountTests.cs" />
- <Compile Include="Expressions\PrimaryExpressionsTest.cs" />
- <Compile Include="Expressions\RelationalExpressionsTest.cs" />
- <Compile Include="Expressions\StringFunctionTests.cs" />
- <Compile Include="Expressions\TestType.cs" />
- <Compile Include="Expressions\UnaryExpressionsTest.cs" />
- <Compile Include="ForeignKeyAttributeTests.cs" />
- <Compile Include="OrmLiteTestBase.cs" />
- <Compile Include="SqlServerExpressionVisitorQueryTest.cs" />
- <Compile Include="Properties\AssemblyInfo.cs" />
- <Compile Include="TypeWithByteArrayFieldTests.cs" />
- <Compile Include="UnicodeTests.cs" />
- <Compile Include="UpdateTests.cs" />
- <Compile Include="UseCase\SimpleUseCase.cs" />
- <Compile Include="UseCase\TestEntity.cs" />
- </ItemGroup>
- <ItemGroup>
- <ProjectReference Include="..\ServiceStack.OrmLite.SqlServer\ServiceStack.OrmLite.SqlServer.csproj">
- <Project>{1887DC99-9139-43E3-A7AA-6D74714B3A5D}</Project>
- <Name>ServiceStack.OrmLite.SqlServer</Name>
- </ProjectReference>
- <ProjectReference Include="..\ServiceStack.OrmLite\ServiceStack.OrmLite.csproj">
- <Project>{96179AC6-F6F1-40C3-9FDD-4F6582F54C5C}</Project>
- <Name>ServiceStack.OrmLite</Name>
- </ProjectReference>
- </ItemGroup>
- <ItemGroup>
- <None Include="app.config">
- <SubType>Designer</SubType>
- </None>
- </ItemGroup>
- <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{96793C11-2A99-4217-8946-3E0DB9534A4D}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>ServiceStack.OrmLite.SqlServerTests</RootNamespace>
+ <AssemblyName>ServiceStack.OrmLite.SqlServerTests</AssemblyName>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="nunit.framework">
+ <HintPath>..\..\lib\tests\nunit.framework.dll</HintPath>
+ </Reference>
+ <Reference Include="ServiceStack.Common">
+ <HintPath>..\..\lib\ServiceStack.Common.dll</HintPath>
+ </Reference>
+ <Reference Include="ServiceStack.Interfaces">
+ <HintPath>..\..\lib\ServiceStack.Interfaces.dll</HintPath>
+ </Reference>
+ <Reference Include="ServiceStack.Text">
+ <HintPath>..\..\lib\ServiceStack.Text.dll</HintPath>
+ </Reference>
+ <Reference Include="System" />
+ <Reference Include="System.ComponentModel.DataAnnotations" />
+ <Reference Include="System.configuration" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Datetime2Tests.cs" />
+ <Compile Include="EnsureUtcTest.cs" />
+ <Compile Include="InsertParam_GetLastInsertId.cs" />
+ <Compile Include="NestedTransactions.cs" />
+ <Compile Include="EnumTests.cs" />
+ <Compile Include="Expressions\AdditiveExpressionsTest.cs" />
+ <Compile Include="Expressions\Author.cs" />
+ <Compile Include="Expressions\AuthorUseCase.cs" />
+ <Compile Include="Expressions\ConditionalExpressionTest.cs" />
+ <Compile Include="Expressions\EqualityExpressionsTest.cs" />
+ <Compile Include="Expressions\ExpressionsTestBase.cs" />
+ <Compile Include="Expressions\LogicalExpressionsTest.cs" />
+ <Compile Include="Expressions\MultiplicativeExpressionsTest.cs" />
+ <Compile Include="Expressions\OrmLiteCountTests.cs" />
+ <Compile Include="Expressions\PrimaryExpressionsTest.cs" />
+ <Compile Include="Expressions\RelationalExpressionsTest.cs" />
+ <Compile Include="Expressions\StringFunctionTests.cs" />
+ <Compile Include="Expressions\TestType.cs" />
+ <Compile Include="Expressions\UnaryExpressionsTest.cs" />
+ <Compile Include="ForeignKeyAttributeTests.cs" />
+ <Compile Include="OrmLiteTestBase.cs" />
+ <Compile Include="SqlServerExpressionVisitorQueryTest.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="TypeWithByteArrayFieldTests.cs" />
+ <Compile Include="UnicodeTests.cs" />
+ <Compile Include="UpdateTests.cs" />
+ <Compile Include="UseCase\SimpleUseCase.cs" />
+ <Compile Include="UseCase\TestEntity.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\ServiceStack.OrmLite.SqlServer\ServiceStack.OrmLite.SqlServer.csproj">
+ <Project>{1887DC99-9139-43E3-A7AA-6D74714B3A5D}</Project>
+ <Name>ServiceStack.OrmLite.SqlServer</Name>
+ </ProjectReference>
+ <ProjectReference Include="..\ServiceStack.OrmLite\ServiceStack.OrmLite.csproj">
+ <Project>{96179AC6-F6F1-40C3-9FDD-4F6582F54C5C}</Project>
+ <Name>ServiceStack.OrmLite</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="app.config">
+ <SubType>Designer</SubType>
+ </None>
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
- -->
+ -->
</Project>
Something went wrong with that request. Please try again.