当前位置: 首页>>代码示例>>C#>>正文


C# IFormatProvider.ToCultureInfo方法代码示例

本文整理汇总了C#中IFormatProvider.ToCultureInfo方法的典型用法代码示例。如果您正苦于以下问题:C# IFormatProvider.ToCultureInfo方法的具体用法?C# IFormatProvider.ToCultureInfo怎么用?C# IFormatProvider.ToCultureInfo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IFormatProvider的用法示例。


在下文中一共展示了IFormatProvider.ToCultureInfo方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Parse

        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);
        }
开发者ID:sebastianhaba,项目名称:api,代码行数:49,代码来源:DateTimeParsing.cs


注:本文中的IFormatProvider.ToCultureInfo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。