sttcl  v0.9c
STTCL C++ template state machine framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SttclPosixTime.h
Go to the documentation of this file.
1 
25 #ifndef STTCLPOSIXTIME_H_
26 #define STTCLPOSIXTIME_H_
27 #if defined(STTCL_POSIX_TIME) or defined(STTCL_POSIX_IMPL)
28 
29 #include <time.h>
30 
31 namespace sttcl
32 {
33 
34 namespace internal
35 {
36 namespace posix_impl
37 {
38 
42 class SttclPosixTimeDuration
43 {
44 public:
45  typedef struct timespec NativeTimeDuration;
46 
47  SttclPosixTimeDuration(unsigned int argHours = 0, unsigned int argMinutes = 0, unsigned int argSeconds = 0, unsigned int argMilliSeconds = 0, unsigned long argMicroSeconds = 0, unsigned long argNanoSeconds = 0)
48  {
49  setNative(argHours,argMinutes,argSeconds,argMilliSeconds,argMicroSeconds,argNanoSeconds);
50  }
51  SttclPosixTimeDuration(const SttclPosixTimeDuration& rhs)
52  {
53  td.tv_sec = rhs.td.tv_sec;
54  td.tv_nsec = rhs.td.tv_nsec;
55  }
56  SttclPosixTimeDuration(const NativeTimeDuration& nativeTimeDuration)
57  {
58  td.tv_sec = nativeTimeDuration.tv_sec;
59  td.tv_nsec = nativeTimeDuration.tv_nsec;
60  }
61  ~SttclPosixTimeDuration()
62  {
63  }
64 
65  SttclPosixTimeDuration& operator=(const SttclPosixTimeDuration& rhs)
66  {
67  td.tv_sec = rhs.td.tv_sec;
68  td.tv_nsec = rhs.td.tv_nsec;
69  return *this;
70  }
71 
72  bool operator==(const SttclPosixTimeDuration& rhs) const
73  {
74  return td.tv_sec == rhs.td.tv_sec &&
75  td.tv_nsec == rhs.td.tv_nsec;
76  }
77 
78  bool operator!=(const SttclPosixTimeDuration& rhs) const
79  {
80  return !operator==(rhs);
81  }
82 
83  bool operator<(const SttclPosixTimeDuration& rhs) const
84  {
85  return td.tv_sec < rhs.td.tv_sec ||
86  (td.tv_sec == rhs.td.tv_sec &&
87  td.tv_nsec < rhs.td.tv_nsec);
88  }
89 
90  bool operator<=(const SttclPosixTimeDuration& rhs) const
91  {
92  return operator==(rhs) || operator<(rhs);
93  }
94 
95  bool operator>(const SttclPosixTimeDuration& rhs) const
96  {
97  return td.tv_sec > rhs.td.tv_sec ||
98  (td.tv_sec == rhs.td.tv_sec &&
99  td.tv_nsec > rhs.td.tv_nsec);
100  }
101  bool operator>=(const SttclPosixTimeDuration& rhs) const
102  {
103  return operator==(rhs) || operator>(rhs);
104  }
105 
106  SttclPosixTimeDuration& operator+=(const SttclPosixTimeDuration& rhs)
107  {
108  add(rhs.td);
109  return *this;
110  }
111 
112  SttclPosixTimeDuration& operator-=(const SttclPosixTimeDuration& rhs)
113  {
114  substract(rhs.td);
115  return *this;
116  }
117 
118  SttclPosixTimeDuration& operator*=(int factor)
119  {
120  multiply(factor);
121  return *this;
122  }
123 
124  SttclPosixTimeDuration& operator/=(int divider)
125  {
126  divide(divider);
127  return *this;
128  }
129 
130 
131  long hours() const
132  {
133  return td.tv_sec / 3600;
134  }
135 
136  long minutes() const
137  {
138  return td.tv_sec / 60;
139  }
140 
141  long seconds() const
142  {
143  return td.tv_sec;
144  }
145  long milliseconds() const
146  {
147  return total_milliseconds();
148  }
149  long microseconds() const
150  {
151  return total_microseconds();
152  }
153  long nanoseconds() const
154  {
155  return total_nanoseconds();
156  }
157 
158  void hours(int newHours)
159  {
160  SttclPosixTimeDuration result(*this);
161  result -= SttclPosixTimeDuration(hours() + newHours);
162  *this = result;
163  }
164  void minutes(int newMinutes)
165  {
166  SttclPosixTimeDuration result(*this);
167  result -= SttclPosixTimeDuration(0,minutes() + newMinutes);
168  *this = result;
169  }
170  void seconds(int newSeconds)
171  {
172  SttclPosixTimeDuration result(*this);
173  result -= SttclPosixTimeDuration(0,0,seconds() + newSeconds);
174  *this = result;
175  }
176  /*
177  void milliseconds(int newMilliSeconds)
178  {
179  }
180  void microseconds(int newMicroSeconds)
181  {
182  }
183  void nanoseconds(long newNanoSeconds)
184  {
185  }
186  */
187 
188  const NativeTimeDuration& getNativeValue() const
189  {
190  return td;
191  }
192 
193 private:
194  void setNative(unsigned int argHours, unsigned int argMinutes, unsigned int argSeconds, unsigned int argMilliSeconds, unsigned long argMicroSeconds, unsigned long argNanoSeconds);
195  void add(const NativeTimeDuration& nativeTimeDuration);
196  void substract(const NativeTimeDuration& nativeTimeDuration);
197  void multiply(int factor);
198  void divide(int divider);
199  void normalizeNative();
200  inline long total_milliseconds() const
201  {
202  return td.tv_sec * 1000 + td.tv_nsec / 1000000;
203  }
204  inline long total_microseconds() const
205  {
206  return td.tv_sec * 1000000 + td.tv_nsec / 1000;
207  }
208  inline long total_nanoseconds() const
209  {
210  return td.tv_sec * 1000000 + td.tv_nsec / 1000;
211  }
212 
213  NativeTimeDuration td;
214 
215 };
216 }
217 }
218 }
219 #endif
220 #endif /* STTCLPOSIXTIME_H_ */