ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
Time.cpp
Go to the documentation of this file.
1 /*
2  * Time.cpp
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 
22 #include "Time.hpp"
23 
24 #include <stdexcept>
25 
26 namespace onposix {
27 
28 /**
29  * \brief Default constructor.
30  *
31  * It initializes the class with the current time (calling clock_gettime()
32  * @param clockType: the type of the clock:
33  * <ul>
34  * <li> CLOCK_REALTIME: System-wide time.
35  * <li> CLOCK_MONOTONIC (default): Monotonic time since some unspecified starting point.
36  * It cannot be set.
37  * <li> CLOCK_PROCESS_CPUTIME_ID: Per-process cpu time.
38  * <li> CLOCK_THREAD_CPUTIME_ID: Per-thread cpu time.
39  * </ul>
40  * This parameter affects the initial value and the result of resetToCurrentTime()
41  * @exception std::runtime_error, thrown by resetToCurrentTime()
42  */
43 Time::Time(clockid_t clockType): clockType_(clockType)
44 {
46 }
47 
48 /**
49  * \brief Method to add seconds and nseconds to the current value
50  *
51  * @param sec Number of seconds to be added
52  * @param usec Number of nseconds to be added
53  */
54 void Time::add(time_t sec, long nsec)
55 {
56  time_.tv_nsec += nsec;
57  time_.tv_sec += sec;
58 }
59 
60 /**
61  * \brief Method to set the time to a specific value
62  *
63  * @param sec Number of seconds to be set
64  * @param usec Number of nseconds to be set
65  */
66 void Time::set(time_t sec, long nsec)
67 {
68  time_.tv_nsec = nsec;
69  time_.tv_sec = sec;
70 }
71 
72 /**
73  * \brief Method to reset the class to the current time.
74  *
75  * This method sets the time equal to the value returned by gettimeofday().
76  * @exception std::runtime_error in case of error
77  */
79 {
80  if (clock_gettime(clockType_, &time_) != 0)
81  throw std::runtime_error("Can't get current time");
82 }
83 
84 /**
85  * \brief Operator to compare two values
86  */
87 bool Time::operator< (const Time& ref) const
88 {
89  if (time_.tv_sec < ref.time_.tv_sec)
90  return true;
91  else if ((time_.tv_sec == ref.time_.tv_sec) &&
92  (time_.tv_nsec < ref.time_.tv_nsec))
93  return true;
94  return false;
95 }
96 
97 /**
98  * \brief Operator to compare two values
99  */
100 bool Time::operator> (const Time& ref) const
101 {
102  if (time_.tv_sec > ref.time_.tv_sec)
103  return true;
104  else if ((time_.tv_sec == ref.time_.tv_sec) &&
105  (time_.tv_nsec > ref.time_.tv_nsec))
106  return true;
107  return false;
108 }
109 
110 /**
111  * \brief Operator to compare two values
112  * @return true if the times expressed by the classes are equal; false otherwise
113  */
114 bool Time::operator== (const Time& ref) const
115 {
116  if ((time_.tv_sec == ref.time_.tv_sec) &&
117  (time_.tv_nsec == ref.time_.tv_nsec))
118  return true;
119  return false;
120 }
121 
122 /**
123  * \brief Method to get timer resolition.
124  *
125  * @param sec: number of seconds of resolution.
126  * @param nsec: number of seconds of resolution.
127  * @exception std::runtime_error in case of error
128  */
129 void Time::getResolution (time_t* sec, long* nsec)
130 {
131  timespec ret;
132  if (clock_getres(clockType_, &ret) != 0)
133  throw std::runtime_error("Can't get time resoultion");
134  *sec = ret.tv_sec;
135  *nsec = ret.tv_nsec;
136 }
137 
138 
139 } /* onposix */