📄 extendedformat.cs
字号:
/// Determines if should expire every hour.
/// </summary>
/// <value>
/// <see langword="true"/> if should expire every hour; otherwise, <see langword="false"/>.
/// </value>
public bool ExpireEveryHour
{
get { return this.hours[0] == -1; }
}
/// <summary>
/// Determines if should expire every month.
/// </summary>
/// <value>
/// <see langword="true"/> if should expire every month; otherwise, <see langword="false"/>.
/// </value>
public bool ExpireEveryMonth
{
get { return this.months[0] == -1; }
}
/// <summary>
/// Determines if should expire every day of the week.
/// </summary>
/// <value>
/// <see langword="true"/> if should expire every day of the week; otherwise, <see langword="false"/>.
/// </value>
public bool ExpireEveryDayOfWeek
{
get { return this.daysOfWeek[0] == -1; }
}
private void ParseMinutes(string[] parsedFormat)
{
this.minutes = ParseValueToInt(parsedFormat[0]);
foreach (int minute in this.minutes)
{
if ((minute > 59) || (minute < -1))
{
throw new ArgumentOutOfRangeException("format", Resources.ExceptionRangeMinute);
}
}
}
private void ParseHours(string[] parsedFormat)
{
this.hours = ParseValueToInt(parsedFormat[1]);
foreach (int hour in this.hours)
{
if ((hour > 23) || (hour < -1))
{
throw new ArgumentOutOfRangeException("format", Resources.ExceptionRangeHour);
}
}
}
private void ParseDays(string[] parsedFormat)
{
this.days = ParseValueToInt(parsedFormat[2]);
foreach (int day in this.days)
{
if ((day > 31) || (day < -1))
{
throw new ArgumentOutOfRangeException("format", Resources.ExceptionRangeDay);
}
}
}
private void ParseMonths(string[] parsedFormat)
{
this.months = ParseValueToInt(parsedFormat[3]);
foreach (int month in this.months)
{
if ((month > 12) || (month < -1))
{
throw new ArgumentOutOfRangeException("format", Resources.ExceptionRangeMonth);
}
}
}
private void ParseDaysOfWeek(string[] parsedFormat)
{
this.daysOfWeek = ParseValueToInt(parsedFormat[4]);
foreach (int dayOfWeek in this.daysOfWeek)
{
if ((dayOfWeek > 6) || (dayOfWeek < -1))
{
throw new ArgumentOutOfRangeException("format", Resources.ExceptionRangeDay);
}
}
}
private static int[] ParseValueToInt(string value)
{
int[] result;
if (value.IndexOf(wildcardAll) != -1)
{
result = new int[1];
result[0] = -1;
}
else
{
string[] values = value.Split(argumentDelimiter);
result = new int[values.Length];
for (int index = 0; index < values.Length; index++)
{
result[index] = int.Parse(values[index], CultureInfo.CurrentUICulture);
}
}
return result;
}
/// <summary>
/// Determines if the time has expired.
/// </summary>
/// <param name="getTime">The time to compare.</param>
/// <param name="nowTime">The current time.</param>
/// <returns>
/// <see langword="true"/> if the time is expired; otherwise, <see langword="false"/>.
/// </returns>
public bool IsExpired(DateTime getTime, DateTime nowTime)
{
// Removes the seconds to provide better precission on calculations
getTime = getTime.AddSeconds(getTime.Second * -1);
nowTime = nowTime.AddSeconds(nowTime.Second * -1);
if (nowTime.Subtract(getTime).TotalMinutes < 1)
{
return false;
}
foreach (int minute in minutes)
{
foreach (int hour in hours)
{
foreach (int day in days)
{
foreach (int month in months)
{
// Set the expiration date parts
int expirMinute = minute == -1 ? getTime.Minute : minute;
int expirHour = hour == -1 ? getTime.Hour : hour;
int expirDay = day == -1 ? getTime.Day : day;
int expirMonth = month == -1 ? getTime.Month : month;
int expirYear = getTime.Year;
// Adjust when wildcards are set
if ((minute == -1) && (hour != -1))
{
expirMinute = 0;
}
if ((hour == -1) && (day != -1))
{
expirHour = 0;
}
if ((minute == -1) && (day != -1))
{
expirMinute = 0;
}
if ((day == -1) && (month != -1))
{
expirDay = 1;
}
if ((hour == -1) && (month != -1))
{
expirHour = 0;
}
if ((minute == -1) && (month != -1))
{
expirMinute = 0;
}
if (DateTime.DaysInMonth(expirYear, expirMonth) < expirDay)
{
// if (expirMonth == 12)
// {
// expirMonth = 1;
// expirYear++;
// }
// else
// {
expirMonth++;
// }
expirDay = 1;
}
// Create the date with the adjusted parts
DateTime expTime = new DateTime(
expirYear, expirMonth, expirDay,
expirHour, expirMinute, 0);
// Adjust when expTime is before getTime
if (expTime < getTime)
{
if ((month != -1) && (getTime.Month >= month))
{
expTime = expTime.AddYears(1);
}
else if ((day != -1) && (getTime.Day >= day))
{
expTime = expTime.AddMonths(1);
}
else if ((hour != -1) && (getTime.Hour >= hour))
{
expTime = expTime.AddDays(1);
}
else if ((minute != -1) && (getTime.Minute >= minute))
{
expTime = expTime.AddHours(1);
}
}
// Is Expired?
if (ExpireEveryDayOfWeek)
{
if (nowTime >= expTime)
{
return true;
}
}
else
{
// Validate WeekDay
foreach (int weekDay in daysOfWeek)
{
DateTime tmpTime = getTime;
tmpTime = tmpTime.AddHours(-1 * tmpTime.Hour);
tmpTime = tmpTime.AddMinutes(-1 * tmpTime.Minute);
while ((int)tmpTime.DayOfWeek != weekDay)
{
tmpTime = tmpTime.AddDays(1);
}
if ((nowTime >= tmpTime) && (nowTime >= expTime))
{
return true;
}
}
}
}
}
}
}
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -