blob: 92699a958cd56a61b8ade1d8d5c2d678aa1a5598 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0f830802018-01-16 23:58:58 -05002/*
Davide Pesavento483087f2020-08-04 22:38:05 -04003 * Copyright (c) 2013-2020 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080020 */
21
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/util/time.hpp"
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070025
Davide Pesavento923ba442019-02-12 22:00:38 -050026#include <boost/lexical_cast.hpp>
Davide Pesavento0f830802018-01-16 23:58:58 -050027#include <thread>
28
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080029namespace ndn {
Alexander Afanasyevf8379172017-01-11 16:56:04 -080030namespace time {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080031namespace tests {
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080032
Junxiao Shi5537d132016-08-05 03:48:01 +000033BOOST_AUTO_TEST_SUITE(Util)
34BOOST_AUTO_TEST_SUITE(TestTime)
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080035
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070036BOOST_AUTO_TEST_CASE(SystemClock)
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080037{
Alexander Afanasyevf8379172017-01-11 16:56:04 -080038 system_clock::TimePoint value = system_clock::now();
Davide Pesavento483087f2020-08-04 22:38:05 -040039 system_clock::TimePoint referenceTime = fromUnixTimestamp(1390966967032_ms);
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080040
Davide Pesavento483087f2020-08-04 22:38:05 -040041 BOOST_TEST(value > referenceTime);
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080042
Alexander Afanasyevf8379172017-01-11 16:56:04 -080043 BOOST_CHECK_EQUAL(toIsoString(referenceTime), "20140129T034247.032000");
44 BOOST_CHECK_EQUAL(toString(referenceTime), "2014-01-29 03:42:47");
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070045
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070046 // Unfortunately, not all systems has lv_LV locale installed :(
Davide Pesavento0f830802018-01-16 23:58:58 -050047 // BOOST_CHECK_EQUAL(toString(referenceTime, "%Y. gada %d. %B", std::locale("lv_LV.UTF-8")),
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070048 // "2014. gada 29. Janvāris");
Davide Pesavento0f830802018-01-16 23:58:58 -050049 BOOST_CHECK_EQUAL(toString(referenceTime, "%Y -- %d -- %B", std::locale("C")),
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070050 "2014 -- 29 -- January");
51
Davide Pesavento483087f2020-08-04 22:38:05 -040052 BOOST_TEST(fromIsoString("20140129T034247.032000") == referenceTime);
53 BOOST_TEST(fromIsoString("20140129T034247.032000Z") == referenceTime);
54 BOOST_TEST(fromIsoString("20140129T034247") != referenceTime);
55 BOOST_TEST(fromIsoString("20140129T034247") == fromUnixTimestamp(1390966967_s));
56 BOOST_TEST(fromString("2014-01-29 03:42:47") != referenceTime);
57 BOOST_TEST(fromString("2014-01-29 03:42:47") == fromUnixTimestamp(1390966967_s));
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070058
59 // Unfortunately, not all systems has lv_LV locale installed :(
Davide Pesavento0f830802018-01-16 23:58:58 -050060 // BOOST_CHECK_EQUAL(fromString("2014. gada 29. Janvāris", "%Y. gada %d. %B", std::locale("lv_LV.UTF-8")),
Davide Pesaventoc5ba65b2018-01-28 00:30:25 -050061 // fromUnixTimestamp(1390953600_s));
Davide Pesavento0f830802018-01-16 23:58:58 -050062 BOOST_CHECK_EQUAL(fromString("2014 -- 29 -- January", "%Y -- %d -- %B", std::locale("C")),
Davide Pesaventoc5ba65b2018-01-28 00:30:25 -050063 fromUnixTimestamp(1390953600_s));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070064}
65
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070066BOOST_AUTO_TEST_CASE(SteadyClock)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070067{
Alexander Afanasyevf8379172017-01-11 16:56:04 -080068 steady_clock::TimePoint oldValue = steady_clock::now();
Davide Pesavento0f830802018-01-16 23:58:58 -050069 std::this_thread::sleep_for(std::chrono::milliseconds(1));
Alexander Afanasyevf8379172017-01-11 16:56:04 -080070 steady_clock::TimePoint newValue = steady_clock::now();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070071 BOOST_CHECK_GT(newValue, oldValue);
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080072}
73
Davide Pesavento483087f2020-08-04 22:38:05 -040074BOOST_AUTO_TEST_CASE(SecondsClock)
75{
76 using SecTimePoint = boost::chrono::time_point<system_clock, seconds>;
77 SecTimePoint tp;
78 tp += 1596592240_s;
79
80 BOOST_TEST(toIsoString(tp) == "20200805T015040");
81 BOOST_TEST(toString(tp) == "2020-08-05 01:50:40");
82 BOOST_TEST(fromIsoString("20200805T015040") == tp);
83 BOOST_TEST(fromIsoString("20200805T015040.123456") != tp);
84 BOOST_TEST(fromString("2020-08-05 01:50:40") == tp);
85}
86
Junxiao Shi5537d132016-08-05 03:48:01 +000087BOOST_AUTO_TEST_CASE(Abs)
88{
Alexander Afanasyevf8379172017-01-11 16:56:04 -080089 BOOST_CHECK_EQUAL(abs(nanoseconds(24422)), nanoseconds(24422));
90 BOOST_CHECK_EQUAL(abs(microseconds(0)), microseconds(0));
91 BOOST_CHECK_EQUAL(abs(milliseconds(-15583)), milliseconds(15583));
92}
93
Alexander Afanasyevf8379172017-01-11 16:56:04 -080094BOOST_AUTO_TEST_CASE(LargeDates)
95{
Davide Pesaventoc5ba65b2018-01-28 00:30:25 -050096 auto value = fromUnixTimestamp(1390966967032_ms);
Alexander Afanasyevf8379172017-01-11 16:56:04 -080097 BOOST_CHECK_EQUAL(toIsoString(value), "20140129T034247.032000");
Alexander Afanasyeve66040e2018-01-25 19:15:30 -050098 BOOST_CHECK_EQUAL(fromIsoString("20140129T034247.032000"), value);
Davide Pesaventoc5ba65b2018-01-28 00:30:25 -050099 BOOST_CHECK_EQUAL(toString(value, "%Y-%m-%d %H:%M:%S%F"), "2014-01-29 03:42:47.032000");
100 BOOST_CHECK_EQUAL(fromString("2014-01-29 03:42:47.032000", "%Y-%m-%d %H:%M:%S%F"), value);
Alexander Afanasyevf8379172017-01-11 16:56:04 -0800101
Alexander Afanasyeve66040e2018-01-25 19:15:30 -0500102 value += 36524_days;
Alexander Afanasyevf8379172017-01-11 16:56:04 -0800103 BOOST_CHECK_EQUAL(toIsoString(value), "21140129T034247.032000");
Alexander Afanasyeve66040e2018-01-25 19:15:30 -0500104 BOOST_CHECK_EQUAL(fromIsoString("21140129T034247.032000"), value);
Davide Pesaventoc5ba65b2018-01-28 00:30:25 -0500105 BOOST_CHECK_EQUAL(toString(value, "%Y-%m-%d %H:%M:%S%F"), "2114-01-29 03:42:47.032000");
106 BOOST_CHECK_EQUAL(fromString("2114-01-29 03:42:47.03200", "%Y-%m-%d %H:%M:%S%F"), value);
Junxiao Shi5537d132016-08-05 03:48:01 +0000107}
108
Davide Pesavento0f830802018-01-16 23:58:58 -0500109BOOST_AUTO_TEST_CASE(Literals)
110{
111 BOOST_CHECK_EQUAL(42_s, seconds(42));
112
113 BOOST_CHECK_EQUAL(1_day, 24_h);
114 BOOST_CHECK_EQUAL(2_days, 48_h);
115 BOOST_CHECK_EQUAL(0.5_day, 12_h);
116 BOOST_CHECK_EQUAL(.5_days, 12_h);
117
118 BOOST_CHECK_EQUAL(1_h, 60_min);
119 BOOST_CHECK_EQUAL(0.5_h, 30_min);
120
121 BOOST_CHECK_EQUAL(1_min, 60_s);
122 BOOST_CHECK_EQUAL(0.5_min, 30_s);
123
124 BOOST_CHECK_EQUAL(1_s, 1000_ms);
125 BOOST_CHECK_EQUAL(0.5_s, 500_ms);
126
127 BOOST_CHECK_EQUAL(1_ms, 1000_us);
128 BOOST_CHECK_EQUAL(0.5_ms, 500_us);
129
130 BOOST_CHECK_EQUAL(1_us, 1000_ns);
131 BOOST_CHECK_EQUAL(0.5_us, 500_ns);
132
133 BOOST_CHECK_EQUAL(1_ns, nanoseconds(1));
134 BOOST_CHECK_EQUAL(5.5_ns, 0.0055_us);
135}
136
Alexander Afanasyeve66040e2018-01-25 19:15:30 -0500137BOOST_AUTO_TEST_CASE(Year2038)
138{
139 auto year2042 = fromIsoString("20420101T000001.042000");
140 auto year2010 = fromIsoString("20100101T000001.042000");
141
Davide Pesavento923ba442019-02-12 22:00:38 -0500142 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(year2010),
143 "1262304001042000000 nanoseconds since Jan 1, 1970");
144 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(year2042),
145 "2272147201042000000 nanoseconds since Jan 1, 1970");
Alexander Afanasyeve66040e2018-01-25 19:15:30 -0500146 BOOST_CHECK_GT(year2042, year2010);
147}
148
Junxiao Shi5537d132016-08-05 03:48:01 +0000149BOOST_AUTO_TEST_SUITE_END() // TestTime
150BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800151
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800152} // namespace tests
Alexander Afanasyevf8379172017-01-11 16:56:04 -0800153} // namespace time
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800154} // namespace ndn