CHAPTER 3Date and Time Manipulation

On occasion, the need for date math arises, such as when you're trying to calculate a time interval between events. The calculations seem easy enough because there are precise numbers of seconds, minutes, and hours in a day. It gets tricky, though, when you consider the fact that values have to roll over when, for example, Monday turns into Tuesday or June becomes July.

For instance, calculating the difference in minutes between 6:53 am and 7:04 am is easy enough: you can multiply the hours (6 and 7) by 60 for each value, add the minutes that do not make up the next full hour, then subtract to find the difference. But what if you want the difference in minutes between 11:57 pm and 1:13 am? This calculation ...

Get Expert Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.