BlogEngine.Core.SyndicationGenerator.ToRfc822DateTime C# (CSharp) Method

ToRfc822DateTime() public static method

Converts the supplied DateTime to its equivalent RFC-822 DateTime string representation.
public static ToRfc822DateTime ( System.DateTime dateTime ) : string
dateTime System.DateTime /// The to convert. ///
return string
        public static string ToRfc822DateTime(DateTime dateTime)
        {
            var offset =
                (int)(TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalHours + BlogSettings.Instance.Timezone);
            var timeZone = string.Format("+{0}", offset.ToString(NumberFormatInfo.InvariantInfo).PadLeft(2, '0'));

            // ------------------------------------------------------------
            // Adjust time zone based on offset
            // ------------------------------------------------------------
            if (offset < 0)
            {
                var i = offset * -1;
                timeZone = string.Format("-{0}", i.ToString(NumberFormatInfo.InvariantInfo).PadLeft(2, '0'));
            }

            return dateTime.ToString(
                string.Format("ddd, dd MMM yyyy HH:mm:ss {0}", timeZone.PadRight(5, '0')), DateTimeFormatInfo.InvariantInfo);
        }

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// Writes the RSS channel item element information to the specified <see cref="XmlWriter"/> using the supplied <see cref="Page"/>.
        /// </summary>
        /// <param name="writer">The <see cref="XmlWriter"/> to write channel item element information to.</param>
        /// <param name="publishable">The <see cref="IPublishable"/> used to generate channel item content.</param>
        private static void WriteRssItem(XmlWriter writer, IPublishable publishable)
        {
            //------------------------------------------------------------
            //	Cast IPublishable as Post to support comments/trackback
            //------------------------------------------------------------
            Post    post    = publishable as Post;
            Comment comment = publishable as Comment;

            //------------------------------------------------------------
            //	Raise serving event
            //------------------------------------------------------------
            ServingEventArgs arg = new ServingEventArgs(publishable.Content, ServingLocation.Feed);

            publishable.OnServing(arg);
            if (arg.Cancel)
            {
                return;
            }

            //------------------------------------------------------------
            //	Modify post content to make references absolute
            //------------------------------------------------------------
            string content = ConvertPathsToAbsolute(arg.Body);

            if (comment != null)
            {
                content = content.Replace(Environment.NewLine, "<br />");
            }

            writer.WriteStartElement("item");
            //------------------------------------------------------------
            //	Write required channel item elements
            //------------------------------------------------------------
            writer.WriteElementString("title", publishable.Title);
            writer.WriteElementString("description", content);
            writer.WriteElementString("link", Utils.ConvertToAbsolute(publishable.RelativeLink).ToString());

            //------------------------------------------------------------
            //	Write enclosure tag for podcasting support
            //------------------------------------------------------------
            if (BlogSettings.Instance.EnableEnclosures)
            {
                string encloser = GetEnclosure(content);
                if (!string.IsNullOrEmpty(encloser))
                {
                    writer.WriteRaw(encloser);
                }
            }

            //------------------------------------------------------------
            //	Write optional channel item elements
            //------------------------------------------------------------
            writer.WriteElementString("author", publishable.Author);
            if (post != null)
            {
                writer.WriteElementString("comments", String.Concat(Utils.ConvertToAbsolute(publishable.RelativeLink).ToString(), "#comment"));
            }
            writer.WriteElementString("guid", SyndicationGenerator.GetPermaLink(publishable).ToString());
            writer.WriteElementString("pubDate", SyndicationGenerator.ToRfc822DateTime(publishable.DateCreated));

            //------------------------------------------------------------
            //	Write channel item category elements
            //------------------------------------------------------------
            if (publishable.Categories != null)
            {
                foreach (Category category in publishable.Categories)
                {
                    writer.WriteElementString("category", category.Title);
                }
            }

            //------------------------------------------------------------
            //	Write Dublin Core syndication extension elements
            //------------------------------------------------------------
            if (!String.IsNullOrEmpty(publishable.Author))
            {
                writer.WriteElementString("dc", "publisher", "http://purl.org/dc/elements/1.1/", publishable.Author);
            }
            //if (!String.IsNullOrEmpty(publishable.Description))
            //{
            //	writer.WriteElementString("dc", "description", "http://purl.org/dc/elements/1.1/", publishable.Description);
            //}

            //------------------------------------------------------------
            //	Write pingback syndication extension elements
            //------------------------------------------------------------
            Uri pingbackServer;

            if (Uri.TryCreate(String.Concat(Utils.AbsoluteWebRoot.ToString().TrimEnd('/'), "/pingback.axd"), UriKind.RelativeOrAbsolute, out pingbackServer))
            {
                writer.WriteElementString("pingback", "server", "http://madskills.com/public/xml/rss/module/pingback/", pingbackServer.ToString());
                writer.WriteElementString("pingback", "target", "http://madskills.com/public/xml/rss/module/pingback/", SyndicationGenerator.GetPermaLink(publishable).ToString());
            }

            //------------------------------------------------------------
            //	Write slash syndication extension elements
            //------------------------------------------------------------
            if (post != null && post.Comments != null)
            {
                writer.WriteElementString("slash", "comments", "http://purl.org/rss/1.0/modules/slash/", post.Comments.Count.ToString(CultureInfo.InvariantCulture));
            }

            //------------------------------------------------------------
            //	Write trackback syndication extension elements
            //------------------------------------------------------------
            if (post != null && post.TrackbackLink != null)
            {
                writer.WriteElementString("trackback", "ping", "http://madskills.com/public/xml/rss/module/trackback/", post.TrackbackLink.ToString());
            }

            //------------------------------------------------------------
            //	Write well-formed web syndication extension elements
            //------------------------------------------------------------
            writer.WriteElementString("wfw", "comment", "http://wellformedweb.org/CommentAPI/", String.Concat(Utils.ConvertToAbsolute(publishable.RelativeLink).ToString(), "#comment"));
            writer.WriteElementString("wfw", "commentRss", "http://wellformedweb.org/CommentAPI/", Utils.AbsoluteWebRoot.ToString().TrimEnd('/') + "/syndication.axd?post=" + publishable.Id.ToString());

            //------------------------------------------------------------
            //	Write </item> element
            //------------------------------------------------------------
            writer.WriteEndElement();
        }