122: Data Types: DateTimes Part 5 C++.
Take Up Code - En podcast af Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Kategorier:
DateTimes in C++ require quite a bit of a learning curve. They’re still evolving and nowhere near to being full-featured or friendly to use. Up until C++11, the best you could do with the builtin date and time types came from the C libraries. C++11 defines the chrono library which allows you to specify durations, time points, and clocks. Think of a time point as a specific point on a number line. This is good because number lines have positive and negative numbers and that means there has to be some point on the number line that represents zero. All the other points are relative to this zero point. In computer dates and times, this zero point is called the epoch. It represents some point that all the dates and times are relative to. Different systems have different epochs. Here’s just three of the more famous epoch dates. There have been more and there still are more epoch dates being used. And there will likely be others. January 1st in the year 1. This is currently used by C#. January 1st, 1601. This is used in the Windows file system. January 1st 1970. This is the UNIX epoch and is used extensively today in a lot of languages including C++, Java, PHP, Python, and Ruby. It’s also used in Linux and Mac computers. You can think of a time interval as the amount of time between two time points on the number line. And a duration can be thought of like a piece of string that matches the length of some interval but is not attached to any specific point. So what’s a clock? You can think of this as something that places regular marks along the number line. This is like a drummer beating out a regular rhythm. When did the drummer first start beating the drum? You see, a clock isn’t just a bunch of regular marks. A clock needs a starting point. And that starting point is the epoch. Unlike a drummer, a clock can place regular marks before the epoch. These marks are called ticks. Chrono library has some nice abstraction. But it doesn’t know anything about leap years, or the fact that February is shorter than March. Listen to the full episode for more or you can also read the full transcript below. Transcript This episode continues the explanation of DateTime data types. We’re getting to actual implementations now and languages tend to have very different notions of how to deal with dates and times. This episode will describe the C++ chrono library. Up until C++11, the best you could do with the builtin date and time types came from the C libraries. C++11 defines the chrono library which allows you to specify durations, time points, and clocks. It’s not a general purpose DateTime library because it really has no concept of calendars or timezones. The clock is somewhat based on a calendar but not like you might expect. The way I think of a DateTime is a data type that can manage points in time, durations, and intervals as they all apply to various time zones and calendar systems. To me, there’s very little difference between a year, a week, a day, or an hour, minute, or second other than the length of time involved. The only real difference is that days can sometimes have leap seconds and years can sometimes have an extra day. We call a year with an extra day, a leap year. But we don’t call a day with an extra second a leap day. I’m always looking for inconsistencies like this. I think the reason is because leap seconds are fairly unnoticeable by the general population. Add an extra second to a day and the day remains mostly unchanged. But an extra day added to a year is enough to affect the year itself. Notice that I didn’t include months when I said that I don’t see much difference between the various lengths of time. That’s because months are variable. To be accurate though, the reason there’s such a big distinction between hours, minutes, and seconds which make up a time portion vs. years, months, weeks, and days which make up a date portion ha