コード例 #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 ParseExact(string s, string format, IFormatProvider provider, DateTimeStyles style)
        {
            // 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
            if (s == null || format == null)
            {
                throw new ArgumentNullException();
            }

            if ((style & DateTimeStyles.AllowLeadingWhite) != 0)
            {
                s = s.TrimStart();
            }
            if ((style & DateTimeStyles.AllowTrailingWhite) != 0)
            {
                s = s.TrimEnd();
            }
            if ((style & DateTimeStyles.AllowWhiteSpaces) != 0)
            {
                s = s.Trim();
            }

            try
            {
                var formatter = DateFormatFactory.GetFormat(format, DateTimeKind.Unspecified, provider);
                formatter.Format.TimeZone = TimeZone.GetTimeZone("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 (ArgumentException ex)
            {
                throw new FormatException(ex.Message);
            }
            catch (ParseException ex)
            {
                throw new ArgumentException(ex.Message, "s");
            }
        }