在编程的过程中要经常使用CTime和CTimeSpan类,尤其是利用CTimeSpan来求时间差。如:
CTime tmBegin(1996,2,28,0,0,0);
CTime tmEnd(1996,3,11,0,0,0);
CTimeSpan tmSpan = tmEnd - tmBegin;
int nDays = tmSpan.GetDays();
int nHours= tmSpan.GetHours();
如果你的系统时间时区是北京时间,那么nDays = 12, nHours = 0;
如果你的系统时间时区是纽约时间,那么nDays = 11, nHours = 23.
CTime tmBegin(1996,10,31,0,0,0);
CTime tmEnd(1996,11,4,0,0,0);
CTimeSpan tmSpan = tmEnd - tmBegin;
int nDays = tmSpan.GetDays();
int nHours= tmSpan.GetHours();
如果你的系统时间时区是北京时间,那么nDays = 4, nHours=0;
如果你的系统时间时区是纽约时间,那么nDays = 4, nHours=1.
原因是DST(daylight saving time),由于世界上的大部分国家都使用了夏令时(中国除外),因此结果可能会出现差异。
美国和加拿大原本于每年10月的最后一个星期日凌晨2时起实施冬令时间;4月的第一个星期日凌晨2时起,恢复夏令时间。
但是根据美国国会最新通过的能源法案,为加强日光节约,自2007年起延长夏令时间,开始日期从每年4月的第一个星期日,提前到3月的第二个星期日,结束日期从每年10月的最后一个星期日,延后到11月的第一个星期日。
因此,对于第一个例子,
如果时区是纽约时间的话,tmSpan.GetDays()=11,tmSpan.GetHours()=23,tmSpan.GetMinutes()=0;
如果时区是北京时间的话,tmSpan.GetDays()=12,tmSpan.GetHours()=0,tmSpan.GetMinutes()=0;
其实在msdn上面已经有了明确的解释:
Remarks
Note that Daylight Savings Time can cause GetDays to return a potentially surprising result. For example, when DST is in effect, GetDays reports the number of days between April 1 and May 1 as 29, not 30, because one day in April is shortened by an hour and therefore does not count as a complete day.
可是,今天我却为了这个问题整整花费了一天的时间来找bug!!基础!基础!打基础!
谨以此文作为自己的教训!