问题描述
我正在处理存储的日期和时间。我以字符串格式
(即 DDMMYYYYHHMMSS
)将它们存储在GMT格式的文件中。当客户端查询时,我将此字符串转换为 struct tm
,然后使用 mktime
将其转换为秒。我这样做是为了检查无效的DateTime。再次,我做转换秒到字符串格式。所有这些处理都很好,没有问题。
I am processing stored dates and times. I store them in a file in GMT in a string format(i.e. DDMMYYYYHHMMSS
). When a client queries, I convert this string to a struct tm
, then convert it to seconds using mktime
. I do this to check for invalid DateTime. Again I do convert seconds to string format. All these processing is fine, no issues at all.
但我有一个奇怪的问题:我存储的日期和时间在GMT与区域设置也GMT。由于节省了日光,我的区域设置时间更改为GMT + 1。现在,如果我查询存储的日期和时间,我得到少1小时,因为 mktime
函数使用locale,即GMT + 1,转换 struct tm
到秒( tm_isdst
设置为-1,因此 mktime
自动检测夏令时等)。
But I have one weird issue: I stored the date and time in GMT with locale also GMT. Because of day light saving, my locale time changed to GMT+1. Now, if I query the stored date and time I get 1 hour less because the mktime
function uses locale, i.e. GMT+1, to convert the struct tm
to seconds (tm_isdst
set to -1 so mktime
detects daylight savings etc. automatically).
任何想法如何解决这个问题?
Any ideas how to solve this issue?
推荐答案
使用 / 作为 mktime
。
time_t mkgmtime(struct tm* tm)
{
#if defined(_WIN32)
return _mkgmtime(tm);
#elif defined(linux)
return timegm(tm);
#endif
}
这篇关于C ++ mktime和DST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!