Ejemplo n.º 1
0
        private int GetOverlappingMinutes(IEnumerable <AvailabilityPeriod> user1AvailabilityPeriodsOnDay, IEnumerable <AvailabilityPeriod> user2AvailabilityPeriodsOnDay)
        {
            var total = 0;

            foreach (var user1Period in user1AvailabilityPeriodsOnDay)
            {
                foreach (var user2Period in user2AvailabilityPeriodsOnDay)
                {
                    if (!user1Period.Overlaps(user2Period))
                    {
                        continue;
                    }
                    var commonPeriod = new AvailabilityPeriod()
                    {
                        StartTime = new TimeSpan(Math.Max(user1Period.StartTime.Ticks, user2Period.StartTime.Ticks)),
                        EndTime   = new TimeSpan(Math.Min(user1Period.EndTimeNormalized.Ticks, user2Period.EndTimeNormalized.Ticks)),
                    };
                    total += commonPeriod.GetMinutes();
                }
            }
            return(total);
        }
Ejemplo n.º 2
0
        private static AvailabilityPeriod[] GetRandomPeriods()
        {
            var periods         = new List <AvailabilityPeriod>();
            var numberOfPeriods = random.Next(3, 10);

            for (int i = 0; i < numberOfPeriods; i++)
            {
                var day       = (DayOfWeek)random.Next(0, 6);
                var startTime = TimeSpan.FromHours(random.Next(0, 19));
                var endTime   = TimeSpan.FromHours(startTime.TotalHours + 4);
                var period    = new AvailabilityPeriod()
                {
                    Day       = day,
                    StartTime = startTime,
                    EndTime   = endTime
                };
                if (periods.Any(p => p.Day == day && p.Overlaps(period)))
                {
                    continue;
                }
                periods.Add(period);
            }
            return(periods.ToArray());
        }
 public bool Overlaps(AvailabilityPeriod period)
 {
     return(!(StartTime >= period.EndTimeNormalized || EndTimeNormalized <= period.StartTime));
 }