Skip to content

Commit

Permalink
imp - brk|doc - Used periods in RecDateInfo
Browse files Browse the repository at this point in the history
---

This adds support for periods in RecDateInfo instances.

---

Type: imp
Breaking: True
Doc Required: True
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Sep 30, 2024
1 parent 739f3e3 commit 7447e62
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
44 changes: 34 additions & 10 deletions VisualCard.Calendar/Parts/Implementations/RecDateInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class RecDateInfo : BaseCalendarPartInfo, IEquatable<RecDateInfo>
/// <summary>
/// The recurrence date list
/// </summary>
public DateTimeOffset[]? RecDates { get; }
public TimePeriod[]? RecDates { get; }

internal static BaseCalendarPartInfo FromStringVcalendarStatic(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion) =>
new RecDateInfo().FromStringVcalendarInternal(value, finalArgs, elementTypes, valueType, cardVersion);
Expand All @@ -48,20 +48,44 @@ internal override string ToStringVcalendarInternal(Version cardVersion)

var builder = new StringBuilder();
if (cardVersion.Major == 1)
builder.Append(string.Join(";", RecDates.Select((dt) => VcardCommonTools.SavePosixDate(dt))));
builder.Append(string.Join(";", RecDates.Select((dt) => VcardCommonTools.SavePosixDate(dt.StartDate))));
else
builder.Append(VcardCommonTools.SavePosixDate(RecDates[0]));
{
builder.Append(VcardCommonTools.SavePosixDate(RecDates[0].StartDate));
if (RecDates[0].StartDate != RecDates[0].EndDate)
builder.Append("/" + VcardCommonTools.SavePosixDate(RecDates[0].EndDate));
}
return builder.ToString();
}

internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
{
// Populate the fields
var recDateStrings =
cardVersion.Major == 1 ?
Regex.Unescape(value).Split(';') :
[Regex.Unescape(value)];
var recDates = recDateStrings.Select((date) => VcardCommonTools.ParsePosixDate(date)).ToArray();
TimePeriod[] recDates = [];
if (cardVersion.Major == 1)
{
var recDateStrings = Regex.Unescape(value).Split(';');
recDates = recDateStrings.Select((date) =>
{
var parsedDate = VcardCommonTools.ParsePosixDate(date);
return new TimePeriod(parsedDate, parsedDate);
}).ToArray();
}
else
{
// Check to see if it's a period
try
{
var parsedPeriod = VcardCommonTools.GetTimePeriod(value);
recDates = [parsedPeriod];
}
catch
{
// Not a period. Continue using normal date and time
var parsedDate = VcardCommonTools.ParsePosixDate(value);
recDates = [new TimePeriod(parsedDate, parsedDate)];
}
}

// Add the fetched information
RecDateInfo _time = new([], elementTypes, valueType, recDates);
Expand Down Expand Up @@ -103,7 +127,7 @@ public override int GetHashCode()
{
int hashCode = 498518712;
hashCode = hashCode * -1521134295 + base.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<DateTimeOffset[]?>.Default.GetHashCode(RecDates);
hashCode = hashCode * -1521134295 + EqualityComparer<TimePeriod[]?>.Default.GetHashCode(RecDates);
return hashCode;
}

Expand All @@ -120,7 +144,7 @@ internal override bool EqualsInternal(BaseCalendarPartInfo source, BaseCalendarP

internal RecDateInfo() { }

internal RecDateInfo(string[] arguments, string[] elementTypes, string valueType, DateTimeOffset[] recDates) :
internal RecDateInfo(string[] arguments, string[] elementTypes, string valueType, TimePeriod[] recDates) :
base(arguments, elementTypes, valueType)
{
RecDates = recDates;
Expand Down
1 change: 1 addition & 0 deletions VisualCard/Parsers/VcardCommonTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public static string SaveUtcOffset(TimeSpan utcOffsetRepresentation)
/// <param name="duration">Duration specifier in the ISO-8601:2004 format</param>
/// <param name="modern">Whether to disable parsing years and months or not</param>
/// <param name="utc">Whether to use UTC</param>
/// <param name="source">Source date/time</param>
/// <returns>A date/time offset instance and a time span instance from the duration specifier</returns>
/// <exception cref="ArgumentException"></exception>
public static (DateTimeOffset result, TimeSpan span) GetDurationSpan(string duration, bool modern = false, bool utc = true, DateTimeOffset? source = null)
Expand Down

0 comments on commit 7447e62

Please sign in to comment.