#18651: Fixing loss of millisenconds when using fields

Work Item : 18651

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2012-06-18 13:08:16 -07:00
parent eaf9cfcd4c
commit 17b300519c
4 changed files with 22 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using NHibernate;
@@ -200,6 +201,14 @@ namespace Orchard.Tests.Data {
Assert.That(two.Name, Is.EqualTo("two"));
}
[Test]
public void StoringDateTimeDoesntRemovePrecision() {
_fooRepos.Create(new FooRecord { Name = "one", Timespan = DateTime.Parse("2001-01-01 16:28:21.489", CultureInfo.InvariantCulture) });
var one = _fooRepos.Fetch(f => f.Name == "one").Single();
Assert.That(one.Name, Is.EqualTo("one"));
Assert.That(one.Timespan.Value.Millisecond, Is.EqualTo(489));
}
}
}

View File

@@ -1,6 +1,9 @@
using System;
namespace Orchard.Tests.Records {
public class FooRecord {
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual DateTime? Timespan { get; set; }
}
}

View File

@@ -28,7 +28,15 @@ namespace Orchard.ContentManagement.FieldStorage {
}
public void Set<T>(string name, T value) {
Setter(name, typeof(T), Convert.ToString(value, CultureInfo.InvariantCulture));
// using a special case for DateTime as it would lose milliseconds otherwise
if (typeof(T) == typeof(DateTime)) {
var text = ((DateTime)(object)value).ToString("o", CultureInfo.InvariantCulture);
Setter(name, typeof(T), text);
}
else {
Setter(name, typeof (T), Convert.ToString(value, CultureInfo.InvariantCulture));
}
}
}
}