RAD Studio my sample to review your TimeZone values and DayLight include changes if necessary by code!

emailx45

BANNED
Регистрация
5 Май 2008
Сообщения
3,582
Реакции
2,451
Credits
578
my sample to review your TimeZone values and DayLight include changes if necessary by code!
[SHOWTOGROUPS=4,20]
UTC = local time + bias

Microsoft API:
Как увидеть ссылки? | How to see hidden links?
Как увидеть ссылки? | How to see hidden links?

Как увидеть ссылки? | How to see hidden links?
Код:
unit uFormMain;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    btnGetTimeZoneLocal: TButton;
    Memo1: TMemo;
    Memo2: TMemo;
    procedure btnGetTimeZoneLocalClick(Sender: TObject);
    private
      { Private declarations }
    public
      { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnGetTimeZoneLocalClick(Sender: TObject);
var
  lTZ: TTimeZoneInformation; // WinAPI.Windows.pas
  // lDividend: SmallInt;
  i            : Integer;
  lStandardName: string;
  lDaylightName: string;
  lText        : string;
  lGMTvalue    : Integer;
begin
  Memo1.Clear;
  //
  lGTZInfoValue := GetTimeZoneInformation(lTZ); // == return equal to Cardinal type
  //
  // Returns a Long value that represents the difference in minutes between the local time in this time zone and the Coordinated Universal (UTC) time.
  // lTZ.Bias := 120;
  // Returns a Long value that represents the travel time in minutes of the Bias to take into account daylight saving time in this time zone
  // lTZ.DaylightBias := -60;
  //
  // lTZ.StandardDate.wYear .. wMilliseconds
  // lTZ.DaylightDate.wYear .. wMilliseconds
  //
  // IMPORTANT: THE "SetTimeZoneInformation()" change your MSWindows Time/Date setting!
  // then, warning when using this command!
  //
  // For example, when changing this setting, your IDE and any others apps can dont working anymore
  // because this apps "watch" the date and time to works!
  //
  // --->>>> SetTimeZoneInformation(lTZ); // == return equal to Cardinal type
  //
  // SetDynamicTimeZoneInformation(lTZ) for MSWindows 7,8 (mayber for later editions)
  //
  // this functions needs privilegies to operate, or, an error can occurr!
  //
  //
  Memo1.Lines.Add(Format('Bias = %d'#13#10'DayLightBias = %d', [lTZ.Bias, lTZ.DaylightBias]));
  //
  Memo1.Lines.Add(Format('GMT = %d', [lTZ.Bias div lTZ.DaylightBias]));
  //
  lStandardName := ''; // 31 chars
  lDaylightName := ''; // 31 chars
  //
  for i := 0 to 31 do // if 31 is for two... then
    begin
      lStandardName := lStandardName + lTZ.StandardName[i];
      lDaylightName := lDaylightName + lTZ.DaylightName[i];
    end;
  //
  {
    //or if you want, do it this:

    for lText in lTZ.StandardName do
    lStandardName := lStandardName + lText;
    //
    for lText in lTZ.DaylightName do
    lDaylightName := lDaylightName + lText;
    //
  }
  Memo1.Lines.Add(Format('StandardName = %s', [lStandardName]));
  Memo1.Lines.Add(Format('DaylightName = %s', [lDaylightName]));
end;

end.

Here, a sample by Microsoft wroted in C++ on DocWiki

Код:
#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;
}

[/SHOWTOGROUPS]
 
Последнее редактирование: