Too many times we have needed to do a bit of math on a date -- say, "today + 7 days" -- but in traditional MM/DD/YYYY format this is really tricky (especially in a shell script). A result of this need we built the datemath tool which can perform these functions from the command line or from a shell script. Examples:
We've been using this for 15 years, and recently got around to porting it and updating for year-2000 compatibility (oops). The port to Bison/flex from yacc/lex was a bit of doing as well.$ datemath today + 5 06/23/2003 $ datemath '12/25/2003 - today' 188 $ datemath today + 5 weeks 07/25/2003 when will my machine be up for one year? $ uptime 11:09am up 317 days, 15:38, 7 users, load average: 0.16, 0.04, 0.01 $ datemath today + 365 - 317 10/24/2003
Dates can be entered in one of three formats:
Keywords are shown here in upper case to make them stand out, but the program doesn't care at all about case, and mixed upper/lower work fine. Remember that parentheses are special to the shell, so they must be quoted.
TODAY | a synonym for today's date |
YESTERDAY | a synonym for TODAY-1 |
TOMORROW | a synonym for TODAY+1 |
FDAY(date) | Returns the date of the first day of the month 1..31 |
LDAY(date) | Returns the date of the last day of the month 1..31 |
DAY(date) | Return the day of the month 1..31 |
MONTH(date) | Returns the number of month 1..12 |
YEAR(date) | Returns the year in four-digit form |
NDAYS(date) | Returns the number of days in the given month 1..31 |
n WEEKS | Returns the number of days in n weeks |
date + ndays | Return the date ndays into the future |
date - ndays | Return the date ndays into the past |
date - date | Return the number of days between the two dates |
In addition to the date expression above (required),
-V | This displays the program's version information, and exits. |
-4 | Display dates with four-digit years (the default) |
-2 | Display dates with two-digit years |
-C | (Collation mode) Display dates in year/month/day format that is useful for sorting. |
-9 ## | When interpreting a date with a two-digit year, years from ##..99 are 1900, otherwise 2000. Default value = 40. |
doesn't work because it's not quite clear what it means: 4/31/2003 is not a valid date, so we couldn't find any obvious direction to adjust the date to conform.1/31/2003 + 3 MONTHS
Note that this package requires GNU flex and bison: those needing to use the older tools may have some troubles. Reports of portability issues are gratefully accepted.$ gtar -tzf datemath.tar.gz datemath.c gram.y lex.l util.c jdate.c defs.h Makefile
Fri Jun 20 23:01:46 PDT 2003 -- version 2.0.10 -- initial release to websiteNavigate: More Tools