Answered by:
Dividing a Timespan
Question

Hi,
I need to make a program that will calculate the rentalprice of a car.
The rental price is depending on the time it will be rented.
The cost for renting the car between 22:00 and 07:00 will be price*1.2 > Nightrate
The cost for renting the care between 07:00 and 22:00 will be price *1 > DayrateNow:
I've got 2 DateTime variables (startRent) and (endRent)
I can make a Timespan of endRent  startRent to get the total rentalTime.
I just need to know how I would "check" that rentalTime and divide it into 2 pieces1) piece to calculate the cost using my nightrate
2) piece to calculate the cost using my dayrate
How would you go about checking what part of the Timespan is Nightrate and what part is Dayrate?
I hope someone can help me with this problem.
Thanks in advance!
Answers

 Marked as answer by galop Saturday, October 27, 2018 10:45 PM
All replies


Hi Andrey Belyakov,
Well to be fair, I haven't gotten much more than I've described.
And I've tried anything
I'm trying to work with normal integers now to compare the Time.
If you want I can paste that part in 20 min. I think, But I need to write it first.
Thanks for your fast reply though 
> I need to write it first.
Yes, you should DO this first. And you also should try to get it working. Using all available for you options.
And here you need to ask a questions when you have something what didn't work as you expected. With explanation of your expectation and a code which allow to recreate a problem and all other details.
Sincerely, Highly skilled coding monkey.

Just wanted to reply to my own thread with the partial solution I've made untill this point. It might not be the best way to solve my problem. But it's a start.
Right now I have written a method to convert 2 DateTimes into a List of Timespans:public List <TimeSpan> tijdNaarLijst(DateTime start, DateTime end)
With these Timespans, I'm hoping to be able to check which Hours "match" with the list of Timespans I just created.
{
List<TimeSpan> result = new List<TimeSpan>();
if (start.Hour < end.Hour)
{
List<TimeSpan> lijst;
for (int i = start.Hour; i<=end.Hour;i++)
{
lijst = new List<TimeSpan>();
var lijstItem = new TimeSpan(0, i, 0, 0);
result.Add(lijstItem);
}
return result;
}
if (start.Hour > end.Hour)
{
List<TimeSpan> lijst;
for (int i = start.Hour; i<24; i++)
{
lijst = new List<TimeSpan>();
var lijstItem = new TimeSpan(0, i, 0, 0);
result.Add(lijstItem);
}
for (int x = 0; x < end.Hour; x++)
{
lijst = new List<TimeSpan>();
var lijstItem2 = new TimeSpan(0, x, 0, 0);
result.Add(lijstItem2);
}
return result;
}
return result;
}
But that is part 2 I'm starting on right now.
So basically this is my own solution to the problem, if there are ways to simplify this problem I am all ears! 
>With these Timespans, I'm hoping to be able to check which Hours "match" with the list of Timespans I just created.
Hmm....Ok
You have the following list:
2.0
6.0.
4.5
1.5
I would say  it's a difference between start and end in hours when car was on rental place.
Can you, please, tell me how to determine a rate?
If here is a trouble  need to think what to have as result... and as source data.

By the way  it would be helpful if you tell where are you in your study. My filling is that you will require knowledge about class or structure.
Sincerely, Highly skilled coding monkey.

Hi,
The List of Timespans I get in return are all the timespans between 2 periods of time in (derrived from a DateTime class)
So for example,
INPUT:DateTime(2018, 10, 26, 22, 0, 0)
DateTime(2018, 10, 26, 7, 0, 0)
Will give this result when given to my Method:
[0] = {22:00:00}
[1] = {23:00:00}
[2] = {00:00:00}
[3] = {01:00:00}
[4] = {02:00:00}
[5] = {03:00:00}
[6] = {04:00:00}
[7] = {05:00:00}
[8] = {06:00:00}
This result is the period of Time that "the business" dictates to be "High Rate". So if you want to have a car reservation from lets say: "5 o' clock" 'till "8 o' clock"
The price has to be 2 hours in "High rate" (price *1.2) & 1 hour in "Low rate" (price * 1)
Basically the second part (I still need to write) should be the check for this. I think I'm able to make this on my own. I just never know if there is a shorter route to fix this problem.
_______
Where I am in my study is hard to say. At work I use PHP and Laravel Framework. But I'm following a course in school that teaches .NET (entity framework) + C#. I'm not saying that I master all the terms. But I do know a big portion of the theory. Just need to get that theory into muscle memory :)

You chose a very difficult approach to the task.
By my opinion you need a class with StartDate and EndDate in constructor and inside option to calculate amount of hours for High and Low rate. This for definitely would not require to build a list of, basically, "namesofthehours" ("{23:00:00}") to late recalculate them into amount of High and Low hours rate. Simple ifelse with addition/sustruction between time/date will do the job.
Sincerely, Highly skilled coding monkey.

