MRPT  2.0.4
datetime_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <gtest/gtest.h>
11 #include <mrpt/core/Clock.h>
12 #include <mrpt/system/datetime.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);
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));
50  const std::string s = mrpt::system::dateTimeToString(t);
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);
65 }
66 
67 TEST(DateTime, timestampAdd)
68 {
69  const auto t1 = mrpt::system::now();
70  const double d1 = mrpt::system::timestampToDouble(t1);
71 
72  const double delta = 10.5; // seconds
73 
74  // Positive incr:
75  {
76  const auto t1p = mrpt::system::timestampAdd(t1, delta);
77  const double d1p = mrpt::system::timestampToDouble(t1p);
78  EXPECT_NEAR(d1p, d1 + delta, 1e-6);
79  }
80  // Negative incr:
81  {
82  const auto t1m = mrpt::system::timestampAdd(t1, -delta);
83  const double d1m = mrpt::system::timestampToDouble(t1m);
84  EXPECT_NEAR(d1m, d1 - delta, 1e-6);
85  }
86 }
EXPECT_LT(out.final_rmse, 3.0)
std::chrono::duration< rep, period > duration
Definition: Clock.h:24
static time_point fromDouble(const double t) noexcept
Create a timestamp from its double representation.
Definition: Clock.cpp:99
EXPECT_GT(out.final_iters, 10UL)
std::chrono::time_point< Clock > time_point
Definition: Clock.h:25
double timestampToDouble(const mrpt::system::TTimeStamp t) noexcept
Transform from TTimeStamp to standard "time_t" (actually a double number, it can contain fractions of...
Definition: datetime.h:116
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:86
static time_point now() noexcept
Returns the current time using the currently selected Clock source.
Definition: Clock.cpp:94
double timestampTotime_t(const mrpt::system::TTimeStamp t) noexcept
Transform from TTimeStamp to standard "time_t" (actually a double number, it can contain fractions of...
Definition: datetime.h:105
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:154
EXPECT_EQ(out.image_pair_was_used.size(), NUM_IMGS)
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:146
EXPECT_NEAR(out.cam_params.rightCameraPose.x, 0.1194, 0.005)
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:91



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020