Пример #1
0
        public static bool TryParseRfc822DateTime(string value, out DateTime result)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            DateTimeFormatInfo dateTimeFormat = CultureInfo.InvariantCulture.DateTimeFormat;

            string[] formats = new string[3];

            //------------------------------------------------------------
            //	Define valid RFC-822 formats
            //------------------------------------------------------------
            formats[0] = dateTimeFormat.RFC1123Pattern;
            formats[1] = "ddd',' d MMM yyyy HH:mm:ss zzz";
            formats[2] = "ddd',' dd MMM yyyy HH:mm:ss zzz";

            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            if (String.IsNullOrEmpty(value))
            {
                result = DateTime.MinValue;
                return(false);
            }

            //------------------------------------------------------------
            //	Perform conversion of RFC-822 formatted date-time string
            //------------------------------------------------------------
            return(DateTime.TryParseExact(SyndicationDateTimeUtility.ReplaceRfc822TimeZoneWithOffset(value), formats, dateTimeFormat, DateTimeStyles.None, out result));
        }
        public static bool TryParseRfc822DateTime(string value, out DateTime result)
        {
            // patterns from http://stackoverflow.com/questions/284775/how-do-i-parse-and-convert-datetimes-to-the-rfc-822-date-time-format
            DateTimeFormatInfo dateTimeFormat = CultureInfo.InvariantCulture.DateTimeFormat;

            string[] formats = new string[36];

            // two-digit day, four-digit year patterns
            formats[0] = "ddd',' dd MMM yyyy HH':'mm':'ss'.'fffffff zzzz";
            formats[1] = "ddd',' dd MMM yyyy HH':'mm':'ss'.'ffffff zzzz";
            formats[2] = "ddd',' dd MMM yyyy HH':'mm':'ss'.'fffff zzzz";
            formats[3] = "ddd',' dd MMM yyyy HH':'mm':'ss'.'ffff zzzz";
            formats[4] = "ddd',' dd MMM yyyy HH':'mm':'ss'.'fff zzzz";
            formats[5] = "ddd',' dd MMM yyyy HH':'mm':'ss'.'ff zzzz";
            formats[6] = "ddd',' dd MMM yyyy HH':'mm':'ss'.'f zzzz";
            formats[7] = "ddd',' dd MMM yyyy HH':'mm':'ss zzzz";

            // two-digit day, two-digit year patterns
            formats[8]  = "ddd',' dd MMM yy HH':'mm':'ss'.'fffffff zzzz";
            formats[9]  = "ddd',' dd MMM yy HH':'mm':'ss'.'ffffff zzzz";
            formats[10] = "ddd',' dd MMM yy HH':'mm':'ss'.'fffff zzzz";
            formats[11] = "ddd',' dd MMM yy HH':'mm':'ss'.'ffff zzzz";
            formats[12] = "ddd',' dd MMM yy HH':'mm':'ss'.'fff zzzz";
            formats[13] = "ddd',' dd MMM yy HH':'mm':'ss'.'ff zzzz";
            formats[14] = "ddd',' dd MMM yy HH':'mm':'ss'.'f zzzz";
            formats[15] = "ddd',' dd MMM yy HH':'mm':'ss zzzz";

            // one-digit day, four-digit year patterns
            formats[16] = "ddd',' d MMM yyyy HH':'mm':'ss'.'fffffff zzzz";
            formats[17] = "ddd',' d MMM yyyy HH':'mm':'ss'.'ffffff zzzz";
            formats[18] = "ddd',' d MMM yyyy HH':'mm':'ss'.'fffff zzzz";
            formats[19] = "ddd',' d MMM yyyy HH':'mm':'ss'.'ffff zzzz";
            formats[20] = "ddd',' d MMM yyyy HH':'mm':'ss'.'fff zzzz";
            formats[21] = "ddd',' d MMM yyyy HH':'mm':'ss'.'ff zzzz";
            formats[22] = "ddd',' d MMM yyyy HH':'mm':'ss'.'f zzzz";
            formats[23] = "ddd',' d MMM yyyy HH':'mm':'ss zzzz";

            // two-digit day, two-digit year patterns
            formats[24] = "ddd',' d MMM yy HH':'mm':'ss'.'fffffff zzzz";
            formats[25] = "ddd',' d MMM yy HH':'mm':'ss'.'ffffff zzzz";
            formats[26] = "ddd',' d MMM yy HH':'mm':'ss'.'fffff zzzz";
            formats[27] = "ddd',' d MMM yy HH':'mm':'ss'.'ffff zzzz";
            formats[28] = "ddd',' d MMM yy HH':'mm':'ss'.'fff zzzz";
            formats[29] = "ddd',' d MMM yy HH':'mm':'ss'.'ff zzzz";
            formats[30] = "ddd',' d MMM yy HH':'mm':'ss'.'f zzzz";
            formats[31] = "ddd',' d MMM yy HH':'mm':'ss zzzz";

            // Fall back patterns
            formats[32] = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK"; // RoundtripDateTimePattern
            formats[33] = DateTimeFormatInfo.InvariantInfo.UniversalSortableDateTimePattern;
            formats[34] = DateTimeFormatInfo.InvariantInfo.SortableDateTimePattern;
            formats[35] = dateTimeFormat.RFC1123Pattern;

            if (String.IsNullOrEmpty(value))
            {
                result = DateTime.MinValue;
                return(false);
            }

            if (DateTime.TryParseExact(SyndicationDateTimeUtility.ReplaceRfc822TimeZoneWithOffset(value), formats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal, out result))
            {
                return(true);
            }
            if (DateTime.TryParse(SyndicationDateTimeUtility.ReplaceRfc822TimeZoneWithOffset(value), out result))
            {
                return(true);
            }

            return(false);
        }