1 00:00:06,540 --> 00:00:10,969 - Now this next function 'get days in period'. 2 00:00:10,969 --> 00:00:14,158 So I need to explain the purpose of this function 3 00:00:14,158 --> 00:00:16,020 and it's quite, 4 00:00:16,020 --> 00:00:17,580 it took me a while to get this correct, 5 00:00:17,580 --> 00:00:18,930 because of a lot of edge conditions 6 00:00:18,930 --> 00:00:20,130 that I didn't first imagine. 7 00:00:20,130 --> 00:00:22,110 You know when you're writing code, 8 00:00:22,110 --> 00:00:23,580 you spend maybe five minutes writing 9 00:00:23,580 --> 00:00:25,200 the main part of the algorithm 10 00:00:25,200 --> 00:00:27,060 and then the next hour thinking about 11 00:00:27,060 --> 00:00:28,230 all the edge conditions, 12 00:00:28,230 --> 00:00:30,180 what if it's this special condition, 13 00:00:30,180 --> 00:00:31,500 or that special condition? 14 00:00:31,500 --> 00:00:33,609 And I had several experiences like that 15 00:00:33,609 --> 00:00:35,850 when it was writing the function. 16 00:00:35,850 --> 00:00:39,270 So, what I'm gonna do, is I'm gonna draw some pictures, 17 00:00:39,270 --> 00:00:43,590 that kind of explain what this function is all about. 18 00:00:43,590 --> 00:00:45,630 Okay? So I'm gonna, to do that, 19 00:00:45,630 --> 00:00:48,270 I'm gonna bring up my whiteboard 20 00:00:48,270 --> 00:00:51,783 and look at the code at the same time as drawing pictures. 21 00:00:53,010 --> 00:00:58,010 Okay then, so let's just move this into view. 22 00:00:58,110 --> 00:01:00,810 So this is my 'get days in period'. 23 00:01:00,810 --> 00:01:03,000 So, the purpose of this function 24 00:01:03,000 --> 00:01:04,290 is to basically figure out 25 00:01:04,290 --> 00:01:09,290 if you visited Carlsburg on the eighth, what's that? 26 00:01:10,110 --> 00:01:12,333 The 8th of May. 27 00:01:13,710 --> 00:01:16,080 If that's when you went and you came back, 28 00:01:16,080 --> 00:01:18,450 well, 15th is the day after you came back. 29 00:01:18,450 --> 00:01:21,030 It basically, the purpose of this function 30 00:01:21,030 --> 00:01:24,300 is to figure out how many days there are in that period. 31 00:01:24,300 --> 00:01:28,680 Okay? So it takes a visit. 32 00:01:28,680 --> 00:01:31,470 The visit itself is the visit object 33 00:01:31,470 --> 00:01:35,010 and the visit object will basically contain the start date 34 00:01:35,010 --> 00:01:38,311 and the end date or the day after the end date. 35 00:01:38,311 --> 00:01:41,763 Okay? So if you imagine this visit object being like this. 36 00:01:45,990 --> 00:01:48,060 So let's say it has a start date, 37 00:01:48,060 --> 00:01:49,983 which is basically the 8th of May, 38 00:01:51,240 --> 00:01:54,548 and the end date, which is basically the 15th of May, 39 00:01:54,548 --> 00:01:56,190 in my example here. 40 00:01:56,190 --> 00:01:58,268 The purpose of the function is to, 41 00:01:58,268 --> 00:02:00,780 to take that visit object, 42 00:02:00,780 --> 00:02:04,032 and basically return how many days I've been away. 43 00:02:04,032 --> 00:02:09,032 So it might seem obvious that I've been away for seven days. 44 00:02:09,268 --> 00:02:12,540 The purpose of the function is to return an integer 45 00:02:12,540 --> 00:02:14,010 telling me how many days I've been away. 46 00:02:14,010 --> 00:02:17,848 I probably could've used I 32 in that example, 47 00:02:17,848 --> 00:02:21,540 and I probably should, but it's too late now. 48 00:02:21,540 --> 00:02:23,970 So you'd think it would just be a matter 49 00:02:23,970 --> 00:02:27,990 of taking the end date, if that's the end date 50 00:02:27,990 --> 00:02:29,040 and that's the start date. 51 00:02:29,040 --> 00:02:32,310 Wouldn't it just be a matter of subtracting the start date 52 00:02:32,310 --> 00:02:34,023 from the end date like so? 53 00:02:34,950 --> 00:02:38,580 And the answer is no, it's not that simple. 54 00:02:38,580 --> 00:02:40,470 Life is never that simple, 55 00:02:40,470 --> 00:02:44,490 because what's relevant isn't just the number 56 00:02:44,490 --> 00:02:45,323 of days I was away, 57 00:02:45,323 --> 00:02:49,230 but how many of those days were in the 180 day period, 58 00:02:49,230 --> 00:02:50,670 I'm looking at. 59 00:02:50,670 --> 00:02:52,860 So there's a window, if this is, 60 00:02:52,860 --> 00:02:57,860 let's say the user specified the 31st of December, 2023 61 00:03:01,170 --> 00:03:03,600 as the end date, as the date from, 62 00:03:03,600 --> 00:03:07,590 from which they want to go back 180 days 63 00:03:07,590 --> 00:03:09,165 to find out how many days 64 00:03:09,165 --> 00:03:10,020 that I've been away in that period. 65 00:03:10,020 --> 00:03:12,050 So this would be, if this end date, 66 00:03:12,050 --> 00:03:14,520 if this, this is the window end date, 67 00:03:14,520 --> 00:03:16,260 I'll call it "W E". 68 00:03:16,260 --> 00:03:18,930 In my code, and if you look at 69 00:03:18,930 --> 00:03:22,308 the 'get days in period' function, 70 00:03:22,308 --> 00:03:25,770 I've called it 'window end date'. 71 00:03:25,770 --> 00:03:29,610 Okay? So window end date in this example, 72 00:03:29,610 --> 00:03:31,110 I've just, let's call it "W E", 73 00:03:31,110 --> 00:03:33,750 would be the 31st of December. 74 00:03:33,750 --> 00:03:36,210 So I guess the window start date, 75 00:03:36,210 --> 00:03:39,600 the window start date would be 180 days before that, 76 00:03:39,600 --> 00:03:41,520 which I'm not sure what the maths is 77 00:03:41,520 --> 00:03:45,210 but let's say it's something like the 30th of June. 78 00:03:45,210 --> 00:03:47,070 It's probably not exactly the 30th. 79 00:03:47,070 --> 00:03:49,140 Maybe it's the 29th or the 1st of July 80 00:03:49,140 --> 00:03:50,580 but it's something like that anyway, 81 00:03:50,580 --> 00:03:53,910 so that's basically 180 days previously. 82 00:03:53,910 --> 00:03:55,560 So this is the window. 83 00:03:55,560 --> 00:03:58,890 It's only dates, days that fall in this window 84 00:03:58,890 --> 00:04:00,271 which are relevant. 85 00:04:00,271 --> 00:04:03,720 Okay? Now that's important because 86 00:04:03,720 --> 00:04:07,260 there are special cases where my visit now 87 00:04:07,260 --> 00:04:11,190 maybe was like this, maybe some of the days were 88 00:04:11,190 --> 00:04:15,330 outside the period of interest and are therefore irrelevant. 89 00:04:15,330 --> 00:04:18,930 Or maybe my visit is like this, 90 00:04:18,930 --> 00:04:22,470 where some of the days are outside the period of interest 91 00:04:22,470 --> 00:04:24,210 and are therefore irrelevant. 92 00:04:24,210 --> 00:04:27,750 So I've gotta decide what part of my visit 93 00:04:27,750 --> 00:04:31,170 is actually relevant and that's what I've done here. 94 00:04:31,170 --> 00:04:33,970 So it takes my visit start date 95 00:04:34,860 --> 00:04:37,170 and it says if your visit start date 96 00:04:37,170 --> 00:04:39,510 is after the window end date, 97 00:04:39,510 --> 00:04:41,583 so let's say my visit was here. 98 00:04:42,990 --> 00:04:46,203 Let's say my, my visit start date is here. 99 00:04:47,640 --> 00:04:52,500 If the visit start date is after the window end, 100 00:04:52,500 --> 00:04:54,270 I'm only looking at this window here. 101 00:04:54,270 --> 00:04:57,930 This is the only bit I'm actually interested in. 102 00:04:57,930 --> 00:04:59,940 And if my visit started here, 103 00:04:59,940 --> 00:05:03,270 it is completely outside the period of interest. 104 00:05:03,270 --> 00:05:06,720 So in that case it returns zero. 105 00:05:06,720 --> 00:05:09,780 Okay? For this particular visit, there was zero days 106 00:05:09,780 --> 00:05:12,663 in this visit that fall in the period of interest. 107 00:05:13,710 --> 00:05:17,760 Okay, well that's one kind of possible edge condition. 108 00:05:17,760 --> 00:05:20,820 There's another edge condition on the other side 109 00:05:20,820 --> 00:05:22,590 of the, of the period if you like. 110 00:05:22,590 --> 00:05:25,090 It says, imagine that the visit was actually here. 111 00:05:26,160 --> 00:05:28,443 Imagine that my visit end date was here. 112 00:05:29,430 --> 00:05:33,300 If the visit end date is less than the window start date 113 00:05:33,300 --> 00:05:38,300 here then again my visit came before the, 114 00:05:38,760 --> 00:05:41,160 was completely finished, I should say, 115 00:05:41,160 --> 00:05:42,780 before my period of interest. 116 00:05:42,780 --> 00:05:45,720 I'm only interested in days that fall in this period 117 00:05:45,720 --> 00:05:47,700 and this is completely outside the area. 118 00:05:47,700 --> 00:05:50,640 So again, in that special edge condition, 119 00:05:50,640 --> 00:05:51,820 I just returned zero 120 00:05:52,710 --> 00:05:56,373 because it's non-overlapping in the period of interest. 121 00:05:57,450 --> 00:05:58,350 Right? 122 00:05:58,350 --> 00:06:01,830 I've got some other things to think about as well. 123 00:06:01,830 --> 00:06:04,443 Have a look at this statement here. 124 00:06:05,310 --> 00:06:09,731 In the std cmp module, 125 00:06:09,731 --> 00:06:12,060 there's a max function 126 00:06:12,060 --> 00:06:14,310 and you can give it two values of the same type 127 00:06:14,310 --> 00:06:16,290 and it'll give you back the maximum of the two. 128 00:06:16,290 --> 00:06:19,260 So this code here, it gives me back the maximum 129 00:06:19,260 --> 00:06:22,413 of my visit start date and the window start date. 130 00:06:23,580 --> 00:06:28,580 Maybe my visit occurred, maybe my visit was like that. 131 00:06:29,130 --> 00:06:31,053 Okay? So that's my visit start date. 132 00:06:32,340 --> 00:06:35,070 This portion here is irrelevant 133 00:06:35,070 --> 00:06:37,890 because it's outside the period of interest. 134 00:06:37,890 --> 00:06:40,350 So the code that I've got here, 135 00:06:40,350 --> 00:06:43,590 will give me back the maximum of my start date 136 00:06:43,590 --> 00:06:45,390 and the window start date. 137 00:06:45,390 --> 00:06:48,549 If my start date for my visit is here, 138 00:06:48,549 --> 00:06:50,700 but the window is here, 139 00:06:50,700 --> 00:06:53,730 it'll gimme back this value here, okay? 140 00:06:53,730 --> 00:06:57,900 It'll say this is the effective start date of interest. 141 00:06:57,900 --> 00:07:00,360 Ignore the date when you actually visited. 142 00:07:00,360 --> 00:07:01,770 That was outside the period. 143 00:07:01,770 --> 00:07:03,780 The first day that was relevant, 144 00:07:03,780 --> 00:07:05,550 the effective start date of relevance 145 00:07:05,550 --> 00:07:08,340 within the period of interest is this date here. 146 00:07:08,340 --> 00:07:09,840 When the window started. 147 00:07:09,840 --> 00:07:11,673 I call that the start effective. 148 00:07:13,708 --> 00:07:16,110 Okay, what's the first date that actually matters 149 00:07:16,110 --> 00:07:18,610 compared to the first date that actually happened. 150 00:07:19,560 --> 00:07:20,883 Right? So, 151 00:07:24,510 --> 00:07:26,133 if I just tidy up my picture, 152 00:07:27,420 --> 00:07:30,300 I've got a similar kind of condition on the end date. 153 00:07:30,300 --> 00:07:32,640 You can see now why these special conditions 154 00:07:32,640 --> 00:07:34,890 passed me by initially. 155 00:07:34,890 --> 00:07:38,460 Imagine that was my visit and this takes the minimum 156 00:07:38,460 --> 00:07:40,380 of the, my actual visit date 157 00:07:40,380 --> 00:07:42,600 and the end of the period of interest. 158 00:07:42,600 --> 00:07:47,600 If my visit ended here, the minimum value of that 159 00:07:48,780 --> 00:07:51,660 and the window end date is the window end date. 160 00:07:51,660 --> 00:07:52,923 That's the minimum value. 161 00:07:54,130 --> 00:07:56,550 Okay? And that's the end of my effective visit. 162 00:07:56,550 --> 00:07:58,530 It says gimme back the minimum 163 00:07:58,530 --> 00:08:00,630 of the actual visit date and the window end date. 164 00:08:00,630 --> 00:08:01,900 So that'll be here 165 00:08:04,140 --> 00:08:05,520 and I've got that the end effective. 166 00:08:05,520 --> 00:08:07,803 That's the effective end date of interest. 167 00:08:09,090 --> 00:08:09,923 There we are. 168 00:08:09,923 --> 00:08:13,440 So I have for whatever it turns out to be, I have, 169 00:08:13,440 --> 00:08:17,490 I'll call it start effective for my visit 170 00:08:17,490 --> 00:08:21,300 and I'll call it end effective, like so. 171 00:08:21,300 --> 00:08:24,900 And in the naive date structure, 172 00:08:24,900 --> 00:08:28,350 there are functions to compare and subtract and add dates. 173 00:08:28,350 --> 00:08:30,780 It's very, very much like you might expect, 174 00:08:30,780 --> 00:08:32,580 if you're gonna have a date structure, 175 00:08:32,580 --> 00:08:35,370 you'd expect it to be able to add days, subtract days 176 00:08:35,370 --> 00:08:37,860 compare and such like, and you can, 177 00:08:37,860 --> 00:08:40,140 you can take the end effective date. 178 00:08:40,140 --> 00:08:42,960 I've call it "ee" in my picture. 179 00:08:42,960 --> 00:08:45,150 And you can basically get the duration 180 00:08:45,150 --> 00:08:47,280 since the start effective date. 181 00:08:47,280 --> 00:08:50,100 It gives you back a positive or negative duration. 182 00:08:50,100 --> 00:08:52,440 It actually gives you back a duration structure 183 00:08:52,440 --> 00:08:54,750 to give you the difference, like that, 184 00:08:54,750 --> 00:08:57,510 expressed as a duration object. 185 00:08:57,510 --> 00:08:59,490 And then on the duration object, 186 00:08:59,490 --> 00:09:01,050 you can express that in days, 187 00:09:01,050 --> 00:09:03,990 the number of days will say how many days difference 188 00:09:03,990 --> 00:09:06,453 is there between, in this duration here. 189 00:09:07,290 --> 00:09:09,300 You could have said "number of months" 190 00:09:09,300 --> 00:09:11,700 or "number of seconds", there are various different ways 191 00:09:11,700 --> 00:09:15,030 of expressing the difference between two dates. 192 00:09:15,030 --> 00:09:16,290 So, that was quite hard work. 193 00:09:16,290 --> 00:09:18,420 I spent a lot of time writing this function, 194 00:09:18,420 --> 00:09:22,470 and the whole purpose was for any, for one particular visit, 195 00:09:22,470 --> 00:09:25,350 it'll tell me how many days in that visit 196 00:09:25,350 --> 00:09:27,690 was I actually away in the period of interest. 197 00:09:27,690 --> 00:09:30,630 If the visit was long time ago, 198 00:09:30,630 --> 00:09:32,460 it would only count those days. 199 00:09:32,460 --> 00:09:34,470 If the visit was partially in the future, 200 00:09:34,470 --> 00:09:36,760 then it would only count those days, like that. 201 00:09:36,760 --> 00:09:39,270 Okay? So that was actually, I was quite pleased 202 00:09:39,270 --> 00:09:42,000 and quite relieved when I got that work in. 203 00:09:42,000 --> 00:09:45,390 So that's the visit structure. 204 00:09:45,390 --> 00:09:47,490 Oh, also I display a visit. 205 00:09:47,490 --> 00:09:49,980 So you can, you can basically do something like this. 206 00:09:49,980 --> 00:09:51,460 You can say println, 207 00:09:56,056 --> 00:09:58,050 and then you can use the default formatter 208 00:09:58,050 --> 00:09:59,160 to print the visit. 209 00:09:59,160 --> 00:10:02,070 Okay? So when you, when you use a default formatter 210 00:10:02,070 --> 00:10:03,570 curly brackets, 211 00:10:03,570 --> 00:10:07,320 it'll only work if your structure implements display 212 00:10:07,320 --> 00:10:09,180 and you have a format function, 213 00:10:09,180 --> 00:10:11,140 basically builds a string 214 00:10:12,660 --> 00:10:15,300 to display the visit on the screen. 215 00:10:15,300 --> 00:10:19,260 When I invoke an option to display all visits on the screen, 216 00:10:19,260 --> 00:10:21,963 this is the function that gets called for each visit.