This week I am teaching the LPI 101 course again and we covered the shadow file today (amongst other topics). I gave the students the exercise to manually edit the shadowfile and let an account expire on 01-01-2011. The manpage of shadow(5) stipulates that the account expiration date has to be “expressed as the number of days since Jan 1, 1970″. One of my students asked me how to calcuate this. I did not immediately know the answer and I did not want to use rely on any online date/time converter, so I went looking for a satisfactory solution.

1. Find the UNIX time / Epoch of the date in question

date -d ’2011-01-01′ +%s

Remember that these are seconds since 01-01-1970.

2. Calculate the number of days since 01-01-1970.

echo `date -d ’2011-01-01′ +%s` / 86400 | bc

A day is 60 * 60 * 24 = 86400 seconds. Notice how ‘bc’ will always be rounding down the number of days.

3. Perform a ‘normal’  round up or down for the number of days

UNIXTIME=`date -d ’2011-01-01′ +%s` ; printf “%.0f\n” `echo “scale=1; $UNIXTIME / 86400″ | bc`