C++获取和设置时区

一、     获取当前系统时区信息

使用API函数GetTimeZoneInformation可以获得当前时区的相关信息,函数原型为

DWORD GetTimeZoneInformation( 

LPTIME_ZONE_INFORMATION lpTimeZoneInformation );

需要传递一个TIME_ZONE_INFORMATION 结构体指针,此结构体定义为

typedef struct _TIME_ZONE_INFORMATION {

    LONG Bias;       //以分钟为单位

    WCHAR StandardName[ 32 ];// 标准时间的名称

    SYSTEMTIME StandardDate;

LONG StandardBias;

WCHAR DaylightName[ 32 ];// 夏令时的名称

    SYSTEMTIME DaylightDate;

    LONG DaylightBias;

} TIME_ZONE_INFORMATION, *PTIME_ZONE_INFORMATION, *LPTIME_ZONE_INFORMATION;

其中UTC = local time + bias(UTC时间 = 本地时间 + bias),具体含义参看MSDN

 

例子:

CString GetTimeZoneNow()
{    
     TIME_ZONE_INFORMATION   tzi;  

     GetSystemTime(&tzi.StandardDate);  

     GetTimeZoneInformation(&tzi);  

     CString   strStandName   =   tzi.StandardName;  

     CString   strDaylightName   =   tzi.DaylightName;  

     int zone = tzi.Bias/ -60; //时区,如果是中国标准时间则得到8 

     return strStandName;
}

 

二、     设置时区

可以使用API函数SetTimeZoneInformation设置时区,函数原型为

BOOL SetTimeZoneInformation(

 const TIME_ZONE_INFORMATION* lpTimeZoneInformation

);

同样需要TIME_ZONE_INFORMATION结构体,不过这是设置时区,所以我们要为这个结构体设置相应的值,我们可以在注册表

HKEY_LOCAL_MACHINE"SOFTWARE"Microsoft"Windows NT"CurrentVersion"Time Zones

下得到所有的时区信息,(如果不想每次都查注册表获得时区列表,可以讲时区列表保存至XML或其他文件中)
      以中国北京时间为例即 China Standard Time子项,



     Display 在控制面板中调整时区时显示的名称 

Std:标准时间名称 

Dlt 如果有夏令时时区则为其名称。 

Tzi 一个数据结构,包含本地时区和0时区相差的分钟数等信息。二进制形式存储的  

用一结构体定义之

typedef struct _REG_TZI_FORMAT

{

    LONG Bias;

    LONG StandardBias;

    LONG DaylightBias;

    SYSTEMTIME StandardDate;

    SYSTEMTIME DaylightDate;

} REG_TZI_FORMAT;

   

 例子:(上面的结构体定义不要忘记加入代码)

代码参看至http://www.diybl.com/course/3_program/c++/cppjs/20090302/156436.html#

 

BOOL CSetTimeZone(CString subKey)
{
     //获得权限

     HANDLE hToken;

     TOKEN_PRIVILEGES tkp; 

     OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken);

     LookupPrivilegeValue(NULL, TEXT("SeTimeZonePrivilege"), &tkp.Privileges[0].Luid);

     tkp.PrivilegeCount = 1;

     tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

     AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
 

     HKEY hKey;

     TIME_ZONE_INFORMATION tziNew;

     REG_TZI_FORMAT regTZI;

     DWORD dwBufLen=sizeof(regTZI);

     LONG lRet;

 

     CString keyName = _T("Software""Microsoft""Windows NT""CurrentVersion""Time Zones""") + subKey;

     CString strStd = _T("");//标准时间名称

     CString strDlt = _T("");//夏令时名称

 

     unsigned char szData[256];

     DWORD dwDataType, dwBufSize;

     dwBufSize = 256;

 

     lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,keyName,    0, KEY_QUERY_VALUE, &hKey ); 

     if( lRet != ERROR_SUCCESS )

         return FALSE;
 

     lRet = RegQueryValueEx( hKey, TEXT("TZI"), NULL, NULL,(LPBYTE)&regTZI, &dwBufLen);

     if(RegQueryValueEx(hKey, _T("Dlt"), 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)

         strDlt = (LPCTSTR)szData;

 

     dwBufSize = 256;

     if(RegQueryValueEx(hKey, _T("Std"), 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)

         strStd = (LPCTSTR)szData;

 

     RegCloseKey(hKey);

 

     if( (lRet != ERROR_SUCCESS) || (dwBufLen > sizeof(regTZI)) )

         return FALSE;

 //设置值

     ZeroMemory(&tziNew, sizeof(tziNew));

     tziNew.Bias = regTZI.Bias;

     tziNew.StandardDate = regTZI.StandardDate;

     wcscpy(tziNew.StandardName, strStd);

     wcscpy(tziNew.DaylightName, strDlt);

     tziNew.DaylightDate = regTZI.DaylightDate;

     tziNew.DaylightBias = regTZI.DaylightBias;

 

     if( !SetTimeZoneInformation( &tziNew ) )

     {

         TRACE("Failure1: %d"n", GetLastError());

         return FALSE;

     }

 

     tkp.Privileges[0].Attributes = 0;

     AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);

 

     return TRUE;

}

 

调用说明:如果要设置成(GMT+05:00) 叶卡捷琳堡

则为 CSetTimeZone(_T(“Ekaterinburg Standard Time”));

posted @ 2009-09-21 21:43  trace007  阅读(5868)  评论(0编辑  收藏  举报