10 Dates and times

 

Programming languages’ core data structures reflect the types of information we work with on a regular basis. It makes sense that we’ll have numbers, because we use numbers a lot. We use lots of text, so strings make sense, as well. And of course we need collections of various sorts, so every language provides some of those—in the case of Python, we have lists, tuples, dictionaries, and sets, for starters.

Modern programming languages also support another type of data, one that we (as people) use on a regular basis but that wasn’t part of the programming canon when I started my career: dates and times. It seems obvious in retrospect that dates and times, which are such essential parts of our lives, should be a main part of our programming languages. But it turns out that dealing with dates and times is hard, with all sorts of tricky problems—from leap years, to time zones, to the odd data structures we need to computerize a calendar that wasn’t exactly designed with computers in mind.

Exercise 39 Short, medium, and long taxi rides

Working it out

Solution

Beyond the exercise

Exercise 40 Writing dates, reading dates

Working it out

Solution

Beyond the exercise

Exercise 41 Oil prices

Working it out

Solution

Beyond the exercise

Exercise 42 Best tippers

Working it out

Solution

Beyond the exercise

Summary