Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
wolf
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mobile_robotics
wolf_projects
wolf_lib
wolf
Commits
9189a5ee
Commit
9189a5ee
authored
4 years ago
by
Joan Solà Ortega
Browse files
Options
Downloads
Plain Diff
Merge branch '345-timestamp-ok' into 'devel'
Resolve "TimeStamp::ok()" Closes
#345
See merge request
!381
parents
8f460727
c59a1af5
No related branches found
No related tags found
1 merge request
!381
Resolve "TimeStamp::ok()"
Pipeline
#5673
failed
4 years ago
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
include/core/common/time_stamp.h
+38
-2
38 additions, 2 deletions
include/core/common/time_stamp.h
src/common/time_stamp.cpp
+20
-4
20 additions, 4 deletions
src/common/time_stamp.cpp
test/gtest_time_stamp.cpp
+25
-4
25 additions, 4 deletions
test/gtest_time_stamp.cpp
with
83 additions
and
10 deletions
include/core/common/time_stamp.h
+
38
−
2
View file @
9189a5ee
...
...
@@ -23,6 +23,7 @@ class TimeStamp
{
protected:
unsigned
long
int
time_stamp_nano_
;
///< Time stamp. Expressed in nanoseconds from 1th jan 1970.
bool
is_valid_
;
// time stamp has a valid value
public:
/** \brief Constructor
...
...
@@ -60,6 +61,16 @@ class TimeStamp
*/
~
TimeStamp
();
/** \brief Value of time stamp is valid
*
*/
static
TimeStamp
Invalid
(
);
bool
ok
(
)
const
;
void
setOk
(
);
void
setNOk
(
);
static
TimeStamp
Now
();
/** \brief Time stamp to now
*/
void
setToNow
();
...
...
@@ -174,19 +185,42 @@ class TimeStamp
};
inline
wolf
::
TimeStamp
TimeStamp
::
Invalid
(
)
{
return
TimeStamp
(
-
1.0
);
}
inline
bool
TimeStamp
::
ok
(
)
const
{
return
is_valid_
;
}
inline
void
TimeStamp
::
setOk
(
)
{
is_valid_
=
true
;
}
inline
void
TimeStamp
::
setNOk
(
)
{
is_valid_
=
false
;
}
inline
void
TimeStamp
::
set
(
const
double
&
ts
)
{
time_stamp_nano_
=
(
ts
>
0
?
(
unsigned
long
int
)(
ts
*
NANOSECS
)
:
0
);
time_stamp_nano_
=
(
ts
>=
0
?
(
unsigned
long
int
)(
ts
*
NANOSECS
)
:
0
);
is_valid_
=
(
ts
>=
0
);
}
inline
void
TimeStamp
::
set
(
const
unsigned
long
int
&
sec
,
const
unsigned
long
int
&
nanosec
)
{
time_stamp_nano_
=
sec
*
NANOSECS
+
nanosec
;
is_valid_
=
true
;
}
inline
void
TimeStamp
::
set
(
const
timeval
&
ts
)
{
time_stamp_nano_
=
(
unsigned
long
int
)(
ts
.
tv_sec
*
NANOSECS
)
+
(
unsigned
long
int
)(
ts
.
tv_usec
*
1000
);
is_valid_
=
(
ts
.
tv_sec
>=
0
and
ts
.
tv_usec
>=
0
);
}
inline
double
TimeStamp
::
get
()
const
...
...
@@ -207,11 +241,13 @@ inline unsigned long int TimeStamp::getNanoSeconds() const
inline
void
TimeStamp
::
operator
=
(
const
TimeStamp
&
ts
)
{
time_stamp_nano_
=
ts
.
time_stamp_nano_
;
is_valid_
=
ts
.
is_valid_
;
}
inline
void
TimeStamp
::
operator
=
(
const
double
&
ts
)
{
time_stamp_nano_
=
(
unsigned
long
int
)(
ts
*
NANOSECS
);
is_valid_
=
(
ts
>=
0
);
}
inline
bool
TimeStamp
::
operator
==
(
const
TimeStamp
&
ts
)
const
...
...
@@ -254,7 +290,7 @@ inline double TimeStamp::operator -(const TimeStamp& ts) const
return
double
((
long
int
)(
time_stamp_nano_
-
ts
.
time_stamp_nano_
))
*
1e-9
;
// long int cast fix overflow in case of negative substraction result
}
static
const
TimeStamp
Invalid
Stamp
(
-
1
,
-
1
);
//
static const TimeStamp
TimeStampInvalid() {return Time
Stamp(-1
.0
);
}
}
// namespace wolf
...
...
This diff is collapsed.
Click to expand it.
src/common/time_stamp.cpp
+
20
−
4
View file @
9189a5ee
...
...
@@ -3,8 +3,17 @@
namespace
wolf
{
TimeStamp
TimeStamp
::
Now
(
)
{
TimeStamp
t
(
0
);
t
.
setToNow
();
return
t
;
}
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
TimeStamp
&
_ts
)
{
if
(
!
_ts
.
ok
())
os
<<
"TimeStamp is invalid! "
;
os
<<
_ts
.
getSeconds
()
<<
"."
<<
std
::
setfill
(
'0'
)
<<
std
::
setw
(
9
)
<<
std
::
right
<<
_ts
.
getNanoSeconds
();
// write obj to stream
os
<<
std
::
setfill
(
' '
);
return
os
;
...
...
@@ -13,24 +22,29 @@ std::ostream& operator<<(std::ostream& os, const TimeStamp& _ts)
TimeStamp
::
TimeStamp
()
:
//time_stamp_(0)
time_stamp_nano_
(
0
)
,
is_valid_
(
false
)
{
setToNow
();
//
setToNow();
}
TimeStamp
::
TimeStamp
(
const
TimeStamp
&
_ts
)
:
time_stamp_nano_
(
_ts
.
time_stamp_nano_
)
time_stamp_nano_
(
_ts
.
time_stamp_nano_
),
is_valid_
(
_ts
.
is_valid_
)
{
//
}
TimeStamp
::
TimeStamp
(
const
double
&
_ts
)
:
time_stamp_nano_
(
_ts
>
0
?
(
unsigned
long
int
)(
_ts
*
1e9
)
:
0
)
time_stamp_nano_
(
_ts
>
0
?
(
unsigned
long
int
)(
_ts
*
1e9
)
:
0
),
is_valid_
(
_ts
>
0
)
{
//
}
TimeStamp
::
TimeStamp
(
const
unsigned
long
int
&
_sec
,
const
unsigned
long
int
&
_nsec
)
:
time_stamp_nano_
(
_sec
*
NANOSECS
+
_nsec
)
time_stamp_nano_
(
_sec
*
NANOSECS
+
_nsec
),
is_valid_
(
true
)
{
//
}
...
...
@@ -45,6 +59,7 @@ void TimeStamp::setToNow()
timeval
ts
;
gettimeofday
(
&
ts
,
NULL
);
set
(
ts
);
setOk
();
}
TimeStamp
TimeStamp
::
operator
+
(
const
double
&
dt
)
const
...
...
@@ -64,6 +79,7 @@ TimeStamp TimeStamp::operator -(const double& dt) const
void
TimeStamp
::
operator
-=
(
const
double
&
dt
)
{
unsigned
long
int
dt_nano
=
(
unsigned
long
int
)(
dt
*
NANOSECS
);
is_valid_
=
(
time_stamp_nano_
>=
dt_nano
);
time_stamp_nano_
=
(
dt_nano
>
time_stamp_nano_
?
0
:
time_stamp_nano_
-
dt_nano
);
}
...
...
This diff is collapsed.
Click to expand it.
test/gtest_time_stamp.cpp
+
25
−
4
View file @
9189a5ee
...
...
@@ -3,9 +3,30 @@
#include
<thread>
using
namespace
wolf
;
TEST
(
WolfTestTimeStamp
,
TimeStampInvalid
)
{
auto
t
=
TimeStamp
::
Invalid
();
WOLF_DEBUG
(
"t = "
,
t
);
ASSERT_FALSE
(
t
.
ok
());
t
=
-
1
;
WOLF_DEBUG
(
"t = "
,
t
);
ASSERT_FALSE
(
t
.
ok
());
t
=
0
;
WOLF_DEBUG
(
"t = "
,
t
);
ASSERT_TRUE
(
t
.
ok
());
t
=
1
;
WOLF_DEBUG
(
"t = "
,
t
);
ASSERT_TRUE
(
t
.
ok
());
}
TEST
(
WolfTestTimeStamp
,
TimeStampInitNow
)
{
wolf
::
TimeStamp
start
;
wolf
::
TimeStamp
start
=
wolf
::
TimeStamp
::
Now
()
;
// If we don't sleep, start == time_stamp sometimes.
// And sometimes start <= time_stamp ...
...
...
@@ -13,7 +34,7 @@ TEST(WolfTestTimeStamp, TimeStampInitNow)
ASSERT_NE
(
start
.
get
(),
0
);
wolf
::
TimeStamp
time_stamp
;
wolf
::
TimeStamp
time_stamp
=
wolf
::
TimeStamp
::
Now
()
;
// std::cout << std::fixed;
// std::cout << std::setprecision(15);
...
...
@@ -137,11 +158,11 @@ TEST(WolfTestTimeStamp, TimeStampEquality)
TEST
(
WolfTestTimeStamp
,
TimeStampInequality
)
{
wolf
::
TimeStamp
start
;
wolf
::
TimeStamp
start
=
wolf
::
TimeStamp
::
Now
()
;
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
microseconds
(
1
));
wolf
::
TimeStamp
time_stamp
;
wolf
::
TimeStamp
time_stamp
=
wolf
::
TimeStamp
::
Now
()
;
// error: no match for ‘operator!=’
//ASSERT_NE(time_stamp, start);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment