 So recently you've been doing a lot of work with dates and formatting dates in shell scripts We looked at looping through dates We looked at a firefighter schedule where you have 24 on 40 off So a three day three shifts schedule and figure out what shift today is today We're going to be looking at figuring out. Do I work this Christmas again as a firefighter if I don't change shifts I can predict out every day. I work until I retire So I be I'll look at any year and see ahead of time am I gonna work Christmas because There's a good chance that I will have to so Let's dive right in. Okay. Let's get started I'm gonna use Vim as my text editor use whatever text editor you prefer for writing code I'll just make sure it's not a word document editor because that is different than a text editor I'm gonna go in here and this is going to be a bash script Although this should work with pretty much all shells We're gonna give it a shebang line at this point now We need to give it Variables for a B and C shift again mice department has a 24 on 48 off Schedule so we have a B and C shift here dates that I know are a B and C shift and as I mentioned the previous video I wrote this script a while ago one thing I might change is instead of calculating this out for a B and C I would put in a date for a shift and then for B shift and C shift Add one and two days to it mainly for if you work at a different department You can set a different date and you don't have to change all three you would only have to change one But what we're doing here is the dollar sign and parentheses is saying we're gonna run everything in between this is a command And whatever the output is we're putting into this variable and we're saying okay. Look at this date and At midnight on that date. Give us the number of seconds since January 1st 1970 so that would be your epoch or Unix timestamp there and then we also need to get the date for Christmas So we can do math on that so I'm gonna set a variable called Christmas And again, we're running date this plus F says whatever date I give just give us a formatted date for midnight on that date and Right here. I'm saying 1225 because Christmas always falls on December 25th And if I don't give it a year it assumes I mean this year So we will always start at the current year and move forward next. We're going to do a for loop. So here I'm saying Loop through these numbers zero through ten so in bash the curly braces here We'll say take this number and go through this number one Digit at a time and we're gonna put that in a variable called I which we're not even gonna really use We could but we're just gonna add one each time Okay, so and then we're done so everything inside this loop is gonna happen ten times now in the previous video We did some math and we replaced our original a b and c variables Well, we can't replace those because we need to use those each time we loop this time So what I'm gonna do is I'm gonna just create a variable called a a b b and cc so there we go a a b b and cc and Again in here. I'm calculating out date for Christmas in seconds each time really if I wanted to be more efficient I would actually calculate out this once put it in a variable and Not have to calculate again. So we're using a little more processing power than we need But this isn't a very labor-intensive code I'm just letting you know that there is a better way of doing this if you needed to improve upon it But what are we doing? Okay, so again inside the dollar sign and outer brackets We're saying we're gonna run some commands the output of that go into this variable the inner Parentheses says we're doing some math and then we have these back ticks Which is also saying we're running command with inside this command and again It's the date command. We're getting the number of seconds since January 1st 1970 for Christmas We're gonna take that number and subtract our Age shift seconds, okay, then we're gonna divide that. What are we gonna divide it by? We're gonna divide it by the number of seconds in a day. Well, there's 24 hours in a day. We're multiplying that by 3600 because there's 3600 seconds in an hour. So that gives us number of seconds a day Of course you do that math and just put that number in there and then the computer doesn't have to do the math But if you can't remember how many seconds are in a day, you know, you can do 60 times 60 Times 24 or whatever you need to do to get that number. What are we doing once we get that? We're subtracting those dates get a number of seconds in a day and dividing that then we're going to say This percent sign says divide by three, but we're not getting What is divide by three? This is saying give us the remainder and that is these three a b and c shift Only one of them should be zero and that would be the day that falls on Christmas or whatever date we're giving it Next we're going to check those numbers and we're going to print some output if the value is zero So here we're going to say inside these brackets. We're saying, okay Does a a equals zero does b b equals zero does cc equals zero only one of those should be true Which everyone is true the ampersand ampersand saying if the last command is true Then echo out this either a b or c shift is Christmas Next we're going to replace the Christmas command. We're going to say Christmas or Christmas variable Equals date and we're going to take the current date of Christmas for this year and we're going to add One year to it and we're going to format it like this and reality That's basically in this context doing the same thing as this plus F. So I'm being a little inconsistent here I probably should have done it the same as up here just to be consistent But basically just realize that we're adding one to Christmas and then we're going to loop around again, and that's it That's our entire code. We're going to save that make it executable and then just run it and again no matter what year this is is going to start at the current year and Iterate through that for 10 years and as you can see here. We got 2021-2022-23-24 all the way through 31 So really we're getting 11 years this year and the next 10 right that I do my my accounting right there And as you can see this year B shift works now. I work on a shift So I work Christmas next year and then C shift C shift B shift a shift C Shift C shift B shift a shift now you you might notice that C shift gets a little against against the shaft It's they get screwed a little bit and why is that you might think I'll give you a moment to think about that if you said leap year You are correct. See now not all holidays fall in a particular day. You may know that like Thanksgiving changes It's not on the same exact date each year, but a lot of holidays are and If you're Christmas, that means you work a few of the big holidays So and Christmas is usually being one of the biggest ones So C shift because of leap year ends up working the big holidays two years in a row Sucks to be them right so I know my script is working because it's showing that So here I can look out the next 10 years 20 years 100 years. If I don't change shifts I can know what years I'm gonna work Christmas and I can plan accordingly. So I know next year. I work Christmas and then 2026 I'll be working Christmas and then 2030 I'll be working Christmas So I can try to put in and get those days off But a limited number of people can be off each shift if I'm unable to get my Request in on time before other people take it off We can also do a thing called shift exchanges now This doesn't have to do anything with coding, but I thought I'd let you know about this So since we work this this regular schedule and I will be scheduled to work Christmas Everyone on a shift is scheduled for Christmas unless they get in their request before the spots are taken up What if you need Christmas off what we can do things called shift exchanges or shift swaps So I can go to somebody on another shift C shift or B shift and I can say hey Can you work Christmas for me? And they can say yeah, I'll work Christmas for you Then I'll owe them a day and then when they need a day off in the future I'll work for them and that's how that works. Anyway, I hope you found this useful and Interesting and even if you're not a firefighter, maybe there's something in this code You can use you can use this code for something else or you just learned something about Calculating dates either way There'll be a link in the description of this video where you can go and download this script and give it a try yourself I thank you for watching and as always, please visit films by Chris calm That's Chris the case. There's a link in the description and as always I hope that you have a great day