Time.cpp
Go to the documentation of this file.
1#include "Time.h"
2
3#include <cmath>
4#include <iomanip>
5
6namespace armarx
7{
8
9
10 std::string
11 armem::toStringMilliSeconds(const Time& time, int decimals)
12 {
13 std::stringstream ss;
14 ss << time.toMicroSecondsSinceEpoch() / 1000;
15 if (decimals > 0)
16 {
17 int div = int(std::pow(10, 3 - decimals));
18 ss << "." << std::setfill('0') << std::setw(decimals)
19 << (time.toMicroSecondsSinceEpoch() % 1000) / div;
20 }
21 ss << " ms";
22 return ss.str();
23 }
24
25 std::string
27 {
28 static const char* mu = "\u03BC";
29 std::stringstream ss;
30 ss << time.toMicroSecondsSinceEpoch() << " " << mu << "s";
31 return ss.str();
32 }
33
34 std::string
35 armem::toDateTimeMilliSeconds(const Time& time, int decimals)
36 {
37 std::stringstream ss;
38 ss << time.toString("%Y-%m-%d %H:%M:%S");
39 if (decimals > 0)
40 {
41 int div = int(std::pow(10, 6 - decimals));
42 ss << "." << std::setfill('0') << std::setw(decimals)
43 << (time.toMicroSecondsSinceEpoch() % int(1e6)) / div;
44 }
45
46 return ss.str();
47 }
48
50 armem::timeFromStringMicroSeconds(const std::string& microSeconds)
51 {
52 return Time(Duration::MicroSeconds(std::stol(microSeconds)), ClockType::Virtual);
53 }
54
55} // namespace armarx
std::string toString(const std::string &format) const
String representation of current date time according to given format string.
Definition DateTime.cpp:81
std::int64_t toMicroSecondsSinceEpoch() const
Definition DateTime.cpp:87
static Duration MicroSeconds(std::int64_t microSeconds)
Constructs a duration in microseconds.
Definition Duration.cpp:24
std::string toDateTimeMilliSeconds(const Time &time, int decimals=6)
Returns timeas e.g.
Definition Time.cpp:35
armarx::core::time::DateTime Time
std::string toStringMicroSeconds(const Time &time)
Returns time as e.g.
Definition Time.cpp:26
Time timeFromStringMicroSeconds(const std::string &microSeconds)
Get a Time from the microseconds as text.
Definition Time.cpp:50
std::string toStringMilliSeconds(const Time &time, int decimals=3)
Returns time as e.g.
Definition Time.cpp:11
This file offers overloads of toIce() and fromIce() functions for STL container types.