Solved

Reading system time from server

Posted on 2002-04-23
5
150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

617 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