468,765 Members | 1,745 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

adding a day to time_t

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
8 3659
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
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
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
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
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
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
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
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.

Similar topics

1 post views Thread by Anand CS | last post: by
4 posts views Thread by Dave Sinkula | last post: by
7 posts views Thread by John J. Hughes II | last post: by
reply views Thread by Zwyatt | last post: by
reply views Thread by Zwyatt | last post: by
8 posts views Thread by Henrik Goldman | last post: by
45 posts views Thread by loudking | last post: by
7 posts views Thread by Nick Keighley | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.