1、Windows c++ 方法
#include <atltime.h>
#include <Windows.h>
int CTimeRyan::GetLocalTimeZoneByCPP()
{
TIME_ZONE_INFORMATION tmp;
GetTimeZoneInformation(&tmp);
return tmp.Bias/(-60);
}
2、Windows c 方法
#include <cstdlib>
#include <stdio.h>
#include <time.h>
int CTimeRyan::GetLocalTimeZoneByC()
{
time_t time_utc;
struct tm tm_local;
// Get the UTC time
time(&time_utc);
// Get the local time
// Use localtime_r for threads safe for linux
//localtime_r(&time_utc, &tm_local);
localtime_s(&tm_local, &time_utc);
time_t time_local;
struct tm tm_gmt;
// Change tm to time_t
time_local = mktime(&tm_local);
// Change it to GMT tm
//gmtime_r(&time_utc, &tm_gmt);//linux
gmtime_s(&tm_gmt, &time_utc);
int time_zone = tm_local.tm_hour - tm_gmt.tm_hour;
if (time_zone < -12) {
time_zone += 24;
} else if (time_zone > 12) {
time_zone -= 24;
}
return time_zone;
}