Skip to content

Commit

Permalink
add - doc - Added time period tools
Browse files Browse the repository at this point in the history
---

We've added time period tools that allow you to parse time periods.

---

Type: add
Breaking: False
Doc Required: True
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Sep 30, 2024
1 parent 22ec11a commit 5e447f2
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 127 deletions.
119 changes: 0 additions & 119 deletions VisualCard.Calendar/Parsers/Durations/DurationTools.cs

This file was deleted.

15 changes: 7 additions & 8 deletions VisualCard.Tests/Durations/DurationParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
using VisualCard.Calendar.Parsers.Durations;
using VisualCard.Calendar.Parsers.Recurrence;
using VisualCard.Parsers;

namespace VisualCard.Tests.Durations
{
Expand All @@ -36,7 +35,7 @@ public class DurationParseTests
[DataRow("P7W")]
public void ParseDurations(string rule)
{
var span = DurationTools.GetDurationSpan(rule);
var span = VcardCommonTools.GetDurationSpan(rule);
span.result.ShouldNotBe(new());
span.span.ShouldNotBe(new());
}
Expand All @@ -50,7 +49,7 @@ public void ParseDurations(string rule)
[DataRow("P7W")]
public void ParseDurationsNoUtc(string rule)
{
var span = DurationTools.GetDurationSpan(rule, utc: false);
var span = VcardCommonTools.GetDurationSpan(rule, utc: false);
span.result.ShouldNotBe(new());
span.span.ShouldNotBe(new());
}
Expand All @@ -64,7 +63,7 @@ public void ParseDurationsNoUtc(string rule)
[DataRow("-P7W")]
public void ParseNegativeDurations(string rule)
{
var span = DurationTools.GetDurationSpan(rule);
var span = VcardCommonTools.GetDurationSpan(rule);
span.result.ShouldNotBe(new());
span.span.ShouldNotBe(new());
}
Expand All @@ -78,15 +77,15 @@ public void ParseNegativeDurations(string rule)
[DataRow("-P7W")]
public void ParseNegativeDurationsNoUtc(string rule)
{
var span = DurationTools.GetDurationSpan(rule, utc: false);
var span = VcardCommonTools.GetDurationSpan(rule, utc: false);
span.result.ShouldNotBe(new());
span.span.ShouldNotBe(new());
}

[TestMethod]
public void ParseDuration()
{
var span = DurationTools.GetDurationSpan("P2Y10M15DT10H30M20S");
var span = VcardCommonTools.GetDurationSpan("P2Y10M15DT10H30M20S");

// We can't test against result because it's uninferrable due to CPU timings.
span.result.ShouldNotBe(new());
Expand All @@ -100,7 +99,7 @@ public void ParseDuration()
[TestMethod]
public void ParseNegativeDuration()
{
var span = DurationTools.GetDurationSpan("-P2Y10M15DT10H30M20S");
var span = VcardCommonTools.GetDurationSpan("-P2Y10M15DT10H30M20S");

// We can't test against result because it's uninferrable due to CPU timings.
span.result.ShouldNotBe(new());
Expand Down
70 changes: 70 additions & 0 deletions VisualCard.Tests/TimePeriod/PeriodParseTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// VisualCard Copyright (C) 2021-2024 Aptivi
//
// This file is part of VisualCard
//
// VisualCard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// VisualCard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
using VisualCard.Parsers;

namespace VisualCard.Tests.TimePeriod
{
[TestClass]
public class PeriodParseTests
{
[TestMethod]
[DataRow("19970101T180000Z/19970102T070000Z")]
[DataRow("19970101T180000Z/PT5H30M")]
public void ParsePeriods(string rule)
{
var span = VcardCommonTools.GetTimePeriod(rule);
span.StartDate.ShouldNotBe(new());
span.EndDate.ShouldNotBe(new());
span.Duration.ShouldNotBe(new());
}

[TestMethod]
public void ParsePeriodWithDateDate()
{
var span = VcardCommonTools.GetTimePeriod("19970101T180000Z/19970102T070000Z");

// We can't test against result because it's uninferrable due to CPU timings.
span.StartDate.ShouldNotBe(new());
span.EndDate.ShouldNotBe(new());
span.Duration.ShouldNotBe(new());
span.Duration.Days.ShouldBe(0);
span.Duration.Hours.ShouldBe(13);
span.Duration.Minutes.ShouldBe(0);
span.Duration.Seconds.ShouldBe(0);
}

[TestMethod]
public void ParsePeriodWithDateDuration()
{
var span = VcardCommonTools.GetTimePeriod("19970101T180000Z/PT5H30M");

// We can't test against result because it's uninferrable due to CPU timings.
span.StartDate.ShouldNotBe(new());
span.EndDate.ShouldNotBe(new());
span.Duration.ShouldNotBe(new());
span.Duration.Days.ShouldBe(0);
span.Duration.Hours.ShouldBe(5);
span.Duration.Minutes.ShouldBe(30);
span.Duration.Seconds.ShouldBe(0);
}
}
}
65 changes: 65 additions & 0 deletions VisualCard/Parsers/TimePeriod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// VisualCard Copyright (C) 2021-2024 Aptivi
//
// This file is part of VisualCard
//
// VisualCard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// VisualCard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using System;
using System.Diagnostics;

namespace VisualCard.Parsers
{
/// <summary>
/// Precise period of time
/// </summary>
[DebuggerDisplay("{StartDate} => {EndDate} in {Duration}")]
public class TimePeriod
{
private DateTimeOffset startDate;
private DateTimeOffset endDate;

/// <summary>
/// Start date
/// </summary>
public DateTimeOffset StartDate =>
startDate;

/// <summary>
/// End date or a resultant date from the duration
/// </summary>
public DateTimeOffset EndDate =>
endDate;

/// <summary>
/// Duration of the time period
/// </summary>
public TimeSpan Duration =>
endDate - startDate;

/// <summary>
/// Makes a new TimePeriod instance
/// </summary>
/// <param name="startDate">Start date</param>
/// <param name="endDate">End date</param>
public TimePeriod(DateTimeOffset startDate, DateTimeOffset endDate)
{
if (startDate > endDate)
throw new ArgumentException("Start date may not be later than the end date");
this.startDate = startDate;
this.endDate = endDate;
}
}
}
Loading

0 comments on commit 5e447f2

Please sign in to comment.