Skip to content

Commit

Permalink
Adding DateTimeRangeOffset to SharedKernel
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis committed Mar 30, 2021
1 parent bf9ae9b commit c84c3a2
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using Ardalis.GuardClauses;

namespace PluralsightDdd.SharedKernel
{
public class DateTimeOffsetRange : ValueObject
{
public DateTimeOffset Start { get; private set; }
public DateTimeOffset End { get; private set; }

public DateTimeOffsetRange(DateTimeOffset start, DateTimeOffset end)
{
// Ardalis.GuardClauses supports extensions with custom guards per project
Guard.Against.OutOfRange(start, nameof(start), start, end);
Start = start;
End = end;
}

public DateTimeOffsetRange(DateTimeOffset start, TimeSpan duration) : this(start, start.Add(duration))
{
}

public int DurationInMinutes()
{
return (int)Math.Round((End - Start).TotalMinutes, 0);
}

public DateTimeOffsetRange NewDuration(TimeSpan newDuration)
{
return new DateTimeOffsetRange(this.Start, newDuration);
}

public DateTimeOffsetRange NewEnd(DateTime newEnd)
{
return new DateTimeOffsetRange(this.Start, newEnd);
}

public DateTimeOffsetRange NewStart(DateTime newStart)
{
return new DateTimeOffsetRange(newStart, this.End);
}

public static DateTimeOffsetRange CreateOneDayRange(DateTime day)
{
return new DateTimeOffsetRange(day, day.AddDays(1));
}

public static DateTimeOffsetRange CreateOneWeekRange(DateTime startDay)
{
return new DateTimeOffsetRange(startDay, startDay.AddDays(7));
}

public bool Overlaps(DateTimeOffsetRange dateTimeRange)
{
return this.Start < dateTimeRange.End &&
this.End > dateTimeRange.Start;
}

protected override IEnumerable<object> GetEqualityComponents()
{
yield return Start;
yield return End;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;

namespace Ardalis.GuardClauses
{
public static class CustomGuards
{
public static DateTimeOffset OutOfRange(this IGuardClause guardClause, DateTimeOffset input, string parameterName, DateTimeOffset rangeFrom, DateTimeOffset rangeTo)
{
return OutOfRange<DateTimeOffset>(guardClause, input, parameterName, rangeFrom, rangeTo);
}

// https://github.com/ardalis/GuardClauses/blob/master/src/GuardClauses/GuardClauseExtensions.cs#L255
private static T OutOfRange<T>(this IGuardClause guardClause, T input, string parameterName, T rangeFrom, T rangeTo)
{
Comparer<T> comparer = Comparer<T>.Default;

if (comparer.Compare(rangeFrom, rangeTo) > 0)
{
throw new ArgumentException($"{nameof(rangeFrom)} should be less or equal than {nameof(rangeTo)}");
}

if (comparer.Compare(input, rangeFrom) < 0 || comparer.Compare(input, rangeTo) > 0)
{
throw new ArgumentOutOfRangeException(parameterName, $"Input {parameterName} was out of range");
}

return input;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<Summary>Includes common abstractions and base types.</Summary>
<RepositoryUrl>https://github.com/ardalis/pluralsight-ddd-fundamentals</RepositoryUrl>
<PackageTags>aspnet asp.net aspnetcore asp.net core ddd dddesign value object entity aggregate domain event pluralsight</PackageTags>
<PackageReleaseNotes>Update repo interface.</PackageReleaseNotes>
<Version>1.1.5</Version>
<PackageReleaseNotes>Adding DateTimeRangeOffset.</PackageReleaseNotes>
<Version>1.1.6</Version>
<AssemblyName>PluralsightDdd.SharedKernel</AssemblyName>
<PackageIconUrl>https://user-images.githubusercontent.com/782127/33497760-facf6550-d69c-11e7-94e4-b3856da259a9.png</PackageIconUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand Down

0 comments on commit c84c3a2

Please sign in to comment.