Esempio n. 1
0
        public static List<DateTime> GetNextDueDate(String repeatFlag, DateTime dueDate, String repeatFrom, DateTime completedTime, String timeZone, int limit)
        {
            List<DateTime> dueDates = new List<DateTime>();
            if (string.IsNullOrEmpty(repeatFlag) || dueDate == null)
            {
                return dueDates;
            }
            bool isLunar;
            try
            {
                TickRRule rRule = new TickRRule(repeatFlag);
                isLunar = rRule.IsLunarFrequency();
                if (IsRepeatFromCompleteTime(repeatFrom, completedTime))
                {
                    // update rRule, We must clear byDay and byMonthDay when the
                    // task repeat from completedTime.
                    rRule.SetByDay(new List<IWeekDay>());
                    rRule.SetByMonthDay(new int[0]);
                    repeatFlag = rRule.ToTickTickIcal();
                }
                if (rRule.GetCompletedRepeatCount() >= rRule.GetCount())
                {
                    return dueDates;
                }

                if (string.IsNullOrEmpty(timeZone))
                {
                    timeZone = NodaTime.DateTimeZoneProviders.Tzdb.GetSystemDefault().Id;
                }
                DateTimeZone taskTimeZone = DateTimeZoneProviders.Tzdb.GetZoneOrNull(timeZone); //.getTimeZone(timeZone);

                // TODO 有问题 阳历
                //GregorianCalendar taskCal = new GregorianCalendar(taskTimeZone);
                //GregorianCalendar utcCal = new GregorianCalendar(TimeUtils.utcTimezone());
                Calendar taskCal = new Calendar();
                taskCal.ChangeTimeZone(taskTimeZone.Id);
                Calendar utcCal = new Calendar();
                utcCal.ChangeTimeZone(DateTimeZoneProviders.Tzdb.GetSystemDefault().Id);

                DateTime taskStart = InitRepeatStartDate(repeatFrom, completedTime, dueDate, taskCal);
                DateTime utcStart = ConvertDateToUTCDate(taskStart, taskCal, utcCal);
                long untilDateTime = GetTimeFromDateValue(rRule.GetUntil());
                if (isLunar)
                {
                    //DateTime now = DateTime.UtcNow; // TODO 为什么要这么写?   DateTimeUtils.GetCurrentDate();
                    ////当前时间已经超过截止重复时间,必定没有下个有效的重复时间
                    //if (isAfterUntilDate(now, untilDateTime))
                    //{
                    //    return dueDates;
                    //}
                    //GregorianCalendar start = new GregorianCalendar();
                    //start.setTime(dueDate);
                    //Date next = getNextLunarDueDate(start, rRule);
                    //int i = 0;
                    //while (next != null && dueDates.size() < limit && !isAfterUntilDate(next,
                    //        untilDateTime))
                    //{
                    //    start.setTime(next);
                    //    next = getNextLunarDueDate(start, rRule);
                    //    if (next != null && !now.after(next))
                    //    {
                    //        dueDates.add(next);
                    //    }
                    //    if (i++ > 1000)
                    //    {
                    //        break;
                    //    }
                    //}
                    //if (dueDates.isEmpty())
                    //{
                    //    Log.e(TAG, "Get next due_date error: repeatFlag = " + repeatFlag);
                    //}
                    //return dueDates;
                }
                String rRuleString = rRule.ToIcal();
                if (untilDateTime > 0)
                {
                    rRuleString = RemoveKeyValueFromRRule(TickRRule.UNTIL_KEY, rRuleString);
                }
                // TODO 这是干嘛的。。。
                //DateIterator di = DateIteratorFactory.createDateIterator(rRuleString, utcStart, TimeUtils.utcTimezone(), true);
                if (Constants.RepeatFromStatus.DEFAULT.Equals(repeatFrom)
                        || string.IsNullOrEmpty(repeatFrom))
                {
                    // Repeat from default, next dueDate 不会返回小于Now
                    DateTime utcNow = ConvertDateToUTCDate(DateTime.UtcNow, taskCal, utcCal);//(DateTimeUtils.getCurrentDate(), taskCal, utcCal);
                    // TODO advanceTo 是什么,比什么早?
                    //if (utcNow > utcStart)
                    //{
                    //    di.advanceTo(utcNow);
                    //}
                }
                DateTime next;
                int count = 0;
                //while (di.hasNext() && dueDates.Count < limit)
                //{
                //    next = ConvertUtcDateToDate(di.next(), taskCal, utcCal);
                //    if (next > taskStart)
                //    {
                //        //得到的下次重复时间已超过截止重复时间,无效
                //        if (IsAfterUntilDate(next, untilDateTime))
                //        {
                //            return dueDates;
                //        }
                //        else
                //        {
                //            dueDates.Add(next);
                //        }
                //    }
                //    if (count++ > 10000)
                //    {
                //        // 对极端的情况跳出来,不要循环了。
                //        break;
                //    }
                //}
            }
            catch (Exception e)
            {
                //Log.e(TAG, "Get next due_date error: repeatFlag = " + repeatFlag, e);
            }
            return dueDates;

        }
 public int GetRepeatSelectedValue()
 {
     int checkPosition = 0;
     var repeatFlag = Tasks.RepeatFlag;
     if (string.IsNullOrEmpty(repeatFlag))
     {
         return checkPosition;
     }
     TickRRule rRule = new TickRRule(repeatFlag);
     var freq = rRule.GetFreq();
     switch (freq)
     {
         case FrequencyType.Daily:
             checkPosition = Constants.Repeats.REPEATS_DAILY;
             break;
         //case FrequencyType.Hourly:
         //    checkPosition = Constants.Repeats.REPEATS_DAILY;
         //    break;
         //case FrequencyType.Minutely:
         //    checkPosition = Constants.Repeats.REPEATS_DAILY;
         //    break;
         case FrequencyType.Monthly:
             bool onByWeekDay = DateTimeUtils.IsRRuleWeekOnDay(rRule);
             if (rRule.GetByMonthDay().Count > 0)
             {
                 return checkPosition;
             }
             checkPosition = onByWeekDay ? Constants.Repeats.REPEATS_MONTHLY_ON_DAY_COUNT :
                     Constants.Repeats.REPEATS_MONTHLY_ON_DAY;
             break;
         //case FrequencyType.None:
         //    checkPosition = Constants.Repeats.REPEATS_DAILY;
         //    break;
         //case FrequencyType.Secondly:
         //    checkPosition = Constants.Repeats.REPEATS_DAILY;
         //    break;
         case FrequencyType.Weekly:
             IList<IWeekDay> weekdayNums = rRule.GetByDay();
             bool isWeekDays = DateTimeUtils.IsRRuleWeekdays(weekdayNums);
             if (isWeekDays)
             {
                 checkPosition = Constants.Repeats.REPEATS_EVERY_WEEKDAY;
             }
             else if (weekdayNums.Count <= 1)
             {
                 checkPosition = Constants.Repeats.REPEATS_WEEKLY_ON_DAY;
             }
             break;
         case FrequencyType.Yearly:
             if (rRule.IsLunarFrequency())
             {
                 // TODO 暂时不实现
                 //if (isChinese)
                 //{
                 //    checkPosition = 7;
                 //}
             }
             else
             {
                 checkPosition = 6;
             }
             break;
         default:
             break;
     }
     return checkPosition;
 }