mirror of
https://github.com/Radarr/Radarr.git
synced 2024-11-04 10:02:40 +01:00
Better RSS date parsing
This commit is contained in:
parent
1fdc27598b
commit
8900c32ae1
25
NzbDrone.Core.Test/IndexerTests/XElementExtensionsFixture.cs
Normal file
25
NzbDrone.Core.Test/IndexerTests/XElementExtensionsFixture.cs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
using System;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
using FluentAssertions;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using NzbDrone.Core.Indexers;
|
||||||
|
using NzbDrone.Test.Common;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Test.IndexerTests
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class XElementExtensionsFixture : TestBase
|
||||||
|
{
|
||||||
|
[TestCase("Wed, 07 Aug 2013 20:37:48 +0000")]
|
||||||
|
[TestCase("Wed, 07 Aug 2013 18:08:46 MST")]
|
||||||
|
public void should_parse_date(string dateString)
|
||||||
|
{
|
||||||
|
var element = new XElement("root");
|
||||||
|
element.Add(new XElement("pubDate", dateString));
|
||||||
|
|
||||||
|
var date = element.PublishDate();
|
||||||
|
date.Year.Should().Be(2013);
|
||||||
|
date.Kind.Should().Be(DateTimeKind.Utc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -123,6 +123,7 @@
|
|||||||
<Compile Include="IndexerTests\BasicRssParserFixture.cs" />
|
<Compile Include="IndexerTests\BasicRssParserFixture.cs" />
|
||||||
<Compile Include="IndexerTests\IndexerServiceFixture.cs" />
|
<Compile Include="IndexerTests\IndexerServiceFixture.cs" />
|
||||||
<Compile Include="IndexerTests\IntegrationTests\IndexerIntegrationTests.cs" />
|
<Compile Include="IndexerTests\IntegrationTests\IndexerIntegrationTests.cs" />
|
||||||
|
<Compile Include="IndexerTests\XElementExtensionsFixture.cs" />
|
||||||
<Compile Include="JobTests\JobRepositoryFixture.cs" />
|
<Compile Include="JobTests\JobRepositoryFixture.cs" />
|
||||||
<Compile Include="DecisionEngineTests\LanguageSpecificationFixture.cs" />
|
<Compile Include="DecisionEngineTests\LanguageSpecificationFixture.cs" />
|
||||||
<Compile Include="JobTests\TestJobs.cs" />
|
<Compile Include="JobTests\TestJobs.cs" />
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
using NLog;
|
using NLog;
|
||||||
|
|
||||||
@ -11,6 +13,8 @@ public static class XElementExtensions
|
|||||||
|
|
||||||
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
private static readonly Regex RemoveTimeZoneRegex = new Regex(@"\s[A-Z]{2,4}$", RegexOptions.Compiled);
|
||||||
|
|
||||||
public static string Title(this XElement item)
|
public static string Title(this XElement item)
|
||||||
{
|
{
|
||||||
return item.TryGetValue("title", "Unknown");
|
return item.TryGetValue("title", "Unknown");
|
||||||
@ -22,7 +26,13 @@ public static DateTime PublishDate(this XElement item)
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return DateTime.Parse(dateString);
|
DateTime result;
|
||||||
|
if (!DateTime.TryParse(dateString, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal, out result))
|
||||||
|
{
|
||||||
|
dateString = RemoveTimeZoneRegex.Replace(dateString, "");
|
||||||
|
result = DateTime.Parse(dateString, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal);
|
||||||
|
}
|
||||||
|
return result.ToUniversalTime();
|
||||||
}
|
}
|
||||||
catch (FormatException e)
|
catch (FormatException e)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user