Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I would like to store in a CTime variable a given date and time adjusted by the timezone. The timezone is a number from -12 to 12.

Can someone give me an example of :

C++
CTime LocalDateTime = ...
int timezone = -8; // example
CTime AdjustedDateTime = ...

Also, how can I store AdjustedDateTime as GMT time?

Thanks in advance
Posted
Comments
[no name] 12-Jul-15 8:08am    
Not clear to me. Are you wishing to calculate UTC from local time? The completed first line of code is crucial to the question.

1 solution

Just add a CTimeSpan[^]:
CTime AdjustedDateTime = LocalDateTime + CTimeSpan(0, -8, 0, 0);


To avoid problems with time zone conversions, all times should be stored as UTC and converted to local time for display.

As an alternative you can also use the COleDateTime[^] class.

To convert between local time and UTC use the Windows 32 API functions (see the below links).

For an overview of date and time handling functions for Windows see Time Format Conversion Made Easy[^] and Date and Time in C++[^].
 
Share this answer
 
Comments
[no name] 12-Jul-15 8:21am    
So this would make AdjustedDateTime actually UTC time which is confusing is it not. LocalDateTime is an adjusted time.
Jochen Arndt 12-Jul-15 8:30am    
I just answered the question how to add a time spane. In this case it would be off course better to name the variable accordingly.

I recommend Michael to read the very good article "Time Format Conversion Made Easy". It should help to handle times in a proper way.
Michael Haephrati 12-Jul-15 8:50am    
Look like your answer works. After this adjustment (which is due to difference time zone in server and in client) I would like to adjust the result again to GMT. How do I do that?
Jochen Arndt 12-Jul-15 8:57am    
The result is UTC (as noted by pwasser) when the server time zone has an offset of +8. GMT without dayligth saving time is UTC.
Michael Haephrati 12-Jul-15 9:23am    
Correct but I have another CTime I need to convert. I check a file's date and time and need to convert the date/time to GMT, keeping the result as CTime as well...

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