index previous next 

file name: time.hpp, time.cpp
classification: simulation
contents: class CNtpTime, struct STimestamp, struct SExtendedTimestamp, and several functions for converting NTP-time into seconds and vice versa
use: provide classes for storing the NTP time (bits [+31,-24]) and for conversion between NTP and seconds
 

global items used:

local items used:

const INT32 Ceiling: The maximum value of the ts member of CNtpTime.

const double TwoPow8:
const double TwoPow24:
const double TwoPow56: some powers of two needed for internal computations.
 

structs:

struct SExtendedTimestamp: is derived from STimestamp and additionally contains the microstamp (us) and two assignment functions: operator=(const STimestamp&) is used to assign an object of the base class to this class (all inherited members are copied, and us is set to 0), whereas operator=(const CNtpTime&) is used to assign a CNtpTime (without accuracy information) to this class (all members of CNtpTime are copied, and acc is set to 0)

struct STimestamp: Contains the three 32 bit values that comprise the clock state that is sent to the other nodes, namely the macrostamp (ms), timestamp (ts) and accuracy (acc) values. The accuracy contains two 16-bit envelopes, each of which corresponds to NTP bits [-8,-23]: the lower envelope ALPHAN is stored in the upper two bytes, and the upper envelope ALPHAP is stored in the lower two bytes.
Note: the structure used by the network driver is ti_acx, which has the same members as STimestamp, but may not contain them in the same order. In all simulation modules, only STimestamp should be used.
 

classes:

class CNtpTime: Contains a 32 bit macrostamp ms (stores bits [+31,+8]), a 32 bit timestamp ts (for bits [+7,-24]) and a 32 bit microstamp us. Normally, the uppermost 8 bits of ms should be zero. Overflows are ignored.

CNtpTime (): Sets macrostamp, timestamp and microstamp to zero.

CNtpTime (INT32 macro, INT32 time, INT32 micro): Sets ms/ts/us to macro/time/micro.

CNtpTime (double seconds): Calls SecondsToNtp() to obtain the NTP values for the specified time and stores them in the internal members.

CNtpTime (const CNtpTime& time): copies the ms/ts/us variables of time into the own members.

CNtpTime operator= (const CNtpTime& time): Copies time.ms/time.ts/time.us into ms/ts/us and returns *this.

CNtpTime operator+ (const CNtpTime& time): Returns the addition of time and the own value. Overflows in the macrostamp are ignored.

CNtpTime operator- (const CNtpTime& time): Returns the result of the subtraction of time from the own value. If time is bigger than the own value, the result is set to (ms,ts,us) = (0,0,0).

CNtpTime operator+= (const CNtpTime& time): Adds time to the own value, returns the result. Again, overflows in the macrostamp are ignored.

CNtpTime operator-= (const CNtpTime& time): Subtracts time from the own value, returns the result. If time is bigger than the own value, the result is set to (ms,ts,us) = (0,0,0).
 

functions:

double NtpToSeconds (INT32 ms, INT32 ts):
double NtpToSeconds (INT32 ms, INT32 ts INT32 us):
double NtpToSeconds (const CNtpTime& time):
double NtpToSeconds (const STimestamp& time):
double NtpToSeconds (const SExtendedTimestamp& time): All five convert NTP time into seconds (the accuracy value in STimestamp and SExtendedTimestamp is ignored).

void SecondsToNtp (double sec, INT32& ms, INT32& ts):
void SecondsToNtp (double sec, INT32& ms, INT32& ts, INT32& us):
void SecondsToNtp (double sec, CNtpTime& time):
void SecondsToNtp (double sec, STimestamp& time):
void SecondsToNtp (double sec, STimestamp& time): All five convert seconds into an NTP time (again, the accuracy value in STimestamp and SExtendedTimestamp is ignored).

ostream& operator<< (ostream& o, const STimestamp& time):
ostream& operator<< (ostream& o, const SExtendedTimestamp& time):
ostream& operator<< (ostream& o, const CNtpTime& time): All three print the time parameter to the output stream.


last modified: Fri Feb 5 18:56:38 1999