banner

For a full list of BASHing data blog posts see the index page.  RSS


Are you 10000 days old yet?

Suppose you were born on 22 December 1995. Have you already had your 10000-day birthday, or is that still in the future? Here are three command-line ways to find out.


dateutils. For many date and time calculations, the easiest commands to use are the ones in the dateutils package, available in the repositories. In this case the right choice is the dadd command, invoked with really simple syntax:

dateutils.dadd 1995-12-22 10000

time1

Or with more familiar formatting

dateutils.dadd -i "%d %B %Y" "22 December 1995" -f "%A, %d %B %Y" 10000

time2

GNU date. One advantage of using GNU date is that it understands most date/time formats, so you often don't have to specify a format for the input string. date also understands simple operations like addition:

date -d "1995-12-22 + 10000 days" +"%Y-%m-%d"
date -d "22 December 1995 + 10000 days" +"%A, %d %B %Y"

time3

GNU AWK. For the hardcore AWK user, there are time functions. The first step is to convert the input date into seconds since the Unix "epoch", 00:00:00 UTC on 1 January 1970. This is done with the mktime function, which takes "YYYY MM DD HH MM SS". To avoid mktime using local time, add a non-zero value (like "1") so the calculation is based on UTC time:

mktime("1995 12 22 0 0 0",1)

Next, add 10000 days expressed in seconds:

mktime("1995 12 22 0 0 0",1) + 10000*60*60*24

Finally, format the result with the strftime function and get AWK to print it:

strftime("%Y-%m-%d",mktime("1995 12 22 0 0 0",1) + 10000*60*60*24)

time4

That's an ugly command. If you need a date/time result for some other operation with AWK, it's usually easier to do the date/time calculation in the shell, then to feed AWK with that result as an AWK variable:

time5

Last update: 2022-01-05
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License