本文整理汇总了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);
}