ningshuxia
2023-03-14 076301ec9c2c2fc117a062231c4c5f0e8ea6c23c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.Model.Monitor
{
    /// <summary>
    /// 月段
    /// </summary>
    public class MonthSlot : JsonModel<MonthSlot>, ICronSlot
    {
        /// <summary>
        /// 是否从前一月开始
        /// </summary>
        public bool Pre { get; set; }
 
        /// <summary>
        /// 开始天数 从1开始 到28
        /// </summary>
        public int Start { get; set; } = 1;
 
        /// <summary>
        /// 结束天数  从1 开始到31 大于当月天数按当月天数算
        /// </summary>
        public int End { get; set; } = 31;
 
        /// <summary>
        /// 验证
        /// </summary>
        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;
        }
 
        /// <summary>
        /// 获取开始时间
        /// </summary>
        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;
        }
 
        /// <summary>
        /// 获取结束时间
        /// </summary>
        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;
        }
 
    }
}