- Right, so the only thing we need to look
at now is how do I actually calculate the number of days
prior to a particular date.
So I'm gonna do a quick drawing
just to help you visualize
what that calculation's gonna look like.
And I'm gonna show the code for that function as well.
And this will be the last bit of code
we need to look at in the lesson.
So let's bring up my whiteboard
so we can just see this side by side.
So imagine I'm looking at a particular date, okay?
So it does it for every date,
but let's say I'm looking at a particular date.
Let's say I'm looking at the date is the 11th of August.
So that's the date we're currently looking at.
And it's trying to figure out how many days
were you away in all the visits?
How many days were you away before the 11th of August?
So that's the window end date there.
And obviously what it'll have to do
is have to take that window end date and subtract 180 days.
I've got a constant at the top which says it's 180 days
which of interest.
So it'll calculate the date, 180 days before
the 11th of August would be, I dunno,
something like the 11th of March,
the 11th of month number three
which is about six months
before the 11th of month number eight,
11th of February actually.
February is six months
before August, the 11th of February.
So that is the window start date.
That's the window start date,
and that's the window end date.
And what I do is I iterate over all my visits.
Now, the visits,
remember the visits will have been something like this.
I might have had visits before
the length of February, and like this,
and I might have had a visit that overlapped it like that.
And some other visits and some other visits,
and some other visits and some other visits
that partially overlapped and some other visits here.
So for each of those visits,
for each visit,
let's say it'll take this visit initially.
For that visit, it'll calculate,
for that particular visit,
how many days were you away that are relevant?
We're only looking at this window here.
That's the window that we're looking at at the moment.
So relative to that window,
how many days were you were you away
that are relevant for that particular visit?
And the answer would be zero.
We spent some time looking at this function a bit earlier,
using some maths, it'll realize
that that visit is wholly outside the window of interest.
So the get days in period in that case would return zero.
As it would for this one as well.
This one here would be a bit more interesting
because it kind of straddles the boundary,
but we've seen that the code caters
for that possibility as well.
For that visit, it'll say effectively
the effective start date is kind of here.
We can ignore the dates that appeared before that,
ignore that, and it'll just look at this part of the visit
and say maybe there were three days there
that were relevant.
And it'll add that visit and that visit and that visit.
So for each of these functions,
it'll tell me the number of days
that are relevant for that visit.
Like zero, zero, three, maybe seven for that one.
Seven, seven, maybe three for that one,
zero for that one because it's wholly after the period.
So for each visit, it'll tell me how many days are relevant
and then I just sum them up.
There's a handy sum function.
Having built a collection of numbers, days,
I can just sum them up like so,
and it'll tell me the total number
of days that I was away from the 11th of August,
the total number of days I was away would be,
and it'll tell me the total days.
So the 11th of August that would be come down here,
well, those are then gonna get sorted of course,
the whole point of this is that
these dates here will be sorted,
in the bit of output I'm looking at here.
If I go to the first part of the output
then it outputs 'em in reverse chronological order.
It's easier to me for me to find a date, there we go.
Okay, so the 11th of August
and the 180 days previous, in other words,
if you add up all the bits which are relevant here,
so that bit, and that bit, and that bit,
and that bit, and that bit.
Then it would give you back the total number of 74.
So I finished the lesson now.
I had a lot of fun writing this demo.
I'd rather hope that soon
I won't need this application anymore
because the UK has rejoined Europe, and we live in hope.