'Divide TimeSpan in c#
I have a TimeSlot class:
public class TimeSlot
{
private TimeSpan start;
private TimeSpan end;
}
I need to implement this function:
Public List<TimeSlot> GetDividedTimeSlot(TimeSlot timeslot, int durationInMinutes)
The function gets a time slot and time-duration in minutes, and divides the time slot into the maximum amount of time slots that are in the range of its start and end time and each one of them has the same time-duration as given. The time slots should also be in differences of quarter of an hour from each other.
For example:
Assume the timespan is 17:00 – 19:00, and the duration is 90 minutes.
The expected list is: 17:00 – 18:30 , 17:15 – 18:45, 17:30 – 19:00
What I tried:
List<TimeSlot> dividedTimeSlots=new List<TimeSlot>();
TimeSpan start = timeSlot.start;
TimeSpan end = timeSlot.end;
TimeSpan currentEnd = new TimeSpan();
TimeSlot newTimeSlot;
while (end.Subtract(start).TotalMinutes >= durationInMinutes)
{
//currentEnd should be equals to the start time + timeDuration
currentEnd = ((DateTime.Now + start).AddMinutes(durationInMinutes)).TimeOfDay;
newTimeSlot = new TimeSlot();
newTimeSlot.start = start;
newTimeSlot.end = currentEnd;
dividedTimeSlots.Add(newTimeSlot);
start = (DateTime.Now.AddMinutes(15) + start).TimeOfDay;
}
return dividedTimeSlots;
But the given result is strange, it is not connected to the original time.
What is wrong is the code?
Thanks a lot.
Solution 1:[1]
Try this
with _data as
(
select 1 as _Group,'A' as Item union all
select 1 as _Group,'B' as Item union all
select 1 as _Group,'C' as Item union all
select 2 as _Group,'X' as Item union all
select 2 as _Group,'Y' as Item union all
select 2 as _Group,'Z' as Item
)
select distinct _Group ,Item from
(
select _Group,
Item
from _data
union all
select _Group,
string_agg(Item ,',') over(partition by _Group order by Item ) as item
from _data
union all
select a._Group ,
concat(a.item,',',b.item)
from _data a left join _data b on a._group = b._group and a.Item < b.Item
)
where item is not null
order by _group
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mr.Batra |
