Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 789769e

Browse files
committed
Merge pull request #415 from brunomlopes/master
Fix for updating entities on PostgresSQL with DateTimeOffsets and mixed cultures
2 parents 2e23dc4 + e130dbb commit 789769e

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/ServiceStack.OrmLite.PostgreSQL.Tests/OrmLiteInsertTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
using System.Globalization;
13
using NUnit.Framework;
24
using ServiceStack.Common.Tests.Models;
35
using ServiceStack.DataAnnotations;
@@ -170,6 +172,58 @@ class ModelWithIdAndName1
170172
[AutoIncrement]
171173
public int Id { get; set; }
172174
public string Name { get; set; }
175+
}
176+
177+
public class PostgreSQLUpdateTests : OrmLiteTestBase
178+
{
179+
public PostgreSQLUpdateTests()
180+
: base(Dialect.PostgreSql)
181+
{
182+
}
183+
184+
[Test]
185+
public void Can_insert_datetimeoffsets_regardless_of_current_culture()
186+
{
187+
// datetimeoffset's default .ToString depends on culture, ensure we use one with MDY
188+
var previousCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
189+
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
190+
try
191+
{
192+
using (var db = OpenDbConnection())
193+
{
194+
// and set datestyle to DMY, crashing the insert when we're formatting it as the default on en-US
195+
db.ExecuteNonQuery("SET datestyle TO \"ISO, DMY\"");
196+
db.CreateTable<ModelWithDateTimeOffset>(true);
197+
198+
var date = new DateTimeOffset(2010, 11, 29, 1, 2, 3, new TimeSpan(0));
199+
var row = new ModelWithDateTimeOffset
200+
{
201+
Id = 1,
202+
Value = date
203+
};
204+
205+
db.Insert(row);
206+
db.Update<ModelWithDateTimeOffset>(new { Value = date.AddDays(30) }, r => r.Id == 1);
207+
208+
var rows = db.Select<ModelWithDateTimeOffset>();
209+
210+
Assert.That(rows, Has.Count.EqualTo(1));
211+
Assert.That(rows[0].Value, Is.EqualTo(date.AddDays(30)));
212+
}
213+
}
214+
finally
215+
{
216+
System.Threading.Thread.CurrentThread.CurrentCulture = previousCulture;
217+
}
218+
}
219+
220+
}
221+
222+
class ModelWithDateTimeOffset
223+
{
224+
[AutoIncrement]
225+
public int Id { get; set; }
226+
public DateTimeOffset Value { get; set; }
173227
}
174228

175229
}

src/ServiceStack.OrmLite.PostgreSQL/PostgreSQLDialectProvider.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ public override string GetQuotedValue(object value, Type fieldType)
164164
const string iso8601Format = "yyyy-MM-dd HH:mm:ss.fff";
165165
return base.GetQuotedValue(dateValue.ToString(iso8601Format), typeof(string));
166166
}
167+
if (fieldType == typeof(DateTimeOffset))
168+
{
169+
var dateValue = (DateTimeOffset)value;
170+
const string iso8601Format = "yyyy-MM-dd HH:mm:ss.fff zzz";
171+
return base.GetQuotedValue(dateValue.ToString(iso8601Format), typeof(string));
172+
}
167173
if (fieldType == typeof(Guid))
168174
{
169175
var guidValue = (Guid)value;

0 commit comments

Comments
 (0)