blob: 84236d213b47111a99186e1ceb7c567c1a29d43a [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 Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
30
31using namespace ndn::time;
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
Davide Pesavento04566432021-06-07 23:37:05 -040036BOOST_AUTO_TEST_CASE(Duration)
37{
38 BOOST_TEST(years(1) == months(12));
39 BOOST_TEST(months(1) == minutes(43829) + seconds(6));
40 BOOST_TEST(weeks(1) == days(7));
41 BOOST_TEST(days(1) == hours(24));
42 BOOST_TEST(hours(1) == minutes(60));
43 BOOST_TEST(minutes(1) == seconds(60));
44 BOOST_TEST(seconds(1) == milliseconds(1000));
45 BOOST_TEST(milliseconds(1) == microseconds(1000));
46 BOOST_TEST(microseconds(1) == nanoseconds(1000));
47}
48
49BOOST_AUTO_TEST_CASE(Abs)
50{
51 BOOST_TEST(abs(nanoseconds(24422)) == nanoseconds(24422));
52 BOOST_TEST(abs(microseconds(0)) == microseconds(0));
53 BOOST_TEST(abs(milliseconds(-15583)) == milliseconds(15583));
54}
55
56BOOST_AUTO_TEST_CASE(Literals)
57{
58 BOOST_TEST(42_s == seconds(42));
59
60 BOOST_TEST(1_day == 24_h);
61 BOOST_TEST(2_days == 48_h);
62 BOOST_TEST(0.5_day == 12_h);
63 BOOST_TEST(.5_days == 12_h);
64
65 BOOST_TEST(1_h == 60_min);
66 BOOST_TEST(0.5_h == 30_min);
67
68 BOOST_TEST(1_min == 60_s);
69 BOOST_TEST(0.5_min == 30_s);
70
71 BOOST_TEST(1_s == 1000_ms);
72 BOOST_TEST(0.5_s == 500_ms);
73
74 BOOST_TEST(1_ms == 1000_us);
75 BOOST_TEST(0.5_ms == 500_us);
76
77 BOOST_TEST(1_us == 1000_ns);
78 BOOST_TEST(0.5_us == 500_ns);
79
80 BOOST_TEST(1_ns == nanoseconds(1));
81 BOOST_TEST(5.5_ns == 0.0055_us);
82}
83
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070084BOOST_AUTO_TEST_CASE(SystemClock)
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080085{
Davide Pesavento04566432021-06-07 23:37:05 -040086 system_clock::time_point value = system_clock::now();
87 system_clock::time_point referenceTime = fromUnixTimestamp(1390966967032_ms);
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080088
Davide Pesavento483087f2020-08-04 22:38:05 -040089 BOOST_TEST(value > referenceTime);
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080090
Davide Pesavento67bc1142023-05-15 21:48:43 -040091 BOOST_TEST(toUnixTimestamp(referenceTime) == 1390966967032_ms);
92 BOOST_TEST(toUnixTimestamp<nanoseconds>(referenceTime) == 1390966967032000000_ns);
93 BOOST_TEST(toUnixTimestamp<seconds>(referenceTime) == 1390966967_s);
94
95 BOOST_TEST(toIsoString(referenceTime) == "20140129T034247.032000");
96 BOOST_TEST(toIsoExtendedString(referenceTime) == "2014-01-29T03:42:47.032000");
97 BOOST_TEST(toString(referenceTime) == "2014-01-29 03:42:47");
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070098
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070099 // Unfortunately, not all systems has lv_LV locale installed :(
Davide Pesavento67bc1142023-05-15 21:48:43 -0400100 // BOOST_TEST(toString(referenceTime, "%Y. gada %d. %B", std::locale("lv_LV.UTF-8")) ==
101 // "2014. gada 29. Janvāris");
102 BOOST_TEST(toString(referenceTime, "%Y -- %d -- %B", std::locale("C")) == "2014 -- 29 -- January");
Alexander Afanasyev53af7a12014-04-10 23:35:28 -0700103
Davide Pesavento483087f2020-08-04 22:38:05 -0400104 BOOST_TEST(fromIsoString("20140129T034247.032000") == referenceTime);
105 BOOST_TEST(fromIsoString("20140129T034247.032000Z") == referenceTime);
106 BOOST_TEST(fromIsoString("20140129T034247") != referenceTime);
107 BOOST_TEST(fromIsoString("20140129T034247") == fromUnixTimestamp(1390966967_s));
Davide Pesavento04566432021-06-07 23:37:05 -0400108 BOOST_TEST(fromIsoExtendedString("2014-01-29T03:42:47.032000") == referenceTime);
109 BOOST_TEST(fromIsoExtendedString("2014-01-29T03:42:47.032000Z") == referenceTime);
110 BOOST_TEST(fromIsoExtendedString("2014-01-29T03:42:47") != referenceTime);
111 BOOST_TEST(fromIsoExtendedString("2014-01-29T03:42:47") == fromUnixTimestamp(1390966967_s));
Davide Pesavento483087f2020-08-04 22:38:05 -0400112 BOOST_TEST(fromString("2014-01-29 03:42:47") != referenceTime);
113 BOOST_TEST(fromString("2014-01-29 03:42:47") == fromUnixTimestamp(1390966967_s));
Alexander Afanasyev53af7a12014-04-10 23:35:28 -0700114
115 // Unfortunately, not all systems has lv_LV locale installed :(
Davide Pesavento67bc1142023-05-15 21:48:43 -0400116 // BOOST_TEST(fromString("2014. gada 29. Janvāris", "%Y. gada %d. %B", std::locale("lv_LV.UTF-8")) ==
117 // fromUnixTimestamp(1390953600_s));
118 BOOST_TEST(fromString("2014 -- 29 -- January", "%Y -- %d -- %B", std::locale("C")) ==
119 fromUnixTimestamp(1390953600_s));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700120}
121
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -0700122BOOST_AUTO_TEST_CASE(SteadyClock)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700123{
Davide Pesavento04566432021-06-07 23:37:05 -0400124 steady_clock::time_point oldValue = steady_clock::now();
Davide Pesavento0f830802018-01-16 23:58:58 -0500125 std::this_thread::sleep_for(std::chrono::milliseconds(1));
Davide Pesavento04566432021-06-07 23:37:05 -0400126 steady_clock::time_point newValue = steady_clock::now();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700127 BOOST_CHECK_GT(newValue, oldValue);
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -0800128}
129
Davide Pesavento483087f2020-08-04 22:38:05 -0400130BOOST_AUTO_TEST_CASE(SecondsClock)
131{
132 using SecTimePoint = boost::chrono::time_point<system_clock, seconds>;
133 SecTimePoint tp;
134 tp += 1596592240_s;
135
136 BOOST_TEST(toIsoString(tp) == "20200805T015040");
Davide Pesavento04566432021-06-07 23:37:05 -0400137 BOOST_TEST(toIsoExtendedString(tp) == "2020-08-05T01:50:40");
Davide Pesavento483087f2020-08-04 22:38:05 -0400138 BOOST_TEST(toString(tp) == "2020-08-05 01:50:40");
139 BOOST_TEST(fromIsoString("20200805T015040") == tp);
140 BOOST_TEST(fromIsoString("20200805T015040.123456") != tp);
Davide Pesavento04566432021-06-07 23:37:05 -0400141 BOOST_TEST(fromIsoExtendedString("2020-08-05T01:50:40") == tp);
142 BOOST_TEST(fromIsoExtendedString("2020-08-05T01:50:40.123456") != tp);
Davide Pesavento483087f2020-08-04 22:38:05 -0400143 BOOST_TEST(fromString("2020-08-05 01:50:40") == tp);
144}
145
Alexander Afanasyevf8379172017-01-11 16:56:04 -0800146BOOST_AUTO_TEST_CASE(LargeDates)
147{
Davide Pesaventoc5ba65b2018-01-28 00:30:25 -0500148 auto value = fromUnixTimestamp(1390966967032_ms);
Alexander Afanasyevf8379172017-01-11 16:56:04 -0800149 BOOST_CHECK_EQUAL(toIsoString(value), "20140129T034247.032000");
Alexander Afanasyeve66040e2018-01-25 19:15:30 -0500150 BOOST_CHECK_EQUAL(fromIsoString("20140129T034247.032000"), value);
Davide Pesaventoc5ba65b2018-01-28 00:30:25 -0500151 BOOST_CHECK_EQUAL(toString(value, "%Y-%m-%d %H:%M:%S%F"), "2014-01-29 03:42:47.032000");
152 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 -0800153
Alexander Afanasyeve66040e2018-01-25 19:15:30 -0500154 value += 36524_days;
Alexander Afanasyevf8379172017-01-11 16:56:04 -0800155 BOOST_CHECK_EQUAL(toIsoString(value), "21140129T034247.032000");
Alexander Afanasyeve66040e2018-01-25 19:15:30 -0500156 BOOST_CHECK_EQUAL(fromIsoString("21140129T034247.032000"), value);
Davide Pesaventoc5ba65b2018-01-28 00:30:25 -0500157 BOOST_CHECK_EQUAL(toString(value, "%Y-%m-%d %H:%M:%S%F"), "2114-01-29 03:42:47.032000");
158 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 +0000159}
160
Alexander Afanasyeve66040e2018-01-25 19:15:30 -0500161BOOST_AUTO_TEST_CASE(Year2038)
162{
163 auto year2042 = fromIsoString("20420101T000001.042000");
164 auto year2010 = fromIsoString("20100101T000001.042000");
165
Davide Pesavento923ba442019-02-12 22:00:38 -0500166 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(year2010),
167 "1262304001042000000 nanoseconds since Jan 1, 1970");
168 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(year2042),
169 "2272147201042000000 nanoseconds since Jan 1, 1970");
Alexander Afanasyeve66040e2018-01-25 19:15:30 -0500170 BOOST_CHECK_GT(year2042, year2010);
171}
172
Junxiao Shi5537d132016-08-05 03:48:01 +0000173BOOST_AUTO_TEST_SUITE_END() // TestTime
174BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800175
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400176} // namespace ndn::tests