Seekford Solutions Inc SSI    Software Development Technologies

Developer Tools

Resources

About Seekford 

Quick Support 

Send Me To Iraq? Save Me from Iraq?
Protocols
ActiveX
.NET

 

Encodings
ActiveX
.NET

 

Hashes
ActiveX
.NET

 


Latest Articles 

 

View all Articles

 

How Can I Synchronize the Client Machines CPU Clock?

Overview.. 1

What is the Computer CPU Clock?. 1

How does a CPU clock work?. 1

Why doesn’t it stay accurate?. 2

Why should you periodically synchronize the CPU Clock?. 2

How can I synchronize the CPU Clock in my application?. 2

What is SNTP?. 2

What is SNTP Wizard v2?. 2

Simple Diagram of Network Architecture?. 2

Could you show me some example code for SNTP Wizard?. 3

[Visual Basic] 3

[VBScript] 3

[Visual C++] 3

[Visual FoxPro] 4

Who makes SNTP Wizard?. 4

Where can I find more information on this product?. 4

Overview

If you have a client server application you may have noticed that the database entries with timestamps taken from the local machines CPU clock are not always very accurate. A process that occurs simultaneously on multiple machines can all have very different time entries. This is due to many reasons; the main being the user just doesn’t set the clock very precisely. There are other reasons though and that is due to the flaws in the CPU clock. All clock have a minimal amount of time differential in their clock updates. This slight imperfection in the accuracy can cause loss of time in the milliseconds to almost a minute a day. This article discusses all the aspects of why you need to synchronize your CPU clock and how to go about doing it with precision and accuracy, not to mention dependability and reliability.

What is the Computer CPU Clock?

The computer CPU clock is the timing device that is built-in to the motherboard to keep track of the current time and date. The clock is generally battery backed up so that the time is kept current even when no power is applied to the machine. Every computer on the market has a basic clock and many electronic devices have them. The CPU clock is very important since it is used as the basis for many operations undertaken by applications and hardware. One example is communications. Serial communications as well as Parallel port communications depend on the CPU clocks frequency counts to control the transmission process of the data flow. A very large number of business applications require the use of the CPU clock as well for logging functions, data processing, database entries, charting functionality and more.

How does a CPU clock work?

The CPU clock works off of a quartz crystal. Quartz -- which is silicon dioxide like most sand -- is unaffected by most solvents and remains crystalline to hundreds of degrees Fahrenheit. The property that makes it useful is that when compressed or bent, it generates a charge or voltage on its surface. This is called the piezoelectric effect. In the same way, if a voltage is applied, quartz will bend or change its shape very slightly.

A quartz crystal is used within the clock. Often, these crystals are made from thin sheets of quartz plated like an integrated circuit and etched chemically to shape. The major difference between good and indifferent time keeping is the initial frequency accuracy and the precision of the angle of cut of the quartz sheet with respect to the crystalline axis. If contaminants are able to reach the crystal, the accuracy can be affected.

The electronics of the clock initially amplifies noise at the crystal frequency. This builds or regenerates into oscillation -- it starts the crystal ringing. The output of the crystal oscillator is then converted to pulses suitable for the digital circuits. These divide the crystal's frequency down to translate into time. Devices use the clocks pulses for driving processes that they need very precise timing for.

Why doesn’t it stay accurate?

No watch or clock is perfect. The Atomic clocks are the closest to being perfectly accurate as possible, but the standard quartz clock can have small deviations. Many computers will lost from a few hundred milliseconds to up to a few second every single day. Considering the average user sets the clock by the minute and then factoring that they don’t reset the clock for months, it is quite possible the time the computer’s CPU clock registers is actually very incorrect. The time keeping flaw is very insignificant when looked at over a small period of time but multiply the flaw by a large period of time and the issue becomes very noticeable.

Why should you periodically synchronize the CPU Clock?

