Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date difference 2

Status
Not open for further replies.

mad411

Technical User
Apr 4, 2002
17
IN
hello e'body

the problem is very simple,
i'm taking a date from the user &
want to calculate its difference with
system date in days.

so plz help!!!!
 
using the "time" header file is the answer. Its not a straight forward procedure, so here is a bit of code that will get the systems date :-

#include <stdio.h>
#include <time.h>

void main()
{
time_t calender_time;
struct tm todays_date;
calender_time = time(NULL);
todays_date = *localtime(&calender_time);

printf(&quot; DATE %d-%d-%d&quot;,todays_date.tm_mday
,todays_date.tm_mon+1
,todays_date.tm_year+1900);
}
 
Hi Denster,

very very thanks for your reply.

your prg. is too good. but it only specifies how to extract the system date.

i was able to find the same but still didn't find the difference.

so pls. can u suggest me the ways to find the diff. between the two date, ie. system date and user specified date.

waiting for your reply.
thanks.
 
A crude but effective way is as follows

{
.
.
.
/*
for jan total days =0, feb = days till jan, ... aug = days till july and so on
codify the logic for your use

*/
int months[]={0, 31, 59, 90, 120, 151 ...... 335}


/*
date has the following components

date dd
month mm
year yy
century cc

lets presume the century is the same for both the dates


*/
system days = year * 365 + month[mm] + date

inpute days = iyy* 365 + imth[mm] + idate


now find the difference between these dates

days = system days - inpute days

}




icici
 
theres probably a million and one ways of doing it but here is another variation to get you started :-

#include <stdio.h>

void main()
{
int loop,total_days = 0,days_in_a_year;
int day,month,year;
int month_days[] = {31,28,31,30,31,30,31,31,30,31,30,31}
/* enter the date - day,month,year*/
if(year % 4 == 0) /* check for leap year */
month_days[1] = 29;
/* calculate days in input year */
for(loop = 0;loop < month;loop++)
total_days = total_days + month_days[loop];
total_days = total_days + day;
/* calculate days upto sytem date */
for(loop = year;loop < /* system year */;loop++)
{
if(loop % 4 == 0) /* check for leap year */
days_in_a_year = 366;
else
days_in_a_year = 365;
total_days = total_days + days_in_a_year;
/* etc etc... */
 
Hi:

Denster is right; There is a million and one ways to do this. When dealing with time in &quot;C&quot;, I'd rather convert everything in seconds from the EPOCH and then do my date arithmetic. This stub program gets the number of whole days, but could easily be changed to get days, hours, minutes, etc. I also assume the user entered hours, minutes, and seconds are zero.

Expanding on Denster's idea, I change the user entered month, day, year strings to integer; build a time structure for the user entered data, and convert that to seconds. The purests out there are going to say I should have used the difftime function to get the difference between two time_t types.

Maybe, but time_t is a long, and we know sooner or later it has to change. Most everybody knows that timet overflows Tue Jan 19 03:14:07 2038 GMT

I learned everything I know on this subject from P.J. Plaugher's book, The Standard &quot;C&quot; Library.

Regards,

Ed
Schaefer


/*
* Get the number of seconds between system date and user entered date
*/
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>

void main()
{
time_t systime, caltime; /* time_t is a long */
struct tm *systm, usertm;

char *mmc=&quot;4&quot;; /* user entered date */
char *ddc=&quot;5&quot;;
char *yyc=&quot;2002&quot;;

long secondsin24=86400; /* seconds in day */
int mm, dd, yy;
long timediff;


time(&systime); /* get the system time in seconds since EPOCH */
systm=localtime(&systime); /* and return a pointer to the time structure */
printf(&quot;time in seconds since EPOCH is %ld\n&quot;, systime);

mm=atoi(mmc); /* convert to int */
dd=atoi(ddc);
yy=atoi(yyc);

/* build the user time structure */
usertm.tm_year = yy - 1900;
usertm.tm_mon = mm - 1;
usertm.tm_mday = dd;
usertm.tm_hour = 0;
usertm.tm_min = 0;
usertm.tm_sec = 0;
usertm.tm_isdst = systm->tm_isdst;

/* get time in seconds from the time structure */
caltime = mktime(&usertm);
printf(&quot;time in seconds for caltime is %ld\n&quot;, caltime);

/* don't care if time difference is negative */
timediff=caltime-systime;
if(timediff < 0)
timediff = timediff * (-1);

printf(&quot;Number of days since user entered date: %ld\n&quot;, timediff/secondsin24);

exit (0);
}
 
hey guys,

thank u very much, it worked [thumbsup2].
this problem was [hammer]ing me since about a month
& u solved it in just a day.

thank u for [openup]ign me up

bye [wavey2]
 
Thanks Denster for codifying the logic.

The problem though has been solved, what happen when u have to find the difference between the two dates say

date1 == may 01, 2002

date2 == october 10, 2001

some calculation has to be done for the year. the logic mentioned by me helps in calculation of the year, and when its leap year, the same can be added as (y2 - y1) % 4

think about it



icici
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top