We help IT Professionals succeed at work.

System Time

Wim_B
Wim_B asked
on
Medium Priority
848 Views
Last Modified: 2012-05-04
Hello,

Can someone give me an example of setting the system-time in Delphi. Or a component with source.
It must work also under Windows NT....!

Greetings,

Wim
Comment
Watch Question

Jason SwebyTechnical Director
CERTIFIED EXPERT

Commented:
function ChangeSystemDateTime(dtNeeded: TDateTime): Boolean;
var
  dtNeeded: TDateTime;
  tzi: TTimeZoneInformation;
  dtSystem: TSystemTime;
begin
  GetTimeZoneInformation(tzi);
  dtNeeded := dtNeeded + tzi.Bias / 1440;
  with dtSystem do
  begin
    wYear := StrToInt(FormatDateTime('yyyy', dtNeeded));
    wMonth := StrToInt(FormatDateTime('mm', dtNeeded));
    wDay := StrToInt(FormatDateTime('dd', dtNeeded));
    wHour := StrToInt(FormatDateTime('hh', dtNeeded));
    wMinute := StrToInt(FormatDateTime('nn', dtNeeded));
    wSecond := StrToInt(FormatDateTime('ss', dtNeeded));
    wMilliseconds := 0;
  end;
  SetPCSystemTime := SetSystemTime(dtSystem);
end;

Also note that, to adhere with the Windows way of working, you should send the WM_TIMECHANGE message to all programs if you change the system clock so that other programs that need to know that this has happened can be made aware of the change. See the help in the Windows API help on WM_TIMECHANGE.

J.
Commented:
Try the following code, It's a win32 command.

I cannot try this on Win NT but it works on win98.

Paul.



procedure TForm1.Button1Click(Sender: TObject);
var
  systime:systemtime;
begin
  systime.wYear:=2002;
  systime.wMonth:=12;
  systime.wDay:=04;
  systime.wHour:=11;
  systime.wMinute:=05;
  systime.wSecond:=00;
  systime.wMilliseconds:=00;
  setlocaltime(systime);
end;


Jason SwebyTechnical Director
CERTIFIED EXPERT

Commented:
chunky1,

Please can you leave suggested answers as comments please, this is the accepted way of working. By Proposing the answer, you take the question out of the Questions Awaiting Answers section and put it into the Locked Questions section. Not only this, but as it isn't the only solution available to Wim, he might want to weigh up the options.

We have two answers here with differing solutions, yours in no more the correct answer than mine.

J.

Commented:
var
        a:systemtime;
begin
        a.wyear:=2001;
        a.wmonth:=1;
        .
        .
        .
        .
        .
 
        setsystemtime(a);
end;







defenition of systemtime record

typedef struct _SYSTEMTIME {  // st  
    WORD wYear;
    WORD wMonth;
    WORD wDayOfWeek;
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME;
 

Members

wYear

Specifies the current year.

wMonth

Specifies the current month; January = 1, February = 2, and so on.

wDayOfWeek

Specifies the current day of the week; Sunday = 0, Monday = 1, and so on.

wDay

Specifies the current day of the month.

wHour

Specifies the current hour.

wMinute

Specifies the current minute.

wSecond

Specifies the current second.

wMilliseconds

Commented:
jsweby,

 Who got out of bed the worng side this morning then !!!

Only following the obvious options on the screen.


P.
Jason SwebyTechnical Director
CERTIFIED EXPERT

Commented:
My message wasn't nasty or personal, just pointing out the fatcs. Three answers have now appeared here, I've been on this site for years and the common approach, whatever options are available to you, are to post your solution as a comment - Wim has the chance to turn comments into answers, you see. Look at other questions, see how many answers are proposed just as a comment.

Don't take this as a personal attack.

J.

Commented:
jsweby,

 Who got out of bed the worng side this morning then !!!

Only following the obvious options on the screen.


P.

Author

Commented:
The answer of jsweby is correct too! But you used the function 'setlocaltime', which is easier for me...

Author

Commented:
An answer as comment is better, like jsweby describes...

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