- But I've also got a structure called Record, okay?
Now you might wonder why I need two structures.
The Record structure, oh, it implements equality
and partial equality traits.
That's an interesting observation.
A Record is a kind of like a summary of a visit.
It just says what date did the visit start
and how many days was it, like so.
So if you compare a visit structure,
and a record structure to see what they contain
let's think about that.
So a visit structure will contain the start date,
like the, we went skiing on the 1st of April, 2023 to
St. Anton in Austria, what a beautiful place.
We were only there for a week, unfortunately.
We came back on the 8th of April 23 and it was to St. Anton.
That's my favorite ski resort.
That would be a visit.
A record on the other hand,
would kind of be a summary of that.
A record would just say it was the 1st of April
when you went, 23.
So it says basically the the date when
the visit started and the number of days that you were away.
So the number of days I've been away
would be seven, isn't it?
I think.
Seven days away like that.
So the visit information is what I read from a file.
So on my file system I'll read visit information,
but it's the record that I'm going to use
in all of my work ins.
When I say, when I'm trying to calculate
the total number of days I've been away,
it'll be this number that I'm going to use.
This record is like a summary of the visit,
and it's only this number really that matters.
And when I output all the information
about how many days I've been away,
it's this record information that'll be displayed.
It'll say you were away on the
1st of April 23 for seven days.
So it's like a simplified summary.
I didn't envisage having this record structure.
Initially I envisaged just using visit,
but then I kept on having to do lots of
like give me the number of days between the start and end.
And I thought I may as well just calculate
that once and store it in here.
So that's the purpose,
the implementation is actually really simple.
I've just got a effectively a constructor
to initialize the date and the number of days,
it implements the display trait.
So it can output a record using the default formatter,
number of days, what date did you start.
It also implements the odd trait and the partial odd trait.
Remember those?
The partial odd trait basically means that you can say,
you could say something like this.
You could say record one, less than record two, like that.
Or record one greater than equal to record two.
So when you implement the partial comp,
it basically, the algorithm here will allow
records to be compared for equality,
less than equal to, I should say.
And then the odd trait above it.
The odd trait, basically it has a max function,
and a min function and a clamp function, remember that?
Where you can bound a value
to be within a certain range, a given maximum and a minimum.
So you could say, for example, something like
if I have record one, and I call max a record two,
it will gimme back the larger
of the two or the smaller of the two.
So this record structure turned out to be
quite useful actually, as we will see in the code.
So there we are.
It's quite, it's not difficult really.
I think it's just tricky rather than being difficult.
And you need to have a look online at the documentation
for NaiveDate to see what options and functions you've got.
So here is the documentation for NaiveDate.
It's the NaiveDate structure
in the Naive Module in the Chrono Crate.
And you can see all the different functions it has here,
for doing date time related operations.
So the function that we called was signed duration since,
you can give it one NaiveDate and another NaiveDate,
and it returns a duration, okay?
And the duration basically allows you to convert
to days and minutes and such like.
So there we are.
That's good coverage, I think,
of how to do date handling in Rust.