Rss feed 的日期转换

发布于 2024-12-11 00:51:24 字数 204 浏览 0 评论 0原文

我正在尝试聚合多个 rss feed,当我尝试转换从 feedd 获得的发布日期时,出现异常,因为日期采用以下格式 “英国夏令时 2010 年 5 月 5 日星期三 14:27:37”

我尝试使用我在此处找到的代码片段转换为 Rfc822 日期时间,但它仍然无法工作(出于明显的原因)。有谁知道如何将其转换为 .Net 中的 DateTime 对象

I am trying to aggregate several rss feeds and when I try to convert the publish date I get from the feedds, I get an exception as the date is in the following format
'Wed, 5 May 2010 14:27:37 BST'.

I have tried converting to Rfc822 datetime using a code snippet I found here but it still won't work (for obviousreasons). Does anyone know how I can convert this to a DateTime object in .Net

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的十字路口 2024-12-18 00:51:24

我写了一个小片段,不支持所有格式,但支持很多格式。我很高兴收到反馈或改进...

    /// <summary>
    /// Parst ein Datum aus dem angegebenen XML Element.
    /// Der Inhalt muss RFS 822, Kap. 5 entsprechen.
    /// </summary>
    /// <param name="current">Das Element mit dem RFS822-Datum (kann null sein, um null auszugeben)</param>
    /// <returns>geparstes Datum oder null, wenn current==null ist.</returns>
    /// <remarks>Unterstützt momentan die Zeitzonen-Angabe nur numerisch oder als UT/GMT, nicht als Mil-Zone oder TLA.</remarks>
    private static DateTime? ParseRfc822DateTime(XElement current)
    {
        DateTime? result = null;
        if (current != null)
        {
            Regex datePattern = new Regex(@"((Mon|Thu|Wed|Thu|Fri|Sat|Sun)\s*,\s*)?([0-9]{1,2})\s*(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s*([0-9]{2,4})\s*([0-9]{2}):([0-9]{2})(:([0-9]{2}))?(.*)", RegexOptions.Singleline);

            Match match = datePattern.Match(current.Value);
            if (match.Success)
            {
                string dayIndi = match.Groups[2].Value;
                int day = int.Parse(match.Groups[3].Value);
                string monText = match.Groups[4].Value;
                int year = int.Parse(match.Groups[5].Value);
                int hour = int.Parse(match.Groups[6].Value);
                int min = int.Parse(match.Groups[7].Value);
                int sec = match.Groups[8].Success ? int.Parse(match.Groups[9].Value) : 0;
                string timezoneIndi = (match.Groups[10].Value ?? String.Empty).Trim();

                if (year < 99)
                {
                    year = System.Globalization.CultureInfo.InvariantCulture.Calendar.ToFourDigitYear(year);
                }

                result = DateTime.ParseExact(String.Format(
                    "{0:00}.{1}.{2:0000} {3:00}:{4:00}:{5:00}",
                    day, monText, year, hour, min, sec),
                    "dd.MMM.yyyy HH:mm:ss",
                    System.Globalization.CultureInfo.InvariantCulture,
                    System.Globalization.DateTimeStyles.AssumeLocal);
                result = DateTime.SpecifyKind(result.Value, DateTimeKind.Unspecified);

                TimeZoneInfo zoneInfo;
                if (timezoneIndi == "UT" || timezoneIndi == "GMT")
                {
                    zoneInfo = TimeZoneInfo.Utc;
                }
                else if (timezoneIndi.StartsWith("+") || timezoneIndi.StartsWith("-"))
                {
                    int hoursOffset = int.Parse(timezoneIndi.Substring(1, 2));
                    int minsOffset = int.Parse(timezoneIndi.Substring(3, 2));

                    if (timezoneIndi.StartsWith("-"))
                    {
                        hoursOffset = -hoursOffset;
                        minsOffset = -minsOffset;
                    }

                    zoneInfo = TimeZoneInfo.CreateCustomTimeZone("RFC822-Offset" + timezoneIndi,
                        new TimeSpan(hoursOffset, minsOffset, 0), "RFS822-Offset" + timezoneIndi, timezoneIndi);

                    //result = result.Value.AddMinutes(minsOffset).AddHours(hoursOffset);
                }
                else
                {
                    /* This WILL fail for the MIL-One-Letter-Zones and some others. */
                    zoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timezoneIndi);
                }

                result = TimeZoneInfo.ConvertTime(result.Value, zoneInfo, TimeZoneInfo.Local);

                return result;
            }
        }

        return result;
    }

