GetDayOfWeek() public abstract method

public abstract GetDayOfWeek ( DateTime time ) : DayOfWeek
time DateTime
return DayOfWeek
Ejemplo n.º 1
0
        public void SetDaysInScope()
        {
            _daysInScope = new List <string>();
            _dayCodes    = new List <int>();

            int day            = _day;
            int month          = _month;
            int year           = _year;
            int numDaysInMonth = _numberOfDaysInMonth;

            string dayString;
            string dayOfWeekAlpha;
            string monthAlpha;
            string dayCode;

            for (int i = 0; i < _numDaysBeingDisplayed; i++, day++)
            {
                dayCode = "";

                if (day > numDaysInMonth)
                {
                    day = day - numDaysInMonth;
                    month++;

                    if (month > 12)
                    {
                        year++;
                        month = 1;
                    }

                    numDaysInMonth = DateTime.DaysInMonth(year, month);
                }


                DateTime currentDay = new DateTime(year, month, day, new GregorianCalendar());
                dayOfWeekAlpha = _myCal.GetDayOfWeek(currentDay).ToString();
                monthAlpha     = GetMonthString(month);

                dayString = dayOfWeekAlpha + ", " + monthAlpha + " " + day.ToString();
                _daysInScope.Add(dayString);


                dayCode += _myCal.GetDayOfMonth(currentDay).ToString();
                dayCode += month.ToString();
                dayCode += year.ToString();
                _dayCodes.Add(Int32.Parse(dayCode));

                Console.WriteLine("Day Code: " + dayCode);
            }
        }
Ejemplo n.º 2
0
        internal static int InternalGetWeekOfYearFullDays(Calendar cal, DateTime time, int firstDayOfWeek, int fullDays, int daysOfMinYearMinusOne)
        {
            int daysInYear = cal.GetDayOfYear(time) - 1;
            int num        = ((int)cal.GetDayOfWeek(time)) - (daysInYear % 7);
            int num2       = ((firstDayOfWeek - num) + 14) % 7;

            if ((num2 != 0) && (num2 >= fullDays))
            {
                num2 -= 7;
            }
            int num3 = daysInYear - num2;

            if (num3 < 0)
            {
                int year = cal.GetYear(time);
                if (year <= cal.GetYear(cal.MinSupportedDateTime))
                {
                    daysInYear = daysOfMinYearMinusOne;
                }
                else
                {
                    daysInYear = cal.GetDaysInYear(year - 1);
                }
                num -= daysInYear % 7;
                num2 = ((firstDayOfWeek - num) + 14) % 7;
                if ((num2 != 0) && (num2 >= fullDays))
                {
                    num2 -= 7;
                }
                num3 = daysInYear - num2;
            }
            return((num3 / 7) + 1);
        }
Ejemplo n.º 3
0
        //---- Configura los timers
        private void timersSettup()
        {
            DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;

            System.Globalization.Calendar cal = dfi.Calendar;

            timer_Notificaciones.Tick    += new EventHandler(timer_Notificaciones_tick);
            timer_Notificaciones.Interval = new TimeSpan(notificaciones_modo > 2 ? 4 : notificaciones_modo, 0, 0);

            timer_Recordatorio.Tick    += new EventHandler(timer_Recordatorio_tick);
            timer_Recordatorio.Interval = new TimeSpan(0, 20, 0);

            if (notificaciones_modo > 0)
            {
                timer_Notificaciones.Start();
            }
            else
            {
                timer_Notificaciones.Stop();
            }
            if (cal.GetDayOfWeek(DateTime.Now).ToString().Equals("Friday") && recordatorio_activo)
            {
                timer_Recordatorio.Start();
            }
            else
            {
                timer_Recordatorio.Stop();
            }
        }
