By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,838 Members | 1,668 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,838 IT Pros & Developers. It's quick & easy.

adding a day to time_t

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Mon, 19 May 2008 09:57:01 +0500, "Abubakar" <q@y.comwrote:
>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.

--
Doug Harrison
Visual C++ MVP
Jun 27 '08 #2

P: n/a
yes but how can it help me add days to my own specified date ?

...ab

"Doug Harrison [MVP]" <ds*@mvps.orgwrote in message
news:c7********************************@4ax.com...
On Mon, 19 May 2008 09:57:01 +0500, "Abubakar" <q@y.comwrote:
>>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.

--
Doug Harrison
Visual C++ MVP

Jun 27 '08 #3

P: n/a
On Mon, 19 May 2008 10:56:16 +0500, "Abubakar" <q@y.comwrote:
>The mktime function takes a broken-down local time value represented as a
struct tm, normalizes the struct fields, and returns a time_t.

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.

--
Doug Harrison
Visual C++ MVP
Jun 27 '08 #4

P: n/a
Abubakar wrote:
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.

--
David Wilkinson
Visual C++ MVP
Jun 27 '08 #5

P: n/a
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

"Doug Harrison [MVP]" <ds*@mvps.orgwrote in message
news:or********************************@4ax.com...
On Mon, 19 May 2008 10:56:16 +0500, "Abubakar" <q@y.comwrote:
>>The mktime function takes a broken-down local time value represented as
a
struct tm, normalizes the struct fields, and returns a time_t.

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.

--
Doug Harrison
Visual C++ MVP

Jun 27 '08 #6

P: n/a
On Tue, 20 May 2008 19:34:39 +0500, "Abubakar" <q@y.comwrote:
>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.

--
Doug Harrison
Visual C++ MVP
Jun 27 '08 #7

P: n/a
Ok cool. Thanks for the help.

...ab
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.

--
Doug Harrison
Visual C++ MVP
Jun 27 '08 #8

P: n/a
Abubakar wrote:
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 [MVP]" <ds*@mvps.orgwrote in message
news:or********************************@4ax.com...
>On Mon, 19 May 2008 10:56:16 +0500, "Abubakar" <q@y.comwrote:
>>>The mktime function takes a broken-down local time value
represented as a
struct tm, normalizes the struct fields, and returns a time_t.

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. --
Doug Harrison
Visual C++ MVP

Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.