时区设置- Windows 平台

本文介绍了在Windows平台上如何通过API函数SetTimeZoneInformation设置时区的方法,包括获取时区信息、设置时区信息结构及调用API函数的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

时区设置- Windows 平台
www.diybl.com    时间 : 2009-03-02  作者:匿名   编辑:sky 点击:  896 [ 评论 ]
-
-
最近研究了一下Windows 上的时区设置方法,记录在此,分享大家。

 

首先分析一下原理。经过一番研究,发现Windows平台上设置时区和以下因素有关系:

 

1. 设置时区时,使用API函数SetTimeZoneInformation来设置时区信息。这个API函数使用了一个结构“TIME_ZONE_INFORMATION”,其定义在winbase.h,具体如下:

 

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;

Bias: 和0时区相差的分钟数

StandardName: 标准时间的名称

StandardDate:如果有夏令时的时区,这个结构确定了什么时间开始实行夏令时

StandardBias:标准时间下,和Bias的差别。通常为0

DaylightName:夏令时的名称

DaylightDate:结束夏令时的时间

DaylightBias:夏令时的时候,时间的偏差

 

另外,要注意一个公式:

 

UTC = local time + bias: 就是说本地时间加上Bais才是UTC时间。

 

 

2. 在以下注册表中存放了所有时区信息:

HKLM\Software\Microsoft\Windows NT\CurrentVersion\Time Zones

 

以北京时间为例,其主要注册表项如下:

 

Display: 就是在控制面板里,调整时区时显示的名称。Vista里面还有一条MUI_Display项,是多语言环境
               的显示。

 

Std:标准时间名称。和设置时区信息的函数有关。

Dlt: 如果有夏令时之类的时区的名称。

Tzi: 一个数据结构,包含本地时区和0时区相差的分钟数等信息。下面我们用一个结构定义。

 

typedef struct _REG_TZI_FORMAT
{
    LONG Bias;
    LONG StandardBias;
    LONG DaylightBias;
    SYSTEMTIME StandardDate;
    SYSTEMTIME DaylightDate;
} REG_TZI_FORMAT;

介绍完上述两点,大家就知道了,要设置时区打大概步骤:

 

1. 从注册表中获得相应的时区信息

2.  根据注册表中的信息,设置TIME_ZONE_INFORMATION结构

3. 调用SetTimeZoneInformation设置时区信息

 

当然,在Windows XP以上,还需要获得一个权限,称为“SE_TIME_ZONE_NAME” privilege。

 

下面是代码,以设置为北京时间为例子:

 

 

 

 BOOL SetTimeZoneToChina(void)
{


 // Enable the required privilege

 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);

 //Query the registry key to get the TZI info of China time zone
 HKEY hKey;
 TIME_ZONE_INFORMATION tziNew;
 REG_TZI_FORMAT regTZI;
//  TCHAR szProductType[BUFSIZE];
 DWORD dwBufLen=sizeof(regTZI);
 LONG lRet;

 lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\China Standard Time"),
  0, KEY_QUERY_VALUE, &hKey );

 if( lRet != ERROR_SUCCESS )
  return FALSE;

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

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

 // Adjust the time zone information
 ZeroMemory(&tziNew, sizeof(tziNew));
 tziNew.Bias = regTZI.Bias;
 tziNew.StandardDate = regTZI.StandardDate;
 wcscpy(tziNew.StandardName, L"China Standard Time");


 wcscpy(tziNew.DaylightName, L"China Standard Time");

 tziNew.DaylightDate = regTZI.DaylightDate;
 tziNew.DaylightBias = regTZI.DaylightBias;

 if( !SetTimeZoneInformation( &tziNew ) )
 {
  printf("Failure1: %d\n", GetLastError());
  return FALSE;
 }

 // Disable the privilege

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

 return TRUE;
}

 

 

 以上仅供参考。具体可参考MSDN上SetTimeZoneInformation相关说明和示例代码。


文章出处:飞诺网(www.diybl.com):http://www.diybl.com/course/3_program/c++/cppjs/20090302/156436.html

unit uTimeZonesMgr; interface uses Windows, SysUtils, Classes, Registry, DateUtils; type {* 用于读取时区注册表TZI(长度为44)的属性值,存储时区信息 *} PRegTZIInfo = ^TRegTZIInfo; TRegTZIInfo = record Bias: Longint; StandardBias: Longint; DaylightBias: Longint; StandardDate: TSystemTime; DaylightDate: TSystemTime; end; {* 单个时区管理对象 *} TTimeZone = class private FTimeZoneName: string; //时区的显示名 FDisplay: string; //夏令时的名字 FDlt: string; //时区标准名字 FStd: string; //是Time zone information的缩写,描述时区的一些重要信息 FTZI: PRegTZIInfo; function GetSelfTimeZoneInformation: TTimeZoneInformation; public constructor Create; destructor Destroy; override; function UTCToLocalDateTime(const AUTC: TDateTime; var ALocalDateTime: TDateTime): Boolean; function LocalDateTimeToUTC(const ALocalDateTime: TDateTime; var AUTC: TDateTime): Boolean; public property TimeZoneName: string read FTimeZoneName write FTimeZoneName; property Display: string read FDisplay write FDisplay; property Dlt: string read FDlt write FDlt; property Std: string read FStd write FStd; property TZI: PRegTZIInfo read FTZI write FTZI; end; {* 所有时区管理对象 *} TTimeZones = class private FTimeZoneKeyPath: string; FTimeZoneList: TStringList; FDefaultTimeZone: TTimeZone; procedure CollectTimeZone; procedure DestoryTimeZones; procedure CheckISDefaultTimeZone(ATimeZone: TTimeZone); public constructor Create; destructor Destroy; override; function FindTimeZone(const ADisplay: string): TTimeZone; public property TimeZoneList: TStringList read FTimeZoneList; property DefaultTimeZone: TTimeZone read FDefaultTimeZone; end; implementation { TTimeZones } procedure TTimeZones.CheckISDefaultTimeZone(ATimeZone: TTimeZone); var DefaultTimeZone: TTimeZoneInformation; begin GetTimeZoneInformation(DefaultTimeZone); if (ATimeZone.TZI.Bias = DefaultTimeZone.Bias) and (ATimeZone.Std = DefaultTimeZone.StandardName) then FDefaultTimeZone := ATimeZone; end; procedure TTimeZones.CollectTimeZone; var reg, tempReg: TRegistry; tempKeyPath: string; tempTimeZoneStrings: TStrings; iCir: Integer; tempTimeZone: TTimeZone; begin reg := TRegistry.Create; try reg.RootKey := HKEY_LOCAL_MACHINE; reg.OpenKey(FTimeZoneKeyPath, False); tempTimeZoneStrings := TStringList.Create; try reg.GetKeyNames(tempTimeZoneStrings); for iCir := 0 to tempTimeZoneStrings.Count - 1 do begin tempKeyPath := FTimeZoneKeyPath + '\' + tempTimeZoneStrings.Strings[iCir]; tempReg := TRegistry.Create; try tempReg.RootKey := HKEY_LOCAL_MACHINE; tempReg.OpenKey(tempKeyPath, False); tempTimeZone := TTimeZone.Create; tempTimeZone.TimeZoneName := tempTimeZoneStrings.Strings[iCir]; tempTimeZone.Display := tempReg.ReadString('Display'); tempTimeZone.Std := tempReg.ReadString('Std'); tempTimeZone.Dlt := tempReg.ReadString('Dlt'); tempReg.ReadBinaryData('TZI', tempTimeZone.TZI^, SizeOf(TRegTZIInfo)); FTimeZoneList.AddObject(tempTimeZone.Display, tempTimeZone); if FDefaultTimeZone = nil then CheckISDefaultTimeZone(tempTimeZone); finally tempReg.CloseKey; tempReg.Free; end; end; finally tempTimeZoneStrings.Free; end; finally reg.CloseKey; reg.Free; end; end; constructor TTimeZones.Create; begin FTimeZoneKeyPath := '\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones'; FTimeZoneList := TStringList.Create; FTimeZoneList.Sorted := True; FDefaultTimeZone := nil; CollectTimeZone; end; procedure TTimeZones.DestoryTimeZones; var iCir: Integer; tempTimeZone: TTimeZone; begin for iCir := 0 to FTimeZoneList.Count - 1 do begin tempTimeZone := TTimeZone(FTimeZoneList.Objects[iCir]); tempTimeZone.Free; end; FTimeZoneList.Clear; FTimeZoneList.Free; end; destructor TTimeZones.Destroy; begin DestoryTimeZones(); inherited; end; function TTimeZones.FindTimeZone(const ADisplay: string): TTimeZone; var iIndex: Integer; begin Result := nil; FTimeZoneList.Sort; if FTimeZoneList.Find(ADisplay, iIndex) then begin Result := TTimeZone(FTimeZoneList.Objects[iIndex]); end; end; { TTimeZone } constructor TTimeZone.Create; begin FTZI := GetMemory(SizeOf(TRegTZIInfo)); FillMemory(FTZI, SizeOf(TRegTZIInfo), 0); end; destructor TTimeZone.Destroy; begin FreeMemory(FTZI); inherited; end; function TTimeZone.GetSelfTimeZoneInformation: TTimeZoneInformation; begin GetTimeZoneInformation(Result); Result.Bias := TZI^.Bias; Result.StandardBias := TZI^.StandardBias; Result.StandardDate := TZI^.StandardDate; Result.DaylightBias := TZI^.DaylightBias; Result.DaylightDate := TZI^.DaylightDate; end; function TTimeZone.LocalDateTimeToUTC(const ALocalDateTime: TDateTime; var AUTC: TDateTime): Boolean; var tempLocalToLocal: TDateTime; iMilliSecond: Int64; begin Result := UTCToLocalDateTime(ALocalDateTime, tempLocalToLocal); if Result then begin if tempLocalToLocal > ALocalDateTime then begin iMilliSecond := MilliSecondsBetween(tempLocalToLocal, ALocalDateTime); AUTC := IncMilliSecond(ALocalDateTime, iMilliSecond * -1); end else begin iMilliSecond := MilliSecondsBetween(ALocalDateTime, tempLocalToLocal); AUTC := IncMilliSecond(ALocalDateTime, iMilliSecond); end; Result := True; end; end; function TTimeZone.UTCToLocalDateTime(const AUTC: TDateTime; var ALocalDateTime: TDateTime): Boolean; var TimeZone: TTimeZoneInformation; stUTC, stLC: SYSTEMTIME; begin Result := False; TimeZone := GetSelfTimeZoneInformation; DateTimeToSystemTime(AUTC, stUTC); if SystemTimeToTzSpecificLocalTime(@TimeZone, stUTC, stLC) then begin ALocalDateTime := SystemTimeToDateTime(stLC); Result := True; end; end; end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值