Ejemplo n.º 4
0
        private TableRow CreateDayHeader(DateTime firstDay, DateTime visibleDate, System.Globalization.Calendar threadCalendar)
        {
            TableRow row = new TableRow();

            DateTimeFormatInfo dtf = DateTimeFormatInfo.CurrentInfo;

            TableItemStyle dayNameStyle = new TableItemStyle();

            dayNameStyle.HorizontalAlign = HorizontalAlign.Center;
            dayNameStyle.CopyFrom(DayHeaderStyle);
            DayNameFormat dayNameFormat = DayNameFormat;

            int numericFirstDay = (int)threadCalendar.GetDayOfWeek(firstDay);

            for (int i = numericFirstDay; i < numericFirstDay + 7; i++)
            {
                string dayName;
                int    dayOfWeek = i % 7;
                switch (dayNameFormat)
                {
                case DayNameFormat.FirstLetter:
                    dayName = dtf.GetDayName((DayOfWeek)dayOfWeek).Substring(0, 1);
                    break;

                case DayNameFormat.FirstTwoLetters:
                    dayName = dtf.GetDayName((DayOfWeek)dayOfWeek).Substring(0, 2);
                    break;

                case DayNameFormat.Full:
                    dayName = dtf.GetDayName((DayOfWeek)dayOfWeek);
                    break;

                case DayNameFormat.Short:
                    dayName = dtf.GetAbbreviatedDayName((DayOfWeek)dayOfWeek);
                    break;

                case DayNameFormat.Shortest:
                    dayName = dtf.GetShortestDayName((DayOfWeek)dayOfWeek);
                    break;

                default:
                    System.Diagnostics.Debug.Assert(false, "Unknown DayNameFormat value!");

                    goto
                case DayNameFormat.Short;
                }

                TableCell cell = new TableCell();
                cell.ApplyStyle(dayNameStyle);
                cell.Text = dayName;
                row.Cells.Add(cell);
            }
            return(row);
        }
