A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ns3::Time Class Reference

keep track of time unit. More...

#include <nstime.h>

Classes

struct  Information
 
struct  Resolution
 

Public Types

enum  Unit {
  S = 0, MS = 1, US = 2, NS = 3,
  PS = 4, FS = 5, LAST = 6
}
 

Public Member Functions

 Time (const Time &o)
 
 Time (double v)
 
 Time (int v)
 
 Time (long int v)
 
 Time (long long int v)
 
 Time (unsigned int v)
 
 Time (unsigned long int v)
 
 Time (unsigned long long int v)
 
 Time (const std::string &s)
 String constructor Construct Time object from common time expressions like " 1ms" or "10s". Supported units include: More...
 
 Time (const int64x64_t &value)
 
int Compare (const Time &o) const
 
double GetDouble (void) const
 
int64_t GetFemtoSeconds (void) const
 
int64_t GetInteger (void) const
 
int64_t GetMicroSeconds (void) const
 
int64_t GetMilliSeconds (void) const
 
int64_t GetNanoSeconds (void) const
 
int64_t GetPicoSeconds (void) const
 
double GetSeconds (void) const
 
int64_t GetTimeStep (void) const
 
bool IsNegative (void) const
 
bool IsPositive (void) const
 
bool IsStrictlyNegative (void) const
 
bool IsStrictlyPositive (void) const
 
bool IsZero (void) const
 
 operator int64x64_t () const
 
Timeoperator= (const Time &o)
 
int64x64_t To (enum Unit timeUnit) const
 
double ToDouble (enum Unit timeUnit) const
 
int64_t ToInteger (enum Unit timeUnit) const
 

Static Public Member Functions

static Time From (const int64x64_t &from, enum Unit timeUnit)
 
static Time From (const int64x64_t &value)
 
static Time FromDouble (double value, enum Unit timeUnit)
 
static Time FromInteger (uint64_t value, enum Unit timeUnit)
 
static enum Unit GetResolution (void)
 
static void SetResolution (enum Unit resolution)
 

Static Private Member Functions

static struct Resolution GetNsResolution (void)
 
static struct InformationPeekInformation (enum Unit timeUnit)
 
static struct ResolutionPeekResolution (void)
 
static void SetResolution (enum Unit unit, struct Resolution *resolution)
 

Private Attributes

int64_t m_data
 

Friends

Time Abs (const Time &time)
 
Time Max (const Time &ta, const Time &tb)
 
Time Min (const Time &ta, const Time &tb)
 
bool operator!= (const Time &lhs, const Time &rhs)
 
Time operator+ (const Time &lhs, const Time &rhs)
 
Timeoperator+= (Time &lhs, const Time &rhs)
 
Time operator- (const Time &lhs, const Time &rhs)
 
Timeoperator-= (Time &lhs, const Time &rhs)
 
bool operator< (const Time &lhs, const Time &rhs)
 
bool operator<= (const Time &lhs, const Time &rhs)
 
bool operator== (const Time &lhs, const Time &rhs)
 
bool operator> (const Time &lhs, const Time &rhs)
 
bool operator>= (const Time &lhs, const Time &rhs)
 

Detailed Description

keep track of time unit.

This template class is used to keep track of the value of a specific time unit: the type TimeUnit<1> is used to keep track of seconds, the type TimeUnit<2> is used to keep track of seconds squared, the type TimeUnit<-1> is used to keep track of 1/seconds, etc.

This base class defines all the functionality shared by all these time unit objects: it defines all the classic arithmetic operators +, -, *, /, and all the classic comparison operators: ==, !=, <, >, <=, >=. It is thus easy to add, substract, or multiply multiple TimeUnit objects. The return type of any such arithmetic expression is always a TimeUnit object.

The ns3::uint64_t, ns3::Time, ns3::TimeSquare, and ns3::TimeInvert classes are aliases for the TimeUnit<0>, TimeUnit<1>, TimeUnit<2> and TimeUnit<-1> types respectively.

For example:

