Click to See Complete Forum and Search --> : Adding days to a Date


muthu555
May 27th, 2002, 07:23 AM
hi,
I need to write a set of utility functions for my comapny and i am really lft with lil time, i jsut thougth that if someone can help or have the source code for a particular function, which adds the given number of days to a date .

thanx
Muthu

blopez
May 27th, 2002, 09:36 AM
You can triy this:

#include <time.h>

struct tm * AddDays(struct tm* day,int nb)
{
long sec = 0;
time_t aday_t;
if(day == NULL)
return NULL;
if(nb < 0)
nb = 1;

sec = nb*24*60*60;
if((aday_t = mktime(day)) == -1)
return NULL;
aday_t += sec;
return gmtime(&aday_t);
}

void main(void)
{
struct tm *aday,*nextday;
time_t tim;
time(&tim);
aday = localtime(&tim);
nextday = AddDays(aday,4);
}

muthu555
May 28th, 2002, 01:38 AM
hey, that was a cool piece of code, i was thinking abt, chacking if bigger than 30 and blah blah blah stuff, this made it neat, thanx man.

regards
Muthu