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 --> Night-rate
The cost for renting the care between 07:00 and 22:00 will be price *1 --> Day-rate

Now:

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 pieces

1) piece to calculate the cost using my night-rate

2) piece to calculate the cost using my day-rate

How would you go about checking what part of the Timespan is Night-rate and what part is Day-rate?

I hope someone can help me with this problem.

Wednesday, October 24, 2018 11:45 AM

All replies

• Hello,

help me with this problem.

What you was able to get done on your own? And where is a problem?

Sincerely, Highly skilled coding monkey.

Wednesday, October 24, 2018 12:00 PM
• 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.

Wednesday, October 24, 2018 12:18 PM
• > 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 re-create a problem and all other details.

Sincerely, Highly skilled coding monkey.

Wednesday, October 24, 2018 1:26 PM
• 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)
{
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);
}
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);
}
for (int x = 0; x < end.Hour; x++)
{
lijst = new List<TimeSpan>();
var lijstItem2 = new TimeSpan(0, x, 0, 0);
}
return result;
}
return result;
}

With these Timespans, I'm hoping to be able to check which Hours "match" with the list of Timespans I just created.
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!
Friday, October 26, 2018 1:49 PM
• >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.

Friday, October 26, 2018 2:15 PM
• 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 :-)

Friday, October 26, 2018 2:54 PM
• 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, "names-of-the-hours" ("{23:00:00}") to late recalculate them into amount of High and Low hours rate. Simple if-else with addition/sustruction between time/date will do the job.

Sincerely, Highly skilled coding monkey.

Friday, October 26, 2018 3:19 PM
• 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 "High-rate" period. This would allow me to not change my code. Your solution of a simple if-else structure will.

Also, keep in mind that the reason I took the names-of-the-hours (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.
Friday, October 26, 2018 4:19 PM
•

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 --> Night-rate
The cost for renting the care between 07:00 and 22:00 will be price *1 --> Day-rate

OK. You have, precisely, determined the interval times relative to the different rates.

So far, your problem seems to be ridiculously simple.

Friday, October 26, 2018 10:01 PM

• 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.

Friday, October 26, 2018 10:07 PM

• I hope someone can help me with this problem.

Your hope is now a reality! I am "ritehere".

Friday, October 26, 2018 10:19 PM

• 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.

Well, knowing if a month has 31 or 30 or 29 or 28 days doesn't really matter for the small problem at hand. It is needed for scalability (for lets say they want to have a discount month).

Friday, October 26, 2018 10:35 PM
• How would you go about checking what part of the Timespan is Night-rate and what part is Day-rate?

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 integral-periods-of-24 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:

Integral-periods-of-24-hours = 5

Hours-exceeding-the-integral-periods-of-24-hours = 5

To calculate Hours-exceeding-the-integral-periods-of-24-hours 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).

Friday, October 26, 2018 10:49 PM

• 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:00

How 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 :).

Friday, October 26, 2018 11:08 PM
• 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!

Friday, October 26, 2018 11:15 PM
• 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!

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)?
Friday, October 26, 2018 11:28 PM

• 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!

Friday, October 26, 2018 11:38 PM
• This is the Main method:

```public static void Main(string[] args)
{
DateTime
begin=... // any date
,end=... // must be *greater* than "begin"
;

TimeSpan ts=end-begin;
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...

Friday, October 26, 2018 11:41 PM
• The full highly elevated and advanced C# code:

Rates: High and Low - the (very) smart way

• Marked as answer by Saturday, October 27, 2018 10:45 PM
Saturday, October 27, 2018 12:24 AM
• The full highly elevated and advanced C# code:

I will never fear when you are ritehere!
You showed me the way... and I will now forever bow down to you.
... Thank you, oh wise one!
Saturday, October 27, 2018 12:21 PM
• Ok. solution found - not need to go through step by step.

Sincerely, Highly skilled coding monkey.

Tuesday, October 30, 2018 10:34 AM