从Rss订阅源转换日期
问题描述:
我想汇总几个rss订阅源,当我尝试转换从feedds获取的发布日期时,由于日期格式如下 'Wed, 5 May 2010 14:27:37 BST'
。从Rss订阅源转换日期
我曾尝试使用的代码片段,我发现这里转换为RFC822日期时间,但它仍然无法正常工作(为obviousreasons)。有谁知道我可以在这个净
答
我写了一个小片段,不支持所有格式,但许多。我很乐意收到反馈或改进...
/// <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;
}
答
“BST”在时间转换为DateTime对象表示英国夏令时间,巴西标准时间或白令夏令时。如果你知道时间是“编码”的时区,你可以得到它来解析它。我认为英国夏令时我的样品中:
var date = DateTime.Parse("Wed, 5 May 2010 14:27:37", CultureInfo.GetCultureInfo("En-GB"));
单从时间字符串的结束“杀”字“BST”,并得到Britsh文化信息,以parese的日期和时间。
+0
谢谢,但由于其从RSS源的,我不知道,这将永远是BST或将B将是巴西,英国或白令!不过谢谢。 –
嗨,我在这里找到答案http://hashfactor.wordpress.com/2009/02/02/c-parsing-datetime-with-timezone/ 该类完美工作,不会抛出任何意外的异常。我只有按照说明书来构建一个类,它的工作:) –