/* datedif - calculates the difference in days between two dates * Copyright (C) 2000 Micael Widell contact: xeniac@linux.nu * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include "checkargs.c" /* Check that the arguments are valid */ #include "datecalc.c" /* Date calculations */ const char VERSION[] = "0.9.1-4"; void printVersion(); void printUsageHelp(); int printArgumentError(char*); int main(int argc, char *argv[]) { int args_ok; int printMore = 0; double dayDifference; /* Check for 'help' and 'version' arguments */ if (2 == argc) { if (0 == strcmp(argv[1], "--version") || 0 == strcmp(argv[1], "-v")) { printVersion(); return 0; } else if (0 == strcmp(argv[1], "--help") || 0 == strcmp(argv[1], "-h")) { printUsageHelp(); return 0; } } /* Check for 'more' argument */ if (4 == argc) { if (0 == strcmp(argv[1], "--more") || 0 == strcmp(argv[1], "-m")) { printMore = 1; argv[1] = argv[2]; argv[2] = argv[3]; argc = 3; } } if (3 != argc) /* If too few/many arguments, display usage help */ { printUsageHelp(); return 0; } /* Check if the user has used the -m argument with only one date */ if (0 == strcmp(argv[1], "--more") || 0 == strcmp(argv[1], "-m")) { printf("You must supply two dates.\n"); return 0; } /* Check if the dates are valid */ args_ok = 1; if (!isDateArgument(argv[1])) args_ok = printArgumentError(argv[1]); if (!isDateArgument(argv[2])) args_ok = printArgumentError(argv[2]); if (!args_ok) return 0; /* Send the arguments to dateCalc() to do the actual calculations, and get the difference in days stored in dayDifference */ dayDifference = dateCalc(argv[1], argv[2]); /* Print the output */ if(!printMore) printf("%.f", dayDifference); else printf("Date difference: %.f days.\n", dayDifference); return 0; } void printVersion() { printf("datedif %s\n", VERSION); } void printUsageHelp() { printf("Datedif calculates the difference in days between two dates.\n\n"); printf("Usage: datedif [option] date1 date2\n\n"); printf(" -m --more print nicer output, for non-script use\n"); printf(" -h --help print this help and quit\n"); printf(" -v --version print version information and quit\n\n"); printf("Dates must be given in YYYYMMDD format. You can also use 'today' as a date.\n\n"); } int printArgumentError(char* d) { printf("'%s' is not a valid date\n", d); return 0; }