|
-
October 9th, 2009, 07:54 AM
#1
Problem creating dates
Hello,
i'm having a problem creating dates. I use this code:
Code:
time_t temps;
struct tm * infoTemps;
time(&temps);
infoTemps=localtime(&temps);
infoTemps->tm_mday=4;
infoTemps->tm_mon=2;
infoTemps->tm_year=109;
infoTemps->tm_hour=18;
infoTemps->tm_min=22;
infoTemps->tm_sec=0;
//printing the structure before maketime
cout << "hh:mm:ss " << infoTemps->tm_hour << ":" << infoTemps->tm_min << ":" <<infoTemps->tm_sec << endl;
cout << "dd-mm-aaaa: " << infoTemps->tm_mday << "-" << infoTemps->tm_mon+1 << "-" << infoTemps->tm_year+1900 << endl;
cout << "Dia de semana: " << infoTemps->tm_wday << endl;
cout << "Dia del año: " << infoTemps->tm_yday << endl;
cout << "Horario de verano: " << (infoTemps->tm_isdst ? "Si" : "No") << endl;
temps=mktime(infoTemps);
//printing the structure after maketime
cout << "hh:mm:ss " << infoTemps->tm_hour << ":" << infoTemps->tm_min << ":" << infoTemps->tm_sec << endl;
cout << "dd-mm-aaaa: " << infoTemps->tm_mday << "-" << infoTemps->tm_mon+1 << "-" << infoTemps->tm_year+1900 << endl;
cout << "Dia de semana: " << infoTemps->tm_wday << endl;
cout << "Dia del año: " << infoTemps->tm_yday << endl;
cout << "Horario de verano: " << (infoTemps->tm_isdst ? "Si" : "No") << endl;
The problem, is that in the first printing the correct hour is printed, 18:22. But after mktime(infoTemps) the hour change and then it become 17:22....
I think that it's caused because now is summertime and in february it isn't... but i'm not sure... and i don't know hot to make it work ok...
Is it posible to fix this problem automatically, 'cause my function have to create a lot of dates and i don't know how to make to know everytime if it's summertime or if it isn't...
Thank you very much!!!
Imanol.
p.d. Sorry my english....
-
October 9th, 2009, 10:17 AM
#2
Re: Problem creating dates
 Originally Posted by dianyu
I think that it's caused because now is summertime and in february it isn't... but i'm not sure...
You think correct. The call to localtime will set infoTemps->tm_isdst to the correct value for the current time. Now you change the date part of infoTemps but leave tm_isdst as it is, so mktime correctly subtracts an hour.
In other words: You are asking the system to create a time value for 4th Feb 2009, 18:22:00 summer time and the system correctly converts this into 17:22:00 winter time.
 Originally Posted by dianyu
and i don't know hot to make it work ok...
Setting infoTemps->tm_isdst to -1 before calling mktime should do the trick.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
October 10th, 2009, 04:40 AM
#3
Re: Problem creating dates
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|