ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
Time.hpp
Go to the documentation of this file.
1 /*
2  * Time.hpp
3  *
4  * Copyright (C) 2012 Evidence Srl - www.evidence.eu.com
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef TIME_HPP_
22 #define TIME_HPP_
23 
24 #include <time.h>
25 
26 namespace onposix {
27 
28 /**
29  * \brief Class to contain a time
30  *
31  * This class wraps a time, with a resolution of nanoseconds.
32  * It is useful to get the current time and to make comparisons between times.
33  */
34 class Time
35 {
36  /**
37  * \brief Time in the form returned by gettimeofday()
38  */
39  struct timespec time_;
40 
41  /**
42  * Type of clock.
43  */
44  clockid_t clockType_;
45 public:
46  Time(clockid_t clockType = CLOCK_MONOTONIC);
47  virtual ~Time(){}
48  void add(time_t sec, long nsec);
49  void set(time_t sec, long nsec);
50  void resetToCurrentTime();
51  bool operator< (const Time& ref) const;
52  bool operator> (const Time& ref) const;
53  bool operator== (const Time& ref) const;
54  void getResolution (time_t* sec, long* nsec);
55 
56 
57  /**
58  * \brief Method to get the number of seconds
59  *
60  * @return Number of seconds
61  */
62  inline time_t getSeconds() const {
63  return time_.tv_sec;
64  }
65 
66  /**
67  * \brief Method to get the number of microseconds
68  *
69  * @return Number of microseconds
70  */
71  inline long getNSeconds() const {
72  return time_.tv_nsec;
73  }
74 };
75 
76 } /* onposix */
77 
78 #endif /* TIME_HPP_ */