First, let’s talk about the basics.
We all know the usual data types – strings, numbers, and booleans. They’re like the bread and butter of programming. Databases and programming languages have matured to the point where they can safely handle these data types, and we don’t think twice about how we store, retrieve and perform operations on them. They know that we know that there are a set of expectations, and it should work. And most of the time, it works.
But data types are not limited to these primitives. We have complex types like date-time, object, and array. Yes, I classified date-time as a complex data type.
You notice the nuances only when you build a system to work with date and time. This starts with understanding what needs to be captured and stored for a date-time field. The capabilities of the programming language and the database affect these decisions. The purpose of storing this information dictates the actions we would perform or at least want to perform on the data. And these activities determine the degree of information collected and stored.
Let’s take any random date – 15th Jan 2021
. It has information regarding the day, month, and year. A random time, e.g., 10:35 PM
tells us the hour, minutes, and on which side of the meridian the sun is. Even then, these two examples don’t paint the whole picture. And if we want to make sense of it all, we might even have to know the location too!
It would be best if you had the time and place to meet with someone on 15th Apr, 2023. And to meet someone at 10:35 PM, you would want to know the date and location.
That doesn’t mean that date and time independently can’t make any sense. It’s all about the context. 15th Jan 2021 might be when you bought your first car or got your driving license. The time information for this date is insignificant. It doesn’t matter if you got it at 10 AM or 12 PM. It might matter for a couple of days, but eventually, the time is excessive information in the context of when you got your first car.
Similarly, if I know today is 15th Jan 2021, and I’m rushing to catch a flight, I’m only interested in the time. 15th Jan 2021 is just unnecessary information.
Talking about operations, let’s take the simple case of addition/concatenation. You can add two strings (concatenate) or numbers.
'Hello' + 'World' = 'HelloWorld'
10 + 20 = 30
But dealing with date and time requires a certain finesse. You can’t just add two dates together and expect a sensible result. Nope, it doesn’t work like that. We don’t just go around adding random dates and times. It makes no sense. To a date value, you can add days, months, or years; to a time value, you can add hours, minutes, or seconds. But as simple as it sounds, it’s not that easy. Because if today is the 25th and I add 5 days to it, it can be the 30th, the 2nd, or the 3rd, depending on the month and the year.
That’s because date, time, or timestamp is not a homogenous information set. It holds multiple degrees or dimensions of interdependent information packed into a few bytes of data.
This is a fascinating problem from a software engineering perspective. The world likes to run as if it is on time, so you can’t have 30th Februaries and 10:62 PMs. There are some patterns that I have observed while working with date and time. In part 2 of this article, we look at what we should consider during design time, and in part 3, we will look at development and deployment time factors.
./J
This is the first part in a 3-part series discussing date and time while designing, building, and deploying solutions.