Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   algorithm: help needed in difference of dates (http://www.programmingforums.org/showthread.php?t=10195)

smith.norton Jun 6th, 2006 5:38 AM

algorithm: help needed in difference of dates
 
lets say i have the date as year, month and day in three variables:-

y1, m1 and d1

and another date in:-

y2, m2 and d2

is there any good algorithm to find the difference in the number of days between these two dates.

i would be thankful if anyone can provide me a link to any such alogrithm or post a description here.

example data:-

14th June 1983: y1=1983, m1=06, d1=14
15th April 2006: y2=2006, m2=04, d2=15

grumpy Jun 6th, 2006 6:37 AM

I'm not aware of any such calculator offhand, but it wouldn't be hard to whip one up.

30 days hath September, April, June, and November
All the rest have 31
Except February clear, which has 28 or 29 (each leap year).

In the gregorian calendar, a leap year is every year divisible by 4. But a year divisible by 100 is not a leap year, unless it is also divisible by 400. This should be reasonably accurate for the next 8000 years or so..... when other effects such as the earth's orbit around the sun wobbling a bit will introduce a slight (unfortunately, not totally predictable) error.

The Dark Jun 6th, 2006 7:48 AM

One way is to convert both dates into a Julian date, which is an integer number of days from a certain date in the past. Here is one method in C for doing so. Yo can then subtract one value from the other to find the number of days between the two dates.

jim mcnamara Jun 6th, 2006 12:07 PM

Consider using strptime - which is pretty flexible. Most compilers now support it. Older Windows compilers don't. Most calendric work uses strptime to convert dates to seconds in the epoch, which is the basis for time and date calculations in C.

:

#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
/* 86400 seconds per day */
#define DAY 86400     

/*
  lose the
  th, nd, st suffixes if they exist
*/
char *remove_th_nd_st(const char *src)
{
        const char *values[3]={{"th "}, {"nd "},{"st "} };
        char *dest=strdup(src);
        char *p=NULL;
        int i=0;
       
        while(i<3)
        {
                if( (p=strstr(src,values[i++]))==NULL ) continue;
                if( !isdigit(*(p-1)) ) continue;
                p+=3;
                sprintf(dest,"%d %s",atoi(src),p);
                break;
        }
        return dest;
}
 
/* return time difference in days
  date1 = recent date
  date1 = older date
  ---otherwise you get a negative value which may be okay.
*/ 
int date_diff(const char *date1, const char *date2)
{
    int retval=0;
    const char *fmt="%d %b %Y";
    struct tm now, then;
    char *wdate1=remove_th_nd_st(date1);
    char *wdate2=remove_th_nd_st(date2);     
   
    if ( strptime(wdate1,fmt,&now)==NULL ||
        strptime(wdate2,fmt,&then)==NULL )
    {
            perror("Bad date format");
            exit(EXIT_FAILURE);
    }
    now.tm_isdst = -1;  /* force mktime to get daylight savings */
    then.tm_isdst = -1;
    retval=difftime(mktime(&now),mktime(&then));
    retval=retval/DAY;
    free(wdate1);
    free(wdate2);
    return retval;
}

int main()
{
    char now[32]={0x0};
    char then[32]={0x0};
        strcpy(then,"14th June 1983");
    strcpy(now,"15th April 2006");
    printf("Date difference in days: %s - %s = %d\n",
        now,then,date_diff(now,then) );
    strcpy(now,"1st June 2005");   
    printf("Date difference in days: %s - %s = %d\n",
        now,then,date_diff(now,then) );
    strcpy(now,"2nd August 2014");
    printf("Date difference in days: %s - %s = %d\n",
        now,then,date_diff(now,then) );
    strcpy(now,"1st July 1975");
    printf("Date difference in days: %s - %s = %d\n",
        now,then,date_diff(now,then) );           
    return 0;   
}



All times are GMT -5. The time now is 8:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC