*nix dates
Something interesting that I found this week, with the GNU version of the date command you can do this:
$ date -d ‘yesterday’ +%Y%m%d
20070106
What’s special about that? Well, the standard *nix date command only supports three options (-a, -u, and -s) and optionally let’s you pass it a string dictating the format that you want it to display the date in. So, it makes it easy to find out what today’s date is, but how do we know what yesterdays date was? Because of that limitation I have this big library of functions that can be invoked in a shell script that perform some of the common time and date manipulations that we need in our scripts at work. The GNU version makes it too easy. For instance, let’s suppose that you need to pass the first and last days of the previous month in a script:
$ date -d “$(date +%Y-%m-1) -1 month” +%Y%m%d
20061201
$ date -d “$(date +%Y-%m-1) -1 day” +%Y%m%d
20061231
How about the start and end of next month:
$ date -d “$(date +%Y-%m-1) +1 month” +%Y%m%d
20070201
$ date -d “$(date -d “$(date +%Y-%m-1) +2 month” +%Y-%m-%d) -1 day” +%Y%m%d
20070228
While I’m talking about date and time stuff, nobody should forget that they moved up Daylight Savings Time this year in the United States to March instead of April. You need to make certain that your servers are patched to handle the new dates.
