General IRI utilities
ctime.h
1 // Copyright (C) 2009-2010 Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
2 // Author Sergi Hernandez (shernand@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 
19 #ifndef CTime_H
20 #define CTime_H
21 
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <time.h>
25 #include <math.h>
26 #include <iostream>
27 #include <sstream>
28 #include <string>
29 #include "limits.h"
30 #include <sys/time.h>
31 #include "ctimeexceptions.h"
32 
45 enum ctimeformat { ctf_secnano, ctf_datetime, ctf_dtfile, ctf_ms, ctf_us };
46 
75 class CTime
76 {
77  private:
90  unsigned long sec;
91 
104  unsigned long nsec;
105 
114  static CTime zero;
115 
120  bool use_zero;
121 
128  ctimeformat print_format;
129  public:
130 
137  CTime();
138 
148  CTime(double relative);
149 
150 
156  ~CTime();
157 
160 
169  unsigned long seconds(void);
170 
179  unsigned long nanoseconds(void);
180 
190  double getTimeInSeconds(void);
191 
201  double getTimeInMilliseconds(void);
202 
210  long getTimeInMicroseconds(void);
211 
226  timespec getTimeInTimespec(void);
227 
242  timeval getTimeInTimeval(void);
243 
253  time_t getTimeInTime_t(void);
254 
262  CTime getRef();
263 
269  void useRef(bool use);
270 
276  bool isRefUsed();
277 
279 
282 
296  void set(double milliseconds=-1.0);
297 
303  void setRef();
304 
311  void resetRef();
312 
313  CTime diff(CTime &t1, CTime &t0);
314 
316 
319 
337  CTime operator - (CTime &t);
338 
357  CTime operator + (CTime &t);
358 
372  CTime operator / (int div);
373 
384  bool operator == (CTime &t);
385 
396  bool operator != (CTime &t);
397 
404  bool operator > (CTime &t);
405 
412  bool operator < (CTime &t);
413 
420  bool operator >= (CTime &t);
421 
428  bool operator <= (CTime &t);
429 
449  friend std::ostream& operator << (std::ostream &o, CTime &t);
450 
478  std::string getString(void);
479 
497  void setFormat(ctimeformat format);
498 
508  ctimeformat getFormat();
509 
511 
514 
526  static double timespecToMs(timespec t);
527 
539  static timespec msToTimespec(double ms);
540 
542 
543 
544 };
545 
546 
547 
548 
549 #endif
550 
double getTimeInMilliseconds(void)
Get time in milliseconds.
Definition: ctime.cpp:70
unsigned long nanoseconds(void)
Get Nanoseconds.
Definition: ctime.cpp:57
bool operator<=(CTime &t)
Comparison <= operator.
Definition: ctime.cpp:262
~CTime()
Destructor.
Definition: ctime.cpp:44
CTime operator+(CTime &t)
Calculates time addition.
Definition: ctime.cpp:208
friend std::ostream & operator<<(std::ostream &o, CTime &t)
outputs time into a ostream
Definition: ctime.cpp:286
static double timespecToMs(timespec t)
conversion from timespec to milliseconds
Definition: ctime.cpp:364
long getTimeInMicroseconds(void)
Get time in microseconds.
Definition: ctime.cpp:75
void useRef(bool use)
sets if use or not the reference internal time
Definition: ctime.cpp:158
bool operator!=(CTime &t)
Comparison operator.
Definition: ctime.cpp:249
void setRef()
Sets the internal reference time.
Definition: ctime.cpp:141
CTime operator-(CTime &t)
Calculates time difference.
Definition: ctime.cpp:199
timeval getTimeInTimeval(void)
Get time in a timeval strcuture.
Definition: ctime.cpp:92
unsigned long seconds(void)
Get Seconds.
Definition: ctime.cpp:52
bool operator>(CTime &t)
Comparison > operator.
Definition: ctime.cpp:270
bool operator==(CTime &t)
Comparison operator.
Definition: ctime.cpp:244
CTime()
Constructor.
Definition: ctime.cpp:30
std::string getString(void)
Gives a formatted string of this time.
Definition: ctime.cpp:293
time_t getTimeInTime_t(void)
Get time in time_t.
Definition: ctime.cpp:102
Implementation of a time class and operations.
Definition: ctime.h:75
ctimeformat getFormat()
Gets output string format.
Definition: ctime.cpp:356
static timespec msToTimespec(double ms)
conversion from milliseconds to timespec
Definition: ctime.cpp:370
timespec getTimeInTimespec(void)
Get time in a timespec strcuture.
Definition: ctime.cpp:82
double getTimeInSeconds(void)
Get time in seconds.
Definition: ctime.cpp:64
void setFormat(ctimeformat format)
Sets output string format.
Definition: ctime.cpp:351
void set(double milliseconds=-1.0)
Sets the internal time.
Definition: ctime.cpp:110
bool operator<(CTime &t)
Comparison < operator.
Definition: ctime.cpp:278
bool isRefUsed()
Get if is used or not the reference internal time.
Definition: ctime.cpp:170
CTime operator/(int div)
Calculates division by integer.
Definition: ctime.cpp:229
bool operator>=(CTime &t)
Comparison >= operator.
Definition: ctime.cpp:254
CTime getRef()
Get the reference internal time.
Definition: ctime.cpp:147
void resetRef()
Resets the reference time to the epoch.
Definition: ctime.cpp:152