Hello, and welcome to another episode of The Bike Shed, a weekly podcast from your friends at ThoughtBot about developing great software. I'm Joel Kinville, and today I'm joined by Sally Hall, colleague here at ThoughtBot. And together, we're here to share a bit of what we've learned along the way. So Sally, what's new in your world? Well, you probably know I love knitting, but I love other crafts too. And recently got the opportunity through this, there's this project called the Loose Ends Project that connects people who have an unfinished craft project left behind by like a loved one who passed away or someone who's not able to do the craft anymore with crafters who can complete that for them. So I got matched a couple of weeks ago and have been working on a hand pieced quilt that someone started in the 60s and is made out of like old clothes from the 50s and 60s and has been sitting untouched for like 45 years. And so I'm hoping I can.
finish it up for the family that it belongs to. It's been fun. Do you have any prior experience with quilting? Yeah, I've been quilting for a while. You know, I'm the kind of exciting person that when my friends were going to clubs in their 20s, I was going to quilting classes with local old ladies. Maybe made you less cool back then, but I think now probably makes you a more well-rounded person. Yeah, we'll go with that. How does quilting contrast with knitting? You mentioned that sort of your more go-to craft.
Quilting, I think for me, tends to be kind of a slower craft. I like to do hand quilting, so I'm not sitting at a sewing machine. And so I'm sewing all the little bits together by hand. There's not a lot of immediate gratification. I don't expect to like, oh, I'll make this quilt for something that's happening in a few months or as a gift for this event. It's like, I'm going to work on this quilt and it will be done when it's done. That might be six months or it might be 10 years and we'll see. Whereas with knitting, I generally find that to be a quicker turnaround, smaller things. I'm also a lot more portable. You can't really bring a king-sized quilt on a plane.
I'm just picturing that and that would be very entertaining. The largest quilt I brought on a plane was for a crib. I know people often like to make comparisons between writing code and more traditional crafts. How do you feel about that comparison? Do you think there's a lot of similarities? Oh, absolutely. If you've ever looked at a knitting pattern, if you have never knit before, it looks like complete gibberish. But once you learn sort of the syntax of a knitting pattern, you can start to read it better. And there are, I think often a good pattern is written in a way that can be similar to a good piece of software. There are little bits that you can extract and repeat. There are bits that you can put together to do different things. It's sort of not just a like start here and go to the end. There's some control flow and stuff like that in a good pattern. And a lot of math.
Oh, I'm sure. Now I'm imagining something almost like a linting program to run over knitting patterns to make them fit a style guide or something like that. That's actually a great idea. I'm going to be thinking about that for a while because I think that could be really useful. There's a lot of tech editing and testing and trial and error and feedback periods for these patterns, but I think it would be amazing to have a linter that could just go through and be like, okay, this step you said you had 45 stitches and then increased 10, but now you're saying you should have 60, but it should be 55. Things that a computer could catch. People could spend more time doing the knitting rather than tedious editing.
Do you find that patterns typically all follow a sort of standard convention in terms of how they're written and expressed? Or are there different sort of schools of thought or styles? You're like, oh, well, this is using the East Coast style and this is the European standard or something like that. I don't think there are differences like that much. I think it's more, there are abbreviations that tend to be used commonly. Like if you see the letters SSK together, that always means slip, slip, knit, which is a specific kind of stitch. I think it's more, the differences are more.
similar to how difference is in people writing code. You know, like we can both write code that does the same thing and it's both right and it might even both be good, but they're going to be different because we have different styles and different approaches. And some of those are going to be more readable for some people versus others. So I do tend to like find a designer whose patterns I find readable and easy to follow and then tend to use more, like purchase more of their patterns because I know that the way they write patterns is sort of the way I think about it. One of my favorite designers has so much math in her patterns and sometimes even has like little spots in there for like, it's like a little math worksheet for calculating like, okay, for...
The specific way you're making this, here's actually the numbers that you need. I feel like the equivalent of that is programmers that love to use macros or metaprogramming or something. You can make this program as customized as you want because you can inject all these executable bits into it. Yeah, it's definitely just like that. I don't know if you ever read the blog post titled, If Hemingway Wrote JavaScript? I own that book. Oh, you own the book. I love that. I do.
I think it's right here, actually. Yeah, I love that book and the idea of code in the style of various authors. I love it because it really highlights how much of programming does have a lot to do with style and in some ways could be considered art. So for our listeners, there's a blog post and then a broader book titled If Hemingway Wrote JavaScript. I'll link it in the show notes. And the idea is basically, let's look at some programs written in what we think is the essential style of a classic literary author. So if Shakespeare were trying to write it versus Hemingway, how would they write this differently? Yeah. And I'm now thinking in the style of that.
Would there be a version of this that you could do for famous knitting pattern designers? Oh, I bet there could be. I mean, I think famous knitting pattern designer is kind of an interesting idea. I don't know who would be considered like the Shakespeare of knitting patterns, but they're absolutely, yeah, I think you could take some patterns and like rewrite them three or four different ways to fit the way that different people write patterns for sure. So as of the time of recording this, this is the week after changing to Daylight Savings in the US. And I'm curious on your current project.
Did you run into any daylight savings bugs or did your test suite fail in the week leading up to it? It did. In the week leading up to it, we had some test failures for sure. The app I'm working on currently is a medical app. So there's a lot of scheduling appointments and finding available times for appointments. You know, like the easy way to talk about it is to just say like, oh, well, write everything in UTC and none of it will matter. Right. But we have situations where it's like the provider will say I'm available from 8 to 3, Monday, Tuesday, and Wednesday in my time zone. Right. And 8 a.m. two weeks ago.
in Eastern time zone is different than it is today in Eastern time zone compared to UTC. So we definitely had some tests that started failing because things were an hour off. Thankfully, what we found was it was just the way the tests were written and we didn't actually discover any bugs in the actual application code due to the daylight savings time situation, which is good just in tests. That's another classic, right? That the code is good, but the tests are not written in a way that's resilient. Yeah. What is your sort of preferred way to write tests around time zones or daylight savings such that you find actual bugs but don't get the false positives?
One thing I like to do is that when I'm first writing a test and I know that the time zone is going to matter, I try to write the test using a time zone that is not mine and is not the one that we run CI. So I don't want to write it in UTC. I'm on Eastern time. So often I'll just choose Chicago time because that way I know if my tests and the application and the browser and everything are not all handling time zones the way I expect them to. It will fail locally rather than me writing everything and it working great because I've asked it to use Eastern time and my system is in Eastern time and my database is in Eastern time and then I put it in CI, which is in UTC and everything explodes. So that's my first step is don't write it for my own time zone if I need to specify a time zone. This is almost like the opposite of works on my machine. You want it to breaks on my machine. Yes, I want to make sure that, yeah, if it works on my machine but not in CI, it's not because of time zones, hopefully. I feel like...
I'm at the point where I've fixed enough broken tests or even had to deal with broken code in the week leading up to daylight savings. I'm looking for ways to have that not happen at that moment in time. And so I'm gravitating towards an approach where I freeze time to eliminate that, but also add test cases for the daylight savings boundary. That way, if there is a bug, a daylight savings bug, I don't want to stumble across it because the calendar has moved around. I want it to fail today in CI or even locally and not sort of rely on the real world to...
sort of accidentally trigger the vulnerability. Yeah, I totally agree with that. When you, like, my hesitation for freezing time in tests is that often then the developer will never stumble onto the thing they forgot to think about, right? And so, you know, as people who have been doing this for a while have stumbled onto those things a lot, I think freezing time and then making sure that you test the edge cases, the daylight savings, boundary crosses, things like that is a great strategy, I think. If you're just starting out or if you're not super familiar with time stuff and you have no idea what those boundaries are or what the extra cases you are going to need to do. I mean, certainly you can do some research, read some blogs, find some things. But I think in some of those cases, it can be preferable to not freeze time so that every time you run your test suite, you're testing a sort of a different edge case and can find some little weird things that you didn't account for. And then every time you find one of those weird things, write a frozen time test to account for it once you've fixed it. Yeah, yeah. So use sort of the non-frozen one as a sort of barometer. Yeah. Wait six months until you hit a time change.
encounter that and then use the ways it breaks to write the real sort of frozen tests that you want. Yeah. And these aren't, I think the boundaries aren't all just like daylight savings time either. You know, that's when you cross like a week boundary or a month, you know, if today is the last day in a month and tomorrow is the first day in a month, is that going to be different than if we're looking at a week that's all in the same month? Correct. Year boundaries are also a thing.
Leap years. Yes, leap years. All the assumptions that we have about the size of time periods that are not actually true. Like a year is not always 365 days. A day is not necessarily 24 hours. Where if you're doing math and you're saying, oh, we'll just add 24 hours to get to the next day. Or shifting an appointment, as you're saying to say, I want to shift an appointment next week at the same time. The naive implementation is, oh, just add seven times 24 hours and that'll shift it. That will work.
Most of the time, except that not all days have 24 hours. If you're going across a time boundary or a daylight savings boundary and then you hit bugs. Yeah, I find that if I am trying to write a custom method to do some time calculations, that is a big sign that I need to stop. Think about what I'm actually doing and see if there's a library that already handles it. Because time is such a finicky thing and there's so many edge cases that we don't consider that it's almost always going to be better to find a reliable library that's already solved the problem than to write your own converter from daylight savings to British daylight savings.
Have you seen the Tom Scott video on time zones? Possibly, but I have a terrible memory for names, so I'll need a little more context. He's a YouTuber who talks about tech stuff. This was made maybe eight or 10 years ago, so it's an older video. I forget if it was on his own channel or on the Computer File channel on YouTube. It's almost like a rant about every reason why time zones are awful. Yes, I have seen this. I think one of my kids actually showed it to me. I've got teenagers who are into gaming and coding. He's sort of like, every time he adds an edge case, he just sort of gets visibly more frustrated. At the end of it, he's like, and the only solution to this is...
You don't try to do this yourself. You find an open source library that somebody has done this for you. You're very grateful to them and you use that and you don't do time math yourself. Yes, that is correct. That is the answer. Which feels like a silly way to talk about it for people who work at a company that likes to build open source stuff and solve problems and things like that. And I guess I wouldn't say never, ever try to solve your own time problem.
But only as a last resort. I feel like it's a little bit like rolling your own authentication. Yes. The general recommendation is don't roll your own. And we should probably make that a thing for time as well. Yeah. But time is so deceptively simple, right? It seems really simple. We all graduated kindergarten. Yeah, I can read an analog clock and a digital clock. I can do some basic time math in my head. And I think our intuitions for it is it's relatively simple. It's a thing that everybody can do. And then we don't realize all the edge cases and get ourselves into trouble. Yeah, I was actually talking about this at a family dinner the other night.
My brother is a designer who works in tech and it was, I guess it was the weekend of the time change and someone brought up being tired or something. And I, my brother and I just started going on this sort of like rant about like, oh, you think that's the worst thing about daylight savings time? Like, do you work with someone in the UK who for the next couple of weeks, your time difference is going to be one hour different than it is for the rest of the year? Or someone in Arizona who doesn't have daylight savings time or someone in that part of Eastern Canada where their time zone is a half hour off rather than a whole hour off? This is a little bit like the falsehoods programmers believe about time. Yeah, I think.
One of the absolutes of time is that there are no absolutes. There's no simple way to just be like, do it this way every time and you will always be okay. You know, like always use UTC. We'll get you 95% of the way there. But there are some times where you do have to think about what different times look like in different time zones. I think on the theme of letting libraries do the work and not trying to roll anything on your own, Rails is really good at working with time zones. And for the most part, you shouldn't have to deal with any sort of settings or do any extra logic to be able to handle time zones correctly.
I think that is true when you were talking about timestamps. I think when you start to work on something that deals with time of day. Yes. So rather than, you know, March 14th at 3.30, Fridays at 3.30, or the first Friday of the month at 3.30. At that point, you're going to have to know what is that in the, like, what's the context for that time. Right. And Ruby doesn't have a built-in object for dealing with that. Depending on your database, you may have some native type that can handle those sorts of things. I think Postgres has a time of day, which is implemented as, I think, microseconds since midnight. Yeah. In general, it's not as super straightforward.
problem to solve. This is actually something that I'm working on a gem right now that is in very early stages for solving a time problem that I didn't find an already made solution for, which is I have these doctors who all have various schedules that are time of day and day of week. And sometimes they have repeating events that are time of day and day of week. Like every Tuesday at three o'clock, we have a staff meeting. And then I also have appointments that are already scheduled for patients. And I need to figure out what times providers are available. I need to be able to display every time slot that somebody could schedule an appointment.
Which feels like a problem that has been solved, but as far as I can tell, it has not. And it turned out to be way, way harder than I thought it was. Because the number of time slots that someone can book an appointment in is not the same as the number of appointments a provider can have during a day. If you say that they can start any 15 minutes, there's an 8 o'clock time slot and an 8.15 and an 8.30 and an 8.45. And if those appointments are longer than 15 minutes, you're not going to have one at every single one of those. But if nothing's been booked, those are all available slots. Right, right. So a slot and an appointment are not the same thing. Right. A slot is a...
starting time for which the duration of the appointment after it is available. I see. And an appointment is a specific, this is happening at this time for this duration. Yep. So slots are kind of like potential appointments. What about, so let's say appointments are an hour to keep things simple. I have an appointment at noon. What do the points, say 1145 and 1130 look like? Would those be described as slots or are they just sort of empty space? Those would be described as unavailable time slots. Okay. Because you can't schedule something at 1130 that lasts an hour when you have something that starts at 12 and lasts an hour.
So there's calculating potential time slots and then calculating which ones of those are actually available based on things that have already been scheduled. So you mentioned that you're writing a gem around some of the intricacies of this different kind of time. They're not points in time, but rather potentials, times of day, days of week, that kind of thing. Is that gem currently public? I mean, the repo that it is in is public. If you go there, you will not see anything helpful yet.
Fair enough. I guess we'll just have to have you back on the show when it is in a more mature phase and you can talk a little bit about the gem itself and what it does. Yeah, I can talk about what it's supposed to do for right now. It's right now, basically what I've done is just extracting the way I solved this problem in a specific case that can be used in a more general case because it's the second time I've needed to solve this problem. And the first time we didn't solve it and ended up just changing the scope of what we were building to avoid this problem.
So what I've got is it's actually going to be a generator that will generate a database view using the Scenic gem. And that database view will calculate, given provider schedules and appointments and recurring events and all of that, will generate time slots, available time slots for a certain period. Oh, I like that. You know, the first time we tried to solve this problem, we just used Ruby and it took forever. It was literally like 45 minutes to figure out what time slots were available. And like, that's just not, there's no web application where you can have a thing last for 45 minutes when someone's trying to schedule an appointment. So then the team realized that, you know, Postgres does a lot of good stuff with time.
So how much of this can we make Postgres, make a problem for Postgres? The answer turned out to be a lot. And we were worried at first that storing all of the available appointment slots would be too much in the database because I think, I want to say it was like 8 million possible time slots with the number of providers and the way their schedules are right now. And the organization is growing, so that will increase. But going back and forth, we realized, you know, having a materialized view with 8 million time slots that we refresh pretty often actually turned out to be the most efficient way to do it. You know, like a big database table is cheaper than a really long running.
Ruby method. And I like the idea of view for something like that too, because otherwise, if you're storing all of these, you're having to regenerate that table all the time anytime somebody books an appointment. Well, we are using a materialized view, so we are regenerating it every time somebody books an appointment, which turns out to be not terribly often. We did some stuff with, we're using good job to handle jobs in this app. And there's some good configuration you can do around concurrency so that we don't enqueue the same job 16 times in a row. And the first one's not even done running before the last one starts. So we made it so you can only enqueue the job to refresh the view. You can only have one job running or enqueue at the time. So we're not...
Basically kind of debouncing the refresh there. Right. That makes sense. So has a lot of your work around time stuff then been sort of separate from traditional sort of timestamp-based development? Yeah, which is why a lot of my like maybe unpopular opinions or spicy takes on time are related to like, yes, but we don't have just timestamps. We have time durations. We have times of day. We have days of week. Yeah, it's been a lot. A lot of my time work has been around things like that, which comes up a lot in scheduling and healthcare apps. I think that was a really big realization for me as a younger developer, the idea that time is not a single kind of thing, but there are sort of multiple things that in our human heads we...
allow to sort of all meld into each other in the concept of time. But for a computer, they're very distinct things. And so when we need that clarity, they just all work very differently from each other. Yeah. And I think this is an area where like the non-coding related skills of a developer can really come in handy, right? Because if you're talking to a product person or a user, the way they use the word time is going to mean, you know, all those different things. So being able to have the conversation and go back and forth and say, okay, when you say that this API endpoint is going to return what time something is happening, is that a timestamp? Is that a time of day? Is that a duration? Is that a feeling?
What does time actually mean in the context of this ticket? What does time actually mean in the context of these requirements? And that's the kind of thing where like ultimately when you get down to writing the code, often it's very simple, but it's the difficult part is all the conversations and the clarifications to make sure that you're using time the way that the users and other stakeholders are expecting it to be used. I gave a talk at RubyConf San Diego in 2023, looking a little bit into breaking down time and what there are different types of time and how even different math operations don't all make sense. Like you can't add two timestamps together. That's a nonsensical thing to do.
You can, however, add two durations together. That's just a larger amount of time. And you can subtract two timestamps, and you get a value back that's not a timestamp, it's a duration of the distance between those two points in time. Yeah. Which I guess if you're deep into time and you're working with a lot of that, that might seem really basic, but when you're encountering those concepts for the first time, that is really mind-blowing.
Yeah. And it's a really, I think, unusual situation because usually when we're talking about mathematical operations on different types of objects, usually if you can add, you can subtract. Right. Or if you can subtract, you can add. Those are generally kind of thought of as variations of the same operation. But with time, like you said, adding two timestamps doesn't make any sense. Subtracting two timestamps does make sense. But you get back a different kind of object. Yeah. Which is also a little bit different than regular arithmetic. Yeah. It's going to be, you have to do an operation on two of the same units and then get a totally different unit. My understanding is that there's a whole sort of mathematical class of objects that behave this way.
They're called affine fields or affine units. I don't fully understand, but it's effectively vectors that are not anchored to a zero point. Another unit type that behaves a little bit like time is temperature. Yes. In that, leaving Kelvin aside, the zero point is just really arbitrary and everything just offsets from that. And so there isn't really a sense of doubling temperature. You know, if it's 50 degrees Fahrenheit, 100 degrees Fahrenheit is not twice as hot. But like culturally and like everyday speech.
There is a concept of like, you can say it's twice as cold as it was yesterday. And we both know that we don't mean that mathematically. We just mean however unpleasant it was yesterday or pleasant, depending on your perspective, it is twice as unpleasant or pleasant because of coldness today. But you can't actually mathematically do that. Now you can compare, just like you can get a duration that's sort of a distance between two points with time. You can also do that with temperature and say that between yesterday's temperature and today's temperature, there is so much distance on the scale. And it's an arbitrary scale so that the number is going to be arbitrary. But you can say, oh, there it is.
30 degrees Fahrenheit warmer today than it was yesterday. And the tricky thing with temperature that we don't get as much with time is that we use the same name for the units, even though now it's a different type of unit. It's kind of like a difference between a timestamp and a duration. Well, yeah, you're right. We do just call those degrees in both cases. We don't do as much temperature math in the code that we write as with time, but it has the same sets of problems. Yeah. I bet there's someone out here there who does do that much temperature math in the code that they write in.
I definitely once was working on a project where we did a lot of energy math with kilowatts and kilowatt hours. Okay. And I do not remember the exact distinction, but I think a kilowatt hour is a specific amount of energy and a kilowatt is a measurement of energy across time, which is like the opposite of what it sounds like. Right. Because it's a kilowatt times hour. And when we think of things over time, it's a...
something over time. Yeah. So a kilowatt hour over hours, the hours cancel out. So a kilowatt is your energy over time. At least that's how I'm interpreting what you're saying here. That sounds reasonable. This was a few years ago, so I don't remember the exact details, but I do remember that being a common cause of bugs was mixing up kilowatt hours or kilowatts because they sound intuitively the opposite of what you would think they are.
Units of measure, I find, are a really fascinating area that I think, as developers, we often don't pay very much attention to. And sometimes that gets us into trouble. And especially, you know, some of these things seem simple, localized. And then as soon as you start talking to somebody in another country or even another part of the same country or another continent, it gets entirely different and not just with time. We here in the United States use bizarre units for things in some cases where people in the rest of the world measure things differently. And so converting from one to the other or having like just a gut understanding from one to the other.
can get complicated. On my current project, we fixed a bug this week relating to capturing people's height in feet and inches. And then some of the math we were doing with that because we had two numbers and we were not, I think we used the feet when we should use the inches or something like that. And so that was a thing we had to fix. And so even just units that come in sort of a compound form can be tricky to work with. Yeah, I was reading a book where two characters were trying to convert a weight in kilograms to stones because they were in England.
which is, I think a stone is 14 pounds and then a kilogram is like two point something pounds. And it was just wild for me to like think about that because neither of those measurements are intuitive to me because I generally think in pounds. So the way that like I would go about doing that and thinking about like the way someone who mostly thinks in stones would go about doing that, it's like totally different. And, you know, if you're working in stones, then you've got the two measurements, like with feet and inches, you've got stones and pounds. What I found...
works best anytime you have a composite measure is to normalize everything to a single number in the smallest unit that you care about. Yeah. So stored in inches or pounds or pennies. Because that makes the math so much easier. It does. You don't have to worry about overflowing one into the other and all of that. And really, it is a single quantity. We just have it as two numbers because it's more convenient for humans to read. Yeah. And I guess that's how we store time, too. We're storing time as seconds since a certain point. Yeah. And then to display it to a human, we're going to turn it into day, month.
year, hour, minute, second, which is, now that we're talking about it this way, a really complicated way to talk about that. You break it up into like four or five different resolutions and give different numbers for all of them. Yeah. I guess it's because we're not good at dealing with large numbers as humans. That's true. The difference between million and billion just is fictional to most people. Look up the timestamp for 10 years ago, the second since 1970, versus the timestamp for today. I couldn't look at that and be like, oh yeah, there's about 10 years between them. No, I couldn't either. I was like, oh, that's two big numbers. That looks about the same to me. Yeah.
I wonder if there are people whose brains actually work better that way, who's like, you know, seeing it in all the different units that we use every day. The difference between that is hard for them to sort of fathom. But if you see it in numbers, like seconds since 1970, that would be interesting to have a brain that works better that way. Some of that might also be cultural. Yeah. Whatever system you were taught in kindergarten is what you're going to be more intuitively good at. Yeah. The way that like the order of month and day in a date, everyone believes that the way they were taught as a child is the correct intuitive way. Month, day, or day, month. And really, depending on your perspective, they both make sense.
Really, we all know that ISO 8601 is the one true date. Yes, the one true date. Putting the spicy takes out there. I'm curious on your thoughts about the idea that if you don't think about storage, if you think about in a programming language like Ruby, time zones more or less don't matter. Converting to UTC more or less doesn't matter. This is something I've been thinking about because if you think about it, the way a Ruby time object works is that it stores a number of milliseconds since 1970.
And if I give you a point in time in UTC, or I give you a point in time in EST, or I give you a point in time in Australian time, what the value shows on the clock will be different. But that number of milliseconds since 1970 will be the same, because they're all referencing the same point in time. And so that object under the hood is the same object, and they're all sort of double equalsable with each other. They are. I think the tricky part comes when you want to communicate that time to a user and what information is important for the user in that context. Yeah, so I'm a fan of localization. We say when I render it out to a string. But sometimes you have to care about time zones before you hit the browser and do localization. Because sometimes you want to group things by day of the week or by segment of the day, right? If I want to show you, I only want to see morning appointments. Don't show me anything in the afternoon because I'm a morning person and I want to filter out just to the morning appointments. Well, what does morning mean to you? Where do you live?
And, you know, if you're doing the filtering or the sorting or the grouping on the back end, which I often am, then you do need to know a little bit about the time zone there. I've run into this when generating, I think they're sales reports grouped by day. So basically give you the number of dollars you made each day, but for something that was worldwide. And so you could just say, I'm going to arbitrarily use UTC here and everybody gets their sales numbers per British day. Which doesn't mean a lot. You're in Asia. It's never trying to break it down by local day, but that means that we need the time zone information.
at the database level. So the timestamp might be stored in UTC, but we're saying for someone viewing these accounts who has said, my account is in this location or my store is in this location, we need to then group those totals by day based off beginning of day, end of day in their time zone. Yeah, I'm starting to think that time zones don't matter until you get to the point where it has to mean something to the user. And oftentimes, and still don't matter at that point, but like we're talking about, you know, if it's, if I'm looking at a report of daily sales, it's going to be a lot more meaningful to me if that report is in my time zone. Right. It starts at the end of my time zone, or if I'm trying to decide when to choose an appointment.
It's going to be more meaningful to me if I can group and filter by date or time of day on my time zone. And the thing that's not relevant to users is the conversion to UTC. Right. I think the advice we all get as young developers is you get a timestamp. The first thing you want to do is convert that to UTC. And if you're in a language like Ruby that has time objects, I don't think you need to do that. Yeah. They will let the time objects just normalize it for you by having that Unix timestamp under the hood.
I've actually seen bugs that get caused where people try to, you get timestamp object in Rails, and then you try to calculate an offset and manually add time to it before you save it to the database, but then you've accidentally shifted time too much and that causes a lot of bugs. And even if you calculate it correctly, you're kind of throwing away a piece of information. If you just totally get rid of what time zone that came to you as, maybe you don't ever need that information, but maybe you do. And so if you convert everything to UTC and save it like that and throw away the time zone, you're just throwing away information that might be necessary. I think the adding, though, the offset is it's not just you did the math wrong and the offset should have been a different number. The offset you need to add is always zero. Because when you're adding time to a Ruby time object, what you're doing is shifting that UTC timestamp or the Unix timestamp further into the future. Right. And now it's a different time. Yes, exactly. So you never need to add time object I got in Ruby plus three dot hours or something because that's the time offset. It's already happened automatically. You should not be manually adding because you're just putting in wrong times. I think it's Rails has.
you know, a method in time zone and then you pass it a time zone and then you can get it in the other time zone. Right. Then you're not adding or subtracting time. You were just looking at it in a different time zone. That's a safe way to do it. Yes. In fact, I think under the hood, the sort of simplified version of what's going on there is that you have an object that has an integer, a number of seconds since 1970. And then you have some kind of like label, a string that says what time zone is associated with this. And when you switch it from one time zone to another, the number doesn't change. All that changes is the string that's like metadata associated to it to say, hey, we received it in this time zone or this is the time zone we want to render it in.
There is no math that happens because no math needs to happen. The time zone is purely some contextual metadata that's tagged on. But that metadata matters or can matter or often matters. Yes. At some point, there is like second degree math, like we're talking about earlier, grouping, that sort of thing where you might care about the time zone. Or at time of rendering, you might care about the time zone. Yeah. As we wrap up here, I'm curious, do you have a sort of time zone bug that haunts you that you sort of keep coming back to as like a, this is how time math goes wrong? Yeah. And it's actually the grouping. And one of these healthcare apps I was talking about with the scheduling, you know, in order to see...
different available time slots, you often want to just show me everything that's available on this day, right? And so then we're just showing you the things on this day or we're showing you the things for this whole week, but broken down by day. And if you don't take the user's time zone into account there, it can get really confusing, especially if you are doing localization at the browser. Right. You'll see a display of times and it will start with 7 p.m. And there'll be like 7 p.m., 8 p.m., 9 p.m., midnight, 1 a.m. It's like, how is this all the same day? They're all listed as the same day. And then sometimes like your mind first goes, these are out of order.
And so the bug first gets reported as these times are being displayed out of order, when in reality, they're actually just being grouped incorrectly. Right. In a way that when you localize it, the order looks wrong. I can imagine that's a tricky one to get to the root of. Yeah, it really can be. Now I've just had the horrible thought of a situation where you're saying you're working with a lot of doctors and finding appointments. You're localizing for someone who's in one time zone, but their doctor's in a different time zone.
So someone who lives, let's say, in Indiana, going to Chicago to see a doctor. And so delocalize to the client's browser, or delocalize to the place they're going to their appointment for? We talked about that and landed on localizing for the client's browser, but also displaying the time zone next to the timestamp all the time. So never just displaying nine o'clock AM, but displaying nine o'clock AM EST. So that then hopefully there's a little less confusion of like, is this my time zone or is this my doctor's time zone? It's a little bit how airlines, if you book a red eye, we'll put a little like plus one next to the time. Just like, hey, just so you know, this flight lands on a different day than you depart. Yeah. And the landing time is the landing time where you'll be landing, not from where you took off. Oh, there's some complicated math in those for sure.
kind of the origin of time zones, right? Not for flights, but for railways. Yeah, that like, everybody says it's just noon when the sun is overhead, but now that we're moving from one place to the other and the time matters, we've got to find a way to mathematically translate my noon to your noon. To write timetables that are actually useful to people. Yeah. I was thinking about, and this might be quite a bit of a tangent, but I was thinking about the New Year's Eve on 1999. Like, I think we are roughly similar ages, so we're like teenagers when that happened, or at least, you know, old enough to be aware that it was happening. And didn't think a lot of it. It was like, okay, this Y2K thing sounds interesting.
but ultimately it's just another New Year's. But then I was thinking about how very few people will ever experience that. And like a thousand years ago, you know, when it went from 999 to 1000, calendars and time wasn't standardized across the world. And people, you know, the average person did not have any need to know what day it was and would just wait until the priest told them it was a feast day and, you know, wait for the seasons to change. And so the idea that like, we may have experienced the only time in human history where we actively were aware of it switching from one millennium to another and cared about it and like mattered.
And a thousand years from now, who knows how people will care about time and if that will ever happen again. Like it could truly have been not a once in a lifetime, but a once in humanity event. It might even be a different calendar. Right. Yeah. So a thousand years from now, the concept of a millennium might not be relevant at all. Now I'm imagining 8,000 years from now when we hit the 10,000 number. It's a nice round number. Some conspiracy theorist will be like, oh, in this ancient calendar that people used to use, you know, predicted the end of the world in the year 10,000. And everybody else will be like, what are you talking about? Why are you?
dividing the year into arbitrary groups of between 28 and 31 days. A little bit like what we do sometimes with a Mayan calendar. Yeah. Calendar systems, timekeeping systems, they're all really fascinating. It's really fun to see the different ways that humanity has come up with to manage that. Partly because it's astronomical, but it's different astronomical phenomena that don't really line up. And all the clever mathematical tricks or abstractions we've created to try to make them line up. It's a bit of a tribute to human ingenuity. Yeah. And to our ability to measure those things. Yeah. Like who you've never thought about a year before and you've decided to figure out how many days are in a year.
How do you go about that? How did you measure that? How did you determine that? Or did you not measure it and just decide it? How much of it is a measurement that reflects true reality and how much of it is a decision that we made to make our lives easier? True. I mean, now we have some pretty close measurements in terms of...
What's a year? Although we have multiple definitions that are like all slightly varying. You're talking about a solar year, a sidereal year, an astronomical year. Yeah. And like now that we've, you know, codified the way we think about time in all of our systems and computers and the way the world works, you know, innovations in measurement and our ability to understand what's actually happening with the movement of planets and stars and whatnot is not necessarily going to change the way we think about time, right? Like how big would a discovery about the way time actually works have to be for us to change calendars? Yeah. It's now a social thing with its own life separate from the stars. Yeah.
On that note, let's wrap up. Sally, thank you so much for joining and chatting about time and knitting and everything in between. Yeah, thanks for having me. This was really fun.