Skip to content

Commit

Permalink
Enable NRT for classes in namespace Ical.Net.Utility (#706)
Browse files Browse the repository at this point in the history
Enable NRT for Ical.Net.Utility classes
  • Loading branch information
axunonb authored Jan 26, 2025
1 parent 71aac50 commit 4be1fd8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
6 changes: 3 additions & 3 deletions Ical.Net/DataTypes/CalDateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,12 +434,12 @@ public CalDateTime ToTimeZone(string? otherTzId)
ZonedDateTime converted;
if (IsFloating)
{
// Make sure, we properly fix the time if it dosen't exist in the target tz.
// Make sure, we properly fix the time if it doesn't exist in the target tz.
converted = DateUtil.ToZonedDateTimeLeniently(Value, otherTzId);
}
else
{
var zonedOriginal = DateUtil.ToZonedDateTimeLeniently(Value, TzId);
var zonedOriginal = DateUtil.ToZonedDateTimeLeniently(Value, TzId!);
converted = zonedOriginal.WithZone(DateUtil.GetZone(otherTzId));
}

Expand Down Expand Up @@ -634,7 +634,7 @@ public int CompareTo(IDateTime? dt)
public string ToString(string? format, IFormatProvider? formatProvider)
{
formatProvider ??= CultureInfo.InvariantCulture;
var dateTimeOffset = DateUtil.ToZonedDateTimeLeniently(Value, _tzId).ToDateTimeOffset();
var dateTimeOffset = DateUtil.ToZonedDateTimeLeniently(Value, _tzId ?? string.Empty).ToDateTimeOffset();

// Use the .NET format options to format the DateTimeOffset
var tzIdString = _tzId is not null ? $" {_tzId}" : string.Empty;
Expand Down
4 changes: 2 additions & 2 deletions Ical.Net/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<PackageIcon>assets/icon.png</PackageIcon>
<PackageReadmeFile>assets/readme.md</PackageReadmeFile>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<!-- netstandard2.0 does not support NRT -->
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0' Or '$(TargetFramework)' == 'netstandard2.1'">
<!-- netstandard2.x does not or not fully support NRT -->
<NoWarn>$(NoWarn);CS8600;CS8601;CS8602;CS8603;CS8604;CS8618;CS8620;CS8714</NoWarn>
</PropertyGroup>
<ItemGroup>
Expand Down
27 changes: 13 additions & 14 deletions Ical.Net/Utility/CollectionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT license.
//

#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -12,7 +13,7 @@ namespace Ical.Net.Utility;
internal static class CollectionHelpers
{
/// <summary> Commutative, stable, order-independent hashing for collections of collections. </summary>
public static int GetHashCode<T>(IEnumerable<T> collection)
public static int GetHashCode<T>(IEnumerable<T>? collection)
{
unchecked
{
Expand All @@ -23,12 +24,12 @@ public static int GetHashCode<T>(IEnumerable<T> collection)

return collection
.Where(e => e != null)
.Aggregate(0, (current, element) => current + element.GetHashCode());
.Aggregate(0, (current, element) => current + element?.GetHashCode() ?? 0);
}
}

/// <summary> Commutative, stable, order-independent hashing for collections of collections. </summary>
public static int GetHashCodeForNestedCollection<T>(IEnumerable<IEnumerable<T>> nestedCollection)
public static int GetHashCodeForNestedCollection<T>(IEnumerable<IEnumerable<T>>? nestedCollection)
{
unchecked
{
Expand All @@ -40,23 +41,18 @@ public static int GetHashCodeForNestedCollection<T>(IEnumerable<IEnumerable<T>>
return nestedCollection
.SelectMany(c => c)
.Where(e => e != null)
.Aggregate(0, (current, element) => current + element.GetHashCode());
.Aggregate(0, (current, element) => current + element?.GetHashCode() ?? 0);
}
}

public static bool Equals<T>(IEnumerable<T> left, IEnumerable<T> right, bool orderSignificant = false)
public static bool Equals<T>(IEnumerable<T>? left, IEnumerable<T>? right, bool orderSignificant = false)
{
if (ReferenceEquals(left, right))
{
return true;
}

if (left == null && right != null)
{
return false;
}

if (left != null && right == null)
if (left == null || right == null)
{
return false;
}
Expand All @@ -66,18 +62,21 @@ public static bool Equals<T>(IEnumerable<T> left, IEnumerable<T> right, bool ord
return left.SequenceEqual(right);
}

// No multiple enumerations
var rightArray = right as T[] ?? right.ToArray();
var leftArray = left as T[] ?? left.ToArray();
try
{
//Many things have natural IComparers defined, but some don't, because no natural comparer exists
return left.OrderBy(l => l).SequenceEqual(right.OrderBy(r => r));
return leftArray.OrderBy(l => l).SequenceEqual(rightArray.OrderBy(r => r));
}
catch (Exception)
{
//It's not possible to sort some collections of things (like Calendars) in any meaningful way. Properties can be null, and there's no natural
//ordering for the contents therein. In cases like that, the best we can do is treat them like sets, and compare them. We don't maintain
//fidelity with respect to duplicates, but it seems better than doing nothing
var leftSet = new HashSet<T>(left);
var rightSet = new HashSet<T>(right);
var leftSet = new HashSet<T>(leftArray);
var rightSet = new HashSet<T>(rightArray);
return leftSet.SetEquals(rightSet);
}
}
Expand Down
16 changes: 9 additions & 7 deletions Ical.Net/Utility/DateUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT license.
//

#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -23,8 +24,6 @@ public static CalDateTime AsCalDateTime(this DateTime t)

public static DateTime AddWeeks(DateTime dt, int interval, DayOfWeek firstDayOfWeek)
{
// NOTE: fixes WeeklyUntilWkst2() eval.
// NOTE: simplified the execution of this - fixes bug #3119920 - missing weekly occurences also
dt = dt.AddDays(interval * 7);
while (dt.DayOfWeek != firstDayOfWeek)
{
Expand Down Expand Up @@ -63,8 +62,11 @@ private static Dictionary<string, string> InitializeWindowsMappings()
/// <param name="tzId">A BCL, IANA, or serialization time zone identifier</param>
/// <param name="useLocalIfNotFound">If true, this method will return the system local time zone if tzId doesn't match a known time zone identifier.
/// Otherwise, it will throw an exception.</param>
/// <exception cref="ArgumentException">Unrecognized time zone id</exception>
public static DateTimeZone GetZone(string tzId, bool useLocalIfNotFound = true)
{
var exMsg = $"Unrecognized time zone id {tzId}";

if (string.IsNullOrWhiteSpace(tzId))
{
return LocalDateTimeZone;
Expand All @@ -83,7 +85,7 @@ public static DateTimeZone GetZone(string tzId, bool useLocalIfNotFound = true)

if (_windowsMapping.Value.TryGetValue(tzId, out var ianaZone))
{
return DateTimeZoneProviders.Tzdb.GetZoneOrNull(ianaZone);
return DateTimeZoneProviders.Tzdb.GetZoneOrNull(ianaZone) ?? throw new ArgumentException(exMsg);
}

zone = NodaTime.Xml.XmlSerializationSettings.DateTimeZoneProvider.GetZoneOrNull(tzId);
Expand All @@ -103,29 +105,29 @@ public static DateTimeZone GetZone(string tzId, bool useLocalIfNotFound = true)
var providerId = DateTimeZoneProviders.Tzdb.Ids.FirstOrDefault(tzId.Contains);
if (providerId != null)
{
return DateTimeZoneProviders.Tzdb.GetZoneOrNull(providerId);
return DateTimeZoneProviders.Tzdb.GetZoneOrNull(providerId) ?? throw new ArgumentException(exMsg);
}

if (_windowsMapping.Value.Keys
.Where(tzId.Contains)
.Any(pId => _windowsMapping.Value.TryGetValue(pId, out ianaZone))
)
{
return DateTimeZoneProviders.Tzdb.GetZoneOrNull(ianaZone);
return DateTimeZoneProviders.Tzdb.GetZoneOrNull(ianaZone!) ?? throw new ArgumentException(exMsg);
}

providerId = NodaTime.Xml.XmlSerializationSettings.DateTimeZoneProvider.Ids.FirstOrDefault(tzId.Contains);
if (providerId != null)
{
return NodaTime.Xml.XmlSerializationSettings.DateTimeZoneProvider.GetZoneOrNull(providerId);
return NodaTime.Xml.XmlSerializationSettings.DateTimeZoneProvider.GetZoneOrNull(providerId) ?? throw new ArgumentException(exMsg);
}

if (useLocalIfNotFound)
{
return LocalDateTimeZone;
}

throw new ArgumentException($"Unrecognized time zone id {tzId}");
throw new ArgumentException(exMsg);
}

public static ZonedDateTime AddYears(ZonedDateTime zonedDateTime, int years)
Expand Down
1 change: 1 addition & 0 deletions Ical.Net/Utility/TextUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT license.
//

#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
Expand Down

0 comments on commit 4be1fd8

Please sign in to comment.