diff options
Diffstat (limited to 'lib/datecalc.c')
-rw-r--r-- | lib/datecalc.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/lib/datecalc.c b/lib/datecalc.c new file mode 100644 index 0000000..46e7179 --- /dev/null +++ b/lib/datecalc.c @@ -0,0 +1,84 @@ +/* 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] = { date1, date2 }; + 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; + + + /* 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; +} + + + + + + |