TimeSpan.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
23 //----------------------------------------------------------------------
24 #include "icl_core/TimeSpan.h"
25 
26 #ifdef _IC_BUILDER_OPENSPLICEDDS_
27 # include "icl_core/iTimeSpan.h"
28 #endif
29 
30 namespace icl_core {
31 
32 const TimeSpan TimeSpan::cZERO(0, 0);
33 
35  : TimeBase(sec, nsec)
36 { }
37 
38 TimeSpan::TimeSpan(const struct timespec& time_span)
39  : TimeBase(time_span)
40 { }
41 
42 #ifdef _IC_BUILDER_OPENSPLICEDDS_
43 TimeSpan::TimeSpan(const iTimeSpan& time_span)
44  : TimeBase(time_span.sec, time_span.nsec)
45 { }
46 
47 TimeSpan& TimeSpan::operator = (const iTimeSpan& time_span)
48 {
49  secs = time_span.sec;
50  nsecs = time_span.nsec;
51  return *this;
52 }
53 
54 TimeSpan::operator iTimeSpan ()
55 {
56  iTimeSpan result;
57  result.sec = secs;
58  result.nsec = nsecs;
59  return result;
60 }
61 #endif
62 
64 {
65  secs = sec;
66  nsecs = 0;
67  normalizeTime();
68  return *this;
69 }
70 
72 {
73  secs = msec / 1000;
74  nsecs = int32_t((msec % 1000) * 1000000);
75  normalizeTime();
76  return *this;
77 }
78 
80 {
81  secs = usec / 1000000;
82  nsecs = int32_t((usec % 1000000) * 1000);
83  normalizeTime();
84  return *this;
85 }
86 
88 {
89  return TimeSpan().fromSec(sec);
90 }
91 
93 {
94  return TimeSpan().fromMSec(msec);
95 }
96 
98 {
99  return TimeSpan().fromUSec(usec);
100 }
101 
103 {
104  secs += span.secs;
105  nsecs += span.nsecs;
106  normalizeTime();
107  return *this;
108 }
109 
111 {
112  secs -= span.secs;
113  nsecs -= span.nsecs;
114  normalizeTime();
115  return *this;
116 }
117 
118 bool TimeSpan::operator != (const TimeSpan& other) const
119 {
120  return TimeBase::operator != (other);
121 }
122 
123 bool TimeSpan::operator == (const TimeSpan& other) const
124 {
125  return TimeBase::operator == (other);
126 }
127 
128 bool TimeSpan::operator < (const TimeSpan& other) const
129 {
130  return TimeBase::operator < (other);
131 }
132 
133 bool TimeSpan::operator > (const TimeSpan& other) const
134 {
135  return TimeBase::operator > (other);
136 }
137 
138 bool TimeSpan::operator <= (const TimeSpan& other) const
139 {
140  return TimeBase::operator <= (other);
141 }
142 
143 bool TimeSpan::operator >= (const TimeSpan& other) const
144 {
145  return TimeBase::operator >= (other);
146 }
147 
149 {
150  return secs;
151 }
152 
154 {
155  return nsecs;
156 }
157 
159 {
160  return nsecs/1000;
161 }
162 
164 {
165  return secs * 1000 + nsecs / 1000000;
166 }
167 
169 {
170  return secs * 1000000 + nsecs / 1000;
171 }
172 
174 {
175  return 1000000000 * secs + nsecs;
176 }
177 
179 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
180 
181  TimeSpan& TimeSpan::FromSec(int64_t sec)
182  {
183  return fromSec(sec);
184  }
185 
186  TimeSpan& TimeSpan::FromMSec(int64_t msec)
187  {
188  return fromMSec(msec);
189  }
190 
191  TimeSpan& TimeSpan::FromUSec(int64_t usec)
192  {
193  return fromUSec(usec);
194  }
195 
196  int64_t TimeSpan::TsSec() const
197  {
198  return tsSec();
199  }
200 
201  int32_t TimeSpan::TsNSec() const
202  {
203  return tsNSec();
204  }
205 
206  int32_t TimeSpan::TsUSec() const
207  {
208  return tsUSec();
209  }
210 
211  int64_t TimeSpan::ToMSec() const
212  {
213  return toMSec();
214  }
215 
216  int64_t TimeSpan::ToUSec() const
217  {
218  return toUSec();
219  }
220 
221  int64_t TimeSpan::ToNSec() const
222  {
223  return toNSec();
224  }
225 
226 #endif
227 
229 std::ostream& operator << (std::ostream& stream, const TimeSpan& time_span)
230 {
231  int64_t calc_secs = time_span.tsSec();
232  int64_t calc_nsec = time_span.tsNSec();
233  if (calc_secs < 0)
234  {
235  stream << "-";
236  calc_secs = -calc_secs;
237  }
238  if (calc_secs > 3600)
239  {
240  stream << calc_secs / 3600 << "h";
241  calc_secs = calc_secs % 3600;
242  }
243  if (calc_secs > 60)
244  {
245  stream << calc_secs / 60 << "m";
246  calc_secs=calc_secs % 60;
247  }
248  if (calc_secs > 0)
249  {
250  stream << calc_secs << "s";
251  }
252 
253  if (calc_nsec / 1000000 * 1000000 == calc_nsec)
254  {
255  stream << calc_nsec / 1000000 << "ms";
256  }
257  else if (calc_nsec / 1000 * 1000 == calc_nsec)
258  {
259  stream << calc_nsec << "us";
260  }
261  else
262  {
263  stream << calc_nsec << "ns";
264  }
265 
266  return stream;
267 }
268 
269 }
static TimeSpan createFromUSec(int64_t usec)
Create a time span with usec microseconds.
Definition: TimeSpan.cpp:97
bool operator>(const TimeBase &other) const
Definition: TimeBase.cpp:68
signed int int32_t
Definition: msvc_stdint.h:90
static TimeSpan createFromMSec(int64_t msec)
Create a time span with msec milliseconds.
Definition: TimeSpan.cpp:92
std::ostream & operator<<(std::ostream &os, StampedBase const &stamped)
Definition: DataHeader.cpp:27
void normalizeTime()
Definition: TimeBase.cpp:83
bool operator<(const TimeSpan &other) const
Definition: TimeSpan.cpp:128
TimeBase(int64_t secs=0, int32_t nsecs=0)
Definition: TimeBase.cpp:239
static const TimeSpan cZERO
Definition: TimeSpan.h:159
TimeSpan(int64_t sec=0, int32_t nsec=0)
Definition: TimeSpan.cpp:34
bool operator==(const TimeSpan &other) const
Definition: TimeSpan.cpp:123
int64_t toMSec() const
May result in an overflow if seconds are too large.
Definition: TimeSpan.cpp:163
bool operator!=(const TimeBase &other) const
Definition: TimeBase.cpp:53
signed __int64 int64_t
Definition: msvc_stdint.h:102
bool operator!=(const TimeSpan &other) const
Definition: TimeSpan.cpp:118
bool operator<(const TimeBase &other) const
Definition: TimeBase.cpp:63
int32_t tsNSec() const
Definition: TimeSpan.cpp:153
int64_t toNSec() const
Definition: TimeSpan.cpp:173
bool operator>=(const TimeSpan &other) const
Definition: TimeSpan.cpp:143
TimeSpan & fromUSec(int64_t usec)
Set the time span to usec microseconds.
Definition: TimeSpan.cpp:79
bool operator>=(const TimeBase &other) const
Definition: TimeBase.cpp:78
Repesents time values.
Definition: TimeBase.h:59
TimeSpan & operator-=(const TimeSpan &span)
Substracts a TimeSpan.
Definition: TimeSpan.cpp:110
TimeSpan & fromMSec(int64_t msec)
Set the time span to msec milliseconds.
Definition: TimeSpan.cpp:71
TimeSpan & operator+=(const TimeSpan &span)
Adds a TimeSpan.
Definition: TimeSpan.cpp:102
Repesents absolute times.
Definition: TimeSpan.h:46
int64_t tsSec() const
Definition: TimeSpan.cpp:148
bool operator>(const TimeSpan &other) const
Definition: TimeSpan.cpp:133
bool operator<=(const TimeSpan &other) const
Definition: TimeSpan.cpp:138
bool operator==(const TimeBase &other) const
Definition: TimeBase.cpp:58
int64_t toUSec() const
May result in an overflow if seconds are too large.
Definition: TimeSpan.cpp:168
static TimeSpan createFromSec(int64_t sec)
Create a time span with sec seconds.
Definition: TimeSpan.cpp:87
int32_t tsUSec() const
Definition: TimeSpan.cpp:158
bool operator<=(const TimeBase &other) const
Definition: TimeBase.cpp:73
TimeSpan & fromSec(int64_t sec)
Set the time span to sec seconds.
Definition: TimeSpan.cpp:63


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58