A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
time.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006 INRIA
4  * Copyright (c) 2007 Emmanuelle Laprise
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20  * TimeStep support by Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
21  */
22 #include "nstime.h"
23 #include "abort.h"
24 #include "global-value.h"
25 #include "enum.h"
26 #include "string.h"
27 #include "object.h"
28 #include "config.h"
29 #include <math.h>
30 #include <sstream>
31 
32 namespace ns3 {
33 
34 Time::Time (const std::string& s)
35 {
36  std::string::size_type n = s.find_first_not_of ("+-0123456789.");
37  if (n != std::string::npos)
38  { // Found non-numeric
39  std::istringstream iss;
40  iss.str (s.substr (0, n));
41  double r;
42  iss >> r;
43  std::string trailer = s.substr (n, std::string::npos);
44  if (trailer == std::string ("s"))
45  {
46  *this = Time::FromDouble (r, Time::S);
47  return;
48  }
49  if (trailer == std::string ("ms"))
50  {
51  *this = Time::FromDouble (r, Time::MS);
52  return;
53  }
54  if (trailer == std::string ("us"))
55  {
56  *this = Time::FromDouble (r, Time::US);
57  return;
58  }
59  if (trailer == std::string ("ns"))
60  {
61  *this = Time::FromDouble (r, Time::NS);
62  return;
63  }
64  if (trailer == std::string ("ps"))
65  {
66  *this = Time::FromDouble (r, Time::PS);
67  return;
68  }
69  if (trailer == std::string ("fs"))
70  {
71  *this = Time::FromDouble (r, Time::FS);
72  return;
73  }
74  NS_ABORT_MSG ("Can't Parse Time " << s);
75  }
76  // else
77  // they didn't provide units, assume seconds
78  std::istringstream iss;
79  iss.str (s);
80  double v;
81  iss >> v;
82  *this = Time::FromDouble (v, Time::S);
83 }
84 
85 struct Time::Resolution
86 Time::GetNsResolution (void)
87 {
88  struct Resolution resolution;
89  SetResolution (Time::NS, &resolution);
90  return resolution;
91 }
92 void
93 Time::SetResolution (enum Unit resolution)
94 {
95  SetResolution (resolution, PeekResolution ());
96 }
97 void
98 Time::SetResolution (enum Unit unit, struct Resolution *resolution)
99 {
100  int8_t power [LAST] = { 15, 12, 9, 6, 3, 0};
101  for (int i = 0; i < Time::LAST; i++)
102  {
103  int shift = power[i] - power[(int)unit];
104  uint64_t factor = (uint64_t) pow (10, fabs (shift));
105  struct Information *info = &resolution->info[i];
106  info->factor = factor;
107  if (shift == 0)
108  {
109  info->timeFrom = int64x64_t (1);
110  info->timeTo = int64x64_t (1);
111  info->toMul = true;
112  info->fromMul = true;
113  }
114  else if (shift > 0)
115  {
116  info->timeFrom = int64x64_t (factor);
117  info->timeTo = int64x64_t::Invert (factor);
118  info->toMul = false;
119  info->fromMul = true;
120  }
121  else
122  {
123  NS_ASSERT (shift < 0);
124  info->timeFrom = int64x64_t::Invert (factor);
125  info->timeTo = int64x64_t (factor);
126  info->toMul = true;
127  info->fromMul = false;
128  }
129  }
130  resolution->unit = unit;
131 }
132 enum Time::Unit
134 {
135  return PeekResolution ()->unit;
136 }
137 
138 
139 std::ostream&
140 operator<< (std::ostream& os, const Time & time)
141 {
142  std::string unit;
143  switch (Time::GetResolution ())
144  {
145  case Time::S:
146  unit = "s";
147  break;
148  case Time::MS:
149  unit = "ms";
150  break;
151  case Time::US:
152  unit = "us";
153  break;
154  case Time::NS:
155  unit = "ns";
156  break;
157  case Time::PS:
158  unit = "ps";
159  break;
160  case Time::FS:
161  unit = "fs";
162  break;
163  case Time::LAST:
164  NS_ABORT_MSG ("can't be reached");
165  unit = "unreachable";
166  break;
167  }
168  int64x64_t v = time;
169  os << v << unit;
170  return os;
171 }
172 std::istream& operator>> (std::istream& is, Time & time)
173 {
174  std::string value;
175  is >> value;
176  time = Time (value);
177  return is;
178 }
179 
182 
183 } // namespace ns3
184