SetTimeZoneInformation 函式 (timezoneapi.h)

設定目前的時區設定。 這些設定會控制從國際標準時間 (UTC) 到當地時間的翻譯。

若要支援從年份變更為年份的日光節約時間界限,請使用 SetDynamicTimeZoneInformation 函式

語法

BOOL SetTimeZoneInformation(
  [in] const TIME_ZONE_INFORMATION *lpTimeZoneInformation
);

參數

[in] lpTimeZoneInformation

包含新設定 之TIME_ZONE_INFORMATION 結構的指標。

傳回值

如果函式成功,則傳回非零的值。

如果此函式失敗,則傳回值為零。 若要取得擴充的錯誤資訊,請呼叫 GetLastError

備註

應用程式必須具有SE_TIME_ZONE_NAME許可權,此函式才能成功。 預設會停用此許可權。 使用 AdjustTokenPrivileges 函式在呼叫 SetTimeZoneInformation 之前啟用許可權,然後在 SetTimeZoneInformation 呼叫之後停用許可權。 如需詳細資訊,請參閱 以特殊許可權執行

Windows Server 2003 和 Windows XP/2000: 應用程式必須具有SE_SYSTEMTIME_NAME許可權。

重要

從 Windows Vista 和 Windows Server 2008 到所有目前版本的 Windows 開始,請呼叫 SetDynamicTimeZoneInformation 而不是 SetTimeZoneInformation 來設定系統時區資訊。 SetDynamicTimeZoneInformation 支援 Windows 登錄中動態數據所提供標準時間和日光節約時間變更的完整歷程記錄。 如果應用程式使用 SetTimeZoneInformation,系統會停用動態日光節約時間支援,並出現「無法辨識您目前的時區」訊息。 請選取有效的時區。」 會顯示在 Windows 時區設定中的使用者。

若要通知 Explorer 時區已變更,請傳送 WM_SETTINGCHANGE 訊息。

UTC 和當地時間之間的所有翻譯都是以下列公式為基礎:

UTC = 當地時間 + 偏差

偏差是UTC和當地時間之間的差異,以分鐘為單位。

範例

下列範例會顯示目前的時區,然後調整一個區域西部的時區。 會顯示舊的和新的時區名稱。 您也可以使用 控制台 中的日期和時間來驗證變更。 新名稱會顯示在 [ 日期&時間 ] 索引標籤上,做為 [目前時區]。 新的時區會顯示在 [ 時區 ] 索引標籤上的下拉式清單中。若要復原這些變更,只要從下拉式清單中選擇舊的時區即可。

#define UNICODE 1
#define _UNICODE 1

#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <strsafe.h>

int main()
{
   TIME_ZONE_INFORMATION tziOld, tziNew, tziTest;
   DWORD dwRet;

   // Enable the required privilege

   HANDLE hToken;
   TOKEN_PRIVILEGES tkp;

   OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken);
   LookupPrivilegeValue(NULL, SE_TIME_ZONE_NAME, &tkp.Privileges[0].Luid);
   tkp.PrivilegeCount = 1;
   tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
   AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);

   // Retrieve the current time zone information

   dwRet = GetTimeZoneInformation(&tziOld);

   if(dwRet == TIME_ZONE_ID_STANDARD || dwRet == TIME_ZONE_ID_UNKNOWN)    
      wprintf(L"%s\n", tziOld.StandardName);
   else if( dwRet == TIME_ZONE_ID_DAYLIGHT )
      wprintf(L"%s\n", tziOld.DaylightName);
   else
   {
      printf("GTZI failed (%d)\n", GetLastError());
      return 0;
   }

   // Adjust the time zone information

   ZeroMemory(&tziNew, sizeof(tziNew));
   tziNew.Bias = tziOld.Bias + 60;
   StringCchCopy(tziNew.StandardName, 32, L"Test Standard Zone");
   tziNew.StandardDate.wMonth = 10;
   tziNew.StandardDate.wDayOfWeek = 0;
   tziNew.StandardDate.wDay = 5;
   tziNew.StandardDate.wHour = 2;

   StringCchCopy(tziNew.DaylightName, 32, L"Test Daylight Zone");
   tziNew.DaylightDate.wMonth = 4;
   tziNew.DaylightDate.wDayOfWeek = 0;
   tziNew.DaylightDate.wDay = 1;
   tziNew.DaylightDate.wHour = 2;
   tziNew.DaylightBias = -60;

   if( !SetTimeZoneInformation( &tziNew ) ) 
   {
      printf("STZI failed (%d)\n", GetLastError());
      return 0;
   }

   // Retrieve and display the newly set time zone information

   dwRet = GetTimeZoneInformation(&tziTest);

   if(dwRet == TIME_ZONE_ID_STANDARD || dwRet == TIME_ZONE_ID_UNKNOWN)    
      wprintf(L"%s\n", tziTest.StandardName);
   else if( dwRet == TIME_ZONE_ID_DAYLIGHT )
      wprintf(L"%s\n", tziTest.DaylightName);
   else printf("GTZI failed (%d)\n", GetLastError());

   // Disable the privilege

   tkp.Privileges[0].Attributes = 0; 
   AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0); 

   return 1;
}

規格需求

   
最低支援的用戶端 Windows 2000 Professional [僅限傳統型應用程式]
最低支援的伺服器 Windows 2000 Server [僅限桌面應用程式]
目標平台 Windows
標頭 timezoneapi.h (包含 Windows.h)
程式庫 Kernel32.lib
DLL Kernel32.dll

另請參閱

GetTimeZoneInformation

SetDynamicTimeZoneInformation

TIME_ZONE_INFORMATION

時間函數