public List <string> NextOccurances(string cron, DateTime startDate, DateTime endDate)
        {
            List <string> FinalOccurances = new List <string>();
            CronPattern   cronPattern     = new CronPattern();

            var now   = new JalaliCalendar().GetPersianDateTime(startDate);
            var later = new JalaliCalendar().GetPersianDateTime(endDate);

            CronPattern parts = cronPattern.Parse(cron, now, later);

            this.Parts = parts;
            var occurances = NextOccurances();

            foreach (var occurance in occurances)
            {
                var occuranceInGregorian = JalaliToGregorian(occurance);
                if (DateTime.Now <= occuranceInGregorian && endDate > occuranceInGregorian)
                {
                    FinalOccurances.Add(occurance);
                }
            }

            if (FinalOccurances.Count == 0)
            {
                throw new Exception("الگوی انتخابی بین بازه تاریخی مشخص شده نیست .لطفا بازه انتخابی بین یک سال شمسی باشد");
            }

            return(FinalOccurances);
        }
Beispiel #2
0
        public int DayOfYearToMonthOfYear(int dayOfYear, int year)
        {
            PersianCalendar pc = new PersianCalendar();
            DateTime        dt = new DateTime(year, 1, 1, pc);

            dt = dt.AddDays(dayOfYear - 1);
            var pers = new JalaliCalendar().GetPersianDateTime(dt);

            return(pers.Month);
        }
Beispiel #3
0
        public int DayOfYearToDayNum(int dayOfYear)
        {
            PersianCalendar pc = new PersianCalendar();
            DateTime        dt = new DateTime(1391, 1, 1, pc);

            dt = dt.AddDays(dayOfYear - 1);
            var pers = new JalaliCalendar().GetPersianDateTime(dt);

            return(pers.DayNumber);
        }
Beispiel #4
0
        public int DaysInYearsToDayNum(int daysInYears)
        {
            int year;
            int month;
            int day;

            DaysInYearsToDateParts(daysInYears, out year, out month, out day);
            var TheDate = new CronTabScheduler().JalaliToGregorian(year + "/" + month + "/" + day);
            var pers    = new JalaliCalendar().GetPersianDateTime(TheDate);

            return(pers.DayNumber);
        }
Beispiel #5
0
        public void MinuteOfYearToDayAndMonth(int minuteOfYear, out int minute, out int hour, out int day, out int month)
        {
            PersianCalendar pc = new PersianCalendar();
            DateTime        dt = new DateTime(1391, 1, 1, pc);

            dt = dt.AddMinutes(minuteOfYear);
            var pers = new JalaliCalendar().GetPersianDateTime(dt);

            month  = pers.Month;
            day    = pers.Day;
            hour   = pers.Hour;
            minute = pers.Minute;
        }
Beispiel #6
0
        public int GetDayInYears(JalaliCalendar j)
        {
            DateTime        theDate = new DateTime(1, 1, 1);
            PersianCalendar pc      = new PersianCalendar();

            int totdalDays = 0;

            for (int i = 1; i < j.Year; i++)
            {
                totdalDays += pc.GetDaysInYear(i);
            }
            return(totdalDays + j.DayOfYear);
        }
