add proper converter for legacy datetime

This commit is contained in:
crobibero 2020-08-10 11:58:06 -06:00
parent 4b87bbf53d
commit b0532c549a
2 changed files with 43 additions and 1 deletions

View File

@ -0,0 +1,37 @@
using System;
using System.ComponentModel;
using System.Globalization;
namespace Jellyfin.Api.TypeConverters
{
/// <summary>
/// Custom datetime parser.
/// </summary>
public class DateTimeTypeConverter : TypeConverter
{
/// <inheritdoc />
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
/// <inheritdoc />
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string dateString)
{
if (DateTime.TryParseExact(dateString, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var dateTime))
{
return dateTime;
}
}
return base.ConvertFrom(context, culture, value);
}
}
}

View File

@ -1,4 +1,6 @@
using System.Net.Http;
using System;
using System.ComponentModel;
using Jellyfin.Api.TypeConverters;
using Jellyfin.Server.Extensions;
using Jellyfin.Server.Middleware;
using Jellyfin.Server.Models;
@ -94,6 +96,9 @@ namespace Jellyfin.Server
});
app.Use(serverApplicationHost.ExecuteHttpHandlerAsync);
// Add type descriptor for legacy datetime parsing.
TypeDescriptor.AddAttributes(typeof(DateTime?), new TypeConverterAttribute(typeof(DateTimeTypeConverter)));
}
}
}