using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IStation.Model.Monitor
{
///
/// 月段
///
public class MonthSlot : JsonModel, ICronSlot
{
///
/// 是否从前一月开始
///
public bool Pre { get; set; }
///
/// 开始天数 从1开始 到28
///
public int Start { get; set; } = 1;
///
/// 结束天数 从1 开始到31 大于当月天数按当月天数算
///
public int End { get; set; } = 31;
///
/// 验证
///
public bool Verify()
{
if (this.Start < 1 || this.Start > 28)
return false;
if (this.End < 1 || this.End > 31)
return false;
if (this.Pre)
{
if (this.End >= this.Start)
return false;
}
else
{
if (this.End <= this.Start)
return false;
}
return true;
}
///
/// 获取开始时间
///
public DateTime GetStartTime(DateTime rhs)
{
rhs = new DateTime(rhs.Year, rhs.Month, 1);
var start = 1;
if (Verify())
{
start = this.Start;
if (this.Pre)
{
var dt_pre = rhs.AddMonths(-2);
var days_pre = DateTime.DaysInMonth(dt_pre.Year, dt_pre.Month);
start -= days_pre;
}
}
rhs = rhs.AddMonths(-1);
var dt = rhs.AddDays(start).AddDays(-1);
return dt;
}
///
/// 获取结束时间
///
public DateTime GetEndTime(DateTime rhs)
{
rhs = new DateTime(rhs.Year, rhs.Month, 1);
rhs = rhs.AddMonths(-1);
var days = DateTime.DaysInMonth(rhs.Year, rhs.Month);
var end = days;
if (Verify())
{
if (this.End < days)
end = this.End;
}
var dt = rhs.AddDays(end);
return dt;
}
}
}