MRPT  1.9.9
datetime.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/core/Clock.h>
12 #include <mrpt/core/exceptions.h>
13 #include <cstdint>
14 #include <string>
15 #include <iosfwd>
16 
17 namespace mrpt::system
18 {
19 /** @defgroup time_date Time and date functions.
20  * Header: `#include <mrpt/system/datetime.h>`.
21  * Library: \ref mrpt_system_grp
22  *
23  * Defines types and functions to handle cross-platform timestamps. The basic
24  * type is mrpt::system::TTimeStamp, representing a high-resolution (100ns)
25  * Clock::time_point, compatible with all C++11 std::chrono functions.
26  *
27  * There are also functions to convert forth and back to a `double`
28  * representation of timestamps: numbers just like UNIX epoch timestamps but
29  * with decimals for the fractionary part of seconds.
30  *
31  * \ingroup mrpt_system_grp
32  * @{ */
33 
34 /** A system independent time type, it holds the the number of 100-nanosecond
35  * intervals since January 1, 1601 (UTC) as a mrpt::Clock::time_point
36  * (uint64_t).
37  * \sa system::getCurrentTime, system::timeDifference, INVALID_TIMESTAMP,
38  * TTimeParts
39  */
41 
42 /** Represents an invalid timestamp, where applicable. */
43 #define INVALID_TIMESTAMP mrpt::Clock::time_point()
44 
45 /** The parts of a date/time (it's like the standard 'tm' but with fractions of
46  * seconds).
47  * \sa TTimeStamp, timestampToParts, buildTimestampFromParts
48  */
49 struct TTimeParts
50 {
51  uint16_t year; /** The year */
52  uint8_t month; /** Month (1-12) */
53  uint8_t day; /** Day (1-31) */
54  uint8_t hour; /** Hour (0-23) */
55  uint8_t minute; /** Minute (0-59) */
56  double second; /** Seconds (0.0000-59.9999) */
57  uint8_t day_of_week; /** Day of week (1:Sunday, 7:Saturday) */
59 };
60 
61 /** Builds a timestamp from the parts (Parts are in UTC)
62  * \sa timestampToParts
63  */
66 
67 /** Builds a timestamp from the parts (Parts are in local time)
68  * \sa timestampToParts, buildTimestampFromParts
69  */
72 
73 /** Gets the individual parts of a date/time (days, hours, minutes, seconds) -
74  * UTC time or local time
75  * \sa buildTimestampFromParts
76  */
77 void timestampToParts(TTimeStamp t, TTimeParts& p, bool localTime = false);
78 
79 /** Returns the current (UTC) system time.
80  * \sa now
81  */
83 
84 /** A shortcut for system::getCurrentTime
85  * \sa getCurrentTime
86  */
88 
89 /** Transform from standard "time_t" (actually a double number, it can contain
90  * fractions of seconds) to TTimeStamp.
91  * \sa timestampTotime_t
92  */
94 {
95  return mrpt::Clock::fromDouble(t);
96 }
97 
98 /** Transform from standard "time_t" to TTimeStamp.
99  * \sa timestampTotime_t
100  */
102 
103 /** Transform from TTimeStamp to standard "time_t" (actually a double number, it
104  * can contain fractions of seconds).
105  * \sa time_tToTimestamp
106  */
108 
109 /** Transform from TTimeStamp to standard "time_t" (actually a double number, it
110  * can contain fractions of seconds).
111  * This function is just an (inline) alias of timestampTotime_t(), with a more
112  * significant name.
113  * \sa time_tToTimestamp
114  */
116 {
117  return timestampTotime_t(t);
118 }
119 
120 /** Returns the time difference from t1 to t2 (positive if t2 is posterior to
121  * t1), in seconds */
122 inline double timeDifference(
123  const mrpt::system::TTimeStamp t_first,
124  const mrpt::system::TTimeStamp t_later)
125 {
126  MRPT_START
127  ASSERT_(t_later != INVALID_TIMESTAMP);
128  ASSERT_(t_first != INVALID_TIMESTAMP);
129  return 1e-6 * std::chrono::duration_cast<std::chrono::microseconds>(
130  t_later - t_first)
131  .count();
132  MRPT_END
133 }
134 
135 /** Returns the current time, as a `double` (fractional version of time_t)
136  * instead of a `TTimeStamp`.
137  * \sa now(), timestampTotime_t() */
138 inline double now_double()
139 {
141 }
142 
143 /** Shifts a timestamp the given amount of seconds (>0: forwards in time, <0:
144  * backwards) */
146  const mrpt::system::TTimeStamp tim, const double num_seconds)
147 {
148  return tim +
149  std::chrono::microseconds(static_cast<uint64_t>(num_seconds * 1e6));
150 }
151 
152 /** Returns a formated string with the given time difference (passed as the
153  * number of seconds), as a string [H]H:MM:SS.MILISECS
154  * \sa unitsFormat
155  */
156 std::string formatTimeInterval(const double timeSeconds);
157 
158 /** Convert a timestamp into this textual form (UTC time):
159  * YEAR/MONTH/DAY,HH:MM:SS.MMM
160  * \sa dateTimeLocalToString
161  */
163 
164 /** Convert a timestamp into this textual form (in local time):
165  * YEAR/MONTH/DAY,HH:MM:SS.MMM
166  * \sa dateTimeToString
167  */
169 
170 /** Convert a timestamp into this textual form: YEAR/MONTH/DAY
171  */
173 
174 /** Returns the number of seconds ellapsed from midnight in the given timestamp
175  */
177 
178 /** Convert a timestamp into this textual form (UTC): HH:MM:SS.MMMMMM
179  */
181 
182 /** Convert a timestamp into this textual form (in local time): HH:MM:SS.MMMMMM
183  */
185  const mrpt::system::TTimeStamp t, unsigned int secondFractionDigits = 6);
186 
187 /** This function implements time interval formatting: Given a time in seconds,
188  * it will return a string describing the interval with the most appropriate
189  * unit.
190  * E.g.: 1.23 year, 3.50 days, 9.3 hours, 5.3 minutes, 3.34 sec, 178.1 ms, 87.1
191  * us.
192  * \sa unitsFormat
193  */
194 std::string intervalFormat(const double seconds);
195 
196 /** Textual representation of a TTimeStamp as the plain number in
197  * time_since_epoch().count() */
198 std::ostream& operator<<(std::ostream& o, const TTimeStamp& t);
199 
200 /** @} */
201 
202 } // namespace mrpt::system
void timestampToParts(TTimeStamp t, TTimeParts &p, bool localTime=false)
Gets the individual parts of a date/time (days, hours, minutes, seconds) - UTC time or local time...
Definition: datetime.cpp:61
GLuint GLuint GLsizei count
Definition: glext.h:3528
std::ostream & operator<<(std::ostream &o, const TTimeStamp &t)
Textual representation of a TTimeStamp as the plain number in time_since_epoch().count() ...
Definition: datetime.cpp:301
#define MRPT_START
Definition: exceptions.h:262
GLdouble GLdouble t
Definition: glext.h:3689
static time_point fromDouble(const double t) noexcept
Create a timestamp from its double representation.
Definition: Clock.cpp:51
unsigned __int16 uint16_t
Definition: rptypes.h:44
std::chrono::time_point< Clock > time_point
Definition: Clock.h:26
mrpt::system::TTimeStamp getCurrentTime()
Returns the current (UTC) system time.
Definition: datetime.h:82
double now_double()
Returns the current time, as a double (fractional version of time_t) instead of a TTimeStamp...
Definition: datetime.h:138
mrpt::system::TTimeStamp buildTimestampFromParts(const mrpt::system::TTimeParts &p)
Builds a timestamp from the parts (Parts are in UTC)
Definition: datetime.cpp:85
uint8_t day_of_week
Seconds (0.0000-59.9999)
Definition: datetime.h:57
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:87
double extractDayTimeFromTimestamp(const mrpt::system::TTimeStamp t)
Returns the number of seconds ellapsed from midnight in the given timestamp.
Definition: datetime.cpp:192
std::string timeToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (UTC): HH:MM:SS.MMMMMM.
Definition: datetime.cpp:241
std::string formatTimeInterval(const double timeSeconds)
Returns a formated string with the given time difference (passed as the number of seconds)...
Definition: datetime.cpp:131
double timestampToDouble(const mrpt::system::TTimeStamp t)
Transform from TTimeStamp to standard "time_t" (actually a double number, it can contain fractions of...
Definition: datetime.h:115
static time_point now() noexcept
Returns the current time, with the highest resolution available.
Definition: Clock.cpp:46
unsigned char uint8_t
Definition: rptypes.h:41
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
mrpt::Clock::time_point TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
Definition: datetime.h:40
int daylight_saving
Day of week (1:Sunday, 7:Saturday)
Definition: datetime.h:58
std::string dateToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form: YEAR/MONTH/DAY.
Definition: datetime.cpp:261
The parts of a date/time (it&#39;s like the standard &#39;tm&#39; but with fractions of seconds).
Definition: datetime.h:49
std::string intervalFormat(const double seconds)
This function implements time interval formatting: Given a time in seconds, it will return a string d...
Definition: datetime.cpp:281
uint8_t day
Month (1-12)
Definition: datetime.h:53
GLsizei const GLchar ** string
Definition: glext.h:4101
double second
Minute (0-59)
Definition: datetime.h:56
mrpt::system::TTimeStamp buildTimestampFromPartsLocalTime(const mrpt::system::TTimeParts &p)
Builds a timestamp from the parts (Parts are in local time)
Definition: datetime.cpp:108
uint8_t minute
Hour (0-23)
Definition: datetime.h:55
std::string dateTimeToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (UTC time): YEAR/MONTH/DAY,HH:MM:SS.MMM.
Definition: datetime.cpp:147
#define MRPT_END
Definition: exceptions.h:266
std::string timeLocalToString(const mrpt::system::TTimeStamp t, unsigned int secondFractionDigits=6)
Convert a timestamp into this textual form (in local time): HH:MM:SS.MMMMMM.
Definition: datetime.cpp:217
uint8_t month
The year.
Definition: datetime.h:52
mrpt::system::TTimeStamp timestampAdd(const mrpt::system::TTimeStamp tim, const double num_seconds)
Shifts a timestamp the given amount of seconds (>0: forwards in time, <0: backwards) ...
Definition: datetime.h:145
uint8_t hour
Day (1-31)
Definition: datetime.h:54
double timeDifference(const mrpt::system::TTimeStamp t_first, const mrpt::system::TTimeStamp t_later)
Returns the time difference from t1 to t2 (positive if t2 is posterior to t1), in seconds...
Definition: datetime.h:122
std::string dateTimeLocalToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (in local time): YEAR/MONTH/DAY,HH:MM:SS.MMM.
Definition: datetime.cpp:170
mrpt::system::TTimeStamp time_tToTimestamp(const double t)
Transform from standard "time_t" (actually a double number, it can contain fractions of seconds) to T...
Definition: datetime.h:93
GLfloat GLfloat p
Definition: glext.h:6305
#define INVALID_TIMESTAMP
Represents an invalid timestamp, where applicable.
Definition: datetime.h:43
double timestampTotime_t(const mrpt::system::TTimeStamp t)
Transform from TTimeStamp to standard "time_t" (actually a double number, it can contain fractions of...
Definition: datetime.cpp:50



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020