Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,16 @@ public override object ConvertSourceToIntermediate(IPublishedElement owner, IPub

internal static DateTime ParseDateTimeValue(object? source)
{
if (source is null)
if (source is DateTime dateTimeValue)
{
return DateTime.MinValue;
return DateTime.SpecifyKind(dateTimeValue, DateTimeKind.Unspecified);
}

if (source is DateTime dateTimeValue)
if (source.TryConvertTo<DateTime>() is { Success: true } attempt)
{
return dateTimeValue;
return DateTime.SpecifyKind(attempt.Result, DateTimeKind.Unspecified);
}

Attempt<DateTime> attempt = source.TryConvertTo<DateTime>();
return attempt.Success
? attempt.Result
: DateTime.MinValue;
return DateTime.MinValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NUnit.Framework;
using Umbraco.Cms.Core.PropertyEditors.ValueConverters;

namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.PropertyEditors.ValueConverters;

[TestFixture]
public class DatePickerValueConverterTests
{
private static object[] _parseDateTimeValueCases =
[
new object[] { null, DateTime.MinValue },
new object[] { DateTime.MinValue, DateTime.MinValue },
new object[] { new DateTime(2021, 01, 20, 9, 0, 36), new DateTime(2021, 01, 20, 9, 0, 36) },
new object[] { "2021-01-20T09:00:36", new DateTime(2021, 01, 20, 9, 0, 36) },
new object[] { "test", DateTime.MinValue },
];

[TestCaseSource(nameof(_parseDateTimeValueCases))]
public void Can_Parse_DateTime_Value(object? input, DateTime expected)
{
var result = DatePickerValueConverter.ParseDateTimeValue(input);
Assert.AreEqual(expected, result);
Assert.AreEqual(DateTimeKind.Unspecified, result.Kind);
}
}