Exemplo n.º 1
0
        /// <summary>
        /// Converts a specified <c>ISO-8601</c> year, week of the year and day of the week to its <see cref="DateTime"/>
        /// equivalent value.
        /// </summary>
        /// <param name="year">The year.</param>
        /// <param name="week">The week of the year.</param>
        /// <param name="dayOfWeek">The day of the week.</param>
        /// <returns>
        /// A <see cref="DateTime"/> value that is equivalent to the specified <c>ISO-8601</c> year,
        /// week of the year and day of the week.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="year"/> is less than 1 or greater than 9999. Or,
        /// <paramref name="week"/> is less than 1 or greater than 53.
        /// </exception>
        public static DateTime DateForIso8601(int year, int week, DayOfWeek dayOfWeek)
        {
            Contract.Requires <ArgumentOutOfRangeException>(year >= 1 && year <= 9999, nameof(year));
            Contract.Requires <ArgumentOutOfRangeException>(week >= 1 && week <= 53, nameof(week));

            var firstDayOfYear = new DateTime(year, 1, 1);
            var firstDayOfYearFromStartOfWeek = firstDayOfYear.DaysFromStartOfTheWeek(DayOfWeek.Monday);
            var daysInWholeWeeks = (week - 1) * DaysPerWeek;

            var daysFromStartOfYear = dayOfWeek.DaysFromStartOfTheWeek() - firstDayOfYearFromStartOfWeek + daysInWholeWeeks;

            if (firstDayOfYearFromStartOfWeek > 3)
            {
                daysFromStartOfYear += DaysPerWeek;
            }

            return(firstDayOfYear.AddDays(daysFromStartOfYear));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns the date for a specific occurrence of a given day of the week in a specified month of a the year.
        /// </summary>
        /// <param name="weekOfMonth">The week instance of the month.</param>
        /// <param name="dayOfWeek">The day of the week.</param>
        /// <param name="month">The month of the year.</param>
        /// <param name="year">The year.</param>
        /// <returns>A <see cref="DateTime"/> value.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="year"/> is less than 1 or greater than 9999. Or,
        /// <paramref name="month"/> is less than 1 or greater than 12.
        /// </exception>
        public static DateTime DateFor(WeekOfMonth weekOfMonth, DayOfWeek dayOfWeek, int month, int year)
        {
            Contract.Requires <ArgumentOutOfRangeException>(year >= 1 && year <= 9999, nameof(year));
            Contract.Requires <ArgumentOutOfRangeException>(month >= 1 && month <= 12, nameof(month));

            var firstOfMonth     = new DateTime(year, month, 1);
            var daysInWholeWeeks = ((int)weekOfMonth - 1) * DaysPerWeek;

            var daysFromStartOfMonth = dayOfWeek.DaysFromStartOfTheWeek() - firstOfMonth.DayOfWeek.DaysFromStartOfTheWeek();

            if (daysFromStartOfMonth < 0)
            {
                daysFromStartOfMonth += DaysPerWeek;
            }
            daysFromStartOfMonth += daysInWholeWeeks;
            if (weekOfMonth == WeekOfMonth.Last && daysFromStartOfMonth >= DateTime.DaysInMonth(year, month))
            {
                daysFromStartOfMonth -= DaysPerWeek;
            }

            return(firstOfMonth.AddDays(daysFromStartOfMonth));
        }
 /// <summary>
 /// Converts a <see cref="DayOfWeek"/> value to a <see cref="DaysOfTheWeek"/> value.
 /// </summary>
 /// <param name="source">The day of the week.</param>
 /// <returns>A <see cref="DaysOfTheWeek"/> value.</returns>
 public static DaysOfTheWeek ToDaysOfWeek(this DayOfWeek source)
 {
     return((DaysOfTheWeek)(1 << source.DaysFromStartOfTheWeek()));
 }
 public int DaysFromStartOfTheWeek(DayOfWeek firstDayOfWeek, DayOfWeek dayOfWeek)
 {
     return(dayOfWeek.DaysFromStartOfTheWeek(firstDayOfWeek));
 }