Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
According to MSDN, if you set the DST flag to >0 "Daylight saving time is in effect".

I have an application that writes some time information to file, then reads it in to do some things then write it all out again.

If my program is being run at 8:00 on the 5th of August 2012, and it reads from the file "15:30 1st Jan 2012", what exactly does the DST flag do?

I will read from the file a string that holds the information "15:30 1st January 2012" and convert that all into numbers so I have

hours: 15
minutes: 30
days: 1
months: 1 (although it could be 0...)
years: 2012 (or with any defined offset)

Then I call the CTime constructor and shove that all in to get a CTime object in local time, but will that be 15:30 because it says so, or 14:30 because it's run in August?

===============

I'd love to get my work done having understood what CTime does, but what I really need to figure out is a way of writing out to file in UTC then reading that back in UTC, but from what I can tell, if I use CTime, it will convert whatever I put into into local time.

Hence, at the moment, I've got a program that back tracks through time by 8 hours every time it runs (I'm in Japan).

Please help!

Thanks in advance.
Posted

The answer is, never store times and/or dates as strings. The only valid way to track times correctly is always to store them as UTC values. That means the timestamp is an accurate representation of the real universal time that your event occurred and is correct forever. When you want to display the value for a user you can apply whatever timezone you wish, and get DST added automatically.
 
Share this answer
 
Comments
PaulowniaK 20-Jun-12 4:16am    
Thanks for your suggestion. I agree with you, but I prefer to store the time as string so that service personnel can read the file in a text editor and make service related decisions. If the file is written in UTC then we will need a separate application for the service personnel.

Actually, I am well aware that writing the file in local time is stupid. If the PC containing the program travels half way around the earth and the PC clock is set to local time, who knows what'll happen when the program next runs.

What I'd really like to do is to write out the UTC in human readable form, then read it back in as UTC without applying any conversion. Is that possible?
[no name] 20-Jun-12 8:48am    
I think you responded without thinking about the problem. The answer is correct and you need to understand time before you play with it. UTC is universal (even on the moon). From UTC you can get local time taking into account time offset and daylight saving. This makes UTC when correctly interpreted failsafe. There are a number of implementations of time but UTC is the reference.
PaulowniaK 20-Jun-12 20:19pm    
Yes, I fully agree with you.

The problem I need to solve is that I don't seem to have full control of whether to convert to local time or not.
[no name] 20-Jun-12 21:57pm    
Thats not true. You are in full control. Every programmer dealing with time has these issues to deal with. If you store as UTC (time_t for example) read it back and convert to local time this will give the correct time. So the steps are convert local time to UTC - store it - read it back and convert to local time. If you are losing or gaining time you have a bug.
PaulowniaK 20-Jun-12 22:08pm    
>So the steps are convert local time to UTC - store it - read it back and convert to local time.

There's the problem.
I want to convert local time to UTC, store and read it back as UTC.

>you have a bug

You don't say!
You may store the values in the standardized ISO-8601 [^] format:
[YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]Z.
This format is supported by most date/time from string parsers. The date/time separator 'T' is often replaced by a space character for human readability (this will break the standard but is widely accepted).

The 'Z' (Zulu) character at the end indicates UTC here. As already noted by Richard, storing as UTC is always preferred.
 
Share this answer
 
Comments
Richard MacCutchan 20-Jun-12 9:09am    
Good suggestion.
To answer your original question: Then I call the CTime constructor and shove that all in to get a CTime object in local time, but will that be 15:30 because it says so, or 14:30 because it's run in August?

If you set the nDST parameter to a negative value (the dafault), CTime will figure out whether on that date in August (and that time) DST will apply and convert your local time according into UTC. It you set nDST to 0, CTime will take it for granted that this time stamp was taken at a time with no DST and hence convert it back to UTC accordingly. If you set nDST to 1, CTime will take it for granted that the timestamp was taken while DST was in effect and convert accordingly.

Note that CTime looks at the current locale settings to figure out in which time zone you are and find out the corresponding correction value. In case of nDST < 0 it will also consult the locale information to find out which rule applies for determining if a timestamp falls into the DST period.

The nDst parameter hence gives you a little more control over whether the DST correction is applied, for example in cases in the past for which different DST rules might have applied. If you can determine from your text-based representation of a timestamp if DST was in effect at that time, I would always use that information instead of using the default nDst = -1.
 
Share this answer
 

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