Time<1> t1 = Seconds (10.0);
Time<1> t2 = Seconds (10.0);
Time<2> t3 = t1 * t2;
Time<0> t4 = t1 / t2;
Time<3> t5 = t3 * t1;
Time<-2> t6 = t1 / t5;
TimeSquare t7 = t3;
uint64_t s = t4;

If you try to assign the result of an expression which does not match the type of the variable it is assigned to, you will get a compiler error. For example, the following will not compile:

Time<1> = Seconds (10.0) * Seconds (1.5);

You can also use the following non-member functions to manipulate any of these ns3::TimeUnit object:

keep track of time values and allow control of global simulation resolution

This class defines all the classic C++ arithmetic operators +, -, *, /, and all the classic comparison operators: ==, !=, <, >, <=, >=. It is thus easy to add, substract, or multiply multiple Time objects.

The ns3::uint64_t, ns3::TimeSquare, and ns3::TimeInvert classes are backward-compatibility aliases for ns3::Time.

For example:

Time t1 = Seconds (10.0);
Time t2 = Seconds (10.0);
Time t3 = t1 * t2;
Time t4 = t1 / t2;
Time t5 = t3 * t1;
Time t6 = t1 / t5;
Time t7 = t3;

You can also use the following non-member functions to manipulate any of these ns3::Time object:

This class also controls the resolution of the underlying time value . The default resolution is nanoseconds. That is, TimeStep (1).GetNanoSeconds () will return

  1. It is possible to either increase or decrease the resolution and the code tries really hard to make this easy.

If your resolution is X (say, nanoseconds) and if you create Time objects with a lower resolution (say, picoseconds), don't expect that this code will return 1: PicoSeconds (1).GetPicoSeconds (). It will most likely return 0 because the Time object has only 64 bits of fractional precision which means that PicoSeconds (1) is stored as a 64-bit aproximation of 1/1000 in the Time object. If you later multiply it again by the exact value 1000, the result is unlikely to be 1 exactly. It will be close to 1 but not exactly 1.

In general, it is thus a really bad idea to try to use time objects of a resolution higher than the global resolution controlled through Time::SetResolution. If you do need to use picoseconds, it's thus best to switch the global resolution to picoseconds to avoid nasty surprises.

Another important issue to keep in mind is that if you increase the global resolution, you also implicitely decrease the range of your simulation. i.e., the global simulation time is stored in a 64 bit integer whose interpretation will depend on the global resolution so, 2^64 picoseconds which is the maximum duration of your simulation if the global resolution is picoseconds is smaller than 2^64 nanoseconds which is the maximum duration of your simulation if the global resolution is nanoseconds.

Finally, don't even think about ever changing the global resolution after creating Time objects: all Time objects created before the call to SetResolution will contain values which are not updated to the new resolution. In practice, the default value for the attributes of many models is indeed calculated before the main function of the main program enters. Because of this, if you use one of these models (and it's likely), it's going to be hard to change the global simulation resolution in a way which gives reasonable results. This issue has been filed as bug 954 in the ns-3 bugzilla installation.

Definition at line 149 of file nstime.h.

Member Enumeration Documentation

The unit to use to interpret a number representing time

Definition at line 155 of file nstime.h.

Constructor & Destructor Documentation

ns3::Time::Time ( const std::string &  s)
explicit

String constructor Construct Time object from common time expressions like " 1ms" or "10s". Supported units include:

  • s (seconds)
  • ms (milliseconds)
  • us (microseconds)
  • ns (nanoseconds)
  • ps (picoseconds)
  • fs (femtoseconds)

There can be no white space between the numerical portion and the units. Any otherwise malformed string causes a fatal error to occur.

Parameters
sThe string to parse into a Time

Definition at line 37 of file time.cc.

References FromDouble(), NS_ABORT_MSG, and NS_LOG_FUNCTION.

Member Function Documentation

static Time ns3::Time::FromDouble ( double  value,
enum Unit  timeUnit 
)
inlinestatic
Parameters
valueto convert into a Time object
timeUnitthe unit of the value to convert
Returns
a new Time object
See Also
FromInteger, ToInteger, ToDouble

Definition at line 388 of file nstime.h.

