Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In a function that displays time, The time is one hour behind.
The function:

C++
s_ct1980 = CTime(1980, 1, 1, 0, 0, 0);


Where The format of CTime=(int year, int month, int day, int hour, int min, int sec,int dst=-1 )

ie: the last variable is for daylight saving, but it is defaulted to -1 and hence not included in the above function. But if I force values in the function ie:
CTime(1980, 1, 1, 0, 0, 0, 1) , then the hour is going one more hour backwards. How do I put it an hour forward?
Posted
Updated 3-May-12 1:38am
v2
Comments
Jochen Arndt 3-May-12 7:42am    
I don't understand the problem. If you pass a value greater 0 for dst, DST is indicated and the hour is less than with standard time. There is no other choice than standard time and DST. If you want other times, just set them. Maybe you are confused by time zones. The CTime object stores UTC times. If you want to display or use local times from a CTime object, you must convert the time.
nv3 3-May-12 7:45am    
How do you come to the conclusion that the time returned by the CTime constructor is not correct? You probably display that time by some function and there is probably the problem. Please use "Improve question" to show the code that displays the time value.

The behaviour of the dst argument is quite normal, as you will see from the documentation on MSDN. The default value performs time conversion according to the standard rules of daylight savings time. By setting it to 1 you force the time conversion as if DST were in effect.

See CTime()[^] for a full explanation.
 
Share this answer
 
Comments
Sumal.V 3-May-12 8:54am    
Oh thank you. So in my program the value nDst is set to -1 which mean the library code has to decide whether daylight saving is in effect or not. So which is that function? I've spent one full day on this n still no clue
:(
Sergey Chepurin 3-May-12 16:53pm    
MSDN: "...tm_isdst is a required field. If not set, its value is undefined and the return value from mktime is unpredictable."
May be this sample can help:
// Example for CTime::CTime
time_t osBinaryTime; // C run-time time (defined in <time.h>)
time( &osBinaryTime ) ; // Get the current time from the
// operating system.
CTime time1; // Empty CTime. (0 is illegal time value.)
CTime time2 = time1; // Copy constructor.
CTime time3( osBinaryTime ); // CTime from C run-time time
CTime time4( 1999, 3, 19, 22, 15, 0 ); // 10:15PM March 19, 1999
From CTime::CTime [^]
MSDN says:
When creating a CTime object, set the nDST parameter to 0 to indicate that standard time is in effect, or to a value greater than 0 to indicate that daylight savings time is in effect, or to a value less than zero to have the C run-time library code compute whether standard time or daylight savings time is in effect.

Under 'constructors' it says:
CTime( int, int, ...); Constructs a CTime object from local time components with each component constrained to the following ranges:
[table edited out]
This constructor makes the appropriate conversion to UTC.

So what timezone does it think it is in? The result will depend on that, since it's converting the result from local time to UTC.

Peter
 
Share this answer
 
C#
Added +1 to the variable that holds hour.

Though this is a temporary solution,(Have to change this when the daylight saving ends) it solves my problem.
 
Share this answer
 
Comments
Chuck O'Toole 4-May-12 4:47am    
No, it does not "solve" your problem, it only makes the symptoms go away. The problem seems to be that you are using a mix of time conversions (including how you output the time which you haven't shown us yet). To "solve" the problem, you need to use the time conversion routines properly, as suggested by others using the links to examples and documentation as suggested by others.
Richard MacCutchan 4-May-12 6:25am    
Exactly right. It really worries me when I see questioners doing things like this: are they writing commercial software for my bank by any chance?
Sumal.V 4-May-12 6:32am    
Okay.. guys. I was glad atleast I got my software display what it has to. I will definitely look into the formula and solve it the proper way, when I find more time. I had some deadline to fix this problem, which explains why I chose the short-cut method. I'm definitely not happy about what I hve done but relieved for now..
Richard MacCutchan 4-May-12 6:43am    
Far better to miss a deadline than to put some bad code into your solution. These things have a terrible habit of getting forgotten until the day an irate customer calls your chairman and starts shouting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900