144 lines
4.4 KiB
C#
144 lines
4.4 KiB
C#
namespace EgwCoreLib.Utils
|
|
{
|
|
public class DtUtils
|
|
{
|
|
#region Public Enums
|
|
|
|
public enum PeriodSet
|
|
{
|
|
Select = 0,
|
|
Today,
|
|
Yesterday,
|
|
LastMonth,
|
|
LastTrim,
|
|
LastWeek,
|
|
LastYear,
|
|
ThisMonth,
|
|
ThisTrim,
|
|
ThisWeek,
|
|
ThisYear
|
|
}
|
|
|
|
#endregion Public Enums
|
|
|
|
#region Public Classes
|
|
|
|
public class Periodo
|
|
{
|
|
#region Public Constructors
|
|
|
|
public Periodo()
|
|
{ }
|
|
|
|
public Periodo(DateTime inizio, DateTime fine)
|
|
{
|
|
this.Inizio = inizio;
|
|
this.Fine = fine;
|
|
}
|
|
|
|
public Periodo(PeriodSet tipo)
|
|
{
|
|
int endMonth = 0;
|
|
DateTime oggi = DateTime.Today;
|
|
switch (tipo)
|
|
{
|
|
case DtUtils.PeriodSet.Today:
|
|
this.Inizio = oggi;
|
|
this.Fine = oggi.AddDays(1);
|
|
break;
|
|
|
|
case DtUtils.PeriodSet.Yesterday:
|
|
this.Inizio = oggi.AddDays(-1);
|
|
this.Fine = oggi;
|
|
break;
|
|
|
|
case DtUtils.PeriodSet.LastMonth:
|
|
this.Inizio = new DateTime(oggi.Year, oggi.Month, 1).AddMonths(-1);
|
|
this.Fine = new DateTime(oggi.Year, oggi.Month, 1);
|
|
break;
|
|
|
|
case DtUtils.PeriodSet.LastTrim:
|
|
// calcolo trimestre da mese...
|
|
endMonth = (int)Math.Ceiling((float)oggi.Month / 3) * 3;
|
|
this.Fine = new DateTime(oggi.Year, endMonth, 1).AddMonths(-2);
|
|
this.Inizio = this.Fine.AddMonths(-3);
|
|
break;
|
|
|
|
case DtUtils.PeriodSet.LastYear:
|
|
this.Inizio = new DateTime(oggi.Year - 1, 1, 1);
|
|
this.Fine = new DateTime(oggi.Year, 1, 1);
|
|
break;
|
|
|
|
case DtUtils.PeriodSet.LastWeek:
|
|
this.Inizio = oggi.StartOfWeek(DayOfWeek.Monday).AddDays(-7);
|
|
this.Fine = this.Inizio.AddDays(7);
|
|
break;
|
|
|
|
case DtUtils.PeriodSet.ThisMonth:
|
|
this.Inizio = new DateTime(oggi.Year, oggi.Month, 1);
|
|
this.Fine = new DateTime(oggi.Year, oggi.Month + 1, 1);
|
|
break;
|
|
|
|
case DtUtils.PeriodSet.ThisTrim:
|
|
// calcolo trimestre da mese...
|
|
endMonth = (int)Math.Ceiling((float)oggi.Month / 3) * 3;
|
|
this.Fine = new DateTime(oggi.Year, endMonth, 1).AddMonths(1);
|
|
this.Inizio = this.Fine.AddMonths(-3);
|
|
break;
|
|
|
|
case DtUtils.PeriodSet.ThisWeek:
|
|
this.Inizio = oggi.StartOfWeek(DayOfWeek.Monday);
|
|
this.Fine = this.Inizio.AddDays(7);
|
|
break;
|
|
|
|
case DtUtils.PeriodSet.ThisYear:
|
|
this.Inizio = new DateTime(oggi.Year, 1, 1);
|
|
this.Fine = new DateTime(oggi.Year + 1, 1, 1);
|
|
break;
|
|
|
|
case DtUtils.PeriodSet.Select:
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Properties
|
|
|
|
public DateTime Fine { get; set; } = DateTime.Today.AddDays(+1);
|
|
|
|
public DateTime Inizio { get; set; } = DateTime.Today;
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Public Methods
|
|
|
|
public override bool Equals(object? obj)
|
|
{
|
|
if (obj == null)
|
|
return false;
|
|
|
|
if (!(obj is Periodo item))
|
|
return false;
|
|
|
|
if (Inizio != item.Inizio)
|
|
return false;
|
|
|
|
if (Fine != item.Fine)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return base.GetHashCode();
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
|
|
#endregion Public Classes
|
|
}
|
|
} |