time_system - ryzom/ryzomcore GitHub Wiki


title: How does time synchronization work on the shard description: published: true date: 2023-03-01T05:18:26.827Z tags: editor: markdown dateCreated: 2022-03-08T07:07:11.785Z

This piece of documentation is quite old. Please help review it! {.is-warning}

Move these paragraphs to NeL Misc {.is-warning}

In the NeL Misc library, there's a class named CTime in the misc module (time_nl.h). This class encapsulates time functions provided by the OS.

The CUniTime class in the net module of NeL provides high level functions to get and set the universal time (unitime.h).

Time

Move these paragraphs to NeL Misc {.is-warning}

For example, the getSecondsSince1970() method returns the number of seconds since midnight (00:00:00), January 1, 1970, coordinated universal time, according to the system clock. This value is computer dependant. This value is the same only if computers are synchronized (with NTP for example).

getLocalTime() returns a time in millisecond. It is used to time a function or compute the frame rate. This value is different on each machine and the update frequency is OS dependant but should be greater than 100Hz.

getPerformanceTime() returns a number of ticks. Presently it works only on Windows ; on other systems, it'll return 0. This function is used to get a very accurate value, to monitor a piece of code for example. You can use ticksToSecond() to convert this tick value into seconds.

Universal Time

DEPRECATED (Time Service) {.is-danger}

The universal time is the basis of time in a NeL system. This value is the same on all computers and is used in all services and clients to date events. This value is a sint64 (TTime type) containing the number of milliseconds since the creation of the virtual universe.

The Time Service stores this value in a file and centralizes time requests from other services.

getUniTime() returns the current universal time but before calling this method the class must be initialized and synchronized

There are 2 different ways to synchronize this universal time:

  • in the case of a service in the shard:

All servers that will run services must synchronize their clock with NTP. If you don't do that, services will not run. Services are synchronized automatically at startup and are resynchronized every hour to avoid accumulation errors. So, there's nothing to add in your code for a service to get the universal time.

  • in the case of a client:

There are 2 methods in the class CUniTime, one that you have to call on the client part (syncUniTimeFromServer()) and another one in the front-end service (installServer()).

How it works ?

DEPRECATED (Time Service) {.is-danger}

We use 2 different algorithms depending of the type the connection between the 2 programs.

  • in the case of a service in the shard:

All servers are synchronized with NTP so the getSecondsSince1970() method returns the same value at the same time on 2 different servers. When a service when to synchronize his universal time, it sends a request to the Time Service. The Time Service sends back a message that contains 2 values: the value returns by the getSecondsSince1970() method and the value returns by the getUniTime() method. When the service receives this message, it computes the new universal time with the 2 values in the message.

  • in case of a client:

We actually use a very simple algorithm to synchronize the universal time across Internet. The client makes several requests for the univeral time to the front-end service, and, according to the ping, it will deduce the current universal time.

For more information, take a look at time_nl.h and unitime.h.

⚠️ **GitHub.com Fallback** ⚠️