Solved

Reading system time from server

Posted on 2002-04-23
5
145 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Your routine works perfect. This was exactly what I needed.
0
 
LVL 8

Expert Comment

by:TOndrej
Comment Utility
Glad to hear that :-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

771 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

12 Experts available now in Live!

Get 1:1 Help Now