Solved

Reading system time from server

Posted on 2002-04-23
5
146 Views
Last Modified: 2010-04-04
Hi Experts,

I my company we're using a small Delphi program on all workstations that's using the system time. It is important that this time is the actual time.
Problem is that the system time on the different workstation differs. You can of course adjust the time of all system clocks, so they all supply the same time. But within a few days there will be a difference again between all workstation.

Now, to be sure my program is registering the same time on all workstations, I like to read the system time of one single pc, for instance the Windows NT server.

Can anyone tell me how to do this? Or is there maybe another way to read the same time on all my workstations?

Thanks,
JDN
0
Comment
Question by:JDN
5 Comments
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6964871
You can use the NISTIME utility which can be downloaded here:

http://www.parkenet.com/apl/NISTime.htm

it will basically synchronise the time on machines based on a time server (it could be a global server or a server that you set up).


HTH
DragonSlayer
0
 
LVL 1

Expert Comment

by:TAZI
ID: 6965161
Hi

If you're making use of SQL Server ... to get the system time from the Server you use the following code:

Select getdate() ... this will return the current system date and time from the server.

You can also syncronize the time on startup of the application.

Regards
0
 
LVL 8

Accepted Solution

by:
TOndrej earned 50 total points
ID: 6965431
Hi, see if this ServerTime function (which I wrote long time ago) works for you.

unit NetTime;

interface

uses
  SysUtils;

type
  TTODInfo = record
    ElapsedTime : Integer; {number of seconds since 00:00:00 January 1, 1970}
    Milliseconds : Integer; {number of milliseconds since last system reset}
    Hours : Integer; {current hour (0-23)}
    Minutes : Integer; {current minute (0-59)}
    Seconds : Integer; {current second (0-59)}
    Hunds : Integer; {current hundredth of a second (0-99)}
    TimeZone : Integer; {time against GMT in minutes}
      {west of Greenwich gives positive, east negative values}
      {value of -1 means undefined time zone}
    Interval : Integer; {clock tick interval in ten-thousandth of a second (0.0001 s)}
    Day : Integer; {day of the month (1-31)}
    Month : Integer; {month of the year (1-12)}
    Year : Integer; {year}
    Weekday : Integer; {day of the week (0-6) 0 = Sunday, 1 = Monday etc.}
  end;
  PTODInfo = ^TTODInfo;

const
  netapi32 = 'netapi32.dll';

function NetApiBufferFree(Buffer: Pointer): Integer; stdcall;
function NetRemoteTOD(UNCServerName: PWideChar; Info: Pointer): Integer; stdcall;

function ServerTime(const UNCServer: String; var Stamp: TDateTime): Integer;

implementation

function NetApiBufferFree; external netapi32 name 'NetApiBufferFree';
function NetRemoteTOD; external netapi32 name 'NetRemoteTOD';

// UNCServer: UNC server name, e.g. \\MYSERVER1
//            blank = local machine
// Stamp: returned time
// Result: 0 if successful, otherwise net error code

function ServerTime(const UNCServer: string; var Stamp: TDateTime): Integer;
var
  ServerName : PWideChar;
  tod : PTODInfo;
  Year, Month, Day, Hour, Min, Sec, MSec : Word;
begin
  GetMem(ServerName, (Length(UNCServer) + 1) * SizeOf(WideChar));
  try
    ServerName := StringToWideChar(UNCServer, ServerName, Length(UNCServer) + 1);
    Result := NetRemoteTOD(ServerName, @tod);
    if Result = 0 then
    begin
      try
        Year := tod^.Year;
        Month := tod^.Month;
        Day := tod^.Day;
        Hour := tod^.Hours;
        Min := tod^.Minutes;
        Sec := tod^.Seconds;
        MSec := tod^.Hunds * 10;
        if tod^.TimeZone = -1 then {undefined timezone}
          Stamp := EncodeDate(Year, Month, Day) +
            EncodeTime(Hour, Min, Sec, MSec)
        else
          Stamp := EncodeDate(Year, Month, Day) +
            EncodeTime(Hour, Min, Sec, MSec) - (tod^.TimeZone / 1440);
      finally
        NetApiBufferFree(tod);
      end;
    end;
  finally
    FreeMem(ServerName);
  end;
end;

end.

HTH
TOndrej
0
 
LVL 1

Author Comment

by:JDN
ID: 6965604
Your routine works perfect. This was exactly what I needed.
0
 
LVL 8

Expert Comment

by:TOndrej
ID: 6965625
Glad to hear that :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now