mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 21:24:58 +00:00
feat: Added CultureInfo extensions for converting between 12 hour and 24 hour time formatting
This commit is contained in:
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||||||
- Added math util method for modulus
|
- Added math util method for modulus
|
||||||
- Added TimeSpan extension methods for cycling hours and minutes without modifying the day
|
- Added TimeSpan extension methods for cycling hours and minutes without modifying the day
|
||||||
- Added a dictionary extension method for getting or adding a new value via func
|
- Added a dictionary extension method for getting or adding a new value via func
|
||||||
|
- Added CultureInfo extensions for converting between 12 hour and 24 hour time formatting
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- The Root Config path in Net Standard will now be the ICD.Connect folder in the current environments ProgramData directory
|
- The Root Config path in Net Standard will now be the ICD.Connect folder in the current environments ProgramData directory
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Globalization;
|
using System;
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
namespace ICD.Common.Utils.Extensions
|
namespace ICD.Common.Utils.Extensions
|
||||||
{
|
{
|
||||||
@@ -11,7 +12,79 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static bool Uses24HourFormat(this CultureInfo extends)
|
public static bool Uses24HourFormat(this CultureInfo extends)
|
||||||
{
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
return extends.DateTimeFormat.ShortTimePattern.Contains("H");
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user