Home > Back-end >  Get the no of weeks from date duration with overlapping scenario
Get the no of weeks from date duration with overlapping scenario

Time:01-15

I need to calculate the count of total assigned week and total benched week for an employee.

An employee is assigned in different projects in some time duration which will always start from Monday and always end on Friday. Monday to Friday will be considered as 1 week.There cant be more than 2 week duration for any project. Any overlapping weeks of assigned status should be adjusted. If employee staus is assigned and that time duration overlaps or falls with any of the benched week duration then that week duration for benched status should not be counted at all .

Below are the relevant scenario as below.

"AssignedHistory":[ 
   {"Project":1,"status":"Assigned","Startdate":"01/03/2022", "Enddate":"01/07/2022" }, 
   {"Project":2,"status":"Assigned","Startdate":"01/10/2022", "Enddate":"01/14/2022" }, 
   {"Project":3,"status":"Assigned", "Startdate":"01/10/2022", "Enddate":"01/21/2022" },
   {"Project":4,"status":"Assigned", "Startdate":"02/21/2022", "Enddate":"02/25/2022" },       
   {"Project":5,"status":"Bench","Startdate":"01/17/2022", "Enddate":"01/21/2022" },
   {"Project":6,"status":"Bench","Startdate":"02/07/2022", "Enddate":"02/11/2022" }
   {"Project":7,"status":"Bench","Startdate":"02/21/2022", "Enddate":"03/04/2022" }    
]

Here I need to find the count of total assigned week and total benched week for an employee, and expected result should be :

Total assigned week:4
Total benched week :1



     Since 1 week of assigned status is overlapping for 2 projects(no2 and no 3) from 10th Jan to 14 Jan so project 2 and 3 together will be counted as 2 .Also for assigned week of  project 3  it is overlapping with  benched week of  project 5  from 17th Jan to 21st Jan so in that case bench week for project 5 will be counted as 0.Similarly for assigned week of project 4,duration from 21st feb to 25th feb is overlapping or falls under benched week of  project 7 so in this case project 7 will be counted as 0.So only we have project 6 for benched week which doest not overlaps with any of the assined project duration.

Thats why total benched count will be 1.

This is how I am thinking.

 var assignedweek = AssignedHistory.Where(x => new []{"Assigned"}.Contains(x.status)).ToList();
    var benchedweek = AssignedHistory.Where(x => new []{"Bench"}.Contains(x.status)).ToList();
     
    List<DateTime> AssignedWeeklist = assignedweek
        .SelectMany(a => {
            DateTime firstSunday = a.Startdate.AddDays(-(int)a.Startdate.DayOfWeek);
            DateTime lastSunday = a.Enddate.AddDays(-(int)a.Enddate.DayOfWeek); 
            int weeks = (lastSunday - firstSunday).Days / 7   1;
    
            // Enumerate one Sunday per week
            return Enumerable.Range(0, weeks).Select(i => firstSunday.AddDays(7 * i));
        })
        .Distinct()
        .ToList();
        
        List<DateTime> BenchWeeklist = benchedweek
        .SelectMany(a => {
            DateTime firstSunday = a.Startdate.AddDays(-(int)a.Startdate.DayOfWeek);
            DateTime lastSunday = a.Enddate.AddDays(-(int)a.Enddate.DayOfWeek); 
            int weeks = (lastSunday - firstSunday).Days / 7   1;
    
            // Enumerate one Sunday per week
            return Enumerable.Range(0, weeks).Select(i => firstSunday.AddDays(7 * i));
        })
        .Distinct()
        .ToList();
        
        int assignedweekcount = AssignedWeeklist.Count();
        int benchedweekcount = BenchWeeklist.Except(AssignedWeeklist).Count();

It is giving correct assign week count But it is giving incorrect bench week count if there is overlapping bench week with assigned week. Any way to do this?

CodePudding user response:

you have to change the logic of your calcul on benchweek, you have to keep the sunday date by project:

        List<List<DateTime>> BenchWeeklist = benchedweek
        .Select(a => {
            DateTime firstSunday = a.StartDate.AddDays(-(int)a.StartDate.DayOfWeek);
            DateTime lastSunday = a.EndDate.AddDays(-(int)a.EndDate.DayOfWeek);

        return new List<DateTime>() { firstSunday, lastSunday }.Distinct().ToList() ;
        })
        .Distinct()
        .ToList();

        int assignedweekcount = AssignedWeeklist.Count();
        var benchedweekcount = 0;

        foreach(var l in BenchWeeklist)
        {
            var found = false;
            foreach(var it in l)
            {
                if (AssignedWeeklist.Contains(it)) found = true;
            }
            if (!found) benchedweekcount  ;
        }
  •  Tags:  
  • Related