Fall back to calling Java.Util.TimeZone.Default.ID if the persist.sys.timezone system

property is not present on the system or its value is an empty string. This ensures that
we obtain valid timezone info on systems which do not define the above property.

Part of fix for https://bugzilla.xamarin.com/show_bug.cgi?id=4902
This commit is contained in:
Marek Habersack committed 2014-04-16 16:23:16 +02:00
1 parent c7c6506ce3
commit 5564dad150
2 files changed
+43 -11

No files matched your search

@@ -398,6 +398,12 @@ namespace System {
FileStream GetTimeZoneData (string name, out int start, out int length)
{
if (name == null) { // Just in case, to avoid NREX as in xambug #4902
start = 0;
length = 0;
return null;
}
var f = new FileInfo (Path.Combine (zoneRoot, name));
if (f.Exists) {
start = 0;
@@ -536,20 +542,34 @@ namespace System {
}
}
}
[DllImport ("__Internal")]
static extern int monodroid_get_system_property (string name, ref IntPtr value);
// <sys/system_properties.h>
[DllImport ("/system/lib/libc.so")]
static extern int __system_property_get (string name, StringBuilder value);
const int MaxPropertyNameLength = 32; // <sys/system_properties.h>
const int MaxPropertyValueLength = 92; // <sys/system_properties.h>
[DllImport ("__Internal")]
static extern void monodroid_free (IntPtr ptr);
static string GetDefaultTimeZoneName ()
{
var buf = new StringBuilder (MaxPropertyValueLength + 1);
int n = __system_property_get ("persist.sys.timezone", buf);
if (n > 0)
return buf.ToString ();
IntPtr value = IntPtr.Zero;
int n = 0;
string defaultTimeZone;
// Used by the tests
if (Environment.GetEnvironmentVariable ("__XA_USE_JAVA_DEFAULT_TIMEZONE_ID__") == null)
n = monodroid_get_system_property ("persist.sys.timezone", ref value);
if (n > 0 && value != IntPtr.Zero) {
defaultTimeZone = (Marshal.PtrToStringAnsi (value) ?? String.Empty).Trim ();
monodroid_free (value);
if (!String.IsNullOrEmpty (defaultTimeZone))
return defaultTimeZone;
}
defaultTimeZone = (AndroidPlatform.GetDefaultTimeZone () ?? String.Empty).Trim ();
if (!String.IsNullOrEmpty (defaultTimeZone))
return defaultTimeZone;
return null;
}
@@ -34,6 +34,7 @@ namespace System {
internal class AndroidPlatform {
static readonly Func<SynchronizationContext> getDefaultSyncContext;
static readonly Func<string> getDefaultTimeZone;
static AndroidPlatform ()
{
@@ -42,12 +43,23 @@ namespace System {
Type.GetType ("Android.Runtime.AndroidEnvironment, Mono.Android", true)
.GetMethod ("GetDefaultSyncContext",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic));
getDefaultTimeZone = (Func<string>)
Delegate.CreateDelegate (typeof(Func<string>),
Type.GetType ("Android.Runtime.AndroidEnvironment, Mono.Android", true)
.GetMethod ("GetDefaultTimeZone",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic));
}
internal static SynchronizationContext GetDefaultSyncContext ()
{
return getDefaultSyncContext ();
}
internal static string GetDefaultTimeZone ()
{
return getDefaultTimeZone ();
}
}
}
#endif