We help IT Professionals succeed at work.

How to get system's Daylight setting?

ysimon
ysimon asked
on
In Delphi programming, I'd like to obtain the system date and time and know if current time is daylight or not.
Thanks
ysimon
Comment
Watch Question

Commented:
I think you need to use the TTimeZoneInformation & getTimeZoneInformation

GL
Mike

Commented:
yeah something like this should work:

function isDayLightSavings:boolean;
var
 tz : TTimeZoneInformation;
begin
 result := getTimeZoneInformation(tz) = TIME_ZONE_ID_DAYLIGHT;
end;


GL
Mike

Commented:
ysimon,

In addition to the previous comments, the following code will extract the local time, UTC, the offset bias and a flag to indicate whether daylight saving is in operation:

type TTimeInfo = record
  LocalTime: TDateTime;
  UTC       : TDateTime;
  TZBias    : TDateTime;
  IsDayLight: Boolean;
  end;

function GetTimeInfo: TTimeInfo;
var
  rTZ : TTimeZoneInformation;
begin
//  Get Timezone Info...
  case GetTimeZoneInformation(rTZ) of
    TIME_ZONE_ID_UNKNOWN  : begin
//                          Don't know the SystemTime value. Assume PC is using UTC at the moment.
                            Result.TZBias := 0;
                            Result.IsDayLight := False;
                            end;
    TIME_ZONE_ID_STANDARD : begin
//                          PC is using "standard" time
                            Result.TZBias := (rTZ.StandardBias / 1440);
                            Result.IsDayLight := False;
                            end;
    TIME_ZONE_ID_DAYLIGHT : begin
//                          PC is using "daylight" time
                            Result.TZBias := (rTZ.DaylightBias / 1440);
                            Result.IsDayLight := True;
                            end;
    TIME_ZONE_ID_INVALID :  begin
                            Result.TZBias := 0;
                            Result.IsDayLight := False;
                            end;
    else begin
      Result.TZBias := 0;
      Result.IsDayLight := False;
      end;
    end;

  with Result do begin
    LocalTime := Now;
    UTC       := LocalTime + TZBias;
    end;
end;

To use it, the following code shows you what you can do (you'll need 3 TEdit and 1 TLabel components on a form):
var
  lInfo: TTimeInfo;
begin
  lInfo:= GetTimeInfo;
  Edit1.Text := FormatDateTime('hh:nn:ss, dd/mm/yyyy', lInfo.LocalTime);
  Edit2.Text := FormatDateTime('hh:nn:ss, dd/mm/yyyy', lInfo.UTC);
  Edit3.Text := FormatDateTime('hh:nn:ss', lInfo.TZBias);
  if (lInfo.TZBias < 0) then
    Edit3.Text := '+' + Edit3.Text
  else
    Edit3.Text := '-' + Edit3.Text;

  if lInfo.IsDayLight then
    Label4.Caption := 'In Daylight time now'
  else
    Label4.Caption := 'Using Standard time now'
end;

Commented:
Wow !

Would you mind adding that to the Delphi section of HowToDoThings.com ?  I love it !


Pete

Author

Commented:
Thanks edey and malsoft, you solved my problem.
BTW I'd like know that if there is a table to record all time difference between local standard time and GMT.
Maybe it doesn't exist.
ysimon

Author

Commented:
Thanks edey and malsoft, you solved my problem.
BTW I'd like know that if there is a table to record all time difference between local standard time and GMT.
Maybe it doesn't exist.
ysimon

Commented:
ysimon,

On my Win98SE machine, there's a whole slew of timezone information in the registry at:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Time Zones

and the current setting is at:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation

Unfortunately, the information you need to get at is held in a TZI binary field - the format of which I'm not 100% sure about (it may be a copy of the structure, but I would bet on that!)

Happy coding!
Commented:
This seems to work:


function DateTimeDelta(DateTime, Delta: TDateTime): TDateTime;
begin
  if DateTime < 0 then
    Result := DateTime - Delta
  else
    Result := DateTime + Delta;
end;

specified time-zone
function GetTzStandardDelta(TzInfo: TTimeZoneInformation): TDateTime;
begin
  Result := (TzInfo.Bias + TzInfo.StandardBias) / 1440;
end;

function GetTzDaylightDelta(TzInfo: TTimeZoneInformation): TDateTime;
begin
  Result := (TzInfo.Bias + TzInfo.DayLightBias) / 1440;
end;

function GetTimeZoneDelta: TDateTime;
var
  TzInfo: TTimeZoneInformation;
begin
  case GetTimeZoneInformation(TzInfo) of
    TIME_ZONE_ID_UNKNOWN: Result := TzInfo.Bias / 1440;
    TIME_ZONE_ID_STANDARD: Result := GetTzStandardDelta(TzInfo);
    TIME_ZONE_ID_DAYLIGHT: Result := GetTzDaylightDelta(TzInfo);
  else
    raise Exception.Create(SysErrorMessage(GetLastError));
  end;
end;

then call getTimeZoneDelta, the value it returns should be equal to (Greenwich Time - Current TimeZone - 1). So, for example if I call:

procedure TForm1.FormCreate(Sender: TObject);
begin
     caption := intToStr(-trunc(GetTimeZoneDelta*24)-1);
end;

I get -7 to match my windows tz setting (Mountain Time).

GL
Mike
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question. Again, please comment to advise the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below and include the question QID/link(s) that it regards.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Please click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.11866558.html
http://www.experts-exchange.com/questions/Q.20107980.html
http://www.experts-exchange.com/questions/Q.20112507.html
http://www.experts-exchange.com/questions/Q.20121203.html
http://www.experts-exchange.com/questions/Q.20122370.html
http://www.experts-exchange.com/questions/Q.20128022.html
http://www.experts-exchange.com/questions/Q.20128024.html
http://www.experts-exchange.com/questions/Q.20143035.html
http://www.experts-exchange.com/questions/Q.20145949.html
http://www.experts-exchange.com/questions/Q.20164917.html
http://www.experts-exchange.com/questions/Q.20164983.html
http://www.experts-exchange.com/questions/Q.20171798.html
http://www.experts-exchange.com/questions/Q.20179154.html
http://www.experts-exchange.com/questions/Q.20192667.html
http://www.experts-exchange.com/questions/Q.20192583.html
http://www.experts-exchange.com/questions/Q.20200806.html
http://www.experts-exchange.com/questions/Q.20201786.html
http://www.experts-exchange.com/questions/Q.20201947.html
http://www.experts-exchange.com/questions/Q.20246959.html
http://www.experts-exchange.com/questions/Q.20252474.html
http://www.experts-exchange.com/questions/Q.20259886.html
http://www.experts-exchange.com/questions/Q.20270462.html
http://www.experts-exchange.com/questions/Q.20273241.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20265181.html
http://www.experts-exchange.com/questions/Q.20277546.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
Zero response from anyone, finalized.
Moondancer - EE Moderator

Explore More ContentExplore courses, solutions, and other research materials related to this topic.