diff options
author | Pixel <Pixel> | 2002-08-20 07:42:21 +0000 |
---|---|---|
committer | Pixel <Pixel> | 2002-08-20 07:42:21 +0000 |
commit | 1b0a5db816b7610c83615e93095155b1709f55da (patch) | |
tree | 6ad2f121c493131e679367933bf07440ec680d43 | |
parent | 396239cc78a75ba7be739788485319c92b07d827 (diff) |
Whoops
-rw-r--r-- | generic/checkargs.c | 85 | ||||
-rw-r--r-- | generic/datecalc.c | 80 |
2 files changed, 165 insertions, 0 deletions
diff --git a/generic/checkargs.c b/generic/checkargs.c new file mode 100644 index 0000000..e1c97be --- /dev/null +++ b/generic/checkargs.c @@ -0,0 +1,85 @@ +/* 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 <stdlib.h> +#include <string.h> +
+int isDateArgument(char* dateString) {
+
+ const int MONTHS[] = {31,0,31,30,31,30,31,31,30,31,30,31};
+ int i, day, month, year;
+ char buffer[5];
+
+ /* 'today' is a valid date */
+ if (strcmp(dateString, "today") == 0)
+ return 1;
+
+ /* Numeric dates must be eight characters long */
+ if (8 != strlen(dateString))
+ return 0;
+
+ /* Check that the date is entirely formed of numbers */
+ for (i = 0; i < 8; i++) {
+ if (dateString[i] < '0' || dateString[i] > '9')
+ return 0;
+ }
+
+ /* Check that the date exists */
+ memset(buffer, 0, 5);
+ strncpy(buffer, dateString + 6, 2);
+ day = atoi(buffer);
+ strncpy(buffer, dateString + 4, 2);
+ month = atoi(buffer);
+ month -= 1;
+ strncpy(buffer, dateString, 4);
+ year = atoi(buffer);
+
+ /* Validate month */
+ if (month < 0 || month > 11)
+ return 0;
+
+ /* Validating dates is simple when the date does not fall into February */
+ if (1 != month) {
+ if (day < 1 || day > MONTHS[month])
+ return 0;
+ } else {
+ int feb = 28;
+
+ /* Februarys are a bit tougher issue */
+ if (0 == year % 4) {
+ if (0 == year % 100) {
+ if (0 == year % 400) {
+ feb = 29;
+ } else {
+ feb = 28;
+ }
+ } else {
+ feb = 29;
+ }
+ }
+ if (day < 1 || day > feb)
+ return 0;
+ }
+
+ /* Avoid user from using dates before 16000301, since those will result in
+ incorrect output */
+ if(16000301 > atoi(dateString))
+ return 0;
+
+ return 1;
+}
diff --git a/generic/datecalc.c b/generic/datecalc.c new file mode 100644 index 0000000..e637b26 --- /dev/null +++ b/generic/datecalc.c @@ -0,0 +1,80 @@ +/* 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 <time.h> +#include <stdlib.h> +#include <string.h> + + +/* Gaus's formula - days since 1.3.1600 (Gregorian calendar) */ +int days(register int n, register int m, register int y) +{ + register int cy; + if((m -= 2) <= 0){ + m += 12; y--; + } + y -= 1600; cy = y/100; + return 365*y+y/4-cy+cy/4+367*m/12+n-31; +} + + +double dateCalc(char * date1, char * date2){ + + /* Declare the needed variables */ + char* date[2]; + struct tm *date_tm[2]; + time_t date_time_t[2]; + double dateDifference; + int isToday[2] = { 0, 0 }; + char buffer[5]; + int day[2], month[2], year[2], i; + + date[0] = date1; + date[1] = date2; + + /* If any of the arguments are "today", then include today's date in the + right variables */ + for(i = 0; i < 2; i++){ + if(!strcmp(date[i], "today")){ + time(&date_time_t[i]); + date_tm[i] = localtime(&date_time_t[i]); + day[i] = (*date_tm[i]).tm_mday; + month[i] = (*date_tm[i]).tm_mon + 1; + year[i] = (*date_tm[i]).tm_year + 1900; + isToday[i] = 1; + } + } + + /* Cut out the year, month and day from 8-digit datestrings */ + for (i = 0; i < 2; i++){ + if(!isToday[i]){ + memset(buffer, 0, 5); + strncpy(buffer, &date[i][6], 2); + day[i] = atoi(buffer); + strncpy(buffer, &date[i][4], 2); + month[i] = atoi(buffer); + strncpy(buffer, date[i], 4); + year[i] = atoi(buffer); + } + } + + /* Calculate the difference */ + dateDifference = days(day[1], month[1], year[1]) - days(day[0], month[0], year[0]); + + return dateDifference; +} |