Link to home
Start Free TrialLog in
Avatar of ysimon
ysimon

asked on

How to get system's Daylight setting?

In Delphi programming, I'd like to obtain the system date and time and know if current time is daylight or not.
Thanks
ysimon
Avatar of edey
edey

I think you need to use the TTimeZoneInformation & getTimeZoneInformation

GL
Mike
yeah something like this should work:

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


GL
Mike
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;
Wow !

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


Pete
Avatar of ysimon

ASKER

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
Avatar of ysimon

ASKER

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
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!
ASKER CERTIFIED SOLUTION
Avatar of edey
edey

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
https://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.  https://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.
https://www.experts-exchange.com/questions/Q.11866558.html
https://www.experts-exchange.com/questions/Q.20107980.html
https://www.experts-exchange.com/questions/Q.20112507.html
https://www.experts-exchange.com/questions/Q.20121203.html
https://www.experts-exchange.com/questions/Q.20122370.html
https://www.experts-exchange.com/questions/Q.20128022.html
https://www.experts-exchange.com/questions/Q.20128024.html
https://www.experts-exchange.com/questions/Q.20143035.html
https://www.experts-exchange.com/questions/Q.20145949.html
https://www.experts-exchange.com/questions/Q.20164917.html
https://www.experts-exchange.com/questions/Q.20164983.html
https://www.experts-exchange.com/questions/Q.20171798.html
https://www.experts-exchange.com/questions/Q.20179154.html
https://www.experts-exchange.com/questions/Q.20192667.html
https://www.experts-exchange.com/questions/Q.20192583.html
https://www.experts-exchange.com/questions/Q.20200806.html
https://www.experts-exchange.com/questions/Q.20201786.html
https://www.experts-exchange.com/questions/Q.20201947.html
https://www.experts-exchange.com/questions/Q.20246959.html
https://www.experts-exchange.com/questions/Q.20252474.html
https://www.experts-exchange.com/questions/Q.20259886.html
https://www.experts-exchange.com/questions/Q.20270462.html
https://www.experts-exchange.com/questions/Q.20273241.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
https://www.experts-exchange.com/questions/Q.20265181.html
https://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 https://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