Ejemplo n.º 5
0
 private void Update()
 {
     firstDayOfMonth = new DateTime(yearNumber, monthNumber, 1, currentCalendar);
     offset          = ((int)currentCalendar.GetDayOfWeek(firstDayOfMonth) - (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek + 7) % 7;
     firstDayOfView  = firstDayOfMonth.AddDays(-1 * offset);
     days            = new DateTime[42];
     for (int i = 0; i < 42; i++)
     {
         days[i] = firstDayOfView + TimeSpan.FromDays(i);
     }
     daysPanel.SetValue(ItemsControl.ItemsSourceProperty, days);
 }
Ejemplo n.º 6
0
        private SortedList <DateTime, string> CreateDatesAndFileNames(int DaysInPast, string FilePath, string FileName, string fileType)
        {
            SortedList <DateTime, string> dateAndFileNames = new SortedList <DateTime, string>();

            System.Globalization.Calendar cal = CultureInfo.InvariantCulture.Calendar;
            SortedList <int, string>      sDates = new SortedList <int, string>();
            int d = 1; int fileCount = 0;

            while (fileCount < DaysInPast)
            {
                DateTime dt = DateTime.Now.AddDays(-d);
                d++;
                if (cal.GetDayOfWeek(dt) != DayOfWeek.Sunday && (cal.GetDayOfWeek(dt) != DayOfWeek.Saturday))
                {
                    string sDate    = dt.ToString("yyyyMMdd");
                    string fileName = FilePath + @"\" + FileName + "_" + sDate + "." + fileType;
                    dateAndFileNames.Add(dt, fileName);
                    fileCount++;
                }
            }
            return(dateAndFileNames);
        }
Ejemplo n.º 7
0
        private DateTime FirstCalendarDay(DateTime visibleDate)
        {
            DateTime firstDayOfMonth = new DateTime(visibleDate.Year, visibleDate.Month, 1);

            System.Globalization.Calendar cal = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar;
            int daysFromLastMonth             = ((int)cal.GetDayOfWeek(firstDayOfMonth)) - NumericFirstDayOfWeek();

            // Always display at least one day from the previous month
            if (daysFromLastMonth <= 0)
            {
                daysFromLastMonth += 7;
            }
            return(cal.AddDays(firstDayOfMonth, -daysFromLastMonth));
        }
Ejemplo n.º 8
0
        CalendarDay getDay(DateTime d, DateTime todaysDate, DateTime visibleDate, System.Globalization.Calendar threadCalendar)
        {
            int    dayOfWeek        = (int)threadCalendar.GetDayOfWeek(d);
            int    dayOfMonth       = threadCalendar.GetDayOfMonth(d);
            string dayNumberText    = d.ToString("dd", CultureInfo.CurrentCulture);
            int    visibleDateMonth = threadCalendar.GetMonth(visibleDate);

            return(new CalendarDay(d,
                                   (dayOfWeek == 0 || dayOfWeek == 6),             // IsWeekend
                                   d.Equals(todaysDate),                           // IsToday
                                   threadCalendar.GetMonth(d) != visibleDateMonth, // IsOtherMonth
                                   dayNumberText                                   // Number Text
                                   ));
        }
Ejemplo n.º 9
0
        // How many days of the previous month need to be displayed
        private int PreviousMonthDays(DateTime firstOfMonth)
        {
            DayOfWeek day = _calendar.GetDayOfWeek(firstOfMonth);
            int       i   = ((day - _firstDayOfWeek + NUMBER_OF_DAYS_IN_WEEK) % NUMBER_OF_DAYS_IN_WEEK);

            if (i == 0)
            {
                return(NUMBER_OF_DAYS_IN_WEEK);
            }
            else
            {
                return(i);
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// https://blogs.msdn.microsoft.com/shawnste/2006/01/24/iso-8601-week-of-year-format-in-microsoft-net/
        /// </summary>
        public static int GetIso8601WeekOfYear(this System.Globalization.Calendar calendar, DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
        {
            // Seriously cheat.  If its Monday, Tuesday or Wednesday, then it'll
            // be the same week# as whatever Thursday, Friday or Saturday are,
            // and we always get those right
            var day = calendar.GetDayOfWeek(time);

            if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
            {
                time = time.AddDays(3);
            }

            // Return the week of our adjusted day
            return(calendar.GetWeekOfYear(time, rule, firstDayOfWeek));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 获取中文星期[星期日,星期一...]
        /// </summary>
        /// <param name="sDate"></param>
        /// <returns></returns>
        public static string getWeek(DateTime sDate)
        {
            System.Globalization.Calendar myCal = CultureInfo.InvariantCulture.Calendar;


            string rStr = "";

            switch (myCal.GetDayOfWeek(sDate).ToString())
            {
            case "Sunday":
                rStr = "星期日";
                break;

            case "Monday":
                rStr = "星期一";
                break;

            case "Tuesday":
                rStr = "星期二";
                break;

            case "Wednesday":
                rStr = "星期三";
                break;

            case "Thursday":
                rStr = "星期四";
                break;

            case "Friday":
                rStr = "星期五";
                break;

            case "Saturday":
                rStr = "星期六";
                break;
            }
            return(rStr);
        }
Ejemplo n.º 12
0
        // How many days of the previous month need to be displayed
        private int GetNumberOfDisplayedDaysFromPreviousMonth(DateTime firstOfMonth)
        {
            DayOfWeek day = _calendar.GetDayOfWeek(firstOfMonth);
            int       i;

            if (this.Owner != null)
            {
                i = ((day - this.Owner.FirstDayOfWeek + NUMBER_OF_DAYS_IN_WEEK) % NUMBER_OF_DAYS_IN_WEEK);
            }
            else
            {
                i = ((day - DateTimeHelper.GetDateFormat(DateTimeHelper.GetCulture(this)).FirstDayOfWeek + NUMBER_OF_DAYS_IN_WEEK) % NUMBER_OF_DAYS_IN_WEEK);
            }

            if (i == 0)
            {
                return(NUMBER_OF_DAYS_IN_WEEK);
            }
            else
            {
                return(i);
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// How many days of the previous month need to be displayed.
        /// </summary>
        private int PreviousMonthDays(DateTime firstOfMonth)
        {
            DayOfWeek day = _calendar.GetDayOfWeek(firstOfMonth);
            int       i;

            if (Owner != null)
            {
                i = ((day - Owner.FirstDayOfWeek + NumberOfDaysPerWeek) % NumberOfDaysPerWeek);
            }
            else
            {
                i = ((day - DateTimeHelper.GetCurrentDateFormat().FirstDayOfWeek + NumberOfDaysPerWeek) % NumberOfDaysPerWeek);
            }

            if (i == 0)
            {
                return(NumberOfDaysPerWeek);
            }
            else
            {
                return(i);
            }
        }
Ejemplo n.º 14
0
        // How many days of the previous month need to be displayed
        private int PreviousMonthDays(DateTime firstOfMonth)
        {
            DayOfWeek day = _calendar.GetDayOfWeek(firstOfMonth);
            int       i;

            if (this.Owner != null)
            {
                i = ((day - this.Owner.FirstDayOfWeek + NUMBER_OF_DAYS_IN_WEEK) % NUMBER_OF_DAYS_IN_WEEK);
            }
            else
            {
                i = ((day - DateTimeHelper.GetCurrentDateFormat().FirstDayOfWeek + NUMBER_OF_DAYS_IN_WEEK) % NUMBER_OF_DAYS_IN_WEEK);
            }

            if (i == 0)
            {
                return(NUMBER_OF_DAYS_IN_WEEK);
            }
            else
            {
                return(i);
            }
        }
        private int GetDayOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek, int weekOfYear, Calendar myCalendar)
        {
            int retVal = -367;
            int offset = 0;
            int dayOfWeekForJan1, dayOfYear, dayOfWeek;
            dayOfYear = myCalendar.GetDayOfYear(time); //1-based
            dayOfWeek = (int)myCalendar.GetDayOfWeek(time) - (int)firstDayOfWeek + 1; //1-based
            if (dayOfWeek <= 0)
                dayOfWeek += c_DAYS_PER_WEEK; //Make it a positive value
            dayOfWeekForJan1 = dayOfWeek - (dayOfYear - 1) % c_DAYS_PER_WEEK; //1-based
            if (dayOfWeekForJan1 <= 0)
                dayOfWeekForJan1 += c_DAYS_PER_WEEK; //Make it a positive value
                                                     // When the day of specific time falls on the previous year,
                                                     // return the number of days from January 1 directly.
                                                     // There could be 6 weeks within a month.
            if (time.Month == 1 && weekOfYear > 6)
            {
                return dayOfWeek - dayOfWeekForJan1 + 1;
            }

            switch (rule)
            {
                case CalendarWeekRule.FirstDay:
                    offset = dayOfWeek - dayOfWeekForJan1;
                    break;
                case CalendarWeekRule.FirstFourDayWeek:
                    if (dayOfWeekForJan1 <= 4)
                    {
                        offset = dayOfWeek - dayOfWeekForJan1;
                    }
                    else
                    {
                        offset = dayOfWeek + c_DAYS_PER_WEEK - dayOfWeekForJan1;
                    }

                    break;
                case CalendarWeekRule.FirstFullWeek:
                    if (dayOfWeekForJan1 == 1)
                    {
                        offset = dayOfWeek - dayOfWeekForJan1;
                    }
                    else
                    {
                        offset = dayOfWeek + c_DAYS_PER_WEEK - dayOfWeekForJan1;
                    }

                    break;
            }
            retVal = (weekOfYear - 1) * c_DAYS_PER_WEEK + offset + 1;
            return retVal;
        }
Ejemplo n.º 16
0
 internal static int InternalGetWeekOfYearFullDays(Calendar cal, DateTime time, int firstDayOfWeek, int fullDays, int daysOfMinYearMinusOne)
 {
     int daysInYear = cal.GetDayOfYear(time) - 1;
     int num = ((int) cal.GetDayOfWeek(time)) - (daysInYear % 7);
     int num2 = ((firstDayOfWeek - num) + 14) % 7;
     if ((num2 != 0) && (num2 >= fullDays))
     {
         num2 -= 7;
     }
     int num3 = daysInYear - num2;
     if (num3 < 0)
     {
         int year = cal.GetYear(time);
         if (year <= cal.GetYear(cal.MinSupportedDateTime))
         {
             daysInYear = daysOfMinYearMinusOne;
         }
         else
         {
             daysInYear = cal.GetDaysInYear(year - 1);
         }
         num -= daysInYear % 7;
         num2 = ((firstDayOfWeek - num) + 14) % 7;
         if ((num2 != 0) && (num2 >= fullDays))
         {
             num2 -= 7;
         }
         num3 = daysInYear - num2;
     }
     return ((num3 / 7) + 1);
 }
 protected virtual void GetDateFormatValues(DateParts parts, Calendar calendar, out int twoDigitYear, out string monthName, out string monthNameShort, out string monthNameGenitive, out string monthNameShortGenitive, out string dayName, out string dayNameShort, out string eraName, out string eraNameShort) {
     var yearString = parts.Year.ToString("00", System.Globalization.CultureInfo.InvariantCulture);
     twoDigitYear = Int32.Parse(yearString.Substring(yearString.Length - 2));
     monthName = parts.Month > 0 ? _dateTimeFormatProvider.MonthNames[parts.Month - 1] : null;
     monthNameShort = parts.Month > 0 ? _dateTimeFormatProvider.MonthNamesShort[parts.Month - 1] : null;
     monthNameGenitive = parts.Month > 0 ? _dateTimeFormatProvider.MonthNamesGenitive[parts.Month - 1] : null;
     monthNameShortGenitive = parts.Month > 0 ? _dateTimeFormatProvider.MonthNamesShortGenitive[parts.Month - 1] : null;
     dayName = parts.Day > 0 ? _dateTimeFormatProvider.DayNames[(int)calendar.GetDayOfWeek(parts.ToDateTime(calendar))] : null;
     dayNameShort = parts.Day > 0 ? _dateTimeFormatProvider.DayNamesShort[(int)calendar.GetDayOfWeek(parts.ToDateTime(calendar))] : null;
     eraName = parts.Day > 0 ? _dateTimeFormatProvider.GetEraName((int)calendar.GetEra(parts.ToDateTime(calendar))) : null;
     eraNameShort = parts.Day > 0 ? _dateTimeFormatProvider.GetShortEraName((int)calendar.GetEra(parts.ToDateTime(calendar))) : null;
 }
Ejemplo n.º 18
0
        // This is copied from the generic implementation of GetWeekOfYearFullDays() in Calendar.cs.  The generic implementation is not good enough 
        // in the case of FristFullWeek and FirstFourDayWeek since it needs the data for B.C. year 1 near 0001/1/1.
        // We override the generic implementation to handle this special case.

        // Parameters 
        //
        internal static int InternalGetWeekOfYearFullDays(Calendar cal, DateTime time, int firstDayOfWeek, int fullDays, int daysOfMinYearMinusOne) { 
            int dayForJan1; 
            int offset;
            int day; 

            int dayOfYear = cal.GetDayOfYear(time) - 1; // Make the day of year to be 0-based, so that 1/1 is day 0.
            //
            // Calculate the number of days between the first day of year (1/1) and the first day of the week. 
            // This value will be a positive value from 0 ~ 6.  We call this value as "offset".
            // 
            // If offset is 0, it means that the 1/1 is the start of the first week. 
            //     Assume the first day of the week is Monday, it will look like this:
            //     Sun      Mon     Tue     Wed     Thu     Fri     Sat 
            //     12/31    1/1     1/2     1/3     1/4     1/5     1/6
            //              +--> First week starts here.
            //
            // If offset is 1, it means that the first day of the week is 1 day ahead of 1/1. 
            //     Assume the first day of the week is Monday, it will look like this:
            //     Sun      Mon     Tue     Wed     Thu     Fri     Sat 
            //     1/1      1/2     1/3     1/4     1/5     1/6     1/7 
            //              +--> First week starts here.
            // 
            // If offset is 2, it means that the first day of the week is 2 days ahead of 1/1.
            //     Assume the first day of the week is Monday, it will look like this:
            //     Sat      Sun     Mon     Tue     Wed     Thu     Fri     Sat
            //     1/1      1/2     1/3     1/4     1/5     1/6     1/7     1/8 
            //                      +--> First week starts here.
 
 

            // Day of week is 0-based. 
            // Get the day of week for 1/1.  This can be derived from the day of week of the target day.
            // Note that we can get a negative value.  It's ok since we are going to make it a positive value when calculating the offset.
            dayForJan1 = (int)cal.GetDayOfWeek(time) - (dayOfYear % 7);
 
            // Now, calucalte the offset.  Substract the first day of week from the dayForJan1.  And make it a positive value.
            offset = (firstDayOfWeek - dayForJan1 + 14) % 7; 
            if (offset != 0 && offset >= fullDays) 
            {
                // 
                // If the offset is greater than the value of fullDays, it means that
                // the first week of the year starts on the week where Jan/1 falls on.
                //
                offset -= 7; 
            }
            // 
            // Calculate the day of year for specified time by taking offset into account. 
            //
            day = dayOfYear - offset; 
            if (day >= 0) {
                //
                // If the day of year value is greater than zero, get the week of year.
                // 
                return (day/7 + 1);
            } 
            // 
            // Otherwise, the specified time falls on the week of previous year.
            // Note that it is not always week 52 or 53, because it depends on the calendar.  Different calendars have different number of days in a year. 
            //

            // Repeat the previous calculation logic using the previous year and calculate the week of year for the last day of previous year.
            int year = cal.GetYear(time); 
            if (year <= cal.GetYear(cal.MinSupportedDateTime)) {
                // This specified time is in 0001/1/1 ~ 0001/1/7. 
                dayOfYear = daysOfMinYearMinusOne; 
            } else {
                dayOfYear = cal.GetDaysInYear(year - 1); 
            }
            dayForJan1 = dayForJan1 - (dayOfYear % 7);
            // Now, calucalte the offset.  Substract the first day of week from the dayForJan1.  And make it a positive value.
            offset = (firstDayOfWeek - dayForJan1 + 14) % 7; 
            if (offset != 0 && offset >= fullDays)
            { 
                // 
                // If the offset is greater than the value of fullDays, it means that
                // the first week of the year starts on the week where Jan/1 falls on. 
                //
                offset -= 7;
            }
            // 
            // Calculate the day of year for specified time by taking offset into account.
            // 
            day = dayOfYear - offset; 
            return (day/7 + 1);
        } 
Ejemplo n.º 19
0
        /// <summary>
        /// Gets the first day of a week.
        /// </summary>
        /// <param name="year">The year.</param>
        /// <param name="week">The week.</param>
        /// <param name="calendar">The calendar.</param>
        /// <param name="calendarWeekRule">The calendar week rule.</param>
        /// <param name="firstDayOfWeek">The first day of week.</param>
        /// <returns>The date of the first day in the week.</returns>
        public static DateTime GetFirstDayOfWeek(
                int year,
                int week,
                Calendar calendar = null,
                CalendarWeekRule calendarWeekRule = CalendarWeekRule.FirstFourDayWeek,
                DayOfWeek firstDayOfWeek = DayOfWeek.Sunday)
        {
            if (calendar == null)
                calendar = CultureInfo.CurrentCulture.Calendar;

            DateTime firstDay = new DateTime(year, 1, 7, calendar);

            while (calendar.GetDayOfWeek(firstDay) != firstDayOfWeek || WeekNumber(firstDay, calendar, calendarWeekRule, firstDayOfWeek) % 52 > 1)
            {
                firstDay = calendar.AddDays(firstDay, -1);
            }

            // Add the week
            return (week != 1) ? calendar.AddWeeks(firstDay, week - 1) : firstDay;
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Get's the next valid second after the current .
        /// </summary>
        /// <param name="dateTime">The date time (fractions of a second are removed).</param>
        /// <param name="month">The month.</param>
        /// <param name="week">The week.</param>
        /// <param name="day">The day.</param>
        /// <param name="weekDay">The week day.</param>
        /// <param name="hour">The hour.</param>
        /// <param name="minute">The minute.</param>
        /// <param name="second">The second.</param>
        /// <param name="calendar">The calendar.</param>
        /// <param name="calendarWeekRule">The calendar week rule.</param>
        /// <param name="firstDayOfWeek">The first day of week.</param>
        /// <param name="inclusive">if set to <c>true</c> can return the time specified, otherwise, starts at the next second..</param>
        /// <returns>
        /// The next valid date (or <see cref="DateTime.MaxValue"/> if none).
        /// </returns>
        public static DateTime NextValid(
                this DateTime dateTime,
                Month month = Month.Every,
                Week week = Week.Every,
                Day day = Day.Every,
                WeekDay weekDay = WeekDay.Every,
                Hour hour = Hour.Zeroth,
                Minute minute = Minute.Zeroth,
                Second second = Second.Zeroth,
                Calendar calendar = null,
                CalendarWeekRule calendarWeekRule = CalendarWeekRule.FirstFourDayWeek,
                DayOfWeek firstDayOfWeek = DayOfWeek.Sunday,
                bool inclusive = false)
        {
            // Never case, if any are set to never, we'll never get a valid date.
            if ((month == Month.Never) || (week == Week.Never) ||
                (day == Day.Never) || (weekDay == WeekDay.Never) || (hour == Hour.Never) ||
                (minute == Minute.Never) ||
                (second == Second.Never))
                return DateTime.MaxValue;

            if (calendar == null)
                calendar = CultureInfo.CurrentCulture.Calendar;

            // Set the time to this second (or the next one if not inclusive), remove fractions of a second.
            dateTime = new DateTime(
                    dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second);
            if (!inclusive)
                dateTime = calendar.AddSeconds(dateTime, 1);

            // Every second case.
            if ((month == Month.Every) && (day == Day.Every) && (weekDay == WeekDay.Every) && (hour == Hour.Every) &&
                (minute == Minute.Every) && (second == Second.Every) &&
                (week == Week.Every))
                return calendar.AddSeconds(dateTime, 1);

            // Get days and months.
            IEnumerable<int> days = day.Days().OrderBy(dy => dy);
            IEnumerable<int> months = month.Months();

            // Remove months where the first day isn't in the month.
            int firstDay = days.First();
            if (firstDay > 28)
            {
                // 2000 is a leap year, so February has 29 days.
                months = months.Where(mn => calendar.GetDaysInMonth(2000, mn) >= firstDay);
                if (months.Count() < 1)
                    return DateTime.MaxValue;
            }

            // Get remaining date components.
            int y = calendar.GetYear(dateTime);
            int m = calendar.GetMonth(dateTime);
            int d = calendar.GetDayOfMonth(dateTime);

            int h = calendar.GetHour(dateTime);
            int n = calendar.GetMinute(dateTime);
            int s = calendar.GetSecond(dateTime);

            IEnumerable<int> weeks = week.Weeks();
            IEnumerable<DayOfWeek> weekDays = weekDay.WeekDays();
            IEnumerable<int> hours = hour.Hours().OrderBy(i => i);
            IEnumerable<int> minutes = minute.Minutes().OrderBy(i => i);
            IEnumerable<int> seconds = second.Seconds();
            
            do
            {
                foreach (int currentMonth in months)
                {
                    if (currentMonth < m)
                        continue;
                    if (currentMonth > m)
                    {
                        d = 1;
                        h = n = s = 0;
                    }
                    m = currentMonth;
                    foreach (int currentDay in days)
                    {
                        if (currentDay < d)
                            continue;
                        if (currentDay > d)
                            h = n = s = 0;
                        d = currentDay;

                        // Check day is valid for this month.
                        if ((d > 28) && (d > calendar.GetDaysInMonth(y, m)))
                            break;

                        // We have a potential day, check week and week day
                        dateTime = new DateTime(y, m, d, h, n, s);
                        if ((week != Week.Every) &&
                            (!weeks.Contains(dateTime.WeekNumber(calendar, calendarWeekRule, firstDayOfWeek))))
                            continue;
                        if ((weekDay != WeekDay.Every) &&
                            (!weekDays.Contains(calendar.GetDayOfWeek(dateTime))))
                            continue;

                        // We have a date match, check time.
                        foreach (int currentHour in hours)
                        {
                            if (currentHour < h) continue;
                            if (currentHour > h)
                                n = s = 0;
                            h = currentHour;
                            foreach (int currentMinute in minutes)
                            {
                                if (currentMinute < n) continue;
                                if (currentMinute > n)
                                    s = 0;
                                n = currentMinute;
                                foreach (int currentSecond in seconds)
                                {
                                    if (currentSecond < s) continue;
                                    return new DateTime(y, m, d, h, n, currentSecond, calendar);
                                }
                                n = s = 0;
                            }
                            h = n = s = 0;
                        }
                        d = 1;
                    }
                    d = 1;
                    h = n = s = 0;
                }
                y++;

                // Don't bother checking max year.
                if (y > 9998)
                    return DateTime.MaxValue;

                // Start next year
                m = d = 1;
                h = n = s = 0;
            } while (true);
        }
Ejemplo n.º 21
0
 private String GetAbbreviatedDayName(DateTime dateTime)
 {
     return(DateTimeFormatInfo.CurrentInfo.GetAbbreviatedDayName(
                _threadCalendar.GetDayOfWeek(dateTime)));
 }
Ejemplo n.º 22
0
        // This is copied from the generic implementation of GetWeekOfYearFullDays() in Calendar.cs.  The generic implementation is not good enough
        // in the case of FristFullWeek and FirstFourDayWeek since it needs the data for B.C. year 1 near 0001/1/1.
        // We override the generic implementation to handle this special case.

        // Parameters
        //
        internal static int InternalGetWeekOfYearFullDays(Calendar cal, DateTime time, int firstDayOfWeek, int fullDays, int daysOfMinYearMinusOne)
        {
            int dayForJan1;
            int offset;
            int day;

            int dayOfYear = cal.GetDayOfYear(time) - 1; // Make the day of year to be 0-based, so that 1/1 is day 0.

            //
            // Calculate the number of days between the first day of year (1/1) and the first day of the week.
            // This value will be a positive value from 0 ~ 6.  We call this value as "offset".
            //
            // If offset is 0, it means that the 1/1 is the start of the first week.
            //     Assume the first day of the week is Monday, it will look like this:
            //     Sun      Mon     Tue     Wed     Thu     Fri     Sat
            //     12/31    1/1     1/2     1/3     1/4     1/5     1/6
            //              +--> First week starts here.
            //
            // If offset is 1, it means that the first day of the week is 1 day ahead of 1/1.
            //     Assume the first day of the week is Monday, it will look like this:
            //     Sun      Mon     Tue     Wed     Thu     Fri     Sat
            //     1/1      1/2     1/3     1/4     1/5     1/6     1/7
            //              +--> First week starts here.
            //
            // If offset is 2, it means that the first day of the week is 2 days ahead of 1/1.
            //     Assume the first day of the week is Monday, it will look like this:
            //     Sat      Sun     Mon     Tue     Wed     Thu     Fri     Sat
            //     1/1      1/2     1/3     1/4     1/5     1/6     1/7     1/8
            //                      +--> First week starts here.



            // Day of week is 0-based.
            // Get the day of week for 1/1.  This can be derived from the day of week of the target day.
            // Note that we can get a negative value.  It's ok since we are going to make it a positive value when calculating the offset.
            dayForJan1 = (int)cal.GetDayOfWeek(time) - (dayOfYear % 7);

            // Now, calucalte the offset.  Substract the first day of week from the dayForJan1.  And make it a positive value.
            offset = (firstDayOfWeek - dayForJan1 + 14) % 7;
            if (offset != 0 && offset >= fullDays)
            {
                //
                // If the offset is greater than the value of fullDays, it means that
                // the first week of the year starts on the week where Jan/1 falls on.
                //
                offset -= 7;
            }
            //
            // Calculate the day of year for specified time by taking offset into account.
            //
            day = dayOfYear - offset;
            if (day >= 0)
            {
                //
                // If the day of year value is greater than zero, get the week of year.
                //
                return(day / 7 + 1);
            }
            //
            // Otherwise, the specified time falls on the week of previous year.
            // Note that it is not always week 52 or 53, because it depends on the calendar.  Different calendars have different number of days in a year.
            //

            // Repeat the previous calculation logic using the previous year and calculate the week of year for the last day of previous year.
            int year = cal.GetYear(time);

            if (year <= cal.GetYear(cal.MinSupportedDateTime))
            {
                // This specified time is in 0001/1/1 ~ 0001/1/7.
                dayOfYear = daysOfMinYearMinusOne;
            }
            else
            {
                dayOfYear = cal.GetDaysInYear(year - 1);
            }
            dayForJan1 = dayForJan1 - (dayOfYear % 7);
            // Now, calucalte the offset.  Substract the first day of week from the dayForJan1.  And make it a positive value.
            offset = (firstDayOfWeek - dayForJan1 + 14) % 7;
            if (offset != 0 && offset >= fullDays)
            {
                //
                // If the offset is greater than the value of fullDays, it means that
                // the first week of the year starts on the week where Jan/1 falls on.
                //
                offset -= 7;
            }
            //
            // Calculate the day of year for specified time by taking offset into account.
            //
            day = dayOfYear - offset;
            return(day / 7 + 1);
        }
 private void Verification(Calendar calendar, DateTime time, DayOfWeek expected, string errorno)
 {
     DayOfWeek RealVal = calendar.GetDayOfWeek(time);
     Assert.Equal(expected, RealVal);
 }