Rede - pegar data e hora de um servidor |
Top Previous Next |
|
// pegar a data-hora de um servidor // só funciona no NT/2000/XP type NET_API_STATUS = DWORD;
type PTime_Of_Day_Info = ^TTime_Of_Day_Info; TTime_Of_Day_Info = record tod_elapsedt :Integer; tod_msecs :Integer; tod_hours :Integer; tod_mins :Integer; tod_secs :Integer; tod_hunds :Integer; tod_timezone :LongInt; tod_tinterval :Integer; tod_day :Integer; tod_month :Integer; tod_year :Integer; tod_weekday :Integer; end;
function NetRemoteTOD(ServerName :PWideChar; var buffer :pointer) :Integer; stdcall; external 'NetApi32.Dll'; function NetApiBufferFree(buffer : Pointer) :Integer; stdcall; external 'NetApi32.Dll';
var Form1: TForm1; ElapseDt :Integer; MSecs :Integer; Hours :Integer; Mins :Integer; Secs :Integer; Hunds :Integer; TimeZone :LongInt; TInterval :Integer; Day :Integer; Month :Integer; Year :Integer; WeekDay :Integer;
implementation
{$R *.DFM}
function GetTOD(ServerName: WideString): Integer; var Buffer :Pointer; Rek :PTime_Of_Day_Info; ADate :TDateTime; ATime :TDateTime; FinalTime :TDateTime; THours, TMins, TSecs, TYear, TMonth, TDay :Word; begin
Result := NetRemoteTOD(PWideChar(ServerName), Buffer);
if Result = 0 then begin Rek := PTime_Of_Day_Info(Buffer); ServerName := ServerName; ElapseDt := Rek.tod_elapsedt; MSecs := Rek.tod_msecs; Hours := Rek.tod_hours; Mins := Rek.tod_mins; Secs := Rek.tod_secs; Hunds := Rek.tod_hunds; TInterval := Rek.tod_tinterval;
if Rek.tod_timezone <> -1 then TimeZone:=Rek.tod_timezone div 60 else TimeZone:=0;
Day := Rek.tod_day; Month := Rek.tod_month; Year := Rek.tod_year; WeekDay := Rek.tod_weekday;
ADate := EncodeDate(Year, Month, Day); ATime := EnCodeTime(Hours, Mins, Secs, Hunds);
FinalTime := StrToDateTime(DateTimeToStr(ADate) + ' ' + TimeToStr(ATime)); FinalTime := FinalTime - (TimeZone / 24);
Decodetime(FinalTime, THours, TMins, TSecs, TSecs);
Hours := Integer(THours);
DecodeDate(FinalTime, TYear, TMonth, TDay); Year := Integer(TYear); Month := Integer(TMonth); Day := Integer(TDay); end; NetApiBufferFree(Buffer); end;
function GetDateTime: string; var TheTime :TDateTime; TheDate :TDateTime; TheSTime :String; TheSDate :String; begin GetTOD(Form1.Edit1.Text); TheTime := EncodeTime(Word(hours),Word(mins),Word(secs),Word(Hunds)); TheSTime := TimeToStr(TheTime); TheDate := EncodeDate(Year,Month,Day); TheSDate := DateToStr(TheDate); Result := TheSDate + ' ' + TheSTime; end;
procedure TForm1.Button1Click(Sender: TObject); begin Label2.Caption := ''; Label2.Caption := GetDateTime; end;
////////////////////////// Other - win9x:
um exemplo
fName := 'umnomequalquer.tmp'; AssignFile(F, fName); Rewrite(F); CloseFile(F); D := GetFileDate(fName); DeleteFile(fName);
// a variavel D conterá a data do arquivo.
function GetFileDate(FileName: string): TDateTime; var Filehandle, Filedate: longint; begin Filehandle := FileOpen(FileName, fmShareDenyNone); Filedate := FileGetDate(Filehandle); FileClose(Filehandle); Result := FileDateTodateTime(FileDate); end;
|