The Date Hack
- Views
- 237K
- 237,096
- Outlier
- ×0.7
- Median 364K
- Length
- 11:21
- Published
- Sep 17
- Thursday
- Comments
- 331
- 5.2K likes
Script
How it's written
- Words
- 2,429
- Words a minute
- 214
- Reading time
- 11 min
- Segments
- 8
The hook
0:00–0:26There are two hard problems when it comes to computer science. That's of course is naming things and getting a date. Now I know from great personal struggles the difficulty of of course getting a date and I thought I would help guide you through the process. You know, really teach those young budding computer scientists and vibe coders what exactly it takes to get a date and go on and live a happily married life. Now of course I'm talking about the fastest way to calculate the day of the week. Now
Structure
There are two hard problems when it comes to computer science. That's of course is naming things and getting a date. Now I know from great personal struggles the difficulty of of course getting a date and I thought I would help guide you through the process. You know, really teach those young budding computer scientists and vibe coders what exactly it takes to get a date and go on and live a happily married life. Now of course I'm talking about the fastest way to calculate the day of the week. Now this is one of the most amazing articles you have to see and we will first derive like the basic formula and then we'll see like a really cool just coincidental way to be able to make this work out and then the most insane bitwise operation that even thinking about it just gets me juiced. And I made a beautiful presentation for this. And you guys are always asking me for some technical video so here is one. So I hope that you like it, okay? Smash the like button, okay? Press the subscribe, whatever they whatever whatever people say. But of course before we begin, a quick thank you from the sponsor. Have you ever deployed to production and everything is green and awesome? But when you go to the website, oh my gosh, IT'S A 500? WELL, DON'T WORRY because Linear has your back. See, when that 500 hits your error reporting can create a Linear ticket which will kick off a Linear loop. So honestly, why spend 30, 40, 50 minutes of your time searching around with these production bugs when you can get automated pull request to get you started with the correct fix. And just like that, a pull request issue is created and patch received, error fixed and production is all good now. So check
Watch from 0:00out Linear today and actually save real time. The ad is the Linear origin. All right, welcome back. So before we get to the most amazing bit manipulation you have ever seen. Like my mind is I I I don't EVEN KNOW HOW TO DESCRIBE MYSELF. I'm giddy right now with how amazing it is. So before we get to that, let's start first with just deriving the very simplified general formula. And so the question we need to answer is what day of the week is the 57th day since the epoch. Apparently, this is a very common database date library question. Apparently, this also can influence compiler authors, all sorts of stuff. So, let's just start here. What day of the week is that? Well, it's a Friday. All right, so for us to start calculating that out, the first thing we need to do is let's put numbers underneath each one of our days. So, Sunday's a zero, Monday's a one, Friday's a five, Saturday's a six. And if we were just to just take the simplest formula, it would simply be whatever our day count passed in, say 57, modulo 7. The answer to that is one, which is well well, that's incorrect. Like that's we we need Friday, okay? We want Friday real real bad. So, this you can't just take modulo 7. We need to do something about it. But what do we need to do? Well, we need this right here. What day was the epoch on? Well, it was on a Thursday. So, we're going to go back to our original equation, and we need to add in the epoch starting day, which is four, which is Thursday. And so now when we redo the formula, it's a five, it's Friday. Hey, we got the right day. Look at that.
Watch from 1:31We're doing pretty good. I would say that this formula represents how we get the day of the week, right? And even doing a little bit of testing right here, this looks correct. Well, it's not correct. What the hell's this, okay? What what what am I looking at, okay? The moment we start using negative numbers, we get negative one. What day of the week is negative one there, tough guy? Your formula sucks. But hey, we're not quitters, okay? We don't even know what the quitterus is. So, we will continue on and figure out what's going on here. So, let's go back to our formula. We need to do something a little bit to this formula. The first thing we need to do is an extremely bad animation in which you're going to forgive me for, and this animation represents the change we need to make. So, let's go through it one step at a time. So, first, let's look at that. The day count. Day count modulo 7. This creates a value somewhere between -6 0 to 6. That makes sense, right? Because if we pass in negative numbers, we're going to go all the way down to -6. Hey, we're going to go down to -6, bro. And then if we go positive numbers, we're going to get a positive six. All right, that makes sense. If we include the plus seven, that shifts everything from being negative into being positive. Now, remember, adding seven and then moduloing seven has a special property, which is it does nothing. But now we've moved everything into the positive land. So, as long as we shift it over and we start from Thursday, boom, we got the correct result. And yes, this is actually the generalized formula, the good one that I created, not the crappy one you created, but the good one. This is it. This is what it looks like right here. It is day count modulo seven. We then we add seven, add the Thursday
Watch from 2:56offset, and then we modulo the whole thing by seven. This will work with both positive numbers and negative numbers. There's only one little problem here, though. We have two modulos. That's slow. We don't we you can't be having that in a database. We can't be having slow operations in a database. So, now let's get to the next optimization we can make. Now, this one, not the cool bitwise manipulation super neat stuff, but it is still kind of mine it's it's like super cool just cuz of like pure happenstance. In 2014, Henan's algorithm was released upon the world, thus being slightly more efficient than the previous version. Now, this one kind of comes with two parts and an if statement instead of multiple modulo operations. Now, the first one you should recognize. Anything that's greater than or equal to -4 can use our original simplified algorithm, which makes sense because, right, it worked all the way up till -4. As you can see right here. It works on Wednesday, it works on Tuesday, it works on Monday, it works all the way to Sunday, but then it doesn't work beyond that moment. So, that means we need a different equation for when it's more than -4. And that's what this thing is right here. Now, this looks kind of strange, and I think it's easiest to understand if we just kind of go by example. First, we're just going to get a value somewhere between -6 and zero. Then we're going to shift that entire value set from -6 to zero to zero go positive six with this plus six. But what makes this work out is that if you look at this value right here, you'll see day count plus five. So that means when I provided a negative five, it becomes a zero, and then we add six, which is our Thursday, which is a
Watch from 4:27negative five. That's our negative five offset. This works out because the first day is Thursday. -1, -2, -3, -4, -5. It works out. And so if you do it again, if you put in a negative six, that's a negative one plus six, that's a five. That works out right here. It's Friday. And so this just works simply because it aligns exactly with the day of the week, which is actually kind of cool. Just this simple plus six shifts everything over, and it also happens to align perfectly with the correct day. I don't know, I just thought that algorithm was pretty neat, but you know what? Honestly, not as neat as Neary's algorithm. Okay, now this, this is the moment that I was talking about where it just gets so I I I am still like look at me, I'm still I I I'm like a I'm like a beautiful bride on her wedding day. I feel so excited right now. By the way, strangest analogy I've ever made in my entire lifetime. I'm glowing, okay? You could mistake Am I pregnant? You could Maybe. Look at how much I'm glowing right now. Now I know a bunch of you are vibe coders, and binary can scare you, so let's just go over this pretty simple. It's very very easy. First off, here's what five looks like in binary. As you can see up here, these little values is what each one of these big values represent if they are a one. And so on this one, since the four is highlighted a one, and the one is a one, we get 1 + 4, which is five. That's simple, right? But how do you represent a negative number? We do something called two's complement, which is actually not even that hard to calculate. All you have to do is take the complement of the positive number, and then add one. The complement's pretty simple. If you see a zero, you write a one. Okay, 1 1 1 1. Okay, but if
Watch from 5:58you see a one, you write a zero. It's actually that simple. Look at that. Okay, so that's the complement, and then well, now you have to add one. Now I know again, I'm working with some vibe coders. This may be some advanced math, you know, things we're doing here. It's pretty easy. When you add one, you just go like that. Okay, it's really simple. You just put one on it. So, this is what a -5 looks like in a signed 8-bit number. This is important. This will come up very important in just a moment. Just hold on, okay? Just Just look at it, okay? This is like Bird Box. I want you to open your eyes and I want you to stare at just just look at it for a few moments. Now, when I say a signed number, what I mean is that this one right here, in a signed number, if that one is a one, it means that the number number is negative. Two's complement is a little bit more confusing why it all works out. Just trust me, it's super cool. Go look up Numberphile or something. I'm sure there's Sure there's some amazing YouTube videos on that. All right, so let's go back to Nieri's algorithm. Now, this U32 is very special. It only works out with a couple bit widths. U32 happens to be one of them, and also U8 happen to be one of them. So, now let's just do our example in U8. Let's take Let's take that that that positive five we had, and let's put it in the binary. If you take a signed number, and you just reinterpret it as an unsigned number, if it is positive, it's still the same value, right? That's because you can If you know anything about binary, that makes perfect sense.
Watch from 7:30But, what about a negative value? Now, remember, a signed version of it is this, which means that if you just reinterpret it as a non-signed number, it's going to be 251. Now, the easy way to do this equation is just do 256 + -5. Now, this is Okay, this is it. This is the magic part. Are you guys ready for the magic part? Okay, let's do the magic part. What is 256 modulo 7? That's right. It's four. Oh my goodness, like what What are the chances? U32 and U8 both happen to line up where if you take the modulo 7, it is four, which is Thursday in our little little time date thingy up there. Which means that if we did the -5, if we do -1, that's a Wednesday. -2, that's a Tuesday. -3 is a Monday. -4 is a Sunday. -5 is a Saturday. It actually lines up with the epoch. That means reinterpreting a signed negative number as an unsigned bit operation is identical to doing this. It's identical to doing that. I mean, this is the craziest thing I've ever seen in my lifetime. How How IS THIS EVEN POSSIBLE? HOW DOES SOMEBODY come up with this? This nearly fella must be a genius. Oh my goodness, I love this. It just makes me so excited. Like, how did he do that?
Watch from 8:49As you can see right here, this part makes a lot of sense because if we're dealing with positive numbers, we don't have that +4 offset. So, if date count is greater than or equal to zero, we need to add an additional +4 to make sure that we work out with our original simplified equation. So, the next part gets even more nuts. I'm not going to go over it. You should check out the blog post, but it does contain my favorite book in the universe, which is Hacker's Delight, which is just the most insane number and bit-wise manipulation fun stuff that you can possibly do on a computer, and it uses just crazy techniques and the weirdest constants in the universe to be able to calculate out this operation, and it even makes the claim that on certain machines, our machines, it can actually calculate out the day of the week based on the day offset in a single machine operation. It's absolutely nuts. It's a fantastic article. You should go read it. Anyways, I thought I'd get you guys excited, get you all kind of pumped up to go in there and understand some bits, you know? Like, just get just get bitted up, you know what I'm talking about? The name. By the way, did I don't know if you know this, but a byte, obviously, is eight bits. But, if you just take four bits, that's a nibble because, obviously, a nibble is a small byte. A gen.
Watch from 10:11Full transcript
- 0:00
There are two hard problems when it comes to computer science. That's of course is naming things and getting a date. Now I know from great personal struggles the difficulty of of course getting a date and I thought I would help guide you through the process. You know, really teach those young budding computer scientists and vibe coders what exactly it takes to get a date and go on
- 0:20
and live a happily married life. Now of course I'm talking about the fastest way to calculate the day of the week. Now this is one of the most amazing articles you have to see and we will first derive like the basic formula and then we'll see like a really cool just coincidental way to be able to make this work out and then the most insane bitwise operation
- 0:40
that even thinking about it just gets me juiced. And I made a beautiful presentation for this. And you guys are always asking me for some technical video so here is one. So I hope that you like it, okay? Smash the like button, okay? Press the subscribe, whatever they whatever whatever people say. But of course before we begin, a quick thank you from the sponsor. Have you ever deployed to production and everything is
- 1:01
green and awesome? But when you go to the website, oh my gosh, IT'S A 500? WELL, DON'T WORRY because Linear has your back. See, when that 500 hits your error reporting can create a Linear ticket which will kick off a Linear loop. So honestly, why spend 30, 40, 50 minutes of your time searching around with these production bugs when you can get automated pull request to get you
- 1:22
started with the correct fix. And just like that, a pull request issue is created and patch received, error fixed and production is all good now. So check out Linear today and actually save real time. The ad is the Linear origin. All right, welcome back. So before we get to the most amazing bit manipulation you have ever seen. Like my mind is I I
- 1:43
I don't EVEN KNOW HOW TO DESCRIBE MYSELF. I'm giddy right now with how amazing it is. So before we get to that, let's start first with just deriving the very simplified general formula. And so the question we need to answer is what day of the week is the 57th day since the epoch. Apparently, this is a very common database date library question. Apparently, this also can influence
- 2:04
compiler authors, all sorts of stuff. So, let's just start here. What day of the week is that? Well, it's a Friday. All right, so for us to start calculating that out, the first thing we need to do is let's put numbers underneath each one of our days. So, Sunday's a zero, Monday's a one, Friday's a five, Saturday's a six. And if we were just to just take the simplest formula, it would simply be
- 2:24
whatever our day count passed in, say 57, modulo 7. The answer to that is one, which is well well, that's incorrect. Like that's we we need Friday, okay? We want Friday real real bad. So, this you can't just take modulo 7. We need to do something about it. But what do we need to do? Well, we need this right here. What day was the epoch on? Well, it was on a Thursday. So, we're going to go
- 2:45
back to our original equation, and we need to add in the epoch starting day, which is four, which is Thursday. And so now when we redo the formula, it's a five, it's Friday. Hey, we got the right day. Look at that. We're doing pretty good. I would say that this formula represents how we get the day of the week, right? And even doing a little bit of testing right here, this looks correct. Well, it's not
- 3:05
correct. What the hell's this, okay? What what what am I looking at, okay? The moment we start using negative numbers, we get negative one. What day of the week is negative one there, tough guy? Your formula sucks. But hey, we're not quitters, okay? We don't even know what the quitterus is. So, we will continue on and figure out what's going on here. So, let's go back to our formula. We need to do
- 3:26
something a little bit to this formula. The first thing we need to do is an extremely bad animation in which you're going to forgive me for, and this animation represents the change we need to make. So, let's go through it one step at a time. So, first, let's look at that. The day count. Day count modulo 7. This creates a value somewhere between -6 0 to 6. That makes sense, right? Because if we pass in negative numbers,
- 3:47
we're going to go all the way down to -6. Hey, we're going to go down to -6, bro. And then if we go positive numbers, we're going to get a positive six. All right, that makes sense. If we include the plus seven, that shifts everything from being negative into being positive. Now, remember, adding seven and then moduloing seven has a special property, which is it does nothing. But now we've
- 4:08
moved everything into the positive land. So, as long as we shift it over and we start from Thursday, boom, we got the correct result. And yes, this is actually the generalized formula, the good one that I created, not the crappy one you created, but the good one. This is it. This is what it looks like right here. It is day count modulo seven. We then we add seven, add the Thursday offset, and then we modulo the whole
- 4:29
thing by seven. This will work with both positive numbers and negative numbers. There's only one little problem here, though. We have two modulos. That's slow. We don't we you can't be having that in a database. We can't be having slow operations in a database. So, now let's get to the next optimization we can make. Now, this one, not the cool bitwise manipulation super neat stuff, but it is still kind of mine
- 4:50
it's it's like super cool just cuz of like pure happenstance. In 2014, Henan's algorithm was released upon the world, thus being slightly more efficient than the previous version. Now, this one kind of comes with two parts and an if statement instead of multiple modulo operations. Now, the first one you should recognize. Anything that's greater than or equal to -4 can use our
- 5:12
original simplified algorithm, which makes sense because, right, it worked all the way up till -4. As you can see right here. It works on Wednesday, it works on Tuesday, it works on Monday, it works all the way to Sunday, but then it doesn't work beyond that moment. So, that means we need a different equation for when it's more than -4. And that's what this thing is right here. Now, this looks kind of strange, and I think it's
- 5:32
easiest to understand if we just kind of go by example. First, we're just going to get a value somewhere between -6 and zero. Then we're going to shift that entire value set from -6 to zero to zero go positive six with this plus six. But what makes this work out is that if you look at this value right here, you'll see day count plus five. So that means when I provided a negative five, it
- 5:54
becomes a zero, and then we add six, which is our Thursday, which is a negative five. That's our negative five offset. This works out because the first day is Thursday. -1, -2, -3, -4, -5. It works out. And so if you do it again, if you put in a negative six, that's a negative one plus six, that's a five. That works out right here. It's Friday. And so this just works simply because it
- 6:14
aligns exactly with the day of the week, which is actually kind of cool. Just this simple plus six shifts everything over, and it also happens to align perfectly with the correct day. I don't know, I just thought that algorithm was pretty neat, but you know what? Honestly, not as neat as Neary's algorithm. Okay, now this, this is the moment that I was talking about where it just gets so I I I am still like look at
- 6:36
me, I'm still I I I'm like a I'm like a beautiful bride on her wedding day. I feel so excited right now. By the way, strangest analogy I've ever made in my entire lifetime. I'm glowing, okay? You could mistake Am I pregnant? You could Maybe. Look at how much I'm glowing right now. Now I know a bunch of you are vibe coders, and binary can scare you, so let's just go over this pretty simple. It's very very easy. First off, here's what five looks like
- 6:57
in binary. As you can see up here, these little values is what each one of these big values represent if they are a one. And so on this one, since the four is highlighted a one, and the one is a one, we get 1 + 4, which is five. That's simple, right? But how do you represent a negative number? We do something called two's complement, which is
- 7:17
actually not even that hard to calculate. All you have to do is take the complement of the positive number, and then add one. The complement's pretty simple. If you see a zero, you write a one. Okay, 1 1 1 1. Okay, but if you see a one, you write a zero. It's actually that simple. Look at that. Okay, so that's the complement, and then well, now you have to add one. Now I know again, I'm working with some vibe
- 7:38
coders. This may be some advanced math, you know, things we're doing here. It's pretty easy. When you add one, you just go like that. Okay, it's really simple. You just put one on it. So, this is what a -5 looks like in a signed 8-bit number. This is important. This will come up very important in just a moment. Just hold on, okay? Just Just look at it, okay? This is like Bird Box. I want
- 7:59
you to open your eyes and I want you to stare at just just look at it for a few moments. Now, when I say a signed number, what I mean is that this one right here, in a signed number, if that one is a one, it means that the number number is negative. Two's complement is a little bit more confusing why it all works out. Just trust me, it's super cool. Go look up Numberphile or something. I'm sure there's Sure there's some amazing YouTube videos on that. All
- 8:19
right, so let's go back to Nieri's algorithm. Now, this U32 is very special. It only works out with a couple bit widths. U32 happens to be one of them, and also U8 happen to be one of them. So, now let's just do our example in U8. Let's take Let's take that that that positive five we had, and let's put it in the binary. If you take a signed number, and you just reinterpret it as
- 8:42
an unsigned number, if it is positive, it's still the same value, right? That's because you can If you know anything about binary, that makes perfect sense. But, what about a negative value? Now, remember, a signed version of it is this, which means that if you just reinterpret it as a non-signed number, it's going to be 251. Now, the easy way to do this equation is just do 256 + -5.
- 9:04
Now, this is Okay, this is it. This is the magic part. Are you guys ready for the magic part? Okay, let's do the magic part. What is 256 modulo 7? That's right. It's four. Oh my goodness, like what What are the chances? U32 and U8 both
- 9:24
happen to line up where if you take the modulo 7, it is four, which is Thursday in our little little time date thingy up there. Which means that if we did the -5, if we do -1, that's a Wednesday. -2, that's a Tuesday. -3 is a Monday. -4 is a Sunday. -5 is a Saturday. It actually
- 9:45
lines up with the epoch. That means reinterpreting a signed negative number as an unsigned bit operation is identical to doing this. It's identical to doing that. I mean, this is the craziest thing I've ever seen in my lifetime. How How IS THIS EVEN POSSIBLE? HOW DOES SOMEBODY come up with this? This nearly fella must be a genius. Oh
- 10:06
my goodness, I love this. It just makes me so excited. Like, how did he do that? As you can see right here, this part makes a lot of sense because if we're dealing with positive numbers, we don't have that +4 offset. So, if date count is greater than or equal to zero, we need to add an additional +4 to make sure that we work out with our original simplified equation. So, the next part gets even more nuts. I'm not going to go
- 10:27
over it. You should check out the blog post, but it does contain my favorite book in the universe, which is Hacker's Delight, which is just the most insane number and bit-wise manipulation fun stuff that you can possibly do on a computer, and it uses just crazy techniques and the weirdest constants in the universe to be able to calculate out this operation, and it even makes the claim that on certain machines, our
- 10:48
machines, it can actually calculate out the day of the week based on the day offset in a single machine operation. It's absolutely nuts. It's a fantastic article. You should go read it. Anyways, I thought I'd get you guys excited, get you all kind of pumped up to go in there and understand some bits, you know? Like, just get just get bitted up, you know what I'm talking about? The name.
- 11:09
By the way, did I don't know if you know this, but a byte, obviously, is eight bits. But, if you just take four bits, that's a nibble because, obviously, a nibble is a small byte. A gen.
Description
Linear helps teams track issues, manage projects, collaborate on docs, and ship products - and the crazy thing is their software is actually fast and a joy to use. Learn more: https://trm.sh/linear
Sources:
https://www.benjoffe.com/fast-day-of-...
/ theprimeagen - I Stream on Twitch
Great News? Want me to research and create video????: / theprimeagen
#theprimeagen #programming #computerscience #ai #chatgippty #terminaldotshop




