Add DateTimeOffset support to range
This commit is contained in:
parent
67bd0bc46d
commit
65dc00cb9f
48
Models/DateTimeOffsetFrame.cs
Normal file
48
Models/DateTimeOffsetFrame.cs
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using NejCommon.Utils;
|
||||||
|
|
||||||
|
namespace NejCommon.Models;
|
||||||
|
public class DateTimeOffsetFrame : IEquatable<DateTimeOffsetFrame>
|
||||||
|
{
|
||||||
|
public DateTimeOffsetFrame(){
|
||||||
|
|
||||||
|
}
|
||||||
|
public DateTimeOffsetFrame(DateTimeOffset fromDate, DateTimeOffset toDate)
|
||||||
|
{
|
||||||
|
FromDate = fromDate;
|
||||||
|
ToDate = toDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DateTimeOffset FromDate { get; set; } = DateTimeOffset.MinValue;
|
||||||
|
public DateTimeOffset ToDate { get; set; } = DateTimeOffset.MaxValue;
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
var from = (FromDate == DateTimeOffset.MinValue) ? "" : ("-" + FromDate.ToString("yyyy-MM-dd"));
|
||||||
|
var to = (ToDate == DateTimeOffset.MaxValue) ? "" : ("-" + ToDate.ToString("yyyy-MM-dd"));
|
||||||
|
return $"{from}{to}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Expression<Func<TType, bool>> Conditional<TType>(Expression<Func<TType, DateTimeOffset>> dateAcessor, DateTimeOffsetFrame 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(DateTimeOffsetFrame? 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 ==(DateTimeOffsetFrame? left, DateTimeOffsetFrame? right)
|
||||||
|
=> left?.Equals(right) ?? right is null;
|
||||||
|
|
||||||
|
public static bool operator !=(DateTimeOffsetFrame? left, DateTimeOffsetFrame? right)
|
||||||
|
=> !(left?.Equals(right) ?? right is null);
|
||||||
|
}
|
||||||
|
|
@ -106,6 +106,8 @@ public static class Extensions
|
||||||
[Projectable]
|
[Projectable]
|
||||||
public static DateOnly ToDateOnly(this DateTime date) => DateOnly.FromDateTime(date);
|
public static DateOnly ToDateOnly(this DateTime date) => DateOnly.FromDateTime(date);
|
||||||
[Projectable]
|
[Projectable]
|
||||||
|
public static DateOnly ToDateOnly(this DateTimeOffset date) => DateOnly.FromDateTime(date.DateTime);
|
||||||
|
[Projectable]
|
||||||
public static DateTime ToDateTime(this DateOnly date) => new DateTime(date.Year, date.Month, date.Day);
|
public static DateTime ToDateTime(this DateOnly date) => new DateTime(date.Year, date.Month, date.Day);
|
||||||
[Projectable]
|
[Projectable]
|
||||||
public static decimal Round(this decimal value, int decimals = 2) => Math.Round(value, decimals);
|
public static decimal Round(this decimal value, int decimals = 2) => Math.Round(value, decimals);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user