Time.cpp
Go to the documentation of this file.
1 #include "Time.h"
2 
3 #include <cmath>
4 #include <iomanip>
5 
6 
7 namespace armarx
8 {
9 
10 
11  std::string 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 
26  std::string armem::toStringMicroSeconds(const Time& time)
27  {
28  static const char* mu = "\u03BC";
29  std::stringstream ss;
30  ss << time.toMicroSecondsSinceEpoch() << " " << mu << "s";
31  return ss.str();
32  }
33 
34 
35  std::string 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 
49 
50  armem::Time armem::timeFromStringMicroSeconds(const std::string& microSeconds)
51  {
52  return Time(Duration::MicroSeconds(std::stol(microSeconds)), ClockType::Virtual);
53  }
54 
55 }
56 
57 
58 
armarx::armem::timeFromStringMicroSeconds
Time timeFromStringMicroSeconds(const std::string &microSeconds)
Get a Time from the microseconds as text.
Definition: Time.cpp:50
armarx::armem::toDateTimeMilliSeconds
std::string toDateTimeMilliSeconds(const Time &time, int decimals=6)
Returns timeas e.g.
Definition: Time.cpp:35
armarx::armem::toStringMilliSeconds
std::string toStringMilliSeconds(const Time &time, int decimals=3)
Returns time as e.g.
Definition: Time.cpp:11
armarx::armem::toStringMicroSeconds
std::string toStringMicroSeconds(const Time &time)
Returns time as e.g.
Definition: Time.cpp:26
armarx::core::time::DateTime::toMicroSecondsSinceEpoch
std::int64_t toMicroSecondsSinceEpoch() const
Definition: DateTime.cpp:95
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::core::time::DateTime::toString
std::string toString(const std::string &format) const
String representation of current date time according to given format string.
Definition: DateTime.cpp:88
Time.h
armarx::core::time::Duration::MicroSeconds
static Duration MicroSeconds(std::int64_t microSeconds)
Constructs a duration in microseconds.
Definition: Duration.cpp:27
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28