private void ShowResults()
        {
            // This scenario illustrates time zone support in Windows.Globalization.Calendar class

            // Displayed time zones in addition to the local time zone.
            string[] timeZones = new[] { "UTC", "America/New_York", "Asia/Kolkata" };

            // Store results here.
            StringBuilder results = new StringBuilder();

            // Create default Calendar object
            Calendar calendar = new Calendar();
            string localTimeZone = calendar.GetTimeZone();

            // Show current time in local time zone
            results.AppendLine("Current date and time:");
            results.AppendLine(ReportCalendarData(calendar));

            // Show current time in additional time zones
            foreach (string timeZone in timeZones)
            {
                calendar.ChangeTimeZone(timeZone);
                results.AppendLine(ReportCalendarData(calendar));
            }
            results.AppendLine();

            // Change back to local time zone
            calendar.ChangeTimeZone(localTimeZone);

            // Show a time on 14th day of second month of next year.
            // Note the effect of daylight saving time on the results.
            results.AppendLine("Same time on 14th day of second month of next year:");
            calendar.AddYears(1); calendar.Month = 2; calendar.Day = 14;
            results.AppendLine(ReportCalendarData(calendar));
            foreach (string timeZone in timeZones)
            {
                calendar.ChangeTimeZone(timeZone);
                results.AppendLine(ReportCalendarData(calendar));
            }
            results.AppendLine();

            // Change back to local time zone
            calendar.ChangeTimeZone(localTimeZone);

            // Show a time on 14th day of tenth month of next year.
            // Note the effect of daylight saving time on the results.
            results.AppendLine("Same time on 14th day of tenth month of next year:");
            calendar.AddMonths(8);
            results.AppendLine(ReportCalendarData(calendar));
            foreach (string timeZone in timeZones)
            {
                calendar.ChangeTimeZone(timeZone);
                results.AppendLine(ReportCalendarData(calendar));
            }
            results.AppendLine();

            // Display the results
            OutputTextBlock.Text = results.ToString();
        }
        /// <summary>
        /// This is the click handler for the 'Display' button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Display_Click(object sender, RoutedEventArgs e)
        {
            // This scenario illustrates TimeZone support in Windows.Globalization.Calendar class

            // Displayed TimeZones (other than local timezone)
            String[] timeZones = new String[] { "UTC", "America/New_York", "Asia/Kolkata" };

            // Store results here.
            StringBuilder results = new StringBuilder();

            // Create default Calendar object
            Calendar calendar = new Calendar();
            String localTimeZone = calendar.GetTimeZone();

            // Show current time in timezones desired to be displayed including local timezone
            results.AppendLine("Current date and time -");
            results.AppendLine(GetFormattedCalendarDateTime(calendar));
            foreach (String timeZone in timeZones)
            {
                calendar.ChangeTimeZone(timeZone);
                results.AppendLine(GetFormattedCalendarDateTime(calendar));
            }
            results.AppendLine();
            calendar.ChangeTimeZone(localTimeZone);

            // Show a time on 14th day of second month of next year in local, GMT, New York and Indian Time Zones
            // This will show if there were day light savings in time
            results.AppendLine("Same time on 14th day of second month of next year -");
            calendar.AddYears(1); calendar.Month = 2; calendar.Day = 14;
            results.AppendLine(GetFormattedCalendarDateTime(calendar));
            foreach (String timeZone in timeZones)
            {
                calendar.ChangeTimeZone(timeZone);
                results.AppendLine(GetFormattedCalendarDateTime(calendar));
            }
            results.AppendLine();
            calendar.ChangeTimeZone(localTimeZone);

            // Show a time on 14th day of 10th month of next year in local, GMT, New York and Indian Time Zones
            // This will show if there were day light savings in time
            results.AppendLine("Same time on 14th day of tenth month of next year -");
            calendar.AddMonths(8);
            results.AppendLine(GetFormattedCalendarDateTime(calendar));
            foreach (String timeZone in timeZones)
            {
                calendar.ChangeTimeZone(timeZone);
                results.AppendLine(GetFormattedCalendarDateTime(calendar));
            }
            results.AppendLine();

            // Display the results
            rootPage.NotifyUser(results.ToString(), NotifyType.StatusMessage);
        }
Example #3
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;

        }