adding a day to time_t

A

Abubakar

Hi,
Lets say I have three integers (date, month, and year) as part of a struct
lets say like
struct mydate{ int day, month, year};
I want to add x number of days to this date. And it should be a real date
calculation thing. Like if the date is 31st december 2008, when i add 1 to
this, it should become 1st feb 2009.
This of course may require converting the above struct values to some data
type that crt's date functons can understand. I want to do that but havent
been able to yet. Thanks for help.
I'm using visual c++ 2k5.
Regards,

...ab
 
D

Doug Harrison [MVP]

Hi,
Lets say I have three integers (date, month, and year) as part of a struct
lets say like
struct mydate{ int day, month, year};
I want to add x number of days to this date. And it should be a real date
calculation thing. Like if the date is 31st december 2008, when i add 1 to
this, it should become 1st feb 2009.
This of course may require converting the above struct values to some data
type that crt's date functons can understand. I want to do that but havent
been able to yet. Thanks for help.

The mktime function takes a broken-down local time value represented as a
struct tm, normalizes the struct fields, and returns a time_t.
 
D

Doug Harrison [MVP]

yes but how can it help me add days to my own specified date ?

For example, add x number of days to the tm_mday field before calling
mktime. Although your thread subject mentions time_t, your original post
talked about a broken down time in your own format, which you can easily
express as a struct tm for use with mktime. If you actually have time_t
values, you can convert them to struct tm with localtime or gmtime.
 
D

David Wilkinson

Abubakar said:
Hi,
Lets say I have three integers (date, month, and year) as part of a struct
lets say like
struct mydate{ int day, month, year};
I want to add x number of days to this date. And it should be a real date
calculation thing. Like if the date is 31st december 2008, when i add 1 to
this, it should become 1st feb 2009.
This of course may require converting the above struct values to some data
type that crt's date functons can understand. I want to do that but havent
been able to yet. Thanks for help.
I'm using visual c++ 2k5.
Regards,

ab:

time_t measures time in seconds, so to add a day you add 86400.
 
A

Abubakar

I have written something like:

void AddDate (int day, int month, int year, int daystoadd)
{
tm mt;
mt.tm_sec = 1;
mt.tm_min = 1;
mt.tm_hour = 1;
mt.tm_wday = day;
mt.tm_mon = month - 1;
mt.tm_year = year - 1900;
mt.tm_mday = day;

time_t tmptime = mktime (&mt);
tm * srctime = localtime (&tmptime);

char buf[100];
strftime (buf, 100, "%d/%m/%Y", srctime);
printf("%s + %d = ", buf, daystoadd);
// add days ...
srctime->tm_mday += daystoadd;
time_t finaldate = mktime (srctime);

strftime (buf, 100, "%d/%m/%Y", localtime (&finaldate));
printf("%s", buf);

}

If there are any problems with the code please let me know.

Regards,

...ab
 
D

Doug Harrison [MVP]

I have written something like:

void AddDate (int day, int month, int year, int daystoadd)
{
tm mt;
mt.tm_sec = 1;
mt.tm_min = 1;
mt.tm_hour = 1;
mt.tm_wday = day;
mt.tm_mon = month - 1;
mt.tm_year = year - 1900;
mt.tm_mday = day;

time_t tmptime = mktime (&mt);
tm * srctime = localtime (&tmptime);

char buf[100];
strftime (buf, 100, "%d/%m/%Y", srctime);
printf("%s + %d = ", buf, daystoadd);
// add days ...
srctime->tm_mday += daystoadd;
time_t finaldate = mktime (srctime);

strftime (buf, 100, "%d/%m/%Y", localtime (&finaldate));
printf("%s", buf);

}

If there are any problems with the code please let me know.

There's no point in setting tm_wday, because mktime calculates its value
from the other fields and overwrites it. Also, you need to set tm_isdst to
one of the values documented as meaningful for it.
 
B

Ben Voigt [C++ MVP]

Abubakar said:
I have written something like:

void AddDate (int day, int month, int year, int daystoadd)
{
tm mt;
mt.tm_sec = 1;
mt.tm_min = 1;
mt.tm_hour = 1;
mt.tm_wday = day;
mt.tm_mon = month - 1;
mt.tm_year = year - 1900;
mt.tm_mday = day;

time_t tmptime = mktime (&mt);

So now you have a time_t. If you aren't worried about leap seconds, then
just add 24*60*60 to the time_t (which is measured in seconds) for each day.
tm * srctime = localtime (&tmptime);

char buf[100];
strftime (buf, 100, "%d/%m/%Y", srctime);
printf("%s + %d = ", buf, daystoadd);
// add days ...
srctime->tm_mday += daystoadd;
time_t finaldate = mktime (srctime);

strftime (buf, 100, "%d/%m/%Y", localtime (&finaldate));
printf("%s", buf);

}

If there are any problems with the code please let me know.

Regards,

..ab

Doug Harrison said:
For example, add x number of days to the tm_mday field before calling
mktime. Although your thread subject mentions time_t, your original
post talked about a broken down time in your own format, which you
can easily express as a struct tm for use with mktime. If you
actually have time_t values, you can convert them to struct tm with
localtime or gmtime. --
Doug Harrison
Visual C++ MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top