Files
mono/mcs/class/System.Core/System/TimeZoneInfo.MonoTouch.cs
T
Rolf Bjarne Kvinge 550d314e51 Don't throw TimeZoneNotFoundExceptions when iOS gives us invalid data.
iOS 8 GM lists "Asia/Chita" among its time zones, but when requesting
the time zone data for that time zone, no data is returned.

With this change we ignore time zones without any data when fetching
all the timezones on the system.
2014-09-11 19:04:00 +02:00

89 lines
2.8 KiB
C#

//
// System.TimeZoneInfo helper for MonoTouch
// because the devices cannot access the file system to read the data
//
// Authors:
// Sebastien Pouliot <[email protected]>
//
// Copyright 2011-2013 Xamarin Inc.
//
// The class can be either constructed from a string (from user code)
// or from a handle (from iphone-sharp.dll internal calls). This
// delays the creation of the actual managed string until actually
// required
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if (INSIDE_CORLIB && MONOTOUCH)
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.InteropServices;
namespace System {
public partial class TimeZoneInfo {
[DllImport ("__Internal")]
extern static IntPtr monotouch_timezone_get_names (ref int count);
static ReadOnlyCollection<string> GetMonoTouchNames ()
{
int count = 0;
IntPtr array = monotouch_timezone_get_names (ref count);
string [] names = new string [count];
for (int i = 0, offset = 0; i < count; i++, offset += IntPtr.Size) {
IntPtr p = Marshal.ReadIntPtr (array, offset);
names [i] = Marshal.PtrToStringAnsi (p);
Marshal.FreeHGlobal (p);
}
Marshal.FreeHGlobal (array);
return new ReadOnlyCollection<string> (names);
}
[DllImport ("__Internal")]
extern static IntPtr monotouch_timezone_get_data (string name, ref int size);
static Stream GetMonoTouchData (string name, bool throw_on_error = true)
{
int size = 0;
IntPtr data = monotouch_timezone_get_data (name, ref size);
if (size <= 0) {
if (throw_on_error)
throw new TimeZoneNotFoundException ();
return null;
}
unsafe {
var s = new UnmanagedMemoryStream ((byte*) data, size);
s.Closed += delegate {
Marshal.FreeHGlobal (data);
};
return s;
}
}
}
}
#endif