📄 extendedformat.cs
字号:
//===============================================================================
// CSDN HeyCache
//===============================================================================
// 修改记录:[按最后修改时间倒排序]
// 2007.06.11 by tangwei
//
// 代码来源:参考于dotnet企业库3.0版
//===============================================================================
using System;
using System.Configuration;
using System.Globalization;
using Microsoft.Practices.EnterpriseLibrary.Caching.Properties;
using System.Collections.Generic;
namespace HeyCacher.Components.Expirations
{
/// <summary>
/// 按特定时间为依赖的失效策略
/// </summary>
/// <remarks>
/// Extended format syntax : <br/><br/>
///
/// Minute - 0-59 <br/>
/// Hour - 0-23 <br/>
/// Day of month - 1-31 <br/>
/// Month - 1-12 <br/>
/// Day of week - 0-6 (Sunday is 0) <br/>
/// Wildcards - * means run every <br/>
/// Examples: <br/>
/// * * * * * - expires every minute<br/>
/// 5 * * * * - expire 5th minute of every hour <br/>
/// * 21 * * * - expire every minute of the 21st hour of every day <br/>
/// 31 15 * * * - expire 3:31 PM every day <br/>
/// 7 4 * * 6 - expire Saturday 4:07 AM <br/>
/// 15 21 4 7 * - expire 9:15 PM on 4 July <br/>
/// Therefore 6 6 6 6 1 means:
/// ?have we crossed/entered the 6th minute AND
/// ?have we crossed/entered the 6th hour AND
/// ?have we crossed/entered the 6th day AND
/// ?have we crossed/entered the 6th month AND
/// ?have we crossed/entered A MONDAY?
///
/// Therefore these cases should exhibit these behaviors:
///
/// getTime = DateTime.Parse( "02/20/2003 04:06:55 AM" );
/// nowTime = DateTime.Parse( "06/07/2003 07:07:00 AM" );
/// isExpired = ExtendedFormatHelper.IsExtendedExpired( "6 6 6 6 1", getTime, nowTime );
/// TRUE, ALL CROSSED/ENTERED
///
/// getTime = DateTime.Parse( "02/20/2003 04:06:55 AM" );
/// nowTime = DateTime.Parse( "06/07/2003 07:07:00 AM" );
/// isExpired = ExtendedFormatHelper.IsExtendedExpired( "6 6 6 6 5", getTime, nowTime );
/// TRUE
///
/// getTime = DateTime.Parse( "02/20/2003 04:06:55 AM" );
/// nowTime = DateTime.Parse( "06/06/2003 06:06:00 AM" );
/// isExpired = ExtendedFormatHelper.IsExtendedExpired( "6 6 6 6 *", getTime, nowTime );
/// TRUE
///
///
/// getTime = DateTime.Parse( "06/05/2003 04:06:55 AM" );
/// nowTime = DateTime.Parse( "06/06/2003 06:06:00 AM" );
/// isExpired = ExtendedFormatHelper.IsExtendedExpired( "6 6 6 6 5", getTime, nowTime );
/// TRUE
///
/// getTime = DateTime.Parse( "06/05/2003 04:06:55 AM" );
/// nowTime = DateTime.Parse( "06/06/2005 05:06:00 AM" );
/// isExpired = ExtendedFormatHelper.IsExtendedExpired( "6 6 6 6 1", getTime, nowTime );
/// TRUE
///
/// getTime = DateTime.Parse( "06/05/2003 04:06:55 AM" );
/// nowTime = DateTime.Parse( "06/06/2003 05:06:00 AM" );
/// isExpired = ExtendedFormatHelper.IsExtendedExpired( "6 6 6 6 1", getTime, nowTime );
/// FALSE: we did not cross 6th hour, nor did we cross Monday
///
/// getTime = DateTime.Parse( "06/05/2003 04:06:55 AM" );
/// nowTime = DateTime.Parse( "06/06/2003 06:06:00 AM" );
/// isExpired = ExtendedFormatHelper.IsExtendedExpired( "6 6 6 6 5", getTime, nowTime );
/// TRUE, we cross/enter Friday
///
///
/// getTime = DateTime.Parse( "06/05/2003 04:06:55 AM" );
/// nowTime = DateTime.Parse( "06/06/2003 06:06:00 AM" );
/// isExpired = ExtendedFormatHelper.IsExtendedExpired( "6 6 6 6 1", getTime, nowTime );
/// FALSE: we don抰 cross Monday but all other conditions satisfied
/// </remarks>
public class ExtendedFormat
{
private readonly string format;
private static readonly char argumentDelimiter = Convert.ToChar(",", CultureInfo.CurrentUICulture);
private static readonly char wildcardAll = Convert.ToChar("*", CultureInfo.CurrentUICulture);
private static readonly char refreshDelimiter = Convert.ToChar(" ", CultureInfo.CurrentUICulture);
private int[] minutes;
private int[] hours;
private int[] days;
private int[] months;
private int[] daysOfWeek;
/// <summary>
/// Validates the format.
/// </summary>
/// <param name="timeFormat">
/// The format to validate.
/// </param>
public static void Validate(string timeFormat)
{
ExtendedFormat ef = new ExtendedFormat(timeFormat);
ef.Initialize();
}
/// <summary>
/// Initializes a new instnace of the <see cref="ExtendedFormat"/> class with a format.
/// </summary>
/// <param name="format">The extended format time.</param>
public ExtendedFormat(string format)
{
if (format == null)
{
throw new ArgumentNullException("format");
}
this.format = format;
Initialize();
}
private void Initialize()
{
string[] parsedFormat = format.Trim().Split(refreshDelimiter);
if (parsedFormat.Length != 5)
{
throw new ArgumentOutOfRangeException(Resources.ExceptionInvalidExtendedFormatArguments);
}
ParseMinutes(parsedFormat);
ParseHours(parsedFormat);
ParseDays(parsedFormat);
ParseMonths(parsedFormat);
ParseDaysOfWeek(parsedFormat);
}
/// <summary>
/// Gets the exteneded format.
/// </summary>
/// <value>
/// The extended format.
/// </value>
public string Format
{
get { return this.format; }
}
/// <summary>
/// Gets the minutes to expire.
/// </summary>
/// <value>
/// The minutes to expire.
/// </value>
/// <remarks>
/// This returns a copy of the integer array of minutes to expire.
/// </remarks>
public int[] GetMinutes()
{
return (int[])minutes.Clone();
}
/// <summary>
/// Gets the hours to expire.
/// </summary>
/// <value>
/// The hours to expire.
/// </value>
/// <remarks>
/// This returns a copy of the integer array of hours to expire.
/// </remarks>
public int[] GetHours()
{
return (int[])hours.Clone();
}
/// <summary>
/// Gets the days to expire.
/// </summary>
/// <value>
/// The days to expire.
/// </value>
/// <remarks>
/// This returns a copy of the integer array of days to expire.
/// </remarks>
public int[] GetDays()
{
return (int[])days.Clone();
}
/// <summary>
/// Gets the months of the year to expire.
/// </summary>
/// <value>
/// The months of the year to expire.
/// </value>
/// <remarks>
/// This returns a copy of the integer array of months to expire.
/// </remarks>
public int[] GetMonths()
{
return (int[])months.Clone();
}
/// <summary>
/// Gets the days of the week to expire.
/// </summary>
/// <value>
/// The days of the week to expire.
/// </value>
/// <remarks>
/// This returns a copy of the integer array of the days of the week to expire.
/// </remarks>
public int[] GetDaysOfWeek()
{
return (int[])daysOfWeek.Clone();
}
/// <summary>
/// Determines if should expire every minute.
/// </summary>
/// <value>
/// <see langword="true"/> if should expire every minute; otherwise, <see langword="false"/>.
/// </value>
public bool ExpireEveryMinute
{
get { return this.minutes[0] == -1; }
}
/// <summary>
/// Determines if item should expire every day.
/// </summary>
/// <value>
/// <see langword="true"/> if should expire every day; otherwise, <see langword="false"/>.
/// </value>
public bool ExpireEveryDay
{
get { return this.days[0] == -1; }
}
/// <summary>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -