GetNextValidTimeAfter() public method

Returns the next date/time after the given date/time which satisfies the cron expression.
public GetNextValidTimeAfter ( DateTimeOffset date ) : DateTimeOffset?
date DateTimeOffset the date/time at which to begin the search for the next valid date/time
return DateTimeOffset?
        /// <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. 2
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. 3
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 {}
     }
 }
        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();
            }
        }
Esempio n. 5
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. 6
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";
     }
 }