I had the feeling I chose a very difficult approach. But what you are suggesting will not result in the answer I need. Also, I think your approach would be twice as long...
So are you sure you are understanding the problem?
The reason I also chose this approach is, what if the business decides to change the Timespan of the "Highrate" period. This would allow me to not change my code. Your solution of a simple ifelse structure will.
Also, keep in mind that the reason I took the namesofthehours (as you put it) is because I need the Timespan/DateTimes to further expand my business.logic. Not using those Classes will create a problem for me in the future, if I need to add new conditions.
If you really want to help, you should try and understand the problem to the core. Because I'm getting the feeling you're underestimating the problem. But if I'm wrong, I appologize for maybe coming accross a bit strong in my opinion. 
The rental price is depending on the time it will be rented.
The cost for renting the car between 22:00 and 07:00 will be price*1.2 > Nightrate
The cost for renting the care between 07:00 and 22:00 will be price *1 > DayrateOK. You have, precisely, determined the interval times relative to the different rates.
So far, your problem seems to be ridiculously simple.
•

Now:
I've got 2 DateTime variables (startRent) and (endRent)
I can make a Timespan of endRent  startRent to get the total rentalTime.
Yes, calculating TimeSpan is, really, necessary, especially, because it takes into account that a month may have either 31, or 30, or 29 or 28 days... without any effort from our side.
Your problem, still seems to be ridiculously simple.
•


Now:
I've got 2 DateTime variables (startRent) and (endRent)
I can make a Timespan of endRent  startRent to get the total rentalTime.
Yes, calculating TimeSpan is, really, necessary, especially, because it takes into account that a month may have either 31, or 30, or 29 or 28 days... without any effort from our side.
Your problem, still seems to be ridiculously simple.
•

How would you go about checking what part of the Timespan is Nightrate and what part is Dayrate?
Ridiculously simple.
You have already determined the charges for any period of time that tallies 24 hours: 9 hours at LOW rate and 15 hours at HIGH rate.
Therefore, you must calculate how many integral periods of 24 hours do exist between the beginning date and time, until the end date and time of the car rental.
With these data at your hand, all you have to do is arithmetic multiplication:
1 (number of integral periods of 24 hours) * 9 hours * LOW rate
2 (number of integral periods of 24 hours) * 15 hours * HIGH rate
3 Sum 1 and 2 above.
What is left? We only need, now, calculate the rates/charges relative to the period of time that exceeds the integralperiodsof24 hours.
With numbers, it is simpler to explain; suppose:
BEGIN: 2018  October  10 05:00:00
END: 2018  October  15 15:00:00
Clearly, the hypothetical car rental above results in:
Integralperiodsof24hours = 5
Hoursexceedingtheintegralperiodsof24hours = 5
To calculate Hoursexceedingtheintegralperiodsof24hours above, it's necessary to intersect the exceeding period ( 5 <> 15 ) with the rates periods ( 0 <> 7 , 7 <> 22 and 22 <> 24 ).
So, summarizing, this is what you need to know:
1 Calculating TimeSpan.
2 Intersecting time intervals.
3 Multiplication; hours times rates per hour (either LOW or HIGH).Here's a code example (in next post).
•

Well, knowing if a month has 31 or 30 or 29 or 28 days doesn't really matter for the small problem at hand.
Clearly, you're wrong. It does matter how many days a month has:
BEGIN: 20??  February  25 13:00:00
BEGIN: 20??  March  05 13:00:00How many days in that car rental interval?...
Of course, you don't know; you even don't know how many days are in that specific month February.
It is needed for scalability (for lets say they want to have a discount month).
My solution is already scalable, because, first, I calculate the total LOW rate hours and the total HIGH rate hours. Only after this, I calculate the price/cost of the rental, thru multiplication (item 3 in a previous post :).
•


I'm quoting myself:
So, summarizing, this is what you need to know:
1 Calculating TimeSpan.
2 Intersecting time intervals.
3 Multiplication; hours times rates per hour (either LOW or HIGH).As you see, my algorithm is short, simple and scalable!
What would you use to compare or verify it with the timespan of 22,0,0  7,0,0 (22:00  07:00)? 
It's the "Intersecting" part I'm curious about how you would approach it.
What would you use to compare or verify it with the timespan of 22,0,0  7,0,0 (22:00  07:00)?ROFL... LOL... ROFL... LOL... ROFL... LOL... ROFL... LOL... ROFL... LOL...
Did I say it's ridiculously simple!
ROFL... LOL... ROFL... LOL... ROFL... LOL... ROFL... LOL... ROFL... LOL...
No?
ROFL... LOL... ROFL... LOL... ROFL... LOL... ROFL... LOL... ROFL... LOL...
So, let me say this: it's ridiculously simple!
•

This is the Main method:
public static void Main(string[] args) { DateTime begin=... // any date ,end=... // must be *greater* than "begin" ; TimeSpan ts=endbegin; int totalIntegral24Hours=ts.Days ,intervalMin=begin.Hour ,intervalMax=intervalMin+ts.Hours ,lowRateHours=... ,highRateHours=... ; }
So far, it's ridiculously simple, rite?
Asynchronously ( you can do other things ), await for the remaining code...
•

 Marked as answer by galop Saturday, October 27, 2018 10:45 PM

