mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-13 19:55:02 +00:00
91 lines
2.6 KiB
C#
91 lines
2.6 KiB
C#
using System;
|
|
using System.Globalization;
|
|
|
|
namespace ICD.Common.Utils.Extensions
|
|
{
|
|
public static class CultureInfoExtensions
|
|
{
|
|
/// <summary>
|
|
/// Returns true if the given culture uses a 24 hour time format.
|
|
/// </summary>
|
|
/// <param name="extends"></param>
|
|
/// <returns></returns>
|
|
public static bool Uses24HourFormat(this CultureInfo extends)
|
|
{
|
|
if (extends == null)
|
|
throw new ArgumentNullException("extends");
|
|
|
|
return extends.DateTimeFormat.ShortTimePattern.Contains("H");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the time patterns for the given culture to use 12 hour time.
|
|
/// </summary>
|
|
/// <param name="extends"></param>
|
|
public static void ConvertTo12HourCulture(this CultureInfo extends)
|
|
{
|
|
if (extends == null)
|
|
throw new ArgumentNullException("extends");
|
|
|
|
if (extends.IsReadOnly)
|
|
throw new InvalidOperationException("Culture is readonly");
|
|
|
|
DateTimeFormatInfo dateTimeFormat = extends.DateTimeFormat;
|
|
|
|
dateTimeFormat.FullDateTimePattern = To12HourPattern(dateTimeFormat.FullDateTimePattern);
|
|
dateTimeFormat.LongTimePattern = To12HourPattern(dateTimeFormat.LongTimePattern);
|
|
dateTimeFormat.ShortTimePattern = To12HourPattern(dateTimeFormat.ShortTimePattern);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the time patterns for the given culture to use 24 hour time.
|
|
/// </summary>
|
|
/// <param name="extends"></param>
|
|
public static void ConvertTo24HourCulture(this CultureInfo extends)
|
|
{
|
|
if (extends == null)
|
|
throw new ArgumentNullException("extends");
|
|
|
|
if (extends.IsReadOnly)
|
|
throw new InvalidOperationException("Culture is readonly");
|
|
|
|
DateTimeFormatInfo dateTimeFormat = extends.DateTimeFormat;
|
|
|
|
dateTimeFormat.FullDateTimePattern = To24HourPattern(dateTimeFormat.FullDateTimePattern);
|
|
dateTimeFormat.LongTimePattern = To24HourPattern(dateTimeFormat.LongTimePattern);
|
|
dateTimeFormat.ShortTimePattern = To24HourPattern(dateTimeFormat.ShortTimePattern);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts the given format pattern to use 24 hour time.
|
|
/// </summary>
|
|
/// <param name="pattern"></param>
|
|
/// <returns></returns>
|
|
private static string To24HourPattern(string pattern)
|
|
{
|
|
if (pattern == null)
|
|
return null;
|
|
|
|
pattern = pattern.Replace(" tt", "");
|
|
|
|
return pattern.Replace("h", "H");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts the given format pattern to use 12 hour time.
|
|
/// </summary>
|
|
/// <param name="pattern"></param>
|
|
/// <returns></returns>
|
|
private static string To12HourPattern(string pattern)
|
|
{
|
|
if (pattern == null)
|
|
return null;
|
|
|
|
if (!pattern.Contains("t"))
|
|
pattern = pattern + " tt";
|
|
|
|
return pattern.Replace("H", "h");
|
|
}
|
|
}
|
|
}
|