Inheritance: ICloneable, IDeserializationCallback
Esempio n. 1
0
 public void EveryTimeButWE()
 {
     var exp = new CronExpression("* * * ? * MON-FRI");
     Assert.IsTrue(exp.IsSatisfiedBy(new DateTime(2010, 6, 16)));
     Assert.IsFalse(exp.IsSatisfiedBy(new DateTime(2010, 6, 19)));
    
 }
        /// <summary>
        /// Understanding how to generate the
        /// </summary>
        /// <param name="crondetailsstring"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public static DateTime?ReturnNextOccurrenceDate(string crondetailsstring, DateTime startDate, DateTime endDate)
        {
            try
            {
                if (!string.IsNullOrEmpty(crondetailsstring))
                {
                    //var crontabCheck = CrontabSchedule.Parse(crondetailsstring);
                    //var nextDueTime = crontabCheck.GetNextOccurrence(startDate, endDate);
                    //var occurrences = crontabCheck.GetNextOccurrences(startDate, endDate);

                    CronExpression exp      = new Quartz.CronExpression(crondetailsstring);
                    var            nextFire = exp.GetNextValidTimeAfter(DateTime.Now);
                    //var quartzoccurrences = crontabCheck.GetNextOccurrences(startDate, endDate);

                    //Console.WriteLine(string.Join(Environment.NewLine, from t in quartzoccurrences
                    //    select $"{t:ddd, dd MMM yyyy HH:mm}"));

                    //  return nextDueTime;
                    return(nextFire?.UtcDateTime);
                }
                else
                {
                }

                return(null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 3
0
 public void LastOfTheMonth()
 {
     var exp = new CronExpression("* * * L * ?");
     Assert.IsTrue(exp.IsSatisfiedBy(new DateTime(2000, 1, 31)));
     Assert.IsTrue(exp.IsSatisfiedBy(new DateTime(2000, 2, 29)));
     Assert.IsFalse(exp.IsSatisfiedBy(new DateTime(2000, 2, 28)));
 }
        private static void InputChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var m = (sender) as Model;
            m.Triggers.Clear();

            TextToScheduleResults results = null;

            try
            {
                CronExpression cron = new CronExpression(m.Input);
                results = new TextToScheduleResults();
                results.Add(TriggerBuilder.Create()
                    .WithCronSchedule(m.Input));
            }
            catch
            {
                var english = TextToScheduleFactory.CreateEnglishParser();
                results = english.Parse(m.Input);
            }

            if (results == null)
            {
                try
                {
                    //try german verion
                    var german = new TextToSchedule(new AndrewSmith.Quartz.TextToSchedule.Grammars.GermanGrammar(), new AndrewSmith.Quartz.TextToSchedule.Grammars.GermanGrammarHelper());
                    results = german.Parse(m.Input);
                }
                catch
                {

                }
            }
            

            if (results != null)
            {            
                List<DateTime> list = new List<DateTime>();

                foreach (var groups in results.RegisterGroups)
	            {
                    var trigger = (IOperableTrigger)groups.TriggerBuilder.Build();
                    var dates = TriggerUtils.ComputeFireTimes(trigger, groups.Calendar, MaxResultsToReturn);

                    foreach (var d in dates)
                    {
                        list.Add(d.ToLocalTime().DateTime);
                    }
	            }

                //sort the list
                list.Sort();

                foreach (var item in list.Take(MaxResultsToReturn))
                {
                    m.Triggers.Add(item.ToString("ddd, MM/dd/yyyy hh:mm:ss tt"));
                }
            }
        }
Esempio n. 5
0
 public static bool Trigger(string cornExpress, DateTime dateUtc)
 {
     if(!CronExpression.IsValidExpression(cornExpress)){
         throw new Exception(string.Format("corn表达式:{0}不正确。",cornExpress));
     }
     CronExpression corn = new CronExpression(cornExpress);
     return corn.IsSatisfiedBy(dateUtc);
 }
Esempio n. 6
0
        private DateTime? UpdateNext(DateTime previous)
        {
            var exp = new CronExpression(txtCron.Text);

            var next = exp.GetNextValidTimeAfter(previous.ToUniversalTime());

            if (next.HasValue)
            {
                var dt = next.Value.DateTime.ToLocalTime();

                lbFires.Items.Add(dt.ToString("MM/dd/yyyy hh:mm:ss tt"));

                return dt;
            }

            return null;
        }
Esempio n. 7
0
 private void CronNextForm_Load( object sender, EventArgs e )
 {
     if( !string.IsNullOrEmpty( cronString ) ) {
         try {
             CronExpression ce = new CronExpression( cronString );
             DateTime dt = lastFinished.HasValue ? lastFinished.Value.ToUniversalTime() : DateTime.Now.ToUniversalTime();
             for( int i = 0; i < 10; i++ ) {
                 DateTime? next = ce.GetNextValidTimeAfter( dt );
                 if( !next.HasValue ) {
                     break;
                 }
                 dt = next.Value;
                 listView1.Items.Add( dt.ToLocalTime().ToString( "yyyy-MM-dd HH:mm:ss" ) );
             }
             listView1.AutoResizeColumns( ColumnHeaderAutoResizeStyle.ColumnContent );
         } catch {}
     }
 }
Esempio n. 8
0
 /// <summary>
 /// Determines whether the specified <see cref="CronExpression"/> is equal to the current <see cref="CronExpression"/>.
 /// </summary>
 /// <returns>
 /// true if the specified <see cref="CronExpression"/> is equal to the current <see cref="CronExpression"/>; otherwise, false.
 /// </returns>
 /// <param name="other">The <see cref="CronExpression"/> to compare with the current <see cref="CronExpression"/>. </param>
 public bool Equals(CronExpression other)
 {
     if (ReferenceEquals(null, other)) return false;
     if (ReferenceEquals(this, other)) return true;
     return Equals(other.cronExpressionString, cronExpressionString) && Equals(other.timeZone, timeZone);
 }
Esempio n. 9
0
 /// <summary>
 /// Creates a new object that is a copy of the current instance.
 /// </summary>
 /// <returns>
 /// A new object that is a copy of this instance.
 /// </returns>
 public object Clone()
 {
     CronExpression copy;
     try
     {
         copy = new CronExpression(CronExpressionString);
         copy.TimeZone = TimeZone;
     }
     catch (FormatException)
     {
         // never happens since the source is valid...
         throw new Exception("Not Cloneable.");
     }
     return copy;
 }
        public DateTimeOffset GetNextScheduleFromQuartzExpression(DateTimeOffset withinThisMinute)
        {
            var expression = new CronExpression(job.Crontab);

            var startMinute = new DateTimeOffset(withinThisMinute.Year, withinThisMinute.Month, withinThisMinute.Day, withinThisMinute.Hour, withinThisMinute.Minute, 0, 0, withinThisMinute.Offset);
            startMinute = startMinute.AddMilliseconds(-1);

            var result = DateTimeOffset.MinValue;

            var nextOccurrence = expression.GetTimeAfter(startMinute.ToUniversalTime());
            if (nextOccurrence.HasValue)
            {
                var isWithinTheNextMinute = 60 > Math.Abs((nextOccurrence.Value.LocalDateTime - startMinute).TotalSeconds);
                Trace.WriteLine("CronExp: Id {0} ('{1}'): '{2}' [{3}].", job.Id, job.Crontab, nextOccurrence.Value.ToString("yyyy-MM-dd HH:mm:sszzz"), isWithinTheNextMinute);
                if (isWithinTheNextMinute)
                {
                    result = nextOccurrence.Value.LocalDateTime;
                }
            }
            else
            {
                Trace.WriteLine("CronExp: Id {0} ('{1}'): '{2}' [{3}].", job.Id, job.Crontab, result.ToString("yyyy-MM-dd HH:mm:sszzz"), false);
            }

            return result;
        }
        public void TestCronExpression()
        {
            //[秒] [分] [小时] [日] [月] [周] [年]
            //实例化表达式类,把字符串转成一个对象
            CronExpression expression = new CronExpression("0 15 10 * * ? 2012");

            while (true)
            {
                DateTimeOffset utcNow = SystemTime.UtcNow();
                Console.WriteLine("UtcNow - " + utcNow);

                //Console.WriteLine("GetFinalFireTime - " + expression.GetFinalFireTime());这个方法没有实现
                //得到给定时间下一个无效的时间
                Console.WriteLine("GetNextInvalidTimeAfter - " + expression.GetNextInvalidTimeAfter(utcNow));
                //得到给定时间的下一个有效的时间
                Console.WriteLine("GetNextValidTimeAfter - " + expression.GetNextValidTimeAfter(utcNow));
                //得到给定时间下一个符合表达式的时间
                Console.WriteLine("GetTimeAfter - " + expression.GetTimeAfter(utcNow));
                //Console.WriteLine("GetTimeBefore - " + expression.GetTimeBefore(utcNow));这个方法没有实现
                //给定时间是否符合表达式
                Console.WriteLine("IsSatisfiedBy - " + expression.IsSatisfiedBy(new DateTimeOffset(2012, 4, 6, 2, 15, 0, TimeSpan.Zero)));
                Console.WriteLine(expression.TimeZone);
                Console.WriteLine("------------------------------------");
                Console.WriteLine(expression.GetExpressionSummary());
                Console.Read();
            }
        }
 /// <summary>
 /// Create a trigger for the given cron expression.
 /// <see cref="Quartz.CronExpression"/>
 /// </summary>
 public CronTrigger(string expression)
 {
     _expression = expression;
     _cronExpression = new CronExpression(expression);
 }
 public void WhenIParseTheSchedule()
 {
     var cron = new CronExpression(ScenarioContext.Current.Get<string>("cronString"));
     ScenarioContext.Current.Add("cronExpression", cron);
 }
Esempio n. 14
0
 private void UpdateNext()
 {
     //string snr = null;
     if( !Configuration.IsServiceRunning() ) {
         lblNextStart.ForeColor = Color.Red;
         //if( !string.Equals( lblNextStart.Text, "Service is not running" ) ) {
         //    lblNextStart.ForeColor = Color.Red;
         //    //snr = "Service is not running";
         //}
     } else {
         lblNextStart.ForeColor = SystemColors.ControlText;
     }
     try {
         CronExpression ce = new CronExpression( job.CronConfig );
         DateTime? next = ce.GetNextValidTimeAfter( job.LastFinished.HasValue ? job.LastFinished.Value.ToUniversalTime() : DateTime.Now.ToUniversalTime() );
         if( next.HasValue ) {
             string ts = next.Value.ToLocalTime().ToString( "yyyy:MM:dd HH:mm:ss" );
             DateTime lt = next.Value.ToLocalTime();
             if( DateTime.Now > lt ) {
                 next = ce.GetNextValidTimeAfter( DateTime.Now.ToUniversalTime() );
                 if( next.HasValue ) {
                     ts = next.Value.ToLocalTime().ToString( "yyyy:MM:dd HH:mm:ss" );
                 }
             }
             //if( snr != null ) {
             //    ts += " ({0})".FillBlanks( snr );
             //}
             lblNextStart.Text = ts;
         }
     } catch {
     }
 }
Esempio n. 15
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="lastRun"></param>
 /// <param name="cronString"></param>
 /// <returns></returns>
 private string GetNextRun( DateTime? lastRun, string cronString )
 {
     try {
         CronExpression ce = new CronExpression( cronString );
         DateTime dt = lastRun.HasValue ? lastRun.Value.ToUniversalTime() : DateTime.Now.ToUniversalTime();
         if( DateTime.Now > dt ) {
             dt = DateTime.Now.ToUniversalTime();
         }
         DateTime? next = ce.GetNextValidTimeAfter( dt );
         return next.HasValue ? next.Value.ToLocalTime().ToString( "yyyy-MM-dd HH:mm:ss" ) : "Never";
     } catch {
         return "Never";
     }
 }