예제 #1
0
        /// <summary>
        /// Test the extended format with a given date.
        /// </summary>
        /// <param name="format">The extended format arguments.</param>
        /// <param name="getTime">The time when the item has been refreshed.</param>
        /// <param name="nowTime">Always DateTime.Now, or the date to test with.</param>
        /// <returns>true if the item was expired, otherwise false</returns>
        public static bool IsExtendedExpired(string format, DateTime getTime, DateTime nowTime)
        {
            // Validate arguments
            if (format == null)
            {
                throw new ArgumentNullException("format");
            }

            // Remove the seconds to provide better precission on calculations
            getTime = getTime.AddSeconds(getTime.Second * -1);
            nowTime = nowTime.AddSeconds(nowTime.Second * -1);

            ExtendedFormat parsedFormat = (ExtendedFormat)_parsedFormatCache[format];

            if (parsedFormat == null)
            {
                parsedFormat = new ExtendedFormat(format);
                lock (_parsedFormatCache.SyncRoot)
                    _parsedFormatCache[format] = parsedFormat;
            }

            if (nowTime.Subtract(getTime).TotalMinutes < 1)
            {
                return(false);
            }

            // Validate the format arguments
            foreach (int minute in parsedFormat.Minutes)
            {
                foreach (int hour in parsedFormat.Hours)
                {
                    foreach (int day in parsedFormat.Days)
                    {
                        foreach (int month in parsedFormat.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 (parsedFormat.ExpireEveryDayOfWeek)
                            {
                                if (nowTime >= expTime)
                                {
                                    return(true);
                                }
                            }
                            else
                            {
                                // Validate WeekDay
                                foreach (int weekDay in parsedFormat.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);
        }
        /// <summary>
        /// Test the extended format with a given date.
        /// </summary>
        /// <param name="format">The extended format arguments.</param>
        /// <param name="getTime">The time when the item has been refreshed.</param>
        /// <param name="nowTime">Always DateTime.Now, or the date to test with.</param>
        /// <returns>true if the item was expired, otherwise false</returns>
        public static bool IsExtendedExpired( string format, DateTime getTime, DateTime nowTime )
        {
            // Validate arguments
            if(format == null)
                throw new ArgumentNullException("format");

            // Remove the seconds to provide better precission on calculations
            getTime = getTime.AddSeconds( getTime.Second * -1 );
            nowTime = nowTime.AddSeconds( nowTime.Second * -1 );

            ExtendedFormat parsedFormat = (ExtendedFormat)_parsedFormatCache[ format ];
            if( parsedFormat == null )
            {
                parsedFormat = new ExtendedFormat( format );
                lock(_parsedFormatCache.SyncRoot)
                    _parsedFormatCache[ format ] = parsedFormat;
            }

            if( nowTime.Subtract( getTime ).TotalMinutes < 1 )
                return false;

            // Validate the format arguments
            foreach( int minute in parsedFormat.Minutes )
            {
                foreach( int hour in parsedFormat.Hours )
                {
                    foreach( int day in parsedFormat.Days )
                    {
                        foreach( int month in parsedFormat.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( parsedFormat.ExpireEveryDayOfWeek )
                            {
                                if(nowTime >= expTime)
                                    return true;
                            }
                            else
                            {
                                // Validate WeekDay
                                foreach( int weekDay in parsedFormat.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;
        }