From b5606f0787d3e1cdfe45c10d03f8a1aea2236dd4 Mon Sep 17 00:00:00 2001 From: j-troc Date: Fri, 19 Apr 2019 17:40:40 +0200 Subject: [PATCH] fix calendar errors --- source/icu.net/Calendar/Calendar.cs | 20 +++++++------------ .../NativeMethods/NativeMethods_Calendar.cs | 16 ++------------- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/source/icu.net/Calendar/Calendar.cs b/source/icu.net/Calendar/Calendar.cs index 139c9e38..3a664b9c 100644 --- a/source/icu.net/Calendar/Calendar.cs +++ b/source/icu.net/Calendar/Calendar.cs @@ -320,17 +320,13 @@ public void SetTimeZone(TimeZone timezone) /// /// Sets the calendar's time zone to be equivalent to the one passed in. /// - /// The given time zone info. + /// The given time zone info. public void SetTimeZone(TimeZoneInfo timeZoneInfo) { var id = timeZoneInfo.Id; - var converted = TimeZone.GetIdForWindowsId(id, _locale?.Country); - - if (!string.IsNullOrWhiteSpace(converted)) - { - id = converted; - } + if(!TimeZone.GetTimeZones().Any(tz=>tz.Id == id)) + id = TimeZone.GetIdForWindowsId(id, _locale?.Country); SetTimeZone(id); } @@ -341,13 +337,11 @@ public void SetTimeZone(TimeZoneInfo timeZoneInfo) /// Time zone set for this calendar. public TimeZone GetTimeZone() { - int length = NativeMethods.ucal_getTimeZoneId(_calendarHandle, out string result, 32, out ErrorCode ec); - if (length >= 32) + string result = NativeMethods.GetUnicodeString((ptr, length) => { - ec = ErrorCode.NoErrors; - NativeMethods.ucal_getTimeZoneId(_calendarHandle, out result, length + 1, out ec); - } - ExceptionFromErrorCode.ThrowIfError(ec); + length = NativeMethods.ucal_getTimeZoneId(_calendarHandle, ptr, length, out ErrorCode status); + return new Tuple(status, length); + }, 255); return new TimeZone(result); } diff --git a/source/icu.net/NativeMethods/NativeMethods_Calendar.cs b/source/icu.net/NativeMethods/NativeMethods_Calendar.cs index 12451b4e..1bf037db 100644 --- a/source/icu.net/NativeMethods/NativeMethods_Calendar.cs +++ b/source/icu.net/NativeMethods/NativeMethods_Calendar.cs @@ -181,26 +181,14 @@ public static void ucal_setTimeZone( public static int ucal_getTimeZoneId( Calendar.SafeCalendarHandle cal, - out string result, + IntPtr result, int resultLength, out ErrorCode ec) { if (CalendarMethods.ucal_getTimeZoneId == null) CalendarMethods.ucal_getTimeZoneId = GetMethod(IcuI18NLibHandle, "ucal_getTimeZoneID"); - IntPtr outBuf = Marshal.AllocHGlobal(resultLength * sizeof(char)); - try - { - int length = CalendarMethods.ucal_getTimeZoneId(cal, outBuf, resultLength, out ec); - char[] buf = new char[Math.Min(resultLength, length)]; - Marshal.Copy(outBuf, buf, 0, buf.Length); - result = new string(buf); - return length; - } - finally - { - Marshal.FreeHGlobal(outBuf); - } + return CalendarMethods.ucal_getTimeZoneId(cal, result, resultLength, out ec); } public static Calendar.SafeCalendarHandle ucal_open(