Skip to content
36 changes: 7 additions & 29 deletions Ical.Net/DataTypes/Duration.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.IO;
using Ical.Net.Serialization.DataTypes;
Expand Down Expand Up @@ -118,7 +119,8 @@
/// Parses the specified value according to RFC 5545.
/// </summary>
/// <exception cref="System.FormatException">Thrown if the value is not a valid duration.</exception>
public static Duration Parse(string value) => (Duration)new DurationSerializer().Deserialize(new StringReader(value));
public static Duration? Parse(string value) =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately we are somewhat inconsistent here. According to the XML doc the method should throw if the passed string is not a valid duration. DurationSerializer throws in some cases and returns null in other. I'd generally suggest to throw and return a non-nullable Duration.

(Duration?) new DurationSerializer().Deserialize(new StringReader(value));

Check warning on line 123 in Ical.Net/DataTypes/Duration.cs

View check run for this annotation

Codecov / codecov/patch

Ical.Net/DataTypes/Duration.cs#L123

Added line #L123 was not covered by tests

/// <summary>
/// Creates an instance that represents the given time span as exact value, that is, time-only.
Expand All @@ -143,32 +145,12 @@
/// <summary>
/// Gets a value representing the time parts of the given instance.
/// </summary>
internal TimeSpan TimeAsTimeSpan
{
get
{
return new TimeSpan(
0,
Hours ?? 0,
Minutes ?? 0,
Seconds ?? 0);
}
}
internal TimeSpan TimeAsTimeSpan => new(0, Hours ?? 0, Minutes ?? 0, Seconds ?? 0);

/// <summary>
/// Gets a value representing the date parts (days and weeks) of the given instance.
/// </summary>
internal TimeSpan DateAsTimeSpan
{
get
{
return new TimeSpan(
(Weeks ?? 0) * 7 + (Days ?? 0),
0,
0,
0);
}
}
internal TimeSpan DateAsTimeSpan => new((Weeks ?? 0) * 7 + (Days ?? 0), 0, 0, 0);

/// <summary>
/// Convert the instance to a <see cref="TimeSpan"/>, ignoring potential
Expand All @@ -181,11 +163,7 @@
/// nominal durations, use <see cref="ToTimeSpan"/>.
/// </remarks>
public TimeSpan ToTimeSpanUnspecified()
=> new TimeSpan(
(Weeks ?? 0) * 7 + (Days ?? 0),
Hours ?? 0,
Minutes ?? 0,
Seconds ?? 0);
=> new TimeSpan((Weeks ?? 0) * 7 + (Days ?? 0), Hours ?? 0, Minutes ?? 0, Seconds ?? 0);

/// <summary>
/// Convert the instance to a <see cref="TimeSpan"/>, treating the days as nominal duration and
Expand Down Expand Up @@ -226,7 +204,7 @@
new Duration(-d.Weeks, -d.Days, -d.Hours, -d.Minutes, -d.Seconds);

/// <inheritdoc/>
public override string ToString()
public override string? ToString()
=> new DurationSerializer().SerializeToString(this);

private static int? GetSign(int? v) =>
Expand Down
4 changes: 2 additions & 2 deletions Ical.Net/DataTypes/PeriodList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void Insert(int index, Period item)
{
EnsureConsistentTimezoneAndPeriodKind(item);
if (Periods.Contains(item)) return;
Periods?.Insert(index, item);
Periods.Insert(index, item);
}

/// <inheritdoc/>
Expand All @@ -140,7 +140,7 @@ public void Add(Period item)
{
EnsureConsistentTimezoneAndPeriodKind(item);
if (Periods.Contains(item)) return;
Periods?.Add(item);
Periods.Add(item);
}

/// <summary>
Expand Down
Loading