Exemplo n.º 1
0
        /// <summary>
        /// Détermine si une année a 53 semaines officielles au lieu de 52.
        /// </summary>
        /// <param name="year">L'année.</param>
        /// <returns>Vrai si 53 semaines ; Faux sinon.</returns>
        public static bool YearIs53Week(int year)
        {
            DayOfWeek[] endOfWeekDays = new DayOfWeek[] { DayOfWeek.Friday, DayOfWeek.Saturday, DayOfWeek.Sunday };

            DateTime firstDayOfNextYear = new DateTime(year + 1, 1, 1);

            return(endOfWeekDays.Contains(firstDayOfNextYear.DayOfWeek));
        }
Exemplo n.º 2
0
        //1
        private static void NombreDejours(int month, int year)
        {
            var weekends = new DayOfWeek[] { DayOfWeek.Saturday, DayOfWeek.Sunday };


            int daysInMonth = DateTime.DaysInMonth(year, month);

            IEnumerable <int> businessDaysInMonth = Enumerable.Range(1, daysInMonth)
                                                    .Where(d => !weekends.Contains(new DateTime(year, month, d).DayOfWeek));

            Console.WriteLine(businessDaysInMonth.ToArray().Length + " jours ouvrés");
        }
Exemplo n.º 3
0
        public static List<int> BusinessDays(DayOfWeek[] WeekEndDays, int Year, int Month)
        {
            List<int> days = new List<int>();

            //Fetch the amount of days in your given month
            int daysInMonth = DateTime.DaysInMonth(Year, Month);

            //Here we create and enumerable from 1 to daysInMonth
            //and ask whether the Datetime object we create belongs to a weekend day,
            //if it doen't add it to our IEumerable<int> collection of days.
            IEnumerable<int> businessDaysInMonth = Enumerable.Range(1, daysInMonth).Where(d => !WeekEndDays.Contains(new DateTime(Year, Month, d).DayOfWeek));

            days.AddRange(businessDaysInMonth);
            return days;
        }
Exemplo n.º 4
0
    public static DateTime[] DatesInInterval(this DateTime start, DateTime end, params DayOfWeek[] DayOfWeek)
    {
        var datesInterval = new List <DateTime>();
        var date          = new DateTime(start.Year, start.Month, start.Day);

        while (date <= end)
        {
            if (DayOfWeek.Contains(date.DayOfWeek))
            {
                datesInterval.Add(date);
            }
            date = date.AddDays(1);
        }

        return(datesInterval.ToArray());
    }
Exemplo n.º 5
0
        public static int GetBussinessDays(int month, int year)
        {
            var weekends = new DayOfWeek[] { DayOfWeek.Saturday, DayOfWeek.Sunday };



            //Fetch the amount of days in your given month.
            int daysInMonth = DateTime.DaysInMonth(year, month);

            //Here we create an enumerable from 1 to daysInMonth,
            //and ask whether the DateTime object we create belongs to a weekend day,
            //if it doesn't, add it to our IEnumerable<int> collection of days.
            IEnumerable <int> businessDaysInMonth = Enumerable.Range(1, daysInMonth)
                                                    .Where(d => !weekends.Contains(new DateTime(year, month, d).DayOfWeek));

            return(businessDaysInMonth.Count());
        }
Exemplo n.º 6
0
        public void Days_of_week_preference_determines_correct_result()
        {
            IEnumerable <DayOfWeek> daysOfWeek = new DayOfWeek[] { DayOfWeek.Friday, DayOfWeek.Monday };
            IPreference             preference = new DaysOfWeekPreference(daysOfWeek);

            for (int d = 4; d <= 10; d++)
            {
                DateTime date = new DateTime(2020, 10, d);
                if (daysOfWeek.Contains(date.DayOfWeek))
                {
                    preference.SendOnDate(date).ShouldBeTrue($"Testing {date.DayOfWeek}");
                }
                else
                {
                    preference.SendOnDate(date).ShouldBeFalse($"Testing {date.DayOfWeek}");
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Remove business days on a DateTime
        /// </summary>
        /// <param name="date">Date to remove days</param>
        /// <param name="days">Days to remove</param>
        /// <param name="businessDays">Business days</param>
        /// <returns>date</returns>
        public static DateTime RemoveBusinessDays(this DateTime date, int days, DayOfWeek[] businessDays)
        {
            days = Math.Abs(days);
            if (businessDays == null || businessDays.Count() == 0)
                businessDays = BaseDateTimeExtensions.GetDefaultBusinessDays();

            var finalCount = 0;
            while (true)
            {
                if (businessDays.Contains(date.DayOfWeek))
                    finalCount++;

                date = date.AddDays(-1);

                if (days == finalCount)
                    break;
            }

            return date;
        }
Exemplo n.º 8
0
        public List <DateTime> DatesForAttendance(DateTime firstTime, long groupId, int attendanceDays)
        {
            List <CalendarEvent> calendarEvents = _db.CalendarEvents.Where(c => c.GroupId == groupId).ToList();

            DayOfWeek[] dayOfWeeks = new DayOfWeek[calendarEvents.Count];
            for (int i = 0; i < calendarEvents.Count; i++)
            {
                dayOfWeeks[i] = calendarEvents[i].DayOfWeek;
            }

            DateTime days;

            if (dayOfWeeks.Length == 3)
            {
                days = firstTime.AddDays(40);
            }
            else if (dayOfWeeks.Length == 2)
            {
                days = firstTime.AddDays(50);
            }
            else
            {
                days = firstTime.AddDays(90);
            }

            List <DateTime> dates = Enumerable.Range(0, days.Subtract(firstTime).Days)
                                    .Select(offset => firstTime.AddDays(offset))
                                    .Where(d => dayOfWeeks.Contains(d.DayOfWeek))
                                    .ToList();

            DateTime [] dateTimes = new DateTime[attendanceDays];
            for (int i = 0; i < attendanceDays; i++)
            {
                dateTimes[i] = dates[i];
            }

            return(dateTimes.ToList());
        }
Exemplo n.º 9
0
		public void SetSchedule(DayOfWeek[] days, HourOfDay fromHour, MinuteOfHour fromMinute, HourOfDay toHour, MinuteOfHour toMinute)
		{
			if (!days.Contains (DayOfWeek.Sunday))
			{
				int num = 0;
				while (num < (int)days.Length)
				{
					if (days[num] < DayOfWeek.Sunday || days[num] > DayOfWeek.Saturday)
					{
						throw new InvalidEnumArgumentException("days", (int)days[num], typeof(DayOfWeek));
					}
					else
					{
						num++;
					}
				}
				for (int i = 0; i < (int)days.Length; i++)
				{
					this.SetSchedule(days[i], fromHour, fromMinute, toHour, toMinute);
				}
				return;
			}
			else
			{
				throw new ArgumentNullException("days");
			}
		}
Exemplo n.º 10
0
        /// <summary>
        /// Get end of business month
        /// </summary>
        /// <param name="date">base date</param>
        /// <param name="businessDays">business days list</param>
        /// <returns>End of business month from date</returns>
        public static DateTime EndOfBusinessMonth(this DateTime date, DayOfWeek[] businessDays)
        {
            date = date.EndOfMonth();

            if (businessDays == null || businessDays.Count() == 0)
                businessDays = BaseDateTimeExtensions.GetDefaultBusinessDays();

            while (true)
            {
                if (businessDays.Contains(date.DayOfWeek))
                    return date;

                date = date.AddDays(-1);
            }
        }