Parse Rfc 2822 Date : Time Span Parse « Date Time « C# / C Sharp






Parse Rfc 2822 Date

      
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Diagnostics;

namespace Opo.Net.Mime
{
    public static class MimeUtilities
    {
        public static DateTime ParseRfc2822Date(string date)
        {
            date = date.Replace("bst", "+0100");
            date = date.Replace("gmt", "-0000");
            date = date.Replace("edt", "-0400");
            date = date.Replace("est", "-0500");
            date = date.Replace("cdt", "-0500");
            date = date.Replace("cst", "-0600");
            date = date.Replace("mdt", "-0600");
            date = date.Replace("mst", "-0700");
            date = date.Replace("pdt", "-0700");
            date = date.Replace("pst", "-0800");

            DateTime parsedDateTime = DateTime.MinValue;

            Regex r = new Regex(@"(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun), )?(?<DateTime>\d{1,2} (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{4} \d{2}\:\d{2}(?:\:\d{2})?)(?: (?<TimeZone>[\+-]\d{4}))?", RegexOptions.IgnoreCase);
            Match m = r.Match(date);
            if (m.Success)
            {
                string dateTime = m.Groups["DateTime"].Value.TrimStart('0');
                parsedDateTime = DateTime.ParseExact(dateTime, new string[] { "d MMM yyyy HH:mm", "d MMM yyyy HH:mm:ss", "d MMM yyyy hh:mm", "d MMM yyyy hh:mm:ss" }, CultureInfo.InvariantCulture, DateTimeStyles.None);

                string timeZone = m.Groups["TimeZone"].Value;
                if (timeZone.Length == 5)
                {
                    int hour = Int32.Parse(timeZone.Substring(0, 3));
                    int minute = Int32.Parse(timeZone.Substring(3));
                    TimeSpan offset = new TimeSpan(hour, minute, 0);
                    parsedDateTime = new DateTimeOffset(parsedDateTime, offset).UtcDateTime;
                }
            }
            return parsedDateTime;
        }
        public static string ToRfc2822Date(this DateTime date)
        {
            //TODO: Implement conversion to RFC2822 Date (offset)
            TimeSpan offset = TimeZoneInfo.Local.BaseUtcOffset;
            Debug.WriteLine(offset.ToString());
            //DateTimeOffset dateTimeOffset = new DateTimeOffset(date, offset);
            return date.ToString("ddd, dd MMM yyyy HH:mm:ss +") + offset.Hours.ToString().PadRight('0') + "00";
        }
    }
}

   
    
    
    
    
    
  








Related examples in the same category

1.Parse string to TimeSpan
2.Parse string to TimeSpan: "18:31.05"
3.Parse string to TimeSpan: "12.035"
4.TimeSpan.Parse("0:0:3.669")
5.Parse strings to TimeSpan
6.Parse hour:minute value with "g" specifier current culture.
7.Parse hour:minute:second value with "G" specifier.
8.Parse hours:minute.second value with "G" specifier and current (en-US) culture.
9.Parse days:hours:minute.second value with "G" specifier and current (en-US) culture.
10.Parse days:hours:minute.second value with "G" specifier and fr-FR culture.
11.Parse a single number using the "c" standard format string.
12.Parse a single number using the "%h" custom format string.
13.Is Date valid