Beispiel #7
0
        public JalaliCalendar GetPersianDateTime(DateTime helper, string format = "yyyy/mm/dd")
        {
            if (helper.Year < 1000)
            {
                helper = DateTime.Now;
            }
            PersianCalendar pc = new PersianCalendar();
            var             persianCalendarPart = new JalaliCalendar();

            persianCalendarPart.Year          = pc.GetYear(helper);
            persianCalendarPart.Month         = pc.GetMonth(helper);
            persianCalendarPart.Day           = pc.GetDayOfMonth(helper);
            persianCalendarPart.DayOfYear     = pc.GetDayOfYear(helper);
            persianCalendarPart.DaysInYears   = GetDayInYears(persianCalendarPart);
            persianCalendarPart.MonthsInYears = GetMonthsInYears(persianCalendarPart);

            persianCalendarPart.Hour         = pc.GetHour(helper);
            persianCalendarPart.Minute       = pc.GetMinute(helper);
            persianCalendarPart.MinuteOfYear = (persianCalendarPart.DayOfYear * 24 * 60) + (persianCalendarPart.Hour * 60) + persianCalendarPart.Minute;
            persianCalendarPart.Second       = pc.GetSecond(helper);
            persianCalendarPart.Milisecond   = pc.GetMilliseconds(helper);
            persianCalendarPart.DayNumber    = GetDayOfWeekNumber(helper);
            return(persianCalendarPart);
        }
        /// <summary>
        /// return next occurances as Jalali DateTime List
        /// </summary>
        /// <param name="parts"></param>
        /// <returns>List<string></returns>
        private List <string> NextOccurances()
        {
            var parts = this.Parts;
            var persianCalendarPart = new JalaliCalendar();

            List <string> dates2 = new List <string>();

            foreach (var dayPart in parts.Days)
            {
                foreach (var hourPart in parts.Hours)
                {
                    foreach (var minutePart in parts.Minutes)
                    {
                        int month;
                        int day;
                        int yearr;
                        persianCalendarPart.DaysInYearsToDateParts(dayPart, out yearr, out month, out day);

                        dates2.Add(string.Format("{0}/{1}/{2} {3}:{4}", yearr, month, day, hourPart.ToString("00"), minutePart.ToString("00")));
                    }
                }
            }
            return(dates2);
        }
        public CronPattern Parse(string cron, JalaliCalendar startDate, JalaliCalendar endDate)
        {
            //CronTabScheduler cronTabScheduler = new NCronTab.CronTabScheduler();
            var persianCalendarPart = new JalaliCalendar();

            var parts = cron.Split(' ');


            #region [Days]
            int dayStep          = 0;
            int currentDayOfYear = 0;
            if (parts[2] == "*")
            {
                for (int i = startDate.DaysInYears; i <= endDate.DaysInYears; i++)
                {
                    Days.Add(i);
                }
            }
            else
            {
                var dayParts = parts[2].Split('/');
                if (dayParts.Count() == 1)
                {
                    Days.Add(int.Parse(dayParts[0]));
                }
                else if (dayParts.Count() == 2)
                {
                    var days = dayParts[1].Split(',');
                    dayStep          = int.Parse(days[0]);
                    currentDayOfYear = startDate.DayOfYear;
                    for (int i = startDate.DaysInYears; i <= endDate.DaysInYears; i = i + dayStep)
                    {
                        Days.Add(i);
                    }
                }
            }

            //else
            //{
            //    if (parts[2].Contains("-"))
            //    {
            //        Days.Clear();
            //        var dayParts = parts[2].Split('-');
            //        var firstOfWeek = int.Parse(dayParts[0]);
            //        var endOfWeek = int.Parse(dayParts[1]);
            //        currentDayOfYear = startDate.DayOfYear;

            //        for (int i = startDate.DaysInYears; i <= endDate.DaysInYears; i++)
            //        {
            //            var dayOfMonth = persianCalendarPart.DayOfYearToDayOfMonth(i);
            //            var dayNum = persianCalendarPart.DayOfYearToDayNum(i);

            //            if (WeekDays.Contains(dayNum) && dayOfMonth >= firstOfWeek && dayOfMonth < endOfWeek)
            //                Days.Add(i);
            //        }
            //        Months = null;
            //        return this;
            //    }
            //    else
            //    {
            //        var dayParts = parts[2].Split('/');
            //        if (dayParts.Count() == 1)
            //        {
            //            Days.Add(int.Parse(dayParts[0]));
            //        }
            //        else if (dayParts.Count() == 2)
            //        {
            //            var days = dayParts[1].Split(',');
            //            dayStep = int.Parse(days[0]);
            //            currentDayOfYear = startDate.DayOfYear;
            //            for (int i = startDate.DayOfYear; i <= endDate.DayOfYear; i = i + dayStep)
            //            {
            //                Days.Add(i);
            //            }
            //        }
            //    }
            //}
            #endregion



            //for (int i = startDate.Year; i <= endDate.Year; i++)
            //{
            //    Years.Add(i);
            //}



            #region [WeekDays]
            if (parts[4] != "*")
            {
                Days = new List <int>();
                var        yearParts   = parts[4].Split(',');
                List <int> daysForWeek = new List <int>();
                for (int i = startDate.DaysInYears; i <= endDate.DaysInYears; i++)
                {
                    daysForWeek.Add(i);
                }
                foreach (var item in yearParts)
                {
                    var dayNum = persianCalendarPart.WeekMapping.FirstOrDefault(q => q.Key == item);
                    WeekDays.Add(dayNum.Value);
                }
                foreach (var dayForWeek in daysForWeek)
                {
                    var dayNum = persianCalendarPart.DaysInYearsToDayNum(dayForWeek);
                    if (WeekDays.Contains(dayNum))
                    {
                        Days.Add(dayForWeek);
                    }
                }
            }
            #endregion


            #region [Minutes]
            if (parts[0] == "*")
            {
                for (int i = 0; i < 60; i++)
                {
                    Minutes.Add(i);
                }
            }
            else
            {
                var minuteParts = parts[0].Split('/');
                if (minuteParts.Count() == 1)
                {
                    Minutes.Add(int.Parse(minuteParts[0]));
                }
                else if (minuteParts.Count() == 2)
                {
                    int minPart = 0;
                    if (minuteParts[0] == "*" && int.TryParse(minuteParts[1].ToString(), out minPart))
                    {
                        for (int i = startDate.MinuteOfYear; i < endDate.MinuteOfYear; i = i + minPart)
                        {
                            Minutes.Add(i);
                        }
                    }
                    return(this);
                }
            }

            #endregion



            #region [Hours]
            if (parts[1] == "*")
            {
                for (int i = 0; i < 24; i++)
                {
                    Hours.Add(i);
                }
            }
            else
            {
                var hourParts = parts[1].Split('/');
                if (hourParts.Count() == 1)
                {
                    Hours.Add(int.Parse(hourParts[0]));
                }
                else if (hourParts.Count() == 2)
                {
                    int minPart = 0;
                    if (hourParts[0] == "*" && int.TryParse(hourParts[1].ToString(), out minPart))
                    {
                        for (int i = startDate.Hour; i < endDate.Hour; i = i + minPart)
                        {
                            Hours.Add(i);
                        }
                    }
                }
            }
            #endregion

            #region [Months]
            bool       monthly      = false;
            List <int> monthStep    = new List <int>();
            int        currentMonth = 0;
            if (parts[3] == "*")
            {
                Months.Add(1);
            }
            else
            {
                var monthParts = parts[3].Split('/');
                if (monthParts.Count() == 1)
                {
                    monthly = false;
                    monthStep.Add(int.Parse(monthParts[0]));
                }
                else if (monthParts.Count() == 2)
                {
                    monthly = true;
                    foreach (var item in monthParts[1].Split(','))
                    {
                        monthStep.Add(int.Parse(item));
                    }
                }

                var daysInMonthPart = new List <int>();
                var days            = new List <int>();
                days.AddRange(Days);
                Days.Clear();
                for (int i = startDate.DaysInYears; i <= endDate.DaysInYears; i++)
                {
                    int yearr;
                    int month;
                    int day;
                    persianCalendarPart.DaysInYearsToDateParts(i, out yearr, out month, out day);
                    if (monthly && (monthStep.Any(q => q == month)) && days.Contains(day))
                    {
                        Days.Add(i);
                    }
                    else
                    if ((month == monthStep[0]) && days.Contains(day))
                    {
                        Days.Add(i);
                    }
                }
            }
            #endregion
            return(this);
        }
Beispiel #10
0
        public int GetMonthsInYears(JalaliCalendar j)
        {
            DateTime theDate = new DateTime(1, 1, 1);

            return((j.Year * 12) + j.Month);
        }