I wrote a little snippet, not supporting all formats, but many. I would be happy to receive feedback or improvements...

    /// <summary>
    /// Parst ein Datum aus dem angegebenen XML Element.
    /// Der Inhalt muss RFS 822, Kap. 5 entsprechen.
    /// </summary>
    /// <param name="current">Das Element mit dem RFS822-Datum (kann null sein, um null auszugeben)</param>
    /// <returns>geparstes Datum oder null, wenn current==null ist.</returns>
    /// <remarks>Unterstützt momentan die Zeitzonen-Angabe nur numerisch oder als UT/GMT, nicht als Mil-Zone oder TLA.</remarks>
    private static DateTime? ParseRfc822DateTime(XElement current)
    {
        DateTime? result = null;
        if (current != null)
        {
            Regex datePattern = new Regex(@"((Mon|Thu|Wed|Thu|Fri|Sat|Sun)\s*,\s*)?([0-9]{1,2})\s*(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s*([0-9]{2,4})\s*([0-9]{2}):([0-9]{2})(:([0-9]{2}))?(.*)", RegexOptions.Singleline);

            Match match = datePattern.Match(current.Value);
            if (match.Success)
            {
                string dayIndi = match.Groups[2].Value;
                int day = int.Parse(match.Groups[3].Value);
                string monText = match.Groups[4].Value;
                int year = int.Parse(match.Groups[5].Value);
                int hour = int.Parse(match.Groups[6].Value);
                int min = int.Parse(match.Groups[7].Value);
                int sec = match.Groups[8].Success ? int.Parse(match.Groups[9].Value) : 0;
                string timezoneIndi = (match.Groups[10].Value ?? String.Empty).Trim();

                if (year < 99)
                {
                    year = System.Globalization.CultureInfo.InvariantCulture.Calendar.ToFourDigitYear(year);
                }

                result = DateTime.ParseExact(String.Format(
                    "{0:00}.{1}.{2:0000} {3:00}:{4:00}:{5:00}",
                    day, monText, year, hour, min, sec),
                    "dd.MMM.yyyy HH:mm:ss",
                    System.Globalization.CultureInfo.InvariantCulture,
                    System.Globalization.DateTimeStyles.AssumeLocal);
                result = DateTime.SpecifyKind(result.Value, DateTimeKind.Unspecified);

                TimeZoneInfo zoneInfo;
                if (timezoneIndi == "UT" || timezoneIndi == "GMT")
                {
                    zoneInfo = TimeZoneInfo.Utc;
                }
                else if (timezoneIndi.StartsWith("+") || timezoneIndi.StartsWith("-"))
                {
                    int hoursOffset = int.Parse(timezoneIndi.Substring(1, 2));
                    int minsOffset = int.Parse(timezoneIndi.Substring(3, 2));

                    if (timezoneIndi.StartsWith("-"))
                    {
                        hoursOffset = -hoursOffset;
                        minsOffset = -minsOffset;
                    }

                    zoneInfo = TimeZoneInfo.CreateCustomTimeZone("RFC822-Offset" + timezoneIndi,
                        new TimeSpan(hoursOffset, minsOffset, 0), "RFS822-Offset" + timezoneIndi, timezoneIndi);

                    //result = result.Value.AddMinutes(minsOffset).AddHours(hoursOffset);
                }
                else
                {
                    /* This WILL fail for the MIL-One-Letter-Zones and some others. */
                    zoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timezoneIndi);
                }

                result = TimeZoneInfo.ConvertTime(result.Value, zoneInfo, TimeZoneInfo.Local);

                return result;
            }
        }

        return result;
    }
静若繁花 2024-12-18 00:51:24

时间中的“BST”是指英国夏令时间、巴西标准时间或白令夏令时间。如果您知道时间被“编码”的时区,您可以获取它来解析它。我在样本中假设了英国夏令时间:

var date = DateTime.Parse("Wed, 5 May 2010 14:27:37", CultureInfo.GetCultureInfo("En-GB"));

只需从时间字符串末尾“删除”单词“BST”并获取英国文化信息来解析日期和时间。

"BST" in time means British Summer Time, Brazil Standard Time or Bering Summer Time. If you know the time zone in which the time was "encoded" you can get it for parsing it. I assumed British Summer Time in my sample:

var date = DateTime.Parse("Wed, 5 May 2010 14:27:37", CultureInfo.GetCultureInfo("En-GB"));

Just "kill" the Word "BST" from end of the time string and get Britsh culture info to parese the date and time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文