Referenced by ns3::RttMeanDeviation::Measurement(), ns3::Seconds(), and Time().

static Time ns3::Time::FromInteger ( uint64_t  value,
enum Unit  timeUnit 
)
inlinestatic
Parameters
valueto convert into a Time object
timeUnitthe unit of the value to convert
Returns
a new Time object

This method interprets the input value according to the input unit and constructs a matching Time object.

See Also
FromDouble, ToDouble, ToInteger

Definition at line 347 of file nstime.h.

Referenced by ns3::FemtoSeconds(), ns3::MicroSeconds(), ns3::MilliSeconds(), ns3::NanoSeconds(), ns3::PicoSeconds(), and ns3::RttMeanDeviation::RetransmitTimeout().

int64_t ns3::Time::GetFemtoSeconds ( void  ) const
inline
Returns
an approximation in femtoseconds of the time stored in this instance.

Definition at line 303 of file nstime.h.

References ToInteger().

int64_t ns3::Time::GetMilliSeconds ( void  ) const
inline
Returns
an approximation in milliseconds of the time stored in this instance.

Definition at line 271 of file nstime.h.

References ToInteger().

Referenced by ns3::TraceFadingLossModel::DoCalcRxPowerSpectralDensity(), RttTestCase::DoRun(), ns3::aodv::RrepHeader::RrepHeader(), ns3::aodv::RrepHeader::SetHello(), and ns3::LteEnbPhy::StartSubFrame().

int64_t ns3::Time::GetPicoSeconds ( void  ) const
inline
Returns
an approximation in picoseconds of the time stored in this instance.

Definition at line 295 of file nstime.h.

References ToInteger().

enum Time::Unit ns3::Time::GetResolution ( void  )
static
Returns
the current global resolution.

Definition at line 140 of file time.cc.

References NS_LOG_FUNCTION_NOARGS.

double ns3::Time::GetSeconds ( void  ) const
inline
Returns
an approximation in seconds of the time stored in this instance.

Definition at line 262 of file nstime.h.

References ToDouble().

Referenced by ns3::dsr::RouteCache::AddRoute_Link(), ns3::UanPhyCalcSinrFhFsk::CalcSinrDb(), ns3::BasicEnergySource::CalculateRemainingEnergy(), ns3::LiIonEnergySource::CalculateRemainingEnergy(), ns3::AcousticModemEnergyModel::ChangeState(), ns3::WifiRadioEnergyModel::ChangeState(), BatteryLifetimeTest::ConstantLoadTest(), ns3::Ns2MobilityHelperTest::CourseChange(), ns3::TraceFadingLossModel::DoCalcRxPowerSpectralDensity(), ns3::MeshWifiInterfaceMac::DoInitialize(), ns3::LteHandoverDelayTestCase::DoRun(), TimesWithSignsTestCase::DoRun(), ns3::TcpWestwood::DupAck(), ns3::UanMacCw::Enqueue(), ns3::TcpWestwood::EstimateBW(), ns3::dsr::RouteCache::FindSameRoute(), ns3::dsdv::RoutingProtocol::GetSettlingTime(), ns3::dsr::RouteCache::IncStability(), ns3::SubscriberStationNetDevice::InitSubscriberStationNetDevice(), ns3::aodv::RoutingTableEntry::Invalidate(), ns3::ArpCache::Entry::IsExpired(), ns3::olsr::RoutingProtocol::LinkSensing(), ns3::aodv::RoutingTable::MarkLinkAsUnidirectional(), ns3::UanMacCw::NotifyTxStart(), ns3::operator*(), ns3::operator<<(), ns3::SeqTsHeader::Print(), ns3::UanHeaderRcData::Print(), ns3::UanHeaderRcRts::Print(), ns3::UanHeaderRcCtsGlobal::Print(), ns3::UanHeaderRcCts::Print(), ns3::dsdv::RoutingProtocol::PrintRoutingTable(), ns3::aodv::RoutingProtocol::PrintRoutingTable(), ns3::Ipv4ListRouting::PrintRoutingTable(), ns3::Ipv6ListRouting::PrintRoutingTable(), ns3::Ipv6StaticRouting::PrintRoutingTable(), ns3::AnimPacketInfo::ProcessRxEnd(), ns3::dsdv::RoutingProtocol::RecvDsdv(), ns3::dot11s::HwmpProtocol::Report(), ns3::flame::FlameProtocol::Report(), ns3::dot11s::PeerLink::Report(), ns3::MeshWifiInterfaceMac::Report(), ns3::MeshHelper::Report(), ns3::FlowMonitor::ReportLastRx(), ns3::TcpWestwood::Retransmit(), ns3::RttMeanDeviation::RetransmitTimeout(), ns3::RvBatteryModel::RvModelAFunction(), ns3::dsr::DsrRouting::ScheduleNetworkPacketRetry(), ns3::dsr::DsrRouting::ScheduleRreqRetry(), ns3::aodv::RoutingProtocol::SendRerrMessage(), ns3::aodv::RoutingProtocol::SendRerrWhenNoRouteToForward(), ns3::UanHeaderRcData::Serialize(), ns3::UanHeaderRcRts::Serialize(), ns3::UanHeaderRcCtsGlobal::Serialize(), ns3::UanHeaderRcCts::Serialize(), ns3::dot11s::PeerLink::SetBeaconInformation(), ns3::SimpleDeviceEnergyModel::SetCurrentA(), ns3::UanPdp::SetTap(), ns3::UanPdp::SumTapsC(), ns3::UanPdp::SumTapsFromMaxC(), ns3::UanPdp::SumTapsFromMaxNc(), ns3::UanPdp::SumTapsNc(), ns3::CsmaNetDevice::TransmitCompleteEvent(), ns3::CsmaChannel::TransmitEnd(), ns3::PointToPointNetDevice::TransmitStart(), ns3::CsmaNetDevice::TransmitStart(), and BatteryLifetimeTest::VariableLoadTest().

