Better RSS date parsing

pull/2/head
Keivan Beigi 11 years ago
parent 1fdc27598b
commit 8900c32ae1

@ -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\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" />

@ -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 @@ namespace NzbDrone.Core.Indexers
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 @@ namespace NzbDrone.Core.Indexers
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)
{

Loading…
Cancel
Save