I have to perform some date calculations and conversion in a shell script.
For example computing the difference in day between a date formatted as Nov 28 20:27:19 2012 GMT
and today.
There are several possibilities (GNU date
, gawk
, perl
, etc.) but I would like to be able to run it on systems without GNU tools (e.g., BSDs, Solaris, etc.)
At the moment the most portable solution I have is Perl but it could require the installation of additional modules for the date conversion.
What should I choose?
Edit: looking at the comments I realized that I need to clarify that the tool I am writing will be publicly distributed. I am not looking on a way to perform computations on my machine or on how to install GNU tools.
I want to use tools that are likely to be found on the majority of the machines.
Best Answer
I had to do this in the past with brute force parsing and calculation in shell script.
Doing it manually in shell script is highly error-prone and slow. You need to account for days per month, leap years, time zones. It will fail with different locales and different languages.
I fixed up one of my old scripts, this would probably need to be modified for different shell environments, but there shouldn't be anything that can't be changed to work in any shell.
I probably made a mistake somewhere and there may be a better way. Let me know if you find a bug, or a better way.
Once you have the epoch time, you can do some useful calculations... although converting that back into a date without the gnu utils... requires doing the above in reverse...