Skip to content

Commit

Permalink
Fix DateTime to DateTimeOffset conversion (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekseyMartynov authored Apr 26, 2021
1 parent 3018ac3 commit 833861b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -223,6 +225,24 @@ public void StringFuncOnTimeSpan() {
AssertEvaluation(obj, new[] { "NullableTime", "contains", "23" });
}

[Theory]
[InlineData(DateParseHandling.None)]
[InlineData(DateParseHandling.DateTime)]
[InlineData(DateParseHandling.DateTimeOffset)]
public void Issue477(DateParseHandling dateParseHandling) {
var date = new DateTimeOffset(2021, 1, 1, 0, 0, 0, TimeSpan.Zero);
var filterJSON = JsonConvert.SerializeObject(new object[] { "this", date });
var deserializedFilter = JsonConvert.DeserializeObject<IList>(filterJSON, new JsonSerializerSettings {
DateParseHandling = dateParseHandling
});

var loadOptions = new SampleLoadOptions {
Filter = deserializedFilter
};

var loadResult = DataSourceLoader.Load(new[] { date }, loadOptions);
Assert.Single(loadResult.data);
}
}

}
3 changes: 3 additions & 0 deletions net/DevExtreme.AspNet.Data/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public static object ConvertClientValue(object value, Type type) {
if(type == typeof(DateTime) && value is String)
return DateTime.Parse((string)value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);

if(type == typeof(DateTimeOffset) && value is DateTime date)
return new DateTimeOffset(date);

var converter = TypeDescriptor.GetConverter(type);
if(converter != null && converter.CanConvertFrom(value.GetType()))
return converter.ConvertFrom(null, CultureInfo.InvariantCulture, value);
Expand Down

0 comments on commit 833861b

Please sign in to comment.