Parse RFC 822 (commonly used for RSS) string to System.DateTime. - CSharp System

CSharp examples for System:DateTime

Description

Parse RFC 822 (commonly used for RSS) string to System.DateTime.

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*ww  w  .j a  v a2s  .  co m*/

public class Main{
        /// <summary>Parse RFC 822 (commonly used for RSS) string to System.DateTime.</summary>
    /// <remarks>Extension method.</remarks>
    public static DateTime ToDateTimeFromRfc822(this string s)
    {
        DateTime dt;
        int pos = s.LastIndexOf(" ");
        try {
            dt = Convert.ToDateTime(s);
            if(s.Substring(pos + 1) == "Z") {
                dt = dt.ToUniversalTime();
            } else if(s.Substring(pos + 1) == "GMT") {
                dt = dt.ToUniversalTime();
            }
            return dt;
        } catch(System.Exception ex) { 
            System.Diagnostics.Trace.WriteLine(ex.Message);
        }
        // Do alternative.
        dt = Convert.ToDateTime(s.Substring(0, pos));
        if(s[pos + 1] == '+') {
            int h = Convert.ToInt32(s.Substring(pos + 2, 2));
            dt = dt.AddHours(-h);
            int m = Convert.ToInt32(s.Substring(pos + 4, 2));
            dt = dt.AddMinutes(-m);
        } else if(s[pos + 1] == '-') {
            int h = Convert.ToInt32(s.Substring(pos + 2, 2));
            dt = dt.AddHours(h);
            int m = Convert.ToInt32(s.Substring(pos + 4, 2));
            dt = dt.AddMinutes(m);
        } else if(s.Substring(pos + 1) == "A") {
            dt = dt.AddHours(1);
        } else if(s.Substring(pos + 1) == "B") {
            dt = dt.AddHours(2);
        } else if(s.Substring(pos + 1) == "C") {
            dt = dt.AddHours(3);
        } else if(s.Substring(pos + 1) == "D") {
            dt = dt.AddHours(4);
        } else if(s.Substring(pos + 1) == "E") {
            dt = dt.AddHours(5);
        } else if(s.Substring(pos + 1) == "F") {
            dt = dt.AddHours(6);
        } else if(s.Substring(pos + 1) == "G") {
            dt = dt.AddHours(7);
        } else if(s.Substring(pos + 1) == "H") {
            dt = dt.AddHours(8);
        } else if(s.Substring(pos + 1) == "I") {
            dt = dt.AddHours(9);
        } else if(s.Substring(pos + 1) == "K") {
            dt = dt.AddHours(10);
        } else if(s.Substring(pos + 1) == "L") {
            dt = dt.AddHours(11);
        } else if(s.Substring(pos + 1) == "M") {
            dt = dt.AddHours(12);
        } else if(s.Substring(pos + 1) == "N") {
            dt = dt.AddHours(-1);
        } else if(s.Substring(pos + 1) == "O") {
            dt = dt.AddHours(-2);
        } else if(s.Substring(pos + 1) == "P") {
            dt = dt.AddHours(-3);
        } else if(s.Substring(pos + 1) == "Q") {
            dt = dt.AddHours(-4);
        } else if(s.Substring(pos + 1) == "R") {
            dt = dt.AddHours(-5);
        } else if(s.Substring(pos + 1) == "S") {
            dt = dt.AddHours(-6);
        } else if(s.Substring(pos + 1) == "T") {
            dt = dt.AddHours(-7);
        } else if(s.Substring(pos + 1) == "U") {
            dt = dt.AddHours(-8);
        } else if(s.Substring(pos + 1) == "V") {
            dt = dt.AddHours(-9);
        } else if(s.Substring(pos + 1) == "W") {
            dt = dt.AddHours(-10);
        } else if(s.Substring(pos + 1) == "X") {
            dt = dt.AddHours(-11);
        } else if(s.Substring(pos + 1) == "Y") {
            dt = dt.AddHours(-12);
        } else if(s.Substring(pos + 1) == "EST") {
            dt = dt.AddHours(5);
        } else if(s.Substring(pos + 1) == "EDT") {
            dt = dt.AddHours(4);
        } else if(s.Substring(pos + 1) == "CST") {
            dt = dt.AddHours(6);
        } else if(s.Substring(pos + 1) == "CDT") {
            dt = dt.AddHours(5);
        } else if(s.Substring(pos + 1) == "MST") {
            dt = dt.AddHours(7);
        } else if(s.Substring(pos + 1) == "MDT") {
            dt = dt.AddHours(6);
        } else if(s.Substring(pos + 1) == "PST") {
            dt = dt.AddHours(8);
        } else if(s.Substring(pos + 1) == "PDT") {
            dt = dt.AddHours(7);
        }
        return dt;
    }
}

Related Tutorials