Пример #1
0
        public static DateTime Parse(string s, IFormatProvider provider, DateTimeStyles style)
        {
            if (s == null)
            {
                throw new ArgumentNullException("s");
            }

            s = s.Trim();

            // try all .NET date time formats
            // Note: the Date/Time handling in java is just broken, and putting a
            //       .NET compatibility layer on top of it will probalby not fix much
            var ci = provider.ToCultureInfo();

            string[] formats =
            {
                ci.DateTimeFormat.UniversalSortableDateTimePattern,
                ci.DateTimeFormat.SortableDateTimePattern,
                ci.DateTimeFormat.FullDateTimePattern,
                ci.DateTimeFormat.RFC1123Pattern,
                ci.DateTimeFormat.LongDatePattern,
                ci.DateTimeFormat.LongTimePattern,
                ci.DateTimeFormat.ShortDatePattern,
                ci.DateTimeFormat.ShortTimePattern
            };

            var utc = TimeZone.GetTimeZone("UTC");

            // Note: I'm not sure this whole approach is the best anyway. Exception handling
            //       for control flow, a loop to tests all formats and such.
            //       It would probably better to use jodatime or nodatime. Maybe the parsing
            //       routine can be ripped off nodatime, so we don't have to include the whole
            //       library.

            foreach (var pattern in formats)
            {
                try
                {
                    var formatter = DateFormatFactory.GetFormat(pattern, DateTimeKind.Unspecified, provider);
                    formatter.Format.TimeZone = utc; // reset mutable value

                    Date parsed = formatter.Format.Parse(s);
                    var  result = FromParsedDate(parsed, s, style, formatter.Format, formatter.ContainsK, formatter.UseUtc);
                    return(result);
                }
                catch (ParseException)
                {
                }
            }
            throw new ArgumentException("unable to parse " + s);
        }
Пример #2
0
        public static DateTime Parse(string s, IFormatProvider provider, DateTimeStyles style)
        {
            if (s == null)
                throw new ArgumentNullException("s");

            s = s.Trim();

            // try all .NET date time formats
            // Note: the Date/Time handling in java is just broken, and putting a 
            //       .NET compatibility layer on top of it will probalby not fix much
            var ci = provider.ToCultureInfo();

            string[] formats = 
            {
                ci.DateTimeFormat.UniversalSortableDateTimePattern,
                ci.DateTimeFormat.SortableDateTimePattern,
                ci.DateTimeFormat.FullDateTimePattern,
                ci.DateTimeFormat.RFC1123Pattern,
                ci.DateTimeFormat.LongDatePattern,
                ci.DateTimeFormat.LongTimePattern,
                ci.DateTimeFormat.ShortDatePattern,
                ci.DateTimeFormat.ShortTimePattern
            };

            var utc = TimeZone.GetTimeZone("UTC");

            // Note: I'm not sure this whole approach is the best anyway. Exception handling
            //       for control flow, a loop to tests all formats and such.
            //       It would probably better to use jodatime or nodatime. Maybe the parsing
            //       routine can be ripped off nodatime, so we don't have to include the whole
            //       library.

            foreach (var pattern in formats)
            {
                try
                {
                    var formatter = DateFormatFactory.GetFormat(pattern, DateTimeKind.Unspecified, provider);
                    formatter.Format.TimeZone = utc; // reset mutable value

                    Date parsed = formatter.Format.Parse(s);
                    var result = FromParsedDate(parsed, s, style, formatter.Format, formatter.ContainsK, formatter.UseUtc);
                    return result;
                }
                catch (ParseException)
                {
                }
            }
            throw new ArgumentException("unable to parse " + s);
        }