General IRI utilities
test_time.cpp
1 // Copyright (C) 2009-2010 Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
2 // Author Martí Morta (mmorta@iri.upc.edu)
3 // All rights reserved.
4 //
5 // This file is part of iriutils
6 // iriutils is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
141 #include "ctime.h"
142 #include <iostream>
143 #include <unistd.h>
144 
145 using namespace std;
146 
147 int main(void)
148 {
149  cout << "\nTIME MANAGER EXAMPLE" << endl;
150 
151  cout << " Example: create times (t0,t1,t2)\n\n";
152  CTime t0,t1,t2,t3,t20,td,ta,td0,ta0,ts,ts0,ta2,ta20;
153  cout << " t0: " << t0 << endl;
154 
155  cout << "\n Example: sleep 300 ms " << endl;
156  usleep(300000);
157 
158  cout << "\n Example: set t1 (0.300 rel)\n\n";
159  t1.set();
160  CTime t10 = t1 - t0;
161  cout << " t1: " << t1 << "\n t1o: " << t10 << endl;
162 
163  cout << "\n Example: set t3 delayed 200 ms (0.200 abs)\n\n";
164  t3.set(200);
165  cout << " t3: " << t3 << endl;
166 
167  cout << "\n Example: sleep 1705 ms " << endl;
168  usleep(1705000);
169 
170  cout << "\n Example: set t2 (2.005 rel)\n\n";
171  t2.set();
172  t20 = t2 - t0;
173  cout << " t2: " << t2 << "\n t2o: " << t20 << endl;
174 
175  cout << "\n Example: t2 - t1 (1.705)\n\n";
176  td = t2 - t1;
177  td0 = t20 - t10;
178  cout << " td: " << td << "\n tdo: " << td0 << endl;
179 
180  cout << "\n Example: + addition (t1+t2)\n\n";
181  ts = t1 + t2;
182  ts0 = t10 + t20;
183  cout << " ts: " << ts << "\n tso: " << ts0 << "\n ulongmax: " << ULONG_MAX << endl;
184 
185  cout << "\n Example: + Exception (overvalue)\n\n";
186  try{
187  ts = ts + ts;
188  }catch(CException &e)
189  {
190  cout << e.what() << endl;
191  }
192  cout << "\n Example: - Exception (negative)\n\n";
193  try{
194  ts = t1 - t2;
195  }catch(CException &e)
196  {
197  cout << e.what() << endl;
198  }
199 
200  cout << "\n Example: average (t1,t2)\n\n";
201  ta = ( t1 + t2 ) / 2;
202  ta0 = (t10 + t20)/2 ;
203  cout << " ta: " << ta << "\n tao: " << ta0 << endl;
204 
205  cout << "\n Example: comparative (t1==t2, t3==t2)\n\n";
206  t3 = t2;
207  cout << " ?: " << boolalpha << (bool)(t1==t2) << " "
208  << (bool)(t3==t2) << endl;
209 
210  cout << "\n Example: comparative (t1<t2, t1<=t2, t3>t2, t3>=t2)\n\n";
211  t3 = t2;
212  cout
213  << " t1: " << t1 << "\n t2: " << t2 << endl << " t3: " << t3 << endl
214  << " ?: <: " << boolalpha << (bool)(t1<t2) << " <=: " << (bool)(t1<=t2) << endl
215  << " ?: >: " << boolalpha << (bool)(t3>t2) << " >=: " << (bool)(t3>=t2) << endl;
216 
217 
218  cout << "\n Example: getting time (t1/t0)\n\n";
219 
220  cout << " seconds and nanoseconds:" << endl
221  << " " << t1.seconds() << " " << t1.nanoseconds() << endl
222  << " " << t10.seconds() << " " << t10.nanoseconds() << endl;
223  cout << " double Seconds:" << endl
224  << " " << t1.getTimeInSeconds() << endl
225  << " " << t10.getTimeInSeconds() << endl
226  << " double milliseconds:" << endl
227  << " " << t1.getTimeInMilliseconds() << endl
228  << " " << t10.getTimeInMilliseconds() << endl;
229 
230  t1.setFormat(ctf_secnano);
231  t10.setFormat(ctf_secnano);
232  cout << " Format ctf_secnano: " << endl
233  << " " << t1 << endl
234  << " " << t10 << endl;
235 
236  t1.setFormat(ctf_datetime);
237  t10.setFormat(ctf_datetime);
238  cout << " Format ctf_datetime: " << endl
239  << " " << t1 << endl
240  << " " << t10 << endl;
241 
242  t1.setFormat(ctf_dtfile);
243  t10.setFormat(ctf_dtfile);
244  cout << " Format ctf_dtfile: " << endl
245  << " " << t1 << endl
246  << " " << t10 << endl;
247 
248  t1.setFormat(ctf_ms);
249  t10.setFormat(ctf_ms);
250  cout << " Format ctf_ms: " << endl
251  << " " << t1 << endl
252  << " " << t10 << endl;
253 
254  t1.setFormat(ctf_us);
255  t10.setFormat(ctf_us);
256  cout << " Format ctf_us: " << endl
257  << " " << t1 << endl
258  << " " << t10 << endl;
259 
260  timespec t1t;
261  t1t = t1.getTimeInTimespec();
262  cout << " Timespec" << endl;
263  cout << " " << t1t.tv_sec << " " << t1t.tv_nsec << endl;
264 
265  time_t t1tt;
266  struct tm * timeinfo;
267  char buffer [80];
268  t1tt = t1.getTimeInTime_t();
269  cout << " Time_t" << endl;
270  cout << " " << t1tt << endl;
271  timeinfo = localtime ( &t1tt );
272  cout << " some formats using time_t" << endl;
273  strftime (buffer,80,"%F,%T\n %I:%M%p\n %c",timeinfo);
274  cout << " " << buffer << endl;
275 
276  cout << "\n Example: Using Reference Time\n\n";
277  cout << " Set Reference and wait 1 second" << endl << endl;
278  t1.setRef();
279  sleep(1);
280  t1.set();
281  CTime tzero = t1.getRef();
282  cout << " Time ref: " << tzero << endl;
283  cout << " t1 (ref.): " << t1 << endl;
284  cout << " t2 (raw): " << t2 << endl << endl;
285  cout << " Set useRef(true) for t2 and set its time" << endl << endl;
286  t2.set();
287  t2.useRef(true);
288  cout << " t2: " << t2 << " Is ref used?: " << t2.isRefUsed() << endl << endl;
289 
290  cout << "\n Example: END\n\n";
291 
292  return(1);
293 }
294 
virtual const std::string & what(void)
Function to get the error message.
Definition: exceptions.cpp:33
unsigned long seconds(void)
Get Seconds.
Definition: ctime.cpp:52
Implementation of a time class and operations.
Definition: ctime.h:75
Generic exception.
Definition: exceptions.h:76
void set(double milliseconds=-1.0)
Sets the internal time.
Definition: ctime.cpp:110
CTime getRef()
Get the reference internal time.
Definition: ctime.cpp:147