bool ns3::Time::IsNegative ( void  ) const
inline
Returns
true if the time is negative or zero, false otherwise.

Definition at line 227 of file nstime.h.

bool ns3::Time::IsPositive ( void  ) const
inline
Returns
true if the time is positive or zero, false otherwise.

Definition at line 234 of file nstime.h.

Referenced by ns3::DefaultSimulatorImpl::Schedule(), ns3::RealtimeSimulatorImpl::Schedule(), and ns3::DistributedSimulatorImpl::Schedule().

bool ns3::Time::IsStrictlyNegative ( void  ) const
inline
Returns
true if the time is strictly negative, false otherwise.

Definition at line 241 of file nstime.h.

bool ns3::Time::IsStrictlyPositive ( void  ) const
inline
Returns
true if the time is strictly positive, false otherwise.

Definition at line 248 of file nstime.h.

bool ns3::Time::IsZero ( void  ) const
inline
Returns
true if the time is zero, false otherwise.

Definition at line 220 of file nstime.h.

void ns3::Time::SetResolution ( enum Unit  resolution)
static
Parameters
resolutionthe new resolution to use

Change the global resolution used to convert all user-provided time values in Time objects and Time objects in user-expected time units.

Definition at line 98 of file time.cc.

References NS_LOG_FUNCTION.

double ns3::Time::ToDouble ( enum Unit  timeUnit) const
inline
Parameters
timeUnitthe unit of the value to return
Returns
double time value

Convert the input time into a floating point value according to the requested time unit.

Definition at line 399 of file nstime.h.

Referenced by GetSeconds(), and ns3::RttMeanDeviation::Measurement().

int64_t ns3::Time::ToInteger ( enum Unit  timeUnit) const
inline
Parameters
timeUnitthe unit of the value to return
Returns
int64_t time value

Convert the input time into an integer value according to the requested time unit.

Definition at line 367 of file nstime.h.

Referenced by GetFemtoSeconds(), GetMicroSeconds(), GetMilliSeconds(), GetNanoSeconds(), GetPicoSeconds(), and ns3::RttMeanDeviation::RetransmitTimeout().


The documentation for this class was generated from the following files: