Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ public static int CalculateDaysUntilVacation(DateTime summerVacationStart, DateT
public static int CalculateDaysUntilEvent(DateTimeOffset eventDateTime, DateTimeOffset currentDateTime)
{
TimeSpan daysUntilEvent = eventDateTime - currentDateTime;

return daysUntilEvent.Days;
}
}

public static int CalculateDaysBetweenDates(DateOnly startDate, DateOnly endDate)
{
return endDate.DayNumber - startDate.DayNumber;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
using CheckNumberOfDaysBetweenTwoDates;

// Days until summer vacation
var summerVacationStart = new DateTime(2024, 6, 1);
var currentDate = DateTime.Today;
var currentDate = new DateTime(2024, 1, 13);

int daysUntilVacation = NumberOfDaysBetweenTwoDates.CalculateDaysUntilVacation(summerVacationStart, currentDate);
Console.WriteLine($"Days until summer vacation: {daysUntilVacation}");

var eventDateTime = new DateTimeOffset(2024, 9, 1, 10, 0, 0, new TimeSpan(-5, 0, 0));
var currentDateTime = DateTimeOffset.Now;
// Time of day
var currentDateAndTime = new DateTime(2024, 1, 13, 18, 0, 0);
TimeSpan untilVacation = summerVacationStart - currentDateAndTime;
Console.WriteLine($"Days: {untilVacation.Days}, TotalDays: {untilVacation.TotalDays}");
Console.WriteLine($"Calendar days: {(summerVacationStart.Date - currentDateAndTime.Date).Days}");

// DateOnly
var vacationStart = new DateOnly(2024, 6, 1);
var today = new DateOnly(2024, 1, 13);

int daysBetween = NumberOfDaysBetweenTwoDates.CalculateDaysBetweenDates(today, vacationStart);
Console.WriteLine($"Days between {today:yyyy-MM-dd} and {vacationStart:yyyy-MM-dd}: {daysBetween}");

// Days until an event in New York
var newYork = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
var eventLocalTime = new DateTime(2024, 9, 1, 10, 0, 0);
var eventDateTime = new DateTimeOffset(eventLocalTime, newYork.GetUtcOffset(eventLocalTime));
var currentDateTime = new DateTimeOffset(2024, 1, 13, 18, 0, 0, TimeSpan.FromHours(1));

Console.WriteLine($"Event offset: {eventDateTime.Offset}");

int daysUntilEvent = NumberOfDaysBetweenTwoDates.CalculateDaysUntilEvent(eventDateTime, currentDateTime);
Console.WriteLine($"Days until the event: {daysUntilEvent}");

// Daylight saving time
var beforeChange = new DateTimeOffset(2024, 3, 9, 12, 0, 0, TimeSpan.FromHours(-5));
var afterChange = new DateTimeOffset(2024, 3, 10, 12, 0, 0, TimeSpan.FromHours(-4));
TimeSpan offsetDifference = afterChange - beforeChange;
Console.WriteLine($"DateTimeOffset: {offsetDifference.TotalHours} hours, Days = {offsetDifference.Days}");

var localBefore = new DateTime(2024, 3, 9, 12, 0, 0);
var localAfter = new DateTime(2024, 3, 10, 12, 0, 0);
TimeSpan localDifference = localAfter - localBefore;
Console.WriteLine($"DateTime: {localDifference.TotalHours} hours, Days = {localDifference.Days}");
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,48 @@ public class NumberOfDaysBetweenTwoDatesTests
[InlineData("2023-01-01T15:30", "2024-01-01T08:15", -364)]
[InlineData("2024-02-29T12:00", "2024-02-28T18:00", 0)]
[InlineData("2074-01-01T10:00", "2024-01-01T05:00", 18263)]
[InlineData("2024-06-01T00:00", "2024-01-13T00:00", 140)]
[InlineData("2024-06-01T00:00", "2024-01-13T18:00", 139)]
public void GivenSpecificVacationStartDate_WhenCalculateDaysUntilVacationCalled_ThenReturnsCorrectNumberOfDays(string vacationDate, string currentDate, int expectedDays)
{
var vacationStartDate = DateTime.Parse(vacationDate);
var currentTestDate = DateTime.Parse(currentDate);
var vacationStartDate = DateTime.Parse(vacationDate, CultureInfo.InvariantCulture);
var currentTestDate = DateTime.Parse(currentDate, CultureInfo.InvariantCulture);

int actualDays = NumberOfDaysBetweenTwoDates.CalculateDaysUntilVacation(vacationStartDate, currentTestDate);

Assert.Equal(expectedDays, actualDays);
}

[Theory]
[InlineData("2024-09-01T10:00:00-05:00", "2024-01-01T00:00:00-05:00", 244)]
[InlineData("2024-09-01T10:00:00-04:00", "2024-01-01T00:00:00-05:00", 244)]
[InlineData("2024-09-01T10:00:00-04:00", "2024-01-13T18:00:00+01:00", 231)]
[InlineData("2024-12-31T23:59:59-05:00", "2024-01-01T00:00:00-05:00", 365)]
[InlineData("2024-07-04T12:00:00-04:00", "2024-07-03T12:00:00-04:00", 1)]
[InlineData("2024-11-28T08:00:00+00:00", "2024-11-27T20:00:00-08:00", 0)]
[InlineData("2024-03-10T12:00:00-04:00", "2024-03-09T12:00:00-05:00", 0)]
public void GivenSpecificDates_WhenCalculateDaysUntilEventCalled_ThenReturnsCorrectNumberOfDays(string eventDate, string currentDate, int expectedDays)
{
var eventDateTime = DateTimeOffset.Parse(eventDate);
var currentDateTime = DateTimeOffset.Parse(currentDate);
var eventDateTime = DateTimeOffset.Parse(eventDate, CultureInfo.InvariantCulture);
var currentDateTime = DateTimeOffset.Parse(currentDate, CultureInfo.InvariantCulture);

int actualDays = NumberOfDaysBetweenTwoDates.CalculateDaysUntilEvent(eventDateTime, currentDateTime);

Assert.Equal(expectedDays, actualDays);
}

[Theory]
[InlineData("2024-01-13", "2024-06-01", 140)]
[InlineData("2024-02-28", "2024-03-01", 2)]
[InlineData("2023-02-28", "2023-03-01", 1)]
[InlineData("2024-06-01", "2024-01-13", -140)]
[InlineData("2024-06-01", "2024-06-01", 0)]
public void GivenTwoDateOnlyValues_WhenCalculateDaysBetweenDatesCalled_ThenReturnsCorrectNumberOfDays(string startDate, string endDate, int expectedDays)
{
var start = DateOnly.Parse(startDate, CultureInfo.InvariantCulture);
var end = DateOnly.Parse(endDate, CultureInfo.InvariantCulture);

int actualDays = NumberOfDaysBetweenTwoDates.CalculateDaysBetweenDates(start, end);

Assert.Equal(expectedDays, actualDays);
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
global using System.Globalization;
global using CheckNumberOfDaysBetweenTwoDates;
global using Xunit;
global using Xunit;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,13 +10,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="10.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading