-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathMainForm.cs
191 lines (154 loc) · 6.43 KB
/
MainForm.cs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#region License and Terms
//
// NCrontab - Crontab for .NET
// Copyright (c) 2008 Atif Aziz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NCrontab;
namespace NCrontabViewer;
public partial class MainForm : Form
{
static readonly char[] Separators = [' '];
DateTime lastChangeTime;
bool dirty;
CrontabSchedule? crontab;
bool isSixPart;
DateTime startTime;
int totalOccurrenceCount;
public MainForm()
{
InitializeComponent();
}
// ReSharper disable once InconsistentNaming
void CronBox_Changed(object sender, EventArgs args)
{
this.lastChangeTime = DateTime.Now;
this.dirty = true;
this.isSixPart = false;
this.crontab = null;
}
// ReSharper disable once InconsistentNaming
void Timer_Tick(object sender, EventArgs args)
{
var changeLapse = DateTime.Now - this.lastChangeTime;
if (!this.dirty || changeLapse <= TimeSpan.FromMilliseconds(500))
return;
this.dirty = false;
DoCrontabbing();
}
void DoCrontabbing()
{
this.resultBox.Clear();
this.errorProvider.SetError(this.cronBox, null);
this.statusBarPanel.Text = "Ready";
this.moreButton.Enabled = false;
const string defaultCustomFormat = "dd/MM/yyyy HH:mm";
if (this.crontab == null)
{
try
{
var expression = this.cronBox.Text.Trim();
if (expression.Length == 0)
return;
this.isSixPart = expression.Split(Separators, StringSplitOptions.RemoveEmptyEntries).Length == 6;
this.crontab = CrontabSchedule.Parse(expression, new CrontabSchedule.ParseOptions { IncludingSeconds = this.isSixPart });
this.totalOccurrenceCount = 0;
this.startTime = DateTime.ParseExact(this.startTimePicker.Text,
this.startTimePicker.CustomFormat ?? defaultCustomFormat, CultureInfo.InvariantCulture,
DateTimeStyles.AssumeLocal) - (this.isSixPart ? TimeSpan.FromSeconds(1): TimeSpan.FromMinutes(1));
}
catch (CrontabException e)
{
this.errorProvider.SetError(this.cronBox, e.Message);
var traceBuilder = new StringBuilder();
Exception traceException = e;
Exception lastException;
do
{
_ = traceBuilder.Append(traceException.Message)
.Append("\r\n");
lastException = traceException;
traceException = traceException.GetBaseException();
}
while (lastException != traceException);
this.resultBox.Text = traceBuilder.ToString();
return;
}
}
var endTime = DateTime.ParseExact(this.endTimePicker.Text,
this.endTimePicker.CustomFormat ?? defaultCustomFormat, CultureInfo.InvariantCulture,
DateTimeStyles.AssumeLocal);
var sb = new StringBuilder();
var count = 0;
const int maxCount = 500;
var info = DateTimeFormatInfo.CurrentInfo;
var dayWidth = info.AbbreviatedDayNames.Max(s => s.Length);
var monthWidth = info.AbbreviatedMonthNames.Max(s => s.Length);
var timeComponent = this.isSixPart ? "HH:mm:ss" : "HH:mm";
var timeFormat = $"{{0,-{dayWidth}:ddd}} {{0:dd}}, {{0,-{monthWidth}:MMM}} {{0:yyyy {timeComponent}}}";
var lastTimeString = new string('?', string.Format(null, timeFormat, DateTime.MinValue).Length);
foreach (var occurrence in this.crontab.GetNextOccurrences(this.startTime, endTime))
{
if (count + 1 > maxCount)
break;
this.startTime = occurrence;
this.totalOccurrenceCount++;
count++;
var timeString = string.Format(null, timeFormat, occurrence);
_ = sb.Append(timeString)
.Append(" | ");
var index = Diff(lastTimeString, timeString, 0, dayWidth, sb);
_ = sb.Append(' ');
index = Diff(lastTimeString, timeString, index + 1, 2, sb);
_ = sb.Append(", ");
index = Diff(lastTimeString, timeString, index + 2, monthWidth, sb);
_ = sb.Append(' ');
index = Diff(lastTimeString, timeString, index + 1, 4, sb);
_ = sb.Append(' ');
index = Diff(lastTimeString, timeString, index + 1, 2, sb);
_ = sb.Append(':');
index = Diff(lastTimeString, timeString, index + 1, 2, sb);
if (this.isSixPart)
{
_ = sb.Append(':');
_ = Diff(lastTimeString, timeString, index + 1, 2, sb);
}
lastTimeString = timeString;
_ = sb.Append("\r\n");
}
this.moreButton.Enabled = count == maxCount;
this.statusBarPanel.Text = $"Last count = {count:N0}, Total = {this.totalOccurrenceCount:N0}";
this.resultBox.Text = sb.ToString();
this.resultBox.Select(0, 0);
this.resultBox.ScrollToCaret();
}
static int Diff(string oldString, string newString, int index, int length, StringBuilder builder)
{
#pragma warning disable IDE0045 // Convert to conditional expression (has side-effects)
if (string.CompareOrdinal(oldString, index, newString, index, length) == 0)
_ = builder.Append('-', length);
else
_ = builder.Append(newString, index, length);
#pragma warning restore IDE0045 // Convert to conditional expression
return index + length;
}
// ReSharper disable once InconsistentNaming
void More_Click(object sender, EventArgs e) => DoCrontabbing();
}