- Now this next function 'get days in period'.
So I need to explain the purpose of this function
and it's quite,
it took me a while to get this correct,
because of a lot of edge conditions
that I didn't first imagine.
You know when you're writing code,
you spend maybe five minutes writing
the main part of the algorithm
and then the next hour thinking about
all the edge conditions,
what if it's this special condition,
or that special condition?
And I had several experiences like that
when it was writing the function.
So, what I'm gonna do, is I'm gonna draw some pictures,
that kind of explain what this function is all about.
Okay? So I'm gonna, to do that,
I'm gonna bring up my whiteboard
and look at the code at the same time as drawing pictures.
Okay then, so let's just move this into view.
So this is my 'get days in period'.
So, the purpose of this function
is to basically figure out
if you visited Carlsburg on the eighth, what's that?
The 8th of May.
If that's when you went and you came back,
well, 15th is the day after you came back.
It basically, the purpose of this function
is to figure out how many days there are in that period.
Okay? So it takes a visit.
The visit itself is the visit object
and the visit object will basically contain the start date
and the end date or the day after the end date.
Okay? So if you imagine this visit object being like this.
So let's say it has a start date,
which is basically the 8th of May,
and the end date, which is basically the 15th of May,
in my example here.
The purpose of the function is to,
to take that visit object,
and basically return how many days I've been away.
So it might seem obvious that I've been away for seven days.
The purpose of the function is to return an integer
telling me how many days I've been away.
I probably could've used I 32 in that example,
and I probably should, but it's too late now.
So you'd think it would just be a matter
of taking the end date, if that's the end date
and that's the start date.
Wouldn't it just be a matter of subtracting the start date
from the end date like so?
And the answer is no, it's not that simple.
Life is never that simple,
because what's relevant isn't just the number
of days I was away,
but how many of those days were in the 180 day period,
I'm looking at.
So there's a window, if this is,
let's say the user specified the 31st of December, 2023
as the end date, as the date from,
from which they want to go back 180 days
to find out how many days
that I've been away in that period.
So this would be, if this end date,
if this, this is the window end date,
I'll call it "W E".
In my code, and if you look at
the 'get days in period' function,
I've called it 'window end date'.
Okay? So window end date in this example,
I've just, let's call it "W E",
would be the 31st of December.
So I guess the window start date,
the window start date would be 180 days before that,
which I'm not sure what the maths is
but let's say it's something like the 30th of June.
It's probably not exactly the 30th.
Maybe it's the 29th or the 1st of July
but it's something like that anyway,
so that's basically 180 days previously.
So this is the window.
It's only dates, days that fall in this window
which are relevant.
Okay? Now that's important because
there are special cases where my visit now
maybe was like this, maybe some of the days were
outside the period of interest and are therefore irrelevant.
Or maybe my visit is like this,
where some of the days are outside the period of interest
and are therefore irrelevant.
So I've gotta decide what part of my visit
is actually relevant and that's what I've done here.
So it takes my visit start date
and it says if your visit start date
is after the window end date,
so let's say my visit was here.
Let's say my, my visit start date is here.
If the visit start date is after the window end,
I'm only looking at this window here.
This is the only bit I'm actually interested in.
And if my visit started here,
it is completely outside the period of interest.
So in that case it returns zero.
Okay? For this particular visit, there was zero days
in this visit that fall in the period of interest.
Okay, well that's one kind of possible edge condition.
There's another edge condition on the other side
of the, of the period if you like.
It says, imagine that the visit was actually here.
Imagine that my visit end date was here.
If the visit end date is less than the window start date
here then again my visit came before the,
was completely finished, I should say,
before my period of interest.
I'm only interested in days that fall in this period
and this is completely outside the area.
So again, in that special edge condition,
I just returned zero
because it's non-overlapping in the period of interest.
Right?
I've got some other things to think about as well.
Have a look at this statement here.
In the std cmp module,
there's a max function
and you can give it two values of the same type
and it'll give you back the maximum of the two.
So this code here, it gives me back the maximum
of my visit start date and the window start date.
Maybe my visit occurred, maybe my visit was like that.
Okay? So that's my visit start date.
This portion here is irrelevant
because it's outside the period of interest.
So the code that I've got here,
will give me back the maximum of my start date
and the window start date.
If my start date for my visit is here,
but the window is here,
it'll gimme back this value here, okay?
It'll say this is the effective start date of interest.
Ignore the date when you actually visited.
That was outside the period.
The first day that was relevant,
the effective start date of relevance
within the period of interest is this date here.
When the window started.
I call that the start effective.
Okay, what's the first date that actually matters
compared to the first date that actually happened.
Right? So,
if I just tidy up my picture,
I've got a similar kind of condition on the end date.
You can see now why these special conditions
passed me by initially.
Imagine that was my visit and this takes the minimum
of the, my actual visit date
and the end of the period of interest.
If my visit ended here, the minimum value of that
and the window end date is the window end date.
That's the minimum value.
Okay? And that's the end of my effective visit.
It says gimme back the minimum
of the actual visit date and the window end date.
So that'll be here
and I've got that the end effective.
That's the effective end date of interest.
There we are.
So I have for whatever it turns out to be, I have,
I'll call it start effective for my visit
and I'll call it end effective, like so.
And in the naive date structure,
there are functions to compare and subtract and add dates.
It's very, very much like you might expect,
if you're gonna have a date structure,
you'd expect it to be able to add days, subtract days
compare and such like, and you can,
you can take the end effective date.
I've call it "ee" in my picture.
And you can basically get the duration
since the start effective date.
It gives you back a positive or negative duration.
It actually gives you back a duration structure
to give you the difference, like that,
expressed as a duration object.
And then on the duration object,
you can express that in days,
the number of days will say how many days difference
is there between, in this duration here.
You could have said "number of months"
or "number of seconds", there are various different ways
of expressing the difference between two dates.
So, that was quite hard work.
I spent a lot of time writing this function,
and the whole purpose was for any, for one particular visit,
it'll tell me how many days in that visit
was I actually away in the period of interest.
If the visit was long time ago,
it would only count those days.
If the visit was partially in the future,
then it would only count those days, like that.
Okay? So that was actually, I was quite pleased
and quite relieved when I got that work in.
So that's the visit structure.
Oh, also I display a visit.
So you can, you can basically do something like this.
You can say println,
and then you can use the default formatter
to print the visit.
Okay? So when you, when you use a default formatter
curly brackets,
it'll only work if your structure implements display
and you have a format function,
basically builds a string
to display the visit on the screen.
When I invoke an option to display all visits on the screen,
this is the function that gets called for each visit.