Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

Reading system time from server

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
JDN
Asked:
JDN
1 Solution
 
DragonSlayerCommented:
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
 
TAZICommented:
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
 
TOndrejCommented:
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
 
JDNAuthor Commented:
Your routine works perfect. This was exactly what I needed.
0
 
TOndrejCommented:
Glad to hear that :-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now