Last active
August 29, 2015 14:13
-
-
Save jamie94bc/7479d2c61bcab86919e7 to your computer and use it in GitHub Desktop.
A workaround for Xamarin.Android date/time format bug https://bugzilla.xamarin.com/show_bug.cgi?id=23544
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class DateTimeFormat { | |
public static bool Is24Hour { | |
get { return CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern.Contains("H"); } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SplashScreen : Activity { | |
protected override void OnCreate(Bundle bundle) { | |
base.OnCreate(bundle); | |
try { | |
var shouldBe24Hour = DateFormat.Is24HourFormat(this); | |
var monoIs24Hour = DateTimeFormat.Is24Hour; | |
if (shouldBe24Hour && !monoIs24Hour) { | |
var newCultureInfo = new CultureInfo(CultureInfo.CurrentCulture.Name); | |
var newUiCultureInfo = new CultureInfo(CultureInfo.CurrentUICulture.Name); | |
Ensure24Hours(newCultureInfo.DateTimeFormat); | |
Ensure24Hours(newUiCultureInfo.DateTimeFormat); | |
Thread.CurrentThread.CurrentCulture = newCultureInfo; | |
Thread.CurrentThread.CurrentUICulture = newUiCultureInfo; | |
CultureInfo.DefaultThreadCurrentCulture = newCultureInfo; | |
CultureInfo.DefaultThreadCurrentUICulture = newUiCultureInfo; | |
} | |
else if (!shouldBe24Hour && monoIs24Hour) { | |
var newCultureInfo = new CultureInfo(CultureInfo.CurrentCulture.Name); | |
var newUiCultureInfo = new CultureInfo(CultureInfo.CurrentUICulture.Name); | |
Ensure12Hours(newCultureInfo.DateTimeFormat); | |
Ensure12Hours(newUiCultureInfo.DateTimeFormat); | |
Thread.CurrentThread.CurrentCulture = newCultureInfo; | |
Thread.CurrentThread.CurrentUICulture = newUiCultureInfo; | |
CultureInfo.DefaultThreadCurrentCulture = newCultureInfo; | |
CultureInfo.DefaultThreadCurrentUICulture = newUiCultureInfo; | |
} | |
} | |
// ReSharper disable once EmptyGeneralCatchClause | |
catch {} | |
} | |
private void Ensure24Hours(DateTimeFormatInfo dtf) { | |
dtf.LongTimePattern = "HH:mm:ss"; | |
dtf.ShortTimePattern = "HH:mm"; | |
var index = dtf.FullDateTimePattern.IndexOf("h", StringComparison.Ordinal); | |
if (index > -1) { | |
dtf.FullDateTimePattern = dtf.FullDateTimePattern.Substring(0, index) + dtf.LongTimePattern; | |
} | |
} | |
private void Ensure12Hours(DateTimeFormatInfo dtf) { | |
dtf.LongTimePattern = "h:mm:ss tt"; | |
dtf.ShortTimePattern = "h:mm tt"; | |
var index = dtf.FullDateTimePattern.IndexOf("H", StringComparison.Ordinal); | |
if (index > -1) { | |
dtf.FullDateTimePattern = dtf.FullDateTimePattern.Substring(0, index) + dtf.LongTimePattern; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment