Add DateTimeOffset support to range

This commit is contained in:
honzapatCZ 2026-09-10 13:33:17 +02:00
parent 67bd0bc46d
commit 65dc00cb9f
2 changed files with 50 additions and 0 deletions

View 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);
}

View File

@ -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);