Artificial intelligent assistant

How to generate a year-quarter date from the command line? I'm interested in outputting a representation of the current year-quarter, as well as the year-quarter for the previous month. If today is 2012 January 1st, I'd like to get 2012q1 and 2011q4 as the respective outputs.

One (kinda ugly) solution, using BASH arithmetic evaluation and the GNU `date` command:


echo $(date +%Y)q$(( ($(date +%-m)-1)/3+1 ))
echo $(date -d "-1 month" +%Y)q$(( ($(date -d "-1 month" +%-m)-1)/3+1 ))


Note that the `%-m` prevents `date` from 0-padding, so this will still work for August and September.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 13adb5656d5e5c71d8e8f013efadb5e2