Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Suppose we have a sequence of time periods...

6:00AM to 7:30AM
7:30AM to 8:00AM
8:00AM to 8:30AM
8:30AM to 10:00AM

and I want to insert a period, say

7:45AM to 8:15AM

such that...
1. The start time of the sequence remains the same (6:00AM)
2. The finish time of the sequence remains the same (10:00AM)
3. The total duration remains the same (4 hours)
4. the start and finish time of the inserted period remains the same.

Using the example above I would expect...

6:00AM to 7:30AM
7:30AM to 7:45AM
7:45AM to 8:15AM
8:15AM to 8:30AM
8:30AM to 10:00AM

Thx
Posted
Comments
OriginalGriff 28-Sep-15 4:33am    
And?
What have you tried?
Where are you stuck?
What help do you need?
Leo Chapiro 28-Sep-15 4:40am    
Please show your source code.I'm a bit confused: the tags are C# / VB.NET?
glen205 28-Sep-15 4:58am    
You probably won't get a code-complete solution here, CodeProject members like/want to help out, but not to provide complete solutions.

Decompose the problem into functional pieces:
- a class to represent a time period (start, finish etc)
- a collection to hold an ordered list of time periods
- a method which detects "overlap" of any two time periods
- a method which shortens a time period (move end time forward)
- a method which shortens a time period (move start time back)
- a mechanism for inserting a time period into a list
- an overall method which solves your problem, which in turn relies on all the other working parts of the application.

Give each piece a try in isolation (use a unit test framework to help) and ask specific questions (with code where possible) if you get stuck.

Sounds like a perfect Test-Driven piece of development!

Best of luck!
BillWoodruff 28-Sep-15 6:47am    
Unless you show some code, your question may be seen as asking us to do your homework.

1 solution

Have a look at Time Period Library for .NET[^], one of the best articles on CP.
Studying its code will for sure give you clues about your own requirement.
 
Share this answer
 
Comments
Leo Chapiro 28-Sep-15 5:56am    
Really a very good article, +5 !
Maciej Los 28-Sep-15 8:15am    
5ed!
BillWoodruff 28-Sep-15 17:36pm    
My vote of #1. The article/code by Jani Giannoudis you refer to is a great article that provides a robust library for all kinds of time-based calculations. It is a very complex library.

What benefit is that link to a no-code-shown OP who may be trying to get us to do his homework ? The idea here is to teach something, not just link.

If you had shown some excerpt from Jani's article that was specifically relevant to the OP's agenda, I would vote another way.
phil.o 29-Sep-15 2:28am    
Well, I had the same requirement once, and this article helped me to implement a solution. It took me some time, though. It was not an easy process, and surely a QA forum is not the place for a full explanation on that matter.
I don't agree with your vote, but, OTOH, I don't care.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900