diff --git a/source/icu.net.tests/CalendarTests.cs b/source/icu.net.tests/CalendarTests.cs index 4f2e0579..4321a79f 100644 --- a/source/icu.net.tests/CalendarTests.cs +++ b/source/icu.net.tests/CalendarTests.cs @@ -15,297 +15,400 @@ class CalendarTests public void GetTimeZoneDisplayNameTest() { var timezone = new TimeZone("AST"); - var cal = new GregorianCalendar(timezone); + using (var cal = new GregorianCalendar(timezone)) + { + var displayName = cal.GetTimeZoneDisplayName(Calendar.UCalendarDisplayNameType.Standard); - var displayName = cal.GetTimeZoneDisplayName(Calendar.UCalendarDisplayNameType.Standard); - - Assert.AreEqual("Alaska Standard Time", displayName); + Assert.AreEqual("Alaska Standard Time", displayName); + } } [Test] public void ClearTest() { - var cal = new GregorianCalendar(); - cal.Month = Calendar.UCalendarMonths.September; - cal.DayOfMonth = 4; + using (var cal = new GregorianCalendar()) + { + cal.Month = Calendar.UCalendarMonths.September; + cal.DayOfMonth = 4; - cal.Clear(); + cal.Clear(); - Assert.AreEqual(Calendar.UCalendarMonths.January, cal.Month); - Assert.AreEqual(1, cal.DayOfMonth); + Assert.AreEqual(Calendar.UCalendarMonths.January, cal.Month); + Assert.AreEqual(1, cal.DayOfMonth); + } } [Test] public void ClearFieldTest() { - var cal = new GregorianCalendar(); - cal.Month = Calendar.UCalendarMonths.September; + using (var cal = new GregorianCalendar()) + { + cal.Month = Calendar.UCalendarMonths.September; - cal.Clear(Calendar.UCalendarDateFields.Month); + cal.Clear(Calendar.UCalendarDateFields.Month); - Assert.AreEqual(Calendar.UCalendarMonths.January, cal.Month); + Assert.AreEqual(Calendar.UCalendarMonths.January, cal.Month); + } } [Test] public void CloneTest() { - var cal1 = new GregorianCalendar(); - - cal1.DayOfMonth = 5; - var cal2 = cal1.Clone(); - cal2.DayOfMonth = 10; - - Assert.AreEqual(5, cal1.DayOfMonth); + using (var cal1 = new GregorianCalendar()) + { + cal1.DayOfMonth = 5; + using (var cal2 = cal1.Clone()) + { + cal2.DayOfMonth = 10; + + Assert.AreEqual(5, cal1.DayOfMonth); + Assert.AreEqual(10, cal2.DayOfMonth); + } + } } [Test] public void RollTest() { - var cal = new GregorianCalendar(); - var startMonth = cal.Month; + using (var cal = new GregorianCalendar()) + { + var startMonth = cal.Month; - cal.Roll(Calendar.UCalendarDateFields.DayOfMonth, 100); + cal.Roll(Calendar.UCalendarDateFields.DayOfMonth, 100); - Assert.AreEqual(startMonth, cal.Month); + Assert.AreEqual(startMonth, cal.Month); + } } [Test] public void SetTest() { - var cal = new GregorianCalendar(new TimeZone("UTC")); - cal.Clear(); + using (var cal = new GregorianCalendar(new TimeZone("UTC"))) + { + cal.Clear(); - cal.Set(Calendar.UCalendarDateFields.DayOfYear, 2); + cal.Set(Calendar.UCalendarDateFields.DayOfYear, 2); - Assert.AreEqual(24 * 60 * 60 * 1000, cal.GetTime()); + Assert.AreEqual(24 * 60 * 60 * 1000, cal.GetTime()); + } + } + + [Test] + public void RollAddDifferenceTest() + { + var month = Calendar.UCalendarMonths.April; + int day = 30; + + using (Calendar cal1 = new GregorianCalendar(new TimeZone("UTC")), + cal2 = new GregorianCalendar(new TimeZone("UTC"))) + { + cal1.Month = month; + cal1.DayOfMonth = day; + + cal2.Month = month; + cal2.DayOfMonth = day; + + cal1.Add(Calendar.UCalendarDateFields.DayOfMonth, 1); + cal2.Roll(Calendar.UCalendarDateFields.DayOfMonth, 1); + + Assert.AreEqual(Calendar.UCalendarMonths.May,cal1.Month); + Assert.AreEqual(1, cal1.DayOfMonth); + + Assert.AreEqual(Calendar.UCalendarMonths.April, cal2.Month); + Assert.AreEqual(1, cal2.DayOfMonth); + } } [Test] public void GetTest() { - var cal = new GregorianCalendar(new TimeZone("UTC")); - var expected = 15; - cal.Set(Calendar.UCalendarDateFields.DayOfYear, expected); + using (var cal = new GregorianCalendar(new TimeZone("UTC"))) + { + var expected = 15; + cal.Set(Calendar.UCalendarDateFields.DayOfYear, expected); - var result = cal.Get(Calendar.UCalendarDateFields.DayOfYear); + var result = cal.Get(Calendar.UCalendarDateFields.DayOfYear); - Assert.AreEqual(expected, result); + Assert.AreEqual(expected, result); + } } [Test] public void FieldDifferenceTest() { - var cal = new GregorianCalendar(); - var time = cal.GetTime(); + using (var cal = new GregorianCalendar()) + { + var time = cal.GetTime(); - cal.Add(Calendar.UCalendarDateFields.Hour, 2); - cal.Add(Calendar.UCalendarDateFields.Minute, 2); + cal.Add(Calendar.UCalendarDateFields.Hour, 2); + cal.Add(Calendar.UCalendarDateFields.Minute, 2); - var difference = cal.FieldDifference(time, Calendar.UCalendarDateFields.Minute); + var difference = cal.FieldDifference(time, Calendar.UCalendarDateFields.Minute); - Assert.AreEqual(time, cal.GetTime()); - Assert.AreEqual(-122, difference); + Assert.AreEqual(time, cal.GetTime()); + Assert.AreEqual(-122, difference); + } } [Test] public void IsSetTest() { - var cal = new GregorianCalendar(); - cal.Month = Calendar.UCalendarMonths.September; - cal.DayOfMonth = 4; - - var setBefore = cal.IsSet(Calendar.UCalendarDateFields.Month); - cal.Clear(); - var setAfter = cal.IsSet(Calendar.UCalendarDateFields.Month); - - Assert.AreEqual(true, setBefore); - Assert.AreEqual(false, setAfter); + using (var cal = new GregorianCalendar()) + { + cal.Month = Calendar.UCalendarMonths.September; + cal.DayOfMonth = 4; + + var setBefore = cal.IsSet(Calendar.UCalendarDateFields.Month); + cal.Clear(); + var setAfter = cal.IsSet(Calendar.UCalendarDateFields.Month); + + Assert.AreEqual(true, setBefore); + Assert.AreEqual(false, setAfter); + } } [Test] public void InDaylightTime() { - var cal = new GregorianCalendar(new TimeZone("Europe/Warsaw")); - - cal.Month = Calendar.UCalendarMonths.September; - cal.DayOfMonth = 1; + using (var cal = new GregorianCalendar(new TimeZone("Europe/Warsaw"))) + { + cal.Month = Calendar.UCalendarMonths.September; + cal.DayOfMonth = 1; - Assert.AreEqual(true, cal.InDaylightTime()); + Assert.AreEqual(true, cal.InDaylightTime()); + } } - + [Test] public void SetTimeTest() { - var cal = new GregorianCalendar(new TimeZone("UTC")); - cal.SetTime(0); + using (var cal = new GregorianCalendar(new TimeZone("UTC"))) + { + cal.SetTime(0); + + Assert.AreEqual(1970, cal.Year); + Assert.AreEqual(0, cal.Hour); + Assert.AreEqual(0, cal.Minute); + Assert.AreEqual(0, cal.Millisecond); + } + } + + [Test] + public void SetTimeDateTimeTest() + { + using (var cal = new GregorianCalendar()) + { + var dateTime = DateTime.Now; + cal.SetTime(dateTime); + + var result = cal.ToDateTime(); + + // Truncate submillisecond part + dateTime = dateTime.AddTicks(-(dateTime.Ticks % TimeSpan.TicksPerMillisecond)); + + Assert.AreEqual(dateTime, result); + } + } + + [Test] + public void SetTimeDateTimeTest2() + { + var timezones = TimeZone.GetTimeZones(); + using (var cal = new GregorianCalendar(new TimeZone("Etc/GMT-7"))) + { + var dateTime = DateTime.Now.ToUniversalTime(); + cal.SetTime(dateTime); - Assert.AreEqual(1970, cal.Year); - Assert.AreEqual(0, cal.Hour); - Assert.AreEqual(0, cal.Minute); - Assert.AreEqual(0, cal.Millisecond); + var result = cal.ToDateTime(); + + // Truncate submillisecond part + dateTime = dateTime.AddTicks(-(dateTime.Ticks % TimeSpan.TicksPerMillisecond)); + + dateTime = dateTime.AddHours(7); + + Assert.AreEqual(dateTime, result); + } } [Test] public void ToDateTimeTest() { - var cal = new GregorianCalendar(); - var datetime = cal.ToDateTime(); - - Assert.AreEqual(cal.Year, datetime.Year); - Assert.AreEqual((int)cal.Month + 1, datetime.Month); - Assert.AreEqual(cal.DayOfMonth, datetime.Day); - Assert.AreEqual(cal.HourOfDay, datetime.Hour); - Assert.AreEqual(cal.Minute, datetime.Minute); - Assert.AreEqual(cal.Second, datetime.Second); - Assert.AreEqual(cal.Millisecond, datetime.Millisecond); + using (var cal = new GregorianCalendar()) + { + var datetime = cal.ToDateTime(); + + Assert.AreEqual(cal.Year, datetime.Year); + Assert.AreEqual((int)cal.Month + 1, datetime.Month); + Assert.AreEqual(cal.DayOfMonth, datetime.Day); + Assert.AreEqual(cal.HourOfDay, datetime.Hour); + Assert.AreEqual(cal.Minute, datetime.Minute); + Assert.AreEqual(cal.Second, datetime.Second); + Assert.AreEqual(cal.Millisecond, datetime.Millisecond); + } } [Test] public void SetTimeZoneTest() { var expected = new TimeZone("AST"); - var cal = new GregorianCalendar(new TimeZone("UTC")); - cal.SetTimeZone(expected); - var result = cal.GetTimeZone(); + using (var cal = new GregorianCalendar(new TimeZone("UTC"))) + { + cal.SetTimeZone(expected); + var result = cal.GetTimeZone(); - Assert.AreEqual(expected.Id, result.Id); + Assert.AreEqual(expected.Id, result.Id); + } } [Test] public void FirstDayOfWeekTest() { - var cal = new GregorianCalendar(new TimeZone("UTC")); - var newDay = Calendar.UCalendarDaysOfWeek.Thursday; + using (var cal = new GregorianCalendar(new TimeZone("UTC"))) + { + var newDay = Calendar.UCalendarDaysOfWeek.Thursday; - var val0 = cal.FirstDayOfWeek; - cal.FirstDayOfWeek = newDay; - var val1 = cal.FirstDayOfWeek; + var val0 = cal.FirstDayOfWeek; + cal.FirstDayOfWeek = newDay; + var val1 = cal.FirstDayOfWeek; - Assert.AreEqual(Calendar.UCalendarDaysOfWeek.Sunday, val0); - Assert.AreEqual(newDay, val1); + Assert.AreEqual(Calendar.UCalendarDaysOfWeek.Sunday, val0); + Assert.AreEqual(newDay, val1); + } } [Test] public void WeekOfYearTest() { - var cal = new GregorianCalendar(new TimeZone("UTC")); - cal.Clear(); - cal.DayOfMonth = 4; - var newDay = Calendar.UCalendarDaysOfWeek.Thursday; + using (var cal = new GregorianCalendar(new TimeZone("UTC"))) + { + cal.Clear(); + cal.DayOfMonth = 4; + var newDay = Calendar.UCalendarDaysOfWeek.Thursday; - var val0 = cal.WeekOfYear; - cal.FirstDayOfWeek = newDay; - var val1 = cal.WeekOfYear; + var val0 = cal.WeekOfYear; + cal.FirstDayOfWeek = newDay; + var val1 = cal.WeekOfYear; - Assert.AreEqual(2, val0); - Assert.AreEqual(1, val1); + Assert.AreEqual(2, val0); + Assert.AreEqual(1, val1); + } } [Test] public void MinimalDaysInFirstWeekTest() { - var cal = new GregorianCalendar(new TimeZone("UTC")); - cal.Clear(); - cal.DayOfMonth = 4; - var newMinimum = 5; - - var val0 = cal.WeekOfYear; - cal.MinimalDaysInFirstWeek = newMinimum; - var val1 = cal.WeekOfYear; - - Assert.AreEqual(2, val0); - Assert.AreEqual(1, val1); + using (var cal = new GregorianCalendar(new TimeZone("UTC"))) + { + cal.Clear(); + cal.DayOfMonth = 4; + var newMinimum = 5; + + var val0 = cal.WeekOfYear; + cal.MinimalDaysInFirstWeek = newMinimum; + var val1 = cal.WeekOfYear; + + Assert.AreEqual(2, val0); + Assert.AreEqual(1, val1); + } } [Test] public void SkippedWallTimeTest() { - var cal = new GregorianCalendar(new TimeZone("America/New_York")); - cal.Year = 2011; - cal.HourOfDay = 0; - cal.Minute = 0; - cal.Month = Calendar.UCalendarMonths.March; - cal.DayOfMonth = 13; - - cal.SkippedWallTimeOption = Calendar.UCalendarWallTimeOption.WalltimeFirst; - cal.Minute = 30; - cal.HourOfDay = 2; - var hour = cal.HourOfDay; - var minute = cal.Minute; - - Assert.AreEqual(1, hour); - Assert.AreEqual(30, minute); + using (var cal = new GregorianCalendar(new TimeZone("America/New_York"))) + { + cal.Year = 2011; + cal.Month = Calendar.UCalendarMonths.March; + cal.DayOfMonth = 13; + cal.HourOfDay = 0; + cal.Minute = 0; + + cal.SkippedWallTimeOption = Calendar.UCalendarWallTimeOption.WalltimeFirst; + cal.HourOfDay = 2; + cal.Minute = 30; + var hour = cal.HourOfDay; + var minute = cal.Minute; + + Assert.AreEqual(1, hour); + Assert.AreEqual(30, minute); + } } [Test] public void RepeatedWallTimeTest() { - var cal = new GregorianCalendar(new TimeZone("America/New_York")); - cal.Year = 2011; - cal.HourOfDay = 0; - cal.Minute = 0; - cal.Month = Calendar.UCalendarMonths.November; - cal.DayOfMonth = 6; - - cal.RepeatedWallTimeOption = Calendar.UCalendarWallTimeOption.WalltimeFirst; - cal.Minute = 30; - cal.HourOfDay = 1; - cal.Add(Calendar.UCalendarDateFields.Minute, 60); - var hour = cal.HourOfDay; - var minute = cal.Minute; - - Assert.AreEqual(1, hour); - Assert.AreEqual(30, minute); + using (var cal = new GregorianCalendar(new TimeZone("America/New_York"))) + { + cal.Year = 2011; + cal.Month = Calendar.UCalendarMonths.November; + cal.DayOfMonth = 6; + cal.HourOfDay = 0; + cal.Minute = 0; + + cal.RepeatedWallTimeOption = Calendar.UCalendarWallTimeOption.WalltimeFirst; + cal.HourOfDay = 1; + cal.Minute = 30; + cal.Add(Calendar.UCalendarDateFields.Minute, 60); + var hour = cal.HourOfDay; + var minute = cal.Minute; + + Assert.AreEqual(1, hour); + Assert.AreEqual(30, minute); + } } [Test] public void LenientTest_ThrowsArgumentException() { - - var cal = new GregorianCalendar(new TimeZone("America/New_York")); - cal.Year = 2011; - cal.HourOfDay = 0; - cal.Minute = 0; - cal.Month = Calendar.UCalendarMonths.March; - cal.DayOfMonth = 13; - - cal.Lenient = false; - cal.SkippedWallTimeOption = Calendar.UCalendarWallTimeOption.WalltimeFirst; - cal.Minute = 30; - cal.HourOfDay = 2; - - Assert.Throws(() => cal.GetTime()); + using (var cal = new GregorianCalendar(new TimeZone("America/New_York"))) + { + cal.Year = 2011; + cal.Month = Calendar.UCalendarMonths.March; + cal.DayOfMonth = 13; + cal.HourOfDay = 0; + cal.Minute = 0; + + cal.Lenient = false; + cal.SkippedWallTimeOption = Calendar.UCalendarWallTimeOption.WalltimeFirst; + cal.Minute = 30; + cal.HourOfDay = 2; + + Assert.Throws(() => cal.GetTime()); + } } [Test] public void WeekOfMonthTest() { - var cal = new GregorianCalendar(new TimeZone("UTC")); - cal.Clear(); - cal.DayOfMonth = 4; - var newMinimum = 5; - - var val0 = cal.WeekOfMonth; - cal.MinimalDaysInFirstWeek = newMinimum; - var val1 = cal.WeekOfMonth; - - Assert.AreEqual(2, val0); - Assert.AreEqual(1, val1); + using (var cal = new GregorianCalendar(new TimeZone("UTC"))) + { + cal.Clear(); + cal.DayOfMonth = 4; + var newMinimum = 5; + + var val0 = cal.WeekOfMonth; + cal.MinimalDaysInFirstWeek = newMinimum; + var val1 = cal.WeekOfMonth; + + Assert.AreEqual(2, val0); + Assert.AreEqual(1, val1); + } } [Test] public void EraTest() { - var cal = new GregorianCalendar(new TimeZone("UTC")); - - var era1 = cal.Era; - cal.Year = -1; - var era0 = cal.Era; - - Assert.AreEqual(1, era1); - Assert.AreEqual(0, era0); + using (var cal = new GregorianCalendar(new TimeZone("UTC"))) + { + var era1 = cal.Era; + cal.Year = -1; + var era0 = cal.Era; + + Assert.AreEqual(1, era1); + Assert.AreEqual(0, era0); + } } [Test] @@ -313,11 +416,13 @@ public void ZoneOffsetTest() { var expected = 60 * 60 * 1000; var zone = new TimeZone("Europe/Paris"); - var cal = new GregorianCalendar(zone); - var offset = cal.ZoneOffset; + using (var cal = new GregorianCalendar(zone)) + { + var offset = cal.ZoneOffset; - Assert.AreEqual(expected, offset); + Assert.AreEqual(expected, offset); + } } [Test] @@ -327,62 +432,67 @@ public void DstOffsetTest() var expected1 = 0; var zone = new TimeZone("Europe/Paris"); - var cal = new GregorianCalendar(zone); - cal.Month = Calendar.UCalendarMonths.July; - cal.DayOfMonth = 20; - var offset0 = cal.DstOffset; - cal.Month = Calendar.UCalendarMonths.January; - var offset1 = cal.DstOffset; + using (var cal = new GregorianCalendar(zone)) + { + cal.Month = Calendar.UCalendarMonths.July; + cal.DayOfMonth = 20; - Assert.AreEqual(expected0, offset0); - Assert.AreEqual(expected1, offset1); + var offset0 = cal.DstOffset; + cal.Month = Calendar.UCalendarMonths.January; + var offset1 = cal.DstOffset; + Assert.AreEqual(expected0, offset0); + Assert.AreEqual(expected1, offset1); + } } [Test] public void AmPmTest() { - var cal = new GregorianCalendar(new TimeZone("UTC")); - - cal.HourOfDay = 3; - var val0 = cal.AmPm; - cal.HourOfDay = 14; - var val1 = cal.AmPm; - - Assert.AreEqual(0, val0); - Assert.AreEqual(1, val1); + using (var cal = new GregorianCalendar(new TimeZone("UTC"))) + { + cal.HourOfDay = 3; + var val0 = cal.AmPm; + cal.HourOfDay = 14; + var val1 = cal.AmPm; + + Assert.AreEqual(Calendar.UCalendarAMPMs.Am, val0); + Assert.AreEqual(Calendar.UCalendarAMPMs.Pm, val1); + } } [Test] - public void SetTimeZone2Test() { var winId = "Romance Standard Time"; var expected = "Europe/Paris"; var timezone = TimeZoneInfo.FindSystemTimeZoneById(winId); - var cal = new GregorianCalendar(new TimeZone("UTC")); - cal.SetTimeZone(timezone); + using (var cal = new GregorianCalendar(new TimeZone("UTC"))) + { + cal.SetTimeZone(timezone); - var tz = cal.GetTimeZone(); + var tz = cal.GetTimeZone(); - Assert.AreEqual(expected, tz.Id); + Assert.AreEqual(expected, tz.Id); + } } [Test] - public void GetTimeZoneTest() + public void GetTimeZoneInfoTest() { var timezone = new TimeZone("Europe/Zagreb"); var expected = "Central European Standard Time"; - var cal = new GregorianCalendar(timezone); + using (var cal = new GregorianCalendar(timezone)) + { + var tz = cal.GetTimeZoneInfo(); - var tz = cal.GetTimeZoneInfo(); - - Assert.AreEqual(expected, tz.Id); + Assert.AreEqual(expected, tz.Id); + } } - + } } diff --git a/source/icu.net/Calendar/Calendar.cs b/source/icu.net/Calendar/Calendar.cs index c655ea88..139c9e38 100644 --- a/source/icu.net/Calendar/Calendar.cs +++ b/source/icu.net/Calendar/Calendar.cs @@ -107,7 +107,7 @@ public enum UCalendarAMPMs Am, Pm }; - + internal protected sealed class SafeCalendarHandle : SafeHandle { public SafeCalendarHandle() : @@ -150,11 +150,11 @@ protected override void Dispose(bool disposing) } } - private bool _disposingValue; // To detect redundant calls + private bool _isDisposed; // To detect redundant calls protected SafeCalendarHandle _calendarHandle = default(SafeCalendarHandle); protected Locale _locale; - + /// /// Gets this Calendar's time as milliseconds. /// @@ -186,21 +186,23 @@ public void SetTime(double date) /// /// Sets this Calendar's current time with the given date. - ///The time specified should be in non-local UTC(GMT) time. + /// Time will be set to matching time in calendar's timezone. /// - /// The given date in UTC (GMT) time. + /// The given date public void SetTime(DateTime dateTime) { - Year = dateTime.Year; - Month = (Calendar.UCalendarMonths)(dateTime.Month - 1); - DayOfMonth = dateTime.Day; - HourOfDay = dateTime.Hour; - Minute = dateTime.Minute; - Second = dateTime.Second; - Millisecond = dateTime.Millisecond; + var universalTime = dateTime.ToUniversalTime(); + + Year = universalTime.Year; + Month = (Calendar.UCalendarMonths)(universalTime.Month - 1); + DayOfMonth = universalTime.Day; + HourOfDay = universalTime.Hour; + Minute = universalTime.Minute; + Second = universalTime.Second; + Millisecond = universalTime.Millisecond; - //correct for utc - Add(UCalendarDateFields.Millisecond, ZoneOffset); + // Offset to calendars local timezone + Add(UCalendarDateFields.Millisecond, ZoneOffset+DstOffset); } /// @@ -343,7 +345,7 @@ public TimeZone GetTimeZone() if (length >= 32) { ec = ErrorCode.NoErrors; - NativeMethods.ucal_getTimeZoneId(_calendarHandle, out result, length+1, out ec); + NativeMethods.ucal_getTimeZoneId(_calendarHandle, out result, length + 1, out ec); } ExceptionFromErrorCode.ThrowIfError(ec); return new TimeZone(result); @@ -359,11 +361,11 @@ public TimeZoneInfo GetTimeZoneInfo() var timeZoneInfo = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(zone => zone.Id == id); - if(timeZoneInfo != null) + if (timeZoneInfo != null) { return timeZoneInfo; } - + string winid = TimeZone.GetWindowsId(id); if (!string.IsNullOrWhiteSpace(winid)) @@ -406,14 +408,11 @@ public DateTime ToDateTime() /// Formatted time zone name public string GetTimeZoneDisplayName(UCalendarDisplayNameType type) { - int length = NativeMethods.ucal_getTimeZoneDisplayName(_calendarHandle, type, _locale.Name, out string result, 60, out ErrorCode ec); - if (length >= 60) + return NativeMethods.GetUnicodeString((ptr, length) => { - NativeMethods.ucal_getTimeZoneDisplayName(_calendarHandle, type, _locale.Name, out result, length + 1, out ec); - } - ExceptionFromErrorCode.ThrowIfError(ec); - - return result; + length = NativeMethods.ucal_getTimeZoneDisplayName(_calendarHandle, type, _locale.Name, ptr, length, out ErrorCode status); + return new Tuple(status, length); + }, 255); } #region Properties @@ -425,7 +424,7 @@ public bool Lenient { get { - var result = NativeMethods.ucal_getAttribute(_calendarHandle, UCalendarAttribute.Lenient, out _); + var result = NativeMethods.ucal_getAttribute(_calendarHandle, UCalendarAttribute.Lenient); return (result != 0); } set @@ -441,7 +440,7 @@ public UCalendarDaysOfWeek FirstDayOfWeek { get { - return (UCalendarDaysOfWeek)NativeMethods.ucal_getAttribute(_calendarHandle, UCalendarAttribute.FirstDayOfWeek, out _); + return (UCalendarDaysOfWeek)NativeMethods.ucal_getAttribute(_calendarHandle, UCalendarAttribute.FirstDayOfWeek); } set { @@ -456,12 +455,12 @@ public int MinimalDaysInFirstWeek { get { - var result = NativeMethods.ucal_getAttribute(_calendarHandle, UCalendarAttribute.FirstDayOfWeek, out _); + var result = NativeMethods.ucal_getAttribute(_calendarHandle, UCalendarAttribute.MinimalDaysInFirstWeek); return result; } set { - NativeMethods.ucal_setAttribute(_calendarHandle, UCalendarAttribute.FirstDayOfWeek, value); + NativeMethods.ucal_setAttribute(_calendarHandle, UCalendarAttribute.MinimalDaysInFirstWeek, value); } } @@ -472,7 +471,8 @@ public UCalendarWallTimeOption RepeatedWallTimeOption { get { - var result = (UCalendarWallTimeOption)NativeMethods.ucal_getAttribute(_calendarHandle, UCalendarAttribute.RepeatedWallTime, out _); + var result = (UCalendarWallTimeOption)NativeMethods.ucal_getAttribute(_calendarHandle, UCalendarAttribute.RepeatedWallTime); + return result; } set @@ -488,7 +488,8 @@ public UCalendarWallTimeOption SkippedWallTimeOption { get { - var result = (UCalendarWallTimeOption)NativeMethods.ucal_getAttribute(_calendarHandle, UCalendarAttribute.SkippedWallTime, out _); + var result = (UCalendarWallTimeOption)NativeMethods.ucal_getAttribute(_calendarHandle, UCalendarAttribute.SkippedWallTime); + return result; } set @@ -504,7 +505,9 @@ public int Era { get { - return NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.Era, out _); + var result = NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.Era, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return result; } set { @@ -519,7 +522,9 @@ public int Year { get { - return NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.Year, out _); + var result = NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.Year, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return result; } set { @@ -534,7 +539,9 @@ public UCalendarMonths Month { get { - return (UCalendarMonths)NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.Month, out _); + var result = (UCalendarMonths)NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.Month, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return result; } set { @@ -549,7 +556,9 @@ public int WeekOfYear { get { - return NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.WeekOfYear, out _); + var result = NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.WeekOfYear, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return result; } set { @@ -564,7 +573,9 @@ public int WeekOfMonth { get { - return NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.WeekOfMonth, out _); + var result = NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.WeekOfMonth, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return result; } set { @@ -579,7 +590,9 @@ public int DayOfWeek { get { - return NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.DayOfWeek, out _); + var result = NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.DayOfWeek, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return result; } set { @@ -594,7 +607,9 @@ public int DayOfMonth { get { - return NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.DayOfMonth, out _); + var result = NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.DayOfMonth, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return result; } set { @@ -617,7 +632,9 @@ public int DayOfWeekInMonth { get { - return NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.DayOfWeekInMonth, out _); + var result = NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.DayOfWeekInMonth, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return result; } set { @@ -632,7 +649,9 @@ public int DayOfYear { get { - return NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.DayOfYear, out _); + var result = NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.DayOfYear, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return result; } set { @@ -643,15 +662,17 @@ public int DayOfYear /// /// Gets or seths whether the hour is before or after noon. /// - public int AmPm + public UCalendarAMPMs AmPm { get { - return NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.AmPm, out _); + var result = (UCalendarAMPMs)NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.AmPm, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return result; } set { - NativeMethods.ucal_set(_calendarHandle, UCalendarDateFields.AmPm, value); + NativeMethods.ucal_set(_calendarHandle, UCalendarDateFields.AmPm, (int)value); } } @@ -662,7 +683,9 @@ public int Hour { get { - return NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.Hour, out _); + var result = NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.Hour, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return result; } set { @@ -677,7 +700,9 @@ public int HourOfDay { get { - return NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.HourOfDay, out _); + var result = NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.HourOfDay, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return result; } set { @@ -692,7 +717,9 @@ public int Minute { get { - return NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.Minute, out _); + var result = NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.Minute, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return result; } set { @@ -707,7 +734,9 @@ public int Second { get { - return NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.Second, out _); + var result = NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.Second, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return result; } set { @@ -722,7 +751,9 @@ public int Millisecond { get { - return NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.Millisecond, out _); + var result = NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.Millisecond, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return result; } set { @@ -737,7 +768,9 @@ public int ZoneOffset { get { - return NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.ZoneOffset, out _); + var result = NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.ZoneOffset, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return result; } set { @@ -752,7 +785,9 @@ public int DstOffset { get { - return NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.DstOffset, out _); + var result = NativeMethods.ucal_get(_calendarHandle, UCalendarDateFields.DstOffset, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return result; } set { @@ -796,15 +831,14 @@ public void Dispose() /// resources; false to release only unmanaged resources. protected virtual void Dispose(bool disposing) { - if (!_disposingValue) + if (!_isDisposed) { if (disposing) { - // Dispose managed state (managed objects), if any. + _calendarHandle.Dispose(); } - _calendarHandle.Dispose(); - _disposingValue = true; + _isDisposed = true; } } ~Calendar() diff --git a/source/icu.net/Calendar/GregorianCalendar.cs b/source/icu.net/Calendar/GregorianCalendar.cs index 25df2d11..3bba54f9 100644 --- a/source/icu.net/Calendar/GregorianCalendar.cs +++ b/source/icu.net/Calendar/GregorianCalendar.cs @@ -25,8 +25,8 @@ public GregorianCalendar(TimeZone timezone, Locale locale) { _locale = locale; _calendarHandle = NativeMethods.ucal_open(timezone.Id, - locale.Name, UCalendarType.Gregorian, out ErrorCode ec); - ExceptionFromErrorCode.ThrowIfError(ec); + locale.Name, UCalendarType.Gregorian, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); } private GregorianCalendar(SafeCalendarHandle handle) @@ -36,13 +36,18 @@ private GregorianCalendar(SafeCalendarHandle handle) public override Calendar Clone() { - var handle = NativeMethods.ucal_clone(_calendarHandle, out ErrorCode status); - return new GregorianCalendar(handle); + var handle = NativeMethods.ucal_clone(_calendarHandle, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + var calendar = new GregorianCalendar(handle); + calendar._locale = _locale; + return calendar; } public override bool InDaylightTime() { - return NativeMethods.ucal_inDaylightTime(_calendarHandle, out _); + bool isDaylightTime = NativeMethods.ucal_inDaylightTime(_calendarHandle, out ErrorCode errorCode); + ExceptionFromErrorCode.ThrowIfError(errorCode); + return isDaylightTime; } } } diff --git a/source/icu.net/NativeMethods/NativeMethods_Calendar.cs b/source/icu.net/NativeMethods/NativeMethods_Calendar.cs index 98fe800f..12451b4e 100644 --- a/source/icu.net/NativeMethods/NativeMethods_Calendar.cs +++ b/source/icu.net/NativeMethods/NativeMethods_Calendar.cs @@ -75,8 +75,7 @@ internal delegate Calendar.SafeCalendarHandle ucal_cloneDelegate( [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)] internal delegate int ucal_getAttributeDelegate( Calendar.SafeCalendarHandle cal, - Calendar.UCalendarAttribute attribute, - out ErrorCode status); + Calendar.UCalendarAttribute attribute); [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)] internal delegate int ucal_getFieldDifferenceDelegate( @@ -256,12 +255,11 @@ public static int ucal_get( public static int ucal_getAttribute( Calendar.SafeCalendarHandle cal, - Calendar.UCalendarAttribute attribute, - out ErrorCode status) + Calendar.UCalendarAttribute attribute) { if (CalendarMethods.ucal_getAttribute == null) CalendarMethods.ucal_getAttribute = GetMethod(IcuI18NLibHandle, "ucal_getAttribute"); - return CalendarMethods.ucal_getAttribute(cal, attribute, out status); + return CalendarMethods.ucal_getAttribute(cal, attribute); } public static void ucal_clear( @@ -319,26 +317,13 @@ public static int ucal_getTimeZoneDisplayName( Calendar.SafeCalendarHandle cal, Calendar.UCalendarDisplayNameType type, string locale, - out string result, + IntPtr result, int resultLength, out ErrorCode status) { if (CalendarMethods.ucal_getTimeZoneDisplayName == null) CalendarMethods.ucal_getTimeZoneDisplayName = GetMethod(IcuI18NLibHandle, "ucal_getTimeZoneDisplayName"); - - IntPtr outBuf = Marshal.AllocHGlobal(resultLength * sizeof(char)); - try - { - int length = CalendarMethods.ucal_getTimeZoneDisplayName(cal, type, locale, outBuf, resultLength, out status); - 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_getTimeZoneDisplayName(cal, type, locale, result, resultLength, out status); } public static bool ucal_inDaylightTime(