1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-27 01:57:21 +00:00

Better RSS date parsing

This commit is contained in:
Keivan Beigi 2013-08-07 19:07:07 -07:00
parent 1fdc27598b
commit 8900c32ae1
3 changed files with 37 additions and 1 deletions

View 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);
}
}
}

View file

@ -123,6 +123,7 @@
<Compile Include="IndexerTests\BasicRssParserFixture.cs" />
<Compile Include="IndexerTests\IndexerServiceFixture.cs" />
<Compile Include="IndexerTests\IntegrationTests\IndexerIntegrationTests.cs" />
<Compile Include="IndexerTests\XElementExtensionsFixture.cs" />
<Compile Include="JobTests\JobRepositoryFixture.cs" />
<Compile Include="DecisionEngineTests\LanguageSpecificationFixture.cs" />
<Compile Include="JobTests\TestJobs.cs" />

View file

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using NLog;
@ -11,6 +13,8 @@ public static class XElementExtensions
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)
{
return item.TryGetValue("title", "Unknown");
@ -22,7 +26,13 @@ public static DateTime PublishDate(this XElement item)
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)
{