Due to the inaccuracy of the time keeping mechanism it is important to reset the clock to the exact time periodically. It may be overkill to set it more than daily but setting it every morning is generally a pretty good idea. This keeps the clock the most accurate during the workday. It is also possible that the user may have mistakenly set the clock to the wrong time or date. It is quite common that a user will set the clock by their watch or some other device that is already registering a deviated time. It is general knowledge that the average clock is set an accuracy of only minutes. Very rarely do people set their watch or desk clocks to the second. If you then add the deviation from the correct time plus the deviation they set their CPU clock from their watch you will notice a large degree of inaccuracy.

How can I synchronize the CPU Clock in my application?

A program that is running on the machine can easily synchronize the CPU clock. The best method of synchronization is to request the current time from a highly accurate source. One such method is to interface with a radio frequency clock or perhaps an atomic clock. This is possible using the SNTP or Simple Network Time Protocol.

What is SNTP?

The Simple Network Time Protocol is used for the synchronization of time and date information from a time server. Some time servers are just a standard PC server and returns the time from its local clock, other time servers are much more accurate and they return the time from an Atomic Clock or a highly accurate radio frequency clock. SNTP uses the UDP transmission method and sends a packet request to the server. The server then responds with a specially formatted data packet that contains the time information as well as some information that allows for the calculation of the packet delay so that a more accurate reading can be made. When a packet is sent over the Internet, the target recipient does not instantly receive the data. There is a latency time for the send and receive process and this needs to be calculated so that it can be applied to the time returned by the server, otherwise the data would be incorrect. The protocol also specifies that the returned time is sent in UTC or Universal Coordinated Time (also known as Greenwich Mean Time).

What is SNTP Wizard v2?

SNTP Wizard v2 is an ActiveX control (OCX) that implements the Simple Network Time Protocol (SNTP) and allows for an easy to use interface for programmers. The control provides a high level abstract view of how the protocol is implemented. The use of the control allows for the simple setting of a time server and then issuing the GetTime command which synchronizes with the server and grabs the exact time from it. It has the option of also automatically setting the local machine CPU clock to the retrieved time for the most accurate setting of the clock. (No delays for the invoking code processing the returned data)

The SNTP only returns the time data in Universal Coordinate Time, SNTP Wizard automatically provides both the UTC time and a converted Local time which takes into account the local machines Time Zone and converts it to the proper time. This makes implementation even easier.

One of the key features for this product is the ability to use it for Time Stamping. You can guarantee that all DATE and TIME record entries are accurate and in synch with the other applications. For distributed applications though we recommend using the Universal Coordinated Time as the main data Time/Date record entry and if possible using the Local time as a secondary entry. This way your application can determine the Time Zone setting but all the applications can see the actual time the data was created. Conflicts can occur when different time zones otherwise.

Simple Diagram of Network Architecture?

Could you show me some example code for SNTP Wizard?

The code below is a basic example of using the Product. Please look for your language to see the implementation.

[Visual Basic]

Assumptions:

Form created with 3 Textboxes, 1 checkbox,  and 1 SNTPWizard control.

Code:

 Dim lTimeDiff As Long

 SNTPWizard1.TimeServer = txtTimeServer.Text

 SNTPWizard1.TimeServerPort = CInt(txtTimeServerPort.Text)

 SNTPWizard1.TimeOut = CInt(txtTimeOut.Text)

 SNTPWizard1.AutoSetLocalClock = IIf(chkSetLocaltime.Value, True, False)

 lTimeDiff = GetTickCount()

 If SNTPWizard1.GetTime Then

   lTimeDiff = GetTickCount - lTimeDiff

   If (chkUseEvents.Value = 0) Then

    lblLocalTime = "Local time:  " & CStr(SNTPWizard1.LocalTime)

    lblUTCTime = "UTC        :  " & CStr(SNTPWizard1.UTCTime)

   End If

  Else

    If (chkUseEvents.Value = 0) Then

        lblLocalTime = "Error#" & SNTPWizard1.LastErrorNumber

        lblUTCTime = SNTPWizard1.LastErrorDescription

    End If

 End If

[VBScript]

MsgBox "Seekford Solutions, Inc. SNTP Wizard VBScript Example"

dim MySNTPWizard

set MySNTPWizard  = CreateObject("SNTPWIZARD.SntpWizardCtrl2")

MySNTPWizard.UnlockSNTPWizard("") '<------- ENTER SERIAL NUMBER HERE

MySNTPWizard.TimeServer = "time.mit.edu"

if (MySNTPWizard.GetTime()) then

msgbox("UTC Time:   " + cstr(MySNTPWizard.UTCTime) + vbcrlf + "Local Time: " + cstr(MySNTPWizard.LocalTime))       

else

            msgbox("Error!" + vbcrlf + "Error Number:" + CStr(MySNTPWizard.LastErrorNumber) + vbcrlf + "Error Description:" + MySNTPWizard.LastErrorDescription)

end if

MsgBox "Please Visit http://www.SeekfordSolutions.com and purchase a license for this great product!"

[Visual C++]

Assumptions:

Dialog with 1 SNTPWizard, 3 Textboxes, 1 Checkbox

Member variables –

m_SNTPWizarad associated with SNTPWizard control

m_iTimeOut, m_sServer, and m_iPort associated with textboxes

m_bAutoSet associated with the checkbox

Code:

CString sTemp;           

m_SNTPWizard.UnlockSNTPWizard("");

            UpdateData(TRUE);

            m_SNTPWizard.SetTimeOut(m_iTimeOut);

            m_SNTPWizard.SetTimeServer(m_sServer);

            m_SNTPWizard.SetTimeServerPort(m_iPort);

            m_SNTPWizard.SetAutoSetLocalClock(m_bAutoSet);

            if (m_SNTPWizard.GetTime())

            {

                        sTemp.Format(“%s\r\n%s”, CString("Local Time: ") + COleDateTime(m_SNTPWizard.GetLocalTime()).Format(),

CString("UTC Time:  ") + COleDateTime(m_SNTPWizard.GetUTCTime()).Format());

                        AfxMessageBox(sTemp);

            }else

            {

                        sTemp.Format(“%s\r\nError#: %d”, m_SNTPWizard.GetLastErrorDescription(), m_SNTPWizard.GetLastErrorNumber());

            }

[Visual FoxPro]

&& This code will retrieve the current time from the internet

            MessageBox( "Seekford Solutions, Inc. SNTP Wizard Foxpro Example")

            local MySNTPWizard

            MySNTPWizard  = CreateObject("SNTPWIZARD.SntpWizardCtrl2")

            MySNTPWizard.UnlockSNTPWizard("") &&<------- ENTER SERIAL NUMBER HERE

            MySNTPWizard.AutoSetLocalClock = .t. && This tells SNTP Wizard to automatically set the client CPU Clock

            MySNTPWizard.TimeServer = "time.mit.edu"

            if (MySNTPWizard.GetTime()) then

                        messagebox("UTC Time:   " + TTOC(MySNTPWizard.UTCTime) + chr(13) + "Local Time: " + TTOC(MySNTPWizard.LocalTime))  

            else

                        messagebox("Error!" + chr(13) + "Error Number:" + Str(MySNTPWizard.LastErrorNumber) + chr(13) + "Error Description:" + MySNTPWizard.LastErrorDescription)

            endif

            messageBox( "Please Visit http://www.SeekfordSolutions.com and purchase a license for this great product!")

Who makes SNTP Wizard?

Seekford Solutions, Inc. is a software development corporation specializing in the design and development of state of the art ActiveX controls and custom projects. Their core product line is focused on Internet technologies primarily in the facilitation of the use of the common Internet protocols. The design philosophy is based on ease of use and quick implementation time. They also handle custom projects for clients who are need of specialty software or who need a framework base to use. The company was founded in early 2001. Their website is http://www.seekfordsolutions.com/.

Where can I find more information on this product?

It is available from Seekford Solutions, Inc. at the products website of http://www.seekfordsolutions.com/Products/SNTPWizard

 



 

© 2001-2005 Seekford Solutions, Inc. All rights reserved.  Privacy Policy  Site Map