Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
Actor Framework
Commits
3562ae6d
Commit
3562ae6d
authored
Jul 31, 2018
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make timespan and timestamp convertible to string
parent
254e30c6
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
0 deletions
+82
-0
libcaf_core/caf/detail/stringification_inspector.hpp
libcaf_core/caf/detail/stringification_inspector.hpp
+18
-0
libcaf_core/src/stringification_inspector.cpp
libcaf_core/src/stringification_inspector.cpp
+31
-0
libcaf_core/test/deep_to_string.cpp
libcaf_core/test/deep_to_string.cpp
+33
-0
No files found.
libcaf_core/caf/detail/stringification_inspector.hpp
View file @
3562ae6d
...
@@ -28,6 +28,8 @@
...
@@ -28,6 +28,8 @@
#include "caf/error.hpp"
#include "caf/error.hpp"
#include "caf/none.hpp"
#include "caf/none.hpp"
#include "caf/string_view.hpp"
#include "caf/string_view.hpp"
#include "caf/timespan.hpp"
#include "caf/timestamp.hpp"
#include "caf/meta/type_name.hpp"
#include "caf/meta/type_name.hpp"
#include "caf/meta/omittable.hpp"
#include "caf/meta/omittable.hpp"
...
@@ -76,6 +78,22 @@ public:
...
@@ -76,6 +78,22 @@ public:
void
consume
(
string_view
str
);
void
consume
(
string_view
str
);
void
consume
(
timespan
&
x
);
void
consume
(
timestamp
&
x
);
template
<
class
Clock
,
class
Duration
>
void
consume
(
std
::
chrono
::
time_point
<
Clock
,
Duration
>&
x
)
{
timestamp
tmp
{
std
::
chrono
::
duration_cast
<
timespan
>
(
x
.
time_since_epoch
())};
consume
(
tmp
);
}
template
<
class
Rep
,
class
Period
>
void
consume
(
std
::
chrono
::
duration
<
Rep
,
Period
>&
x
)
{
auto
tmp
=
std
::
chrono
::
duration_cast
<
timespan
>
(
x
);
consume
(
tmp
);
}
inline
void
consume
(
bool
&
x
)
{
inline
void
consume
(
bool
&
x
)
{
result_
+=
x
?
"true"
:
"false"
;
result_
+=
x
?
"true"
:
"false"
;
}
}
...
...
libcaf_core/src/stringification_inspector.cpp
View file @
3562ae6d
...
@@ -67,5 +67,36 @@ void stringification_inspector::consume(string_view str) {
...
@@ -67,5 +67,36 @@ void stringification_inspector::consume(string_view str) {
result_
+=
'"'
;
result_
+=
'"'
;
}
}
void
stringification_inspector
::
consume
(
timespan
&
x
)
{
auto
count
=
x
.
count
();
auto
res
=
[
&
](
const
char
*
suffix
)
{
result_
+=
std
::
to_string
(
count
);
result_
+=
suffix
;
};
// Check whether it's nano-, micro-, or milliseconds.
for
(
auto
suffix
:
{
"ns"
,
"us"
,
"ms"
})
{
if
(
count
%
1000
!=
0
)
return
res
(
suffix
);
count
/=
1000
;
}
// After the loop we only need to differentiate between seconds and minutes.
if
(
count
%
60
!=
0
)
return
res
(
"s"
);
count
/=
60
;
return
res
(
"min"
);
}
void
stringification_inspector
::
consume
(
timestamp
&
x
)
{
char
buf
[
64
];
auto
y
=
std
::
chrono
::
time_point_cast
<
timestamp
::
clock
::
duration
>
(
x
);
auto
z
=
timestamp
::
clock
::
to_time_t
(
y
);
strftime
(
buf
,
sizeof
(
buf
),
"%FT%T"
,
std
::
localtime
(
&
z
));
result_
+=
buf
;
// time_t has no milliseconds, so we need to insert them manually.
auto
ms
=
(
x
.
time_since_epoch
().
count
()
/
1000000
)
%
1000
;
result_
+=
'.'
;
result_
+=
std
::
to_string
(
ms
);
}
}
// namespace detail
}
// namespace detail
}
// namespace caf
}
// namespace caf
libcaf_core/test/deep_to_string.cpp
0 → 100644
View file @
3562ae6d
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#define CAF_SUITE deep_to_string
#include "caf/deep_to_string.hpp"
#include "caf/test/dsl.hpp"
using
namespace
caf
;
CAF_TEST
(
timespans
)
{
CAF_CHECK_EQUAL
(
deep_to_string
(
timespan
{
1
}),
"1ns"
);
CAF_CHECK_EQUAL
(
deep_to_string
(
timespan
{
1000
}),
"1us"
);
CAF_CHECK_EQUAL
(
deep_to_string
(
timespan
{
1000000
}),
"1ms"
);
CAF_CHECK_EQUAL
(
deep_to_string
(
timespan
{
1000000000
}),
"1s"
);
CAF_CHECK_EQUAL
(
deep_to_string
(
timespan
{
60000000000
}),
"1min"
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment