Skip to content

Commit

Permalink
fix calendar errors
Browse files Browse the repository at this point in the history
  • Loading branch information
j-troc authored and j-troc committed Feb 24, 2020
1 parent 1bcaaf7 commit 6994ae9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 27 deletions.
20 changes: 7 additions & 13 deletions source/icu.net/Calendar/Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,17 +320,13 @@ public void SetTimeZone(TimeZone timezone)
/// <summary>
/// Sets the calendar's time zone to be equivalent to the one passed in.
/// </summary>
/// <param name="timezone">The given time zone info.</param>
/// <param name="timeZoneInfo">The given time zone info.</param>
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);
}
Expand All @@ -341,13 +337,11 @@ public void SetTimeZone(TimeZoneInfo timeZoneInfo)
/// <returns>Time zone set for this calendar.</returns>
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<ErrorCode, int>(status, length);
}, 255);
return new TimeZone(result);
}

Expand Down
16 changes: 2 additions & 14 deletions source/icu.net/NativeMethods/NativeMethods_Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CalendarMehodsContainer.ucal_getTimeZoneIdDelegate>(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(
Expand Down

0 comments on commit 6994ae9

Please sign in to comment.