Skip to content

Commit

Permalink
Date parsing refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
a-gubskiy committed Dec 9, 2017
1 parent 6b66fbf commit 70fa50f
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 30 deletions.
16 changes: 14 additions & 2 deletions src/X.Web.RSS/Extensions/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;

namespace X.Web.RSS.Extensions
{
Expand All @@ -9,6 +10,14 @@ public static class DateTimeExtensions
///</summary>
///<returns>The specified date formatted as a RFC822 date string.</returns>
public static string ToRFC822Date(this DateTime date)
{
var timeZone = GetTimeZone();

var result = date.ToString("ddd, dd MMM yyyy HH:mm:ss " + timeZone.PadRight(5, '0'));
return result;
}

private static string GetTimeZone()
{
var utcOffset = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);
var offset = utcOffset.Hours;
Expand All @@ -19,9 +28,12 @@ public static string ToRFC822Date(this DateTime date)
int i = offset * -1;
timeZone = "-" + i.ToString().PadLeft(2, '0');
}
return timeZone;
}

var result = date.ToString("ddd, dd MMM yyyy HH:mm:ss " + timeZone.PadRight(5, '0'));
return result;
public static DateTime FromRFC822Date(this string date)
{
return DateTime.Parse(date);
}
}
}
4 changes: 2 additions & 2 deletions src/X.Web.RSS/Structure/RssChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ public RssChannel()
public string InternalLastBuildDate
{
get => LastBuildDate?.ToRFC822Date();
set => throw new System.NotSupportedException("Setting this property is not supported");
set => LastBuildDate = value?.FromRFC822Date();
}

[XmlElement("pubDate")]
public string InternalPubDate
{
get => PubDate?.ToRFC822Date();
set => throw new System.NotSupportedException("Setting this property is not supported");
set => PubDate = value?.FromRFC822Date();
}

[XmlElement("ttl")]
Expand Down
4 changes: 1 addition & 3 deletions src/X.Web.RSS/Structure/RssItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,9 @@ public class RssItem
public string InternalPubDate
{
get => PubDate?.ToRFC822Date();
set => throw new System.NotSupportedException("Setting this property is not supported");
set => PubDate = value?.FromRFC822Date();
}



#endregion
}
}
17 changes: 2 additions & 15 deletions src/X.Web.RSS/Structure/RssPerson.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Xml.Serialization;
using X.Web.RSS.Enumerators;
using X.Web.RSS.Structure.Validators;

namespace X.Web.RSS.Structure
{
Expand All @@ -12,21 +10,10 @@ public RssPerson()

public RssPerson(string name, string email)
{
Name = name;
Email = email;
Value = $"{email} ({name})";
}

[XmlIgnore]
public string Email { get; set; }

[XmlIgnore]
public string Name { get; set; }

[XmlText]
public string Value
{
get => $"{Email} ({Name})";
set => throw new System.NotSupportedException("Setting this property is not supported");
}
public string Value { get; set; }
}
}
10 changes: 4 additions & 6 deletions src/X.Web.RSS/X.Web.RSS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
<Version>2.1.0</Version>
<Version>2.2.0</Version>
<VersionSuffix>beta</VersionSuffix>
<Description>
X.Web.Rss is a part of X-Framework library.
This library allows you quickly and easily generate a RSS feeds.
</Description>
<Description>X.Web.Rss is a part of X-Framework library.
This library allows you quickly and easily generate a RSS feeds.</Description>
<Copyright>Andrew Gubskiy</Copyright>
<PackageProjectUrl>https://github.com/ernado-x/X.Web.RSS</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/ernado-x/X.Web.RSS</RepositoryUrl>
<PackageIconUrl>https://ru.gravatar.com/userimage/8071071/f2dc08ee7e4016451f64a7ae9cffd110.png?size=200</PackageIconUrl>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>xwebrss</PackageId>
<Authors>Andrew Gubskiu</Authors>
<Authors>Andrew Gubskiy</Authors>
<Company>.NET Core Ukrainian User Group</Company>
</PropertyGroup>

Expand Down
3 changes: 1 addition & 2 deletions tests/X.Web.RSS.Tests/RSSHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.IO;
using System.Net;
using System.Text;
using X.Web.RSS;
using X.Web.RSS.Enumerators;
using X.Web.RSS.Structure;
using X.Web.RSS.Structure.Validators;
Expand Down Expand Up @@ -61,7 +60,7 @@ public void Test()

var rss = RssDocument.Load(stream);

Assert.Equal("News Center", rss.Channel.Title);
Assert.Equal("Stories", rss.Channel.Title);
Assert.Equal("Microsoft news, features, events, and press materials", rss.Channel.Description);
}

Expand Down
13 changes: 13 additions & 0 deletions tests/X.Web.RSS.Tests/Validators/RssDateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Globalization;
using X.Web.RSS.Exceptions;
using X.Web.RSS.Extensions;
using X.Web.RSS.Structure.Validators;

namespace X.Web.RSS.Tests.Validators
Expand Down Expand Up @@ -242,5 +243,17 @@ public void Ctor_InvalidDateFormat_Error()
// Assert
Assert.NotNull(e);
}

[Fact]
public void DateExtensionTest()
{
var date = DateTime.Now;

var rfcDateInString = date.ToRFC822Date();
var parsedDate = rfcDateInString.FromRFC822Date();

Assert.Equal(date.ToLongDateString(), parsedDate.ToLongDateString());
Assert.Equal(date.ToLongTimeString(), parsedDate.ToLongTimeString());
}
}
}

0 comments on commit 70fa50f

Please sign in to comment.