基于TimeZone和夏时制计算时间 [英] Calculate time on base of TimeZone, Daylight saving
本文介绍了基于TimeZone和夏时制计算时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找返回时间的现成函数,
输入将是时间,目标时区(例如EDT),是否考虑夏令时的标记.
输入和结果时间可以采用任何格式,例如CTime,SYSTEMTIME等.
谢谢.
I am searching for a ready made function which return time,
input will be time, Target time zone (for ex: EDT), flag to consider day light saving or not.
Input and result time can be in any format like CTime, SYSTEMTIME, etc.
Thanks.
推荐答案
您只需要将时间值设置为struct tm
并使用 localtime() [时间管理 [
You just need to set your time value into astruct tm
and use the localtime()[^] function. See also the Time Management[^] routines.
不要使用本地时间.它不能正常工作,尤其是在美国以外的时区使用时.您可以尝试BOOST.
Don''t use localtime. It does not work properly, especially if you are using time zones outside the US. You might try BOOST.
这是我想您要的:
输入1:UTC时间(格林威治"时间)
输入2:时区字符串(3个字符)
输入3:说出是否应考虑DST
输出:当时那个时区的当地时间
这是一个困难的问题.首先,您必须限制输入时间的范围.很难说DST是/将在过去很长的日期或将来的日期生效.
无法使用标准C/C ++函数来获取此信息.我什至不认为您可以询问Windows过去某天是否使用过DST. (DST的开始和结束时间通常每年都不同).如果可以的话,我不会相信答案.
我认为您必须自己进行研究,以建立一个表,以了解您感兴趣的所有时区的DST开始和结束时间,以及所需的日期范围.您还需要一个表格将3个字符的时区代码转换为UTC时间的+/-小时.
我怀疑是否有人编写了用于执行此操作的常规功能,或者是否已将执行此操作的所有信息收集到一个地方.
某些输入时间可能对应于2个不同的输出时间,这也可能是一个问题.
This is what I think you are asking for:
INPUT 1: time in UTC ("Greenwich" time)
INPUT 2: time zone string (3 characters)
INPUT 3: bool to say whether DST is to be considered
OUTPUT: local time in that time zone at that time
This is a difficult problem. First you would have to restrict the range of input times. It would be hard to say whether DST was/will be in effect for dates a long time in the past or dates in the future.
There is no way using standard C/C++ functions to get this information. I don''t even think you would be able to ask Windows if a certain day in the past used DST. (DST start and end times often vary from year to year). And if you can I wouldn''t trust the answer.
I think you would have to research this yourself to build up a table of when DST started and ended for all the time zones you are interested in for the range of dates you want. You also need a table to convert the 3 character time zone codes to +/- hours from UTC time.
I doubt whether anyone has written a general function to do this, or even if all the information to do so has been gathered into the one place.
It may also be a problem that some input times can correspond to 2 different output times.
这篇关于基于TimeZone和夏时制计算时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文