-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--- 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
Showing
5 changed files
with
264 additions
and
127 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.