从SYSTEMTIME到time_t的转换以UTC/GMT表示时间
Conversion from SYSTEMTIME to time_t gives out Time in UTC/GMT
我正在尝试通过在各种论坛中找到的实现将SYSTEMTIME
转换为time_t
。
time_t TimeFromSystemTime(const SYSTEMTIME * pTime)
{
struct tm tm;
memset(&tm, 0, sizeof(tm));
tm.tm_year = pTime->wYear - 1900; // EDIT 2 : 1900's Offset as per comment
tm.tm_mon = pTime->wMonth - 1;
tm.tm_mday = pTime->wDay;
tm.tm_hour = pTime->wHour;
tm.tm_min = pTime->wMinute;
tm.tm_sec = pTime->wSecond;
tm.tm_isdst = -1; // Edit 2: Added as per comment
return mktime(&tm);
}
但令我惊讶的是,tm
携带的数据对应于当地时间,但mktime()
返回的time_t
对应于UTC时间。
是这样工作的,还是我遗漏了什么?
感谢您的提前帮助!!
编辑1:我想将携带我的本地时间的SYSTEMTIME
准确地转换为time_t
。
我在基于VC6的MFC应用程序中使用这个。
第2版:修改后的代码。
我终于从Windows SDK中找到了解决方案,通过TIME_ZONE_INFORMATION和_timezone
time_t GetLocaleDateTime( time_t ttdateTime) // The time_t from the mktime() is fed here as the Parameter
{
if(ttdateTime <= 0)
return 0;
TIME_ZONE_INFORMATION tzi;
GetTimeZoneInformation(&tzi); // We can also use the StandardBias of the TIME_ZONE_INFORMATION
int iTz = -_timezone; // Current Timezone Offset from UTC in Seconds
iTz = (iTz > 12*3600) ? (iTz - 24*3600) : iTz; // 14 ==> -10
iTz = (iTz < -11*3600) ? (iTz + 24*3600) : iTz; // -14 ==> 10
ttdateTime += iTz;
return ttdateTime;
}
编辑1:请添加您的评论,如果您看到任何错误,请随时评论或编辑。谢谢
相关文章:
- Log4cpp:以UTC/GMT时区打印日期
- C++:floor unix时间戳到UTC月份
- std::strftime 在 Windows 中使用 GMTIME 将时区 UTC 显示为 UTC+1
- 提升,以分钟精度获得 UTC 时间
- 从ubuntu上的utc时间计算unix时间
- 为什么 std::p ut_time(std::gmtime 采用 %z 格式返回 +0100 表示 UTC?
- 如何使用闰秒以微秒为单位获取 UTC 时间
- 从C 中的GMT读取时间
- 将具有 DST 的时间字符串转换为 UTC 时间戳
- 使用 std::chrono / date::gps_clock 将双 GPS 时间戳转换为 UTC/tai
- 高效生成 UTC 时间戳
- 如何在C++中以毫秒为单位获取今天的 UTC 午夜时间?
- 有没有办法在 C++ 中获取 uint64_t 日期的 UTC 纳秒
- C 自定义时间日期结构到UTC时期
- 如何将 UTC 时间戳转换为本地时间,即整点后的几秒钟
- 如何从 UTC 时间戳的组件(Y,M,D,H,M,S,MS)创建system_clock::time_point
- 无UTC时区中的正确时间总和
- 如何知道Solaris上GMT和C 的本地时间的差异
- 从SYSTEMTIME到time_t的转换以UTC/GMT表示时间
- 从GMT/UTC字符串到UNIX时间戳的Qt5(C++)转换