Solved

Reading system time from server

Posted on 2002-04-23
5
147 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Internet Explorer View Settings Question 15 111
select query - oracle 16 100
When i run adoquery my application freezes 26 166
Firemonkey DbLookupComboBox equivalent ? 2 47
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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