Это может быть и то, и другое, так как нет ничего, чтобы предотвратить культуру с использованием 12 для ToShortTimeString()
и 24 для ToLongTimeString()
или наоборот.
Однако, принимая во внимание, что вызов ToShortTimeString()
такой же, как вызов DateTimeFormat.Format(this, "t", DateTimeFormatInfo.CurrentInfo)
, то мы можем использовать это с помощью метода из this answer:
[Flags]
public enum HourRepType
{
None = 0,
Twelve = 1,
TwentyFour = 2,
Both = Twelve | TwentyFour
}
public static HourRepType FormatStringHourType(string format, CultureInfo culture = null)
{
if(string.IsNullOrEmpty(format))
format = "G";//null or empty is treated as general, long time.
if(culture == null)
culture = CultureInfo.CurrentCulture;//allow null as a shortcut for this
if(format.Length == 1)
switch(format)
{
case "O": case "o": case "R": case "r": case "s": case "u":
return HourRepType.TwentyFour;//always the case for these formats.
case "m": case "M": case "y": case "Y":
return HourRepType.None;//always the case for these formats.
case "d":
return CustomFormatStringHourType(culture.DateTimeFormat.ShortDatePattern);
case "D":
return CustomFormatStringHourType(culture.DateTimeFormat.LongDatePattern);
case "f":
return CustomFormatStringHourType(culture.DateTimeFormat.LongDatePattern + " " + culture.DateTimeFormat.ShortTimePattern);
case "F":
return CustomFormatStringHourType(culture.DateTimeFormat.FullDateTimePattern);
case "g":
return CustomFormatStringHourType(culture.DateTimeFormat.ShortDatePattern + " " + culture.DateTimeFormat.ShortTimePattern);
case "G":
return CustomFormatStringHourType(culture.DateTimeFormat.ShortDatePattern + " " + culture.DateTimeFormat.LongTimePattern);
case "t":
return CustomFormatStringHourType(culture.DateTimeFormat.ShortTimePattern);
case "T":
return CustomFormatStringHourType(culture.DateTimeFormat.LongTimePattern);
default:
throw new FormatException();
}
return CustomFormatStringHourType(format);
}
private static HourRepType CustomFormatStringHourType(string format)
{
format = new Regex(@"('.*')|("".*"")|(\\.)").Replace(format, "");//remove literals
if(format.Contains("H"))
return format.Contains("h") ? HourRepType.Both : HourRepType.TwentyFour;
return format.Contains("h") ? HourRepType.Twelve : HourRepType.None;
}
Позвони FormatStringHourType("t")
, чтобы выяснить, является ли это 12 или 24 часов (или возможно, ни то, ни другое, но это было бы странно).
Аналогичным образом FormatStringHourType("T")
расскажет нам о длинной строке.
Один из способов использования - использовать ShortTimePattern и проверять, соответствует ли он спецификатору «tt». Но большинство из них не содержат «tt». Это означает, что мы будем использовать формат времени 24 часа. Или мы можем знать, поддерживает ли рынок только 24-часовой формат времени? – pthread