48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
using System.Linq.Expressions;
|
|
using NejCommon.Utils;
|
|
|
|
namespace NejCommon.Models;
|
|
public class DateTimeFrame : IEquatable<DateTimeFrame>
|
|
{
|
|
public DateTimeFrame(){
|
|
|
|
}
|
|
public DateTimeFrame(DateTime fromDate, DateTime toDate)
|
|
{
|
|
FromDate = fromDate;
|
|
ToDate = toDate;
|
|
}
|
|
|
|
public DateTime FromDate { get; set; } = DateTime.MinValue;
|
|
public DateTime ToDate { get; set; } = DateTime.MaxValue;
|
|
|
|
public override string ToString()
|
|
{
|
|
var from = (FromDate == DateTime.MinValue) ? "" : ("-" + FromDate.ToString("yyyy-MM-dd"));
|
|
var to = (ToDate == DateTime.MaxValue) ? "" : ("-" + ToDate.ToString("yyyy-MM-dd"));
|
|
return $"{from}{to}";
|
|
}
|
|
|
|
public static Expression<Func<TType, bool>> Conditional<TType>(Expression<Func<TType, DateTime>> dateAcessor, DateTimeFrame frame)
|
|
{
|
|
return e => dateAcessor.Compile()(e) >= frame.FromDate && dateAcessor.Compile()(e) <= frame.ToDate;
|
|
}
|
|
|
|
public DateOnlyFrame ToDateOnlyFrame()
|
|
{
|
|
return new DateOnlyFrame(FromDate.ToDateOnly(), ToDate.ToDateOnly());
|
|
}
|
|
public bool Equals(DateTimeFrame? other)
|
|
{
|
|
if (other is null) return false;
|
|
return this.FromDate == other.FromDate && this.ToDate == other.ToDate;
|
|
}
|
|
public override int GetHashCode()
|
|
=> HashCode.Combine(FromDate, ToDate);
|
|
|
|
public static bool operator ==(DateTimeFrame left, DateTimeFrame right)
|
|
=> left.Equals(right);
|
|
|
|
public static bool operator !=(DateTimeFrame left, DateTimeFrame right)
|
|
=> !left.Equals(right);
|
|
} |