Глава 37. Zend_TimeSync

37.1. Introduction

Zend_TimeSync is able to receive internet or network time from a timeserver using the NTP or SNTP protocol. With Zend_TimeSync the Zend Framework is able to act indepentendly from the timesettings of the server where it is running.

To be independent from the actual time of the server, Zend_TimeSync does internally work just with the difference of the real time which is send through NTP or SNTP and the internal servers time.

[Замечание] Background

Zend_TimeSync is not able to change the server's time, but it will return a Zend_Date instance from which the difference to the servers time can be worked with.

37.1.1. Why Zend_TimeSync ?

So why would someone use Zend_TimeSync ?

Normally every server within a multiserver farm will have a service running which syncronises the own time with a timeserver. So within a standard environment it should not be necessary to use Zend_TimeSync. But it can become handy if there is no service available and if you don't have the right to install such a service.

Here are some example usecases, where Zend_TimeSync is perfect suited for:

  • Server without timeservice

    If your application is running on a server and this server does not have any timeservice running it can be good to implement Zend_TimeSync within the own application.

  • Seperated database server

    If your database is running on a seperated server and the other server is not connected with NTP or SNTP to the application server you would expect problems with data stored into the database where timestamps are used.

  • Multiple servers

    If your application is running on more than one server and the timebase of this servers are not coupled together you can expect problems within your application when part of the application are coming from one server and others from other servers.

  • Batch processing

    If your want to include or work with a timeservice within a batch file or within a command line application.

In all this cases Zend_TimeSync is a perfect solution and can be used if you are not able to run any service on your server.

37.1.2. What is NTP ?

The Network Time Protocol (NTP) is a protocol for synchronizing the clocks of computer systems over packet-switched, variable-latency data networks. NTP uses UDP port 123 as it's transport layer. See this wikipedia article for details about this protocol.

37.1.3. What is SNTP?

The Simple Network Time Protocol (SNTP) is a protocol for syncronising with clocks of computer systems over packet-switched, variable-latency data networks. SNTP uses UDP port 37 as it's transport layer. It is nearly related to the NTP Protocol but simpler.

37.1.4. Problematic usage

Be warned that when you are using Zend_TimeSync you will have to think about some details related to the structure of timesync and the web itself. How problems can be avoided and best practice will be described here. Read carefully before using Zend_TimeSync.

37.1.5. Decide which server to use

You have to select the timeserver which you want to use very carefully. This has several reasons which are described here:

  • Distance

    The distance from the server where your application is running to the timeserver you are requesting. If your server is in europe it would make no sense to use a timeserver in tahiti. Select always a server which is not far away. This reduces the time for the request and reduced network load.

  • Speed

    How long it takes to receive the request is also relevant. Try some servers to get the best result. If you are requesting a server which is never accessible you will always have a unnecessary delay.

  • Splitting

    Do not use always the same server. All timeservers will lock request from servers which are flooding the server. If your application makes excessive use of timeservers you should not use a single timeserver but one of the pools described later.

So where can you find a timeserver ? Generally you can use any timeserver you know. This can be a timeserver within you LAN or any public timeserver you know. If you decide to use a public timeserver you should use a server pool. Serverpools are public addresses where you will get a random timeserver from the pool if you request the time. This way you will not have to split your requests. There are public serverpools available for different regions so you will not have any of the problems mentioned above.

Take a look at pool.ntp.org to get your nearest serverpool. So if your server is located within germany for example you can connect to 0.europe.pool.ntp.org and so on.

    Поддержать сайт на родительском проекте КГБ