diff --git a/CHANGELOG.md b/CHANGELOG.md index baf4273..893dec5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 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 CultureInfo extensions for converting between 12 hour and 24 hour time formatting ### Changed - The Root Config path in Net Standard will now be the ICD.Connect folder in the current environments ProgramData directory diff --git a/ICD.Common.Utils/Extensions/CultureInfoExtensions.cs b/ICD.Common.Utils/Extensions/CultureInfoExtensions.cs index 4199c54..20f9053 100644 --- a/ICD.Common.Utils/Extensions/CultureInfoExtensions.cs +++ b/ICD.Common.Utils/Extensions/CultureInfoExtensions.cs @@ -1,4 +1,5 @@ -using System.Globalization; +using System; +using System.Globalization; namespace ICD.Common.Utils.Extensions { @@ -11,7 +12,79 @@ namespace ICD.Common.Utils.Extensions /// public static bool Uses24HourFormat(this CultureInfo extends) { + if (extends == null) + throw new ArgumentNullException("extends"); + return extends.DateTimeFormat.ShortTimePattern.Contains("H"); } + + /// + /// Updates the time patterns for the given culture to use 12 hour time. + /// + /// + 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); + } + + /// + /// Updates the time patterns for the given culture to use 24 hour time. + /// + /// + 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); + } + + /// + /// Converts the given format pattern to use 24 hour time. + /// + /// + /// + private static string To24HourPattern(string pattern) + { + if (pattern == null) + return null; + + pattern = pattern.Replace(" tt", ""); + + return pattern.Replace("h", "H"); + } + + /// + /// Converts the given format pattern to use 12 hour time. + /// + /// + /// + private static string To12HourPattern(string pattern) + { + if (pattern == null) + return null; + + if (!pattern.Contains("t")) + pattern = pattern + " tt"; + + return pattern.Replace("H", "h"); + } } }