DateTime.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include <cstdint>
5 #include <ostream>
6 #include <string>
7 
10 
11 namespace IceUtil
12 {
13  class Time;
14 }
15 
16 namespace armarx::core::time
17 {
18 
19  /**
20  * @brief Represents a point in time.
21  *
22  * API and implementation to match IceUtil::Time (where applicable).
23  */
24  class DateTime
25  {
26 
27  public:
28  DateTime();
29 
30  DateTime(Duration timeSinceEpoch,
32  const std::string& hostname = "unknown");
33 
34  DateTime(std::int64_t microSecondsSinceEpoch,
36  const std::string& hostname = "unknown");
37 
38  static DateTime Now();
39  static DateTime Invalid();
40 
41  std::string toDateString() const;
42 
43  std::string toTimeString() const;
44 
45  std::string toDateTimeString() const;
46 
47  /**
48  * @brief String representation of current date time according to given format string.
49  *
50  * The format is according to https://en.cppreference.com/w/cpp/chrono/c/strftime. For
51  * milli seconds and micro seconds, special specifiers "%%msec" and "%%usec" were added
52  * respectively.
53  *
54  * Example format string for "2022-03-04_16-59-10.981789": "%Y-%m-%d_%H-%M-%S.%%usec".
55  *
56  * @param format Format string.
57  * @return Formatted date time.
58  */
59  std::string toString(const std::string& format) const;
60 
61  std::int64_t toMicroSecondsSinceEpoch() const;
62 
63  std::int64_t toMilliSecondsSinceEpoch() const;
64 
65  std::int64_t toSecondsSinceEpoch() const;
66 
68 
69  ClockType clockType() const;
70 
71  std::string hostname() const;
72 
73  bool isValid() const;
74  bool isInvalid() const;
75 
76 
77  // DEPRECATED IceUtil::Time-style API.
78  [[deprecated("Using IceUtil::Time in ArmarX is deprecated. Use armarx::DateTime instead.")]]
79  DateTime(const IceUtil::Time& ice);
80 
81  [[deprecated("Using IceUtil::Time in ArmarX is deprecated. Use armarx::DateTime instead.")]]
82  operator IceUtil::Time() const;
83 
84  // IceUtil::Time-compatible legacy API.
85  [[deprecated("The IceUtil::Time-style API is deprecated. Use DateTime::Now() instead.")]]
86  static DateTime now()
87  {
88  return Now();
89  }
90  [[deprecated("The IceUtil::Time-style API is deprecated. Use DateTime(Duration::MicroSeconds(...)) instead.")]]
92  {
94  }
95 
96  [[deprecated("The IceUtil::Time-style API is deprecated. Use toMicroSecondsSinceEpoch() instead.")]]
97  std::int64_t toMicroSeconds() const
98  {
99  return toMicroSecondsSinceEpoch();
100  }
101 
102  // Operators.
103  public:
104  DateTime operator+(const Duration& rhs) const;
105 
106  DateTime& operator+=(const Duration& rhs);
107 
108  DateTime operator-(const Duration& rhs) const;
109 
110  DateTime& operator-=(const Duration& rhs);
111 
112  Duration operator-(const DateTime& rhs) const;
113 
114  bool operator<(const DateTime& rhs) const;
115 
116  bool operator<=(const DateTime& rhs) const;
117 
118  bool operator==(const DateTime& rhs) const;
119 
120  bool operator>(const DateTime& rhs) const;
121 
122  bool operator>=(const DateTime& rhs) const;
123 
124  bool operator!=(const DateTime& rhs) const;
125 
126  protected:
127 
129 
131 
133 
134  std::string _hostname;
135  };
136 
137 
138  std::ostream& operator<<(std::ostream& out, const DateTime& rhs);
139 
140 } // namespace armarx::core::time
141 
142 
143 namespace armarx
144 {
145  using core::time::DateTime;
146 } // namespace armarx
armarx::core::time::DateTime::isValid
bool isValid() const
Definition: DateTime.cpp:135
armarx::core::time::DateTime::operator<
bool operator<(const DateTime &rhs) const
Definition: DateTime.cpp:184
armarx::core::time::DateTime::isInvalid
bool isInvalid() const
Definition: DateTime.cpp:140
armarx::core::time::DateTime::toDateString
std::string toDateString() const
Definition: DateTime.cpp:67
armarx::core::time::DateTime::toDurationSinceEpoch
Duration toDurationSinceEpoch() const
Definition: DateTime.cpp:116
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::core::time::DateTime::clockType
ClockType clockType() const
Definition: DateTime.cpp:123
Duration.h
armarx::core::time::DateTime::operator==
bool operator==(const DateTime &rhs) const
Definition: DateTime.cpp:198
armarx::core::time
Definition: Clock.cpp:13
armarx::core::time::DateTime::toSecondsSinceEpoch
std::int64_t toSecondsSinceEpoch() const
Definition: DateTime.cpp:109
armarx::core::time::DateTime::operator+
DateTime operator+(const Duration &rhs) const
Definition: DateTime.cpp:147
armarx::core::time::DateTime::now
static DateTime now()
Definition: DateTime.h:86
armarx::core::time::operator<<
std::ostream & operator<<(std::ostream &out, const DateTime &rhs)
Definition: DateTime.cpp:226
IceUtil
Definition: Instance.h:21
armarx::core::time::DateTime::operator-=
DateTime & operator-=(const Duration &rhs)
Definition: DateTime.cpp:169
armarx::core::time::DateTime::DateTime
DateTime()
Definition: DateTime.cpp:18
armarx::core::time::DateTime::operator>=
bool operator>=(const DateTime &rhs) const
Definition: DateTime.cpp:212
armarx::core::time::DateTime::operator<=
bool operator<=(const DateTime &rhs) const
Definition: DateTime.cpp:191
armarx::core::time::DateTime::toMicroSeconds
std::int64_t toMicroSeconds() const
Definition: DateTime.h:97
armarx::core::time::ClockType
ClockType
Describes the type of clock.
Definition: ClockType.h:10
armarx::core::time::DateTime::_hostname
std::string _hostname
Definition: DateTime.h:134
armarx::core::time::DateTime::_timeSinceEpoch
Duration _timeSinceEpoch
Definition: DateTime.h:130
armarx::core::time::DateTime::operator>
bool operator>(const DateTime &rhs) const
Definition: DateTime.cpp:205
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::toMilliSecondsSinceEpoch
std::int64_t toMilliSecondsSinceEpoch() const
Definition: DateTime.cpp:102
armarx::core::time::DateTime::operator-
DateTime operator-(const Duration &rhs) const
Definition: DateTime.cpp:162
armarx::core::time::ClockType::Virtual
@ Virtual
Time given by time server if configured, realtime otherwise.
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
armarx::core::time::DateTime::hostname
std::string hostname() const
Definition: DateTime.cpp:130
armarx::core::time::DateTime::toTimeString
std::string toTimeString() const
Definition: DateTime.cpp:74
armarx::core::time::Duration
Represents a duration.
Definition: Duration.h:17
armarx::core::time::DateTime::operator!=
bool operator!=(const DateTime &rhs) const
Definition: DateTime.cpp:219
armarx::core::time::DateTime::_clockType
ClockType _clockType
Definition: DateTime.h:132
armarx::core::time::DateTime::microSeconds
static DateTime microSeconds(long microSeconds)
Definition: DateTime.h:91
armarx::core::time::Duration::MicroSeconds
static Duration MicroSeconds(std::int64_t microSeconds)
Constructs a duration in microseconds.
Definition: Duration.cpp:27
armarx::core::time::DateTime::_invalidTimeSinceEpoch
static const Duration _invalidTimeSinceEpoch
Definition: DateTime.h:128
armarx::core::time::DateTime::Invalid
static DateTime Invalid()
Definition: DateTime.cpp:60
armarx::core::time::DateTime::toDateTimeString
std::string toDateTimeString() const
Definition: DateTime.cpp:81
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::core::time::DateTime::operator+=
DateTime & operator+=(const Duration &rhs)
Definition: DateTime.cpp:154
ClockType.h