如何根据当地时区计算时间 [英] How do I calculate the time based on the local time zone

查看:125
本文介绍了如何根据当地时区计算时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在CTime变量中存储由时区调整的给定日期和时间。时区是从-12到12的数字。



有人能给我一个例子:



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 :

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



另外,如何将AdjustedDateTime存储为GMT时间?



提前致谢


Also, how can I store AdjustedDateTime as GMT time?

Thanks in advance

推荐答案

只需添加CTimeSpan [ ^ ]:

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





为避免时区转换出现问题,所有时间都应存储为UTC并转换为当地时间进行显示。



作为替代方案,您还可以使用 COleDateTime [ ^ ] class。



要在本地时间和UTC之间进行转换,请使用Windows 32 API函数(请参阅以下链接)。



有关日期的概述和Windows的时间处理函数见时间格式转换变得轻松 [ ^ ]和 C ++中的日期和时间 [ ^ ]。



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++[^].


这篇关于如何根据当地时区计算时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