Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 6th, 2006, 5:38 AM   #1
smith.norton
Newbie
 
Join Date: Jun 2006
Posts: 8
Rep Power: 0 smith.norton is on a distinguished road
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
smith.norton is offline   Reply With Quote
Old Jun 6th, 2006, 6:37 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,253
Rep Power: 5 grumpy will become famous soon enough
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.
grumpy is offline   Reply With Quote
Old Jun 6th, 2006, 7:48 AM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 873
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old Jun 6th, 2006, 12:07 PM   #4
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
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;     
}
jim mcnamara is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:46 AM.

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