MRPT  1.9.9
datetime_unittest.cpp
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 
10 #include <mrpt/system/datetime.h>
11 #include <mrpt/core/Clock.h>
12 #include <gtest/gtest.h>
13 
14 #include <thread>
15 
16 TEST(DateTime, dateTimeVsClock)
17 {
18  using namespace std::chrono_literals;
19  auto now1 = mrpt::Clock::now().time_since_epoch();
20  std::this_thread::sleep_for(1ms);
21  auto now_timestamp1 = mrpt::system::getCurrentTime();
22  EXPECT_LT(now1.count(), now_timestamp1.time_since_epoch().count());
23 
24  auto now_timestamp2 = mrpt::system::getCurrentTime();
25  std::this_thread::sleep_for(1ms);
26  auto now2 = mrpt::Clock::now().time_since_epoch();
27  EXPECT_GT(now2.count(), now_timestamp2.time_since_epoch().count());
28 }
29 
30 TEST(DateTime, time_t_forth_back)
31 {
32  const double td5 = 1534142320.5;
33  const time_t t = 1534142320;
34 
35  auto t1 = mrpt::system::time_tToTimestamp(td5);
37  EXPECT_NEAR(
38  std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t2).count(),
39  500, 1);
40 
41  const double t1d = mrpt::system::timestampTotime_t(t1);
42  const double t2d = mrpt::system::timestampTotime_t(t2);
43  EXPECT_NEAR((t1d - t2d), 0.5, 1e-3);
44 }
45 
46 TEST(DateTime, fixed_date_check)
47 {
48  const uint64_t t_raw = 127822463930948526;
49  const auto t = mrpt::Clock::time_point(mrpt::Clock::duration(t_raw));
51  EXPECT_EQ(std::string("2006/01/20,15:59:53.094852"), s);
52 
53  const double t_d = mrpt::system::timestampTotime_t(t);
54  EXPECT_NEAR(1137772793.09485, t_d, 1e-5);
55 }
56 
57 TEST(DateTime, double_to_from)
58 {
59  auto t1 = mrpt::system::now();
60  const double d1 = mrpt::system::timestampToDouble(t1);
61  auto t2 = mrpt::Clock::fromDouble(d1);
62  EXPECT_NEAR(
65 }
std::chrono::duration< rep, period > duration
Definition: Clock.h:25
GLuint GLuint GLsizei count
Definition: glext.h:3528
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
std::chrono::time_point< Clock > time_point
Definition: Clock.h:26
TEST(DateTime, dateTimeVsClock)
mrpt::system::TTimeStamp getCurrentTime()
Returns the current (UTC) system time.
Definition: datetime.h:82
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:87
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
GLdouble s
Definition: glext.h:3676
GLsizei const GLchar ** string
Definition: glext.h:4101
unsigned __int64 uint64_t
Definition: rptypes.h:50
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
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
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