public static DateTime ParseRfc3339DateTime(string value) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ DateTime result = DateTime.MinValue; //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ if (value == null || value.Length == 0) { return(result); } //------------------------------------------------------------ // Parse RFC-3339 formatted date //------------------------------------------------------------ if (RSSDateTimeUtility.TryParseRfc3339DateTime(value, out result)) { return(result); } else { throw new FormatException(String.Format(null, "'{0}' is not a valid RFC-3339 formatted date-time value.", value)); } }
public static bool TryParseRfc822DateTime(string value, out DateTime result) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ DateTimeFormatInfo dateTimeFormat = CultureInfo.InvariantCulture.DateTimeFormat; string[] formats = new string[3]; //------------------------------------------------------------ // Define valid RFC-822 formats //------------------------------------------------------------ formats[0] = dateTimeFormat.RFC1123Pattern; formats[1] = "ddd',' d MMM yyyy HH:mm:ss zzz"; formats[2] = "ddd',' dd MMM yyyy HH:mm:ss zzz"; //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ if (String.IsNullOrEmpty(value)) { result = DateTime.MinValue; return(false); } //------------------------------------------------------------ // Perform conversion of RFC-822 formatted date-time string //------------------------------------------------------------ return(DateTime.TryParseExact(RSSDateTimeUtility.ReplaceRfc822TimeZoneWithOffset(value), formats, dateTimeFormat, DateTimeStyles.None, out result)); }
private static List <NewsFeedItem> ParseFeed(string rss) { var list = new List <NewsFeedItem>(); try { var xdoc = XDocument.Parse(rss); var items = xdoc.Descendants("item"); var id = 0; foreach (var item in items) { NewsFeedItem article = new NewsFeedItem(); // Fri, 04 May 2018 15:31:44 EDT <-- district version // Wed, 02 May 2018 12:58:20 -0000 <-- RSS aggregator version string dateString = item.Element("pubDate").Value; DateTime pubDate = DateTime.MinValue; RSSDateTimeUtility.TryParseRfc822DateTime(dateString, out pubDate); if (pubDate == DateTime.MinValue) { RSSDateTimeUtility.TryParseRfc3339DateTime(dateString, out pubDate); } article.Author = ""; article.Title = item.Element("title") != null?item.Element("title").Value : ""; article.Description = item.Element("description") != null?item.Element("description").Value : ""; article.Link = item.Element("link") != null?item.Element("link").Value : ""; article.PublishDate = item.Element("pubDate") != null ? pubDate : DateTime.MinValue; article.Id = (id++).ToString(); list.Add(article); } } catch { } return(list); }