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 8 3930
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
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
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
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
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
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
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
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
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
|
18 posts
views
Thread by Sven |
last post: by
|
45 posts
views
Thread by loudking |
last post: by
|
7 posts
views
Thread by Nick Keighley |
last post: by
| | | | | | | | | | |