Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 22 | #include "util/time.hpp" |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 23 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 24 | #include "boost-test.hpp" |
| 25 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 26 | #include <thread> |
| 27 | |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 28 | namespace ndn { |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 29 | namespace time { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 30 | namespace tests { |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 31 | |
Junxiao Shi | 5537d13 | 2016-08-05 03:48:01 +0000 | [diff] [blame] | 32 | BOOST_AUTO_TEST_SUITE(Util) |
| 33 | BOOST_AUTO_TEST_SUITE(TestTime) |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 35 | BOOST_AUTO_TEST_CASE(SystemClock) |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 36 | { |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 37 | system_clock::TimePoint value = system_clock::now(); |
| 38 | system_clock::TimePoint referenceTime = fromUnixTimestamp(milliseconds(1390966967032LL)); |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 39 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 40 | BOOST_CHECK_GT(value, referenceTime); |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 42 | BOOST_CHECK_EQUAL(toIsoString(referenceTime), "20140129T034247.032000"); |
| 43 | BOOST_CHECK_EQUAL(toString(referenceTime), "2014-01-29 03:42:47"); |
| 44 | BOOST_CHECK_EQUAL(toString(referenceTime), "2014-01-29 03:42:47"); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 45 | |
Alexander Afanasyev | 53af7a1 | 2014-04-10 23:35:28 -0700 | [diff] [blame] | 46 | // Unfortunately, not all systems has lv_LV locale installed :( |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 47 | // BOOST_CHECK_EQUAL(toString(referenceTime, "%Y. gada %d. %B", std::locale("lv_LV.UTF-8")), |
Alexander Afanasyev | 53af7a1 | 2014-04-10 23:35:28 -0700 | [diff] [blame] | 48 | // "2014. gada 29. Janvāris"); |
| 49 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(toString(referenceTime, "%Y -- %d -- %B", std::locale("C")), |
Alexander Afanasyev | 53af7a1 | 2014-04-10 23:35:28 -0700 | [diff] [blame] | 51 | "2014 -- 29 -- January"); |
| 52 | |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 53 | BOOST_CHECK_EQUAL(fromIsoString("20140129T034247.032000"), referenceTime); |
| 54 | BOOST_CHECK_EQUAL(fromIsoString("20140129T034247.032000Z"), referenceTime); |
Davide Pesavento | c5ba65b | 2018-01-28 00:30:25 -0500 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL(fromString("2014-01-29 03:42:47"), fromUnixTimestamp(1390966967_s)); |
Alexander Afanasyev | 53af7a1 | 2014-04-10 23:35:28 -0700 | [diff] [blame] | 56 | |
| 57 | // Unfortunately, not all systems has lv_LV locale installed :( |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 58 | // BOOST_CHECK_EQUAL(fromString("2014. gada 29. Janvāris", "%Y. gada %d. %B", std::locale("lv_LV.UTF-8")), |
Davide Pesavento | c5ba65b | 2018-01-28 00:30:25 -0500 | [diff] [blame] | 59 | // fromUnixTimestamp(1390953600_s)); |
Alexander Afanasyev | 53af7a1 | 2014-04-10 23:35:28 -0700 | [diff] [blame] | 60 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 61 | BOOST_CHECK_EQUAL(fromString("2014 -- 29 -- January", "%Y -- %d -- %B", std::locale("C")), |
Davide Pesavento | c5ba65b | 2018-01-28 00:30:25 -0500 | [diff] [blame] | 62 | fromUnixTimestamp(1390953600_s)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 65 | BOOST_AUTO_TEST_CASE(SteadyClock) |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 66 | { |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 67 | steady_clock::TimePoint oldValue = steady_clock::now(); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 68 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 69 | steady_clock::TimePoint newValue = steady_clock::now(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 70 | BOOST_CHECK_GT(newValue, oldValue); |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Junxiao Shi | 5537d13 | 2016-08-05 03:48:01 +0000 | [diff] [blame] | 73 | BOOST_AUTO_TEST_CASE(Abs) |
| 74 | { |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL(abs(nanoseconds(24422)), nanoseconds(24422)); |
| 76 | BOOST_CHECK_EQUAL(abs(microseconds(0)), microseconds(0)); |
| 77 | BOOST_CHECK_EQUAL(abs(milliseconds(-15583)), milliseconds(15583)); |
| 78 | } |
| 79 | |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 80 | BOOST_AUTO_TEST_CASE(LargeDates) |
| 81 | { |
Davide Pesavento | c5ba65b | 2018-01-28 00:30:25 -0500 | [diff] [blame] | 82 | auto value = fromUnixTimestamp(1390966967032_ms); |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 83 | BOOST_CHECK_EQUAL(toIsoString(value), "20140129T034247.032000"); |
Alexander Afanasyev | e66040e | 2018-01-25 19:15:30 -0500 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(fromIsoString("20140129T034247.032000"), value); |
Davide Pesavento | c5ba65b | 2018-01-28 00:30:25 -0500 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(toString(value, "%Y-%m-%d %H:%M:%S%F"), "2014-01-29 03:42:47.032000"); |
| 86 | BOOST_CHECK_EQUAL(fromString("2014-01-29 03:42:47.032000", "%Y-%m-%d %H:%M:%S%F"), value); |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | e66040e | 2018-01-25 19:15:30 -0500 | [diff] [blame] | 88 | value += 36524_days; |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 89 | BOOST_CHECK_EQUAL(toIsoString(value), "21140129T034247.032000"); |
Alexander Afanasyev | e66040e | 2018-01-25 19:15:30 -0500 | [diff] [blame] | 90 | BOOST_CHECK_EQUAL(fromIsoString("21140129T034247.032000"), value); |
Davide Pesavento | c5ba65b | 2018-01-28 00:30:25 -0500 | [diff] [blame] | 91 | BOOST_CHECK_EQUAL(toString(value, "%Y-%m-%d %H:%M:%S%F"), "2114-01-29 03:42:47.032000"); |
| 92 | BOOST_CHECK_EQUAL(fromString("2114-01-29 03:42:47.03200", "%Y-%m-%d %H:%M:%S%F"), value); |
Junxiao Shi | 5537d13 | 2016-08-05 03:48:01 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 95 | BOOST_AUTO_TEST_CASE(Literals) |
| 96 | { |
| 97 | BOOST_CHECK_EQUAL(42_s, seconds(42)); |
| 98 | |
| 99 | BOOST_CHECK_EQUAL(1_day, 24_h); |
| 100 | BOOST_CHECK_EQUAL(2_days, 48_h); |
| 101 | BOOST_CHECK_EQUAL(0.5_day, 12_h); |
| 102 | BOOST_CHECK_EQUAL(.5_days, 12_h); |
| 103 | |
| 104 | BOOST_CHECK_EQUAL(1_h, 60_min); |
| 105 | BOOST_CHECK_EQUAL(0.5_h, 30_min); |
| 106 | |
| 107 | BOOST_CHECK_EQUAL(1_min, 60_s); |
| 108 | BOOST_CHECK_EQUAL(0.5_min, 30_s); |
| 109 | |
| 110 | BOOST_CHECK_EQUAL(1_s, 1000_ms); |
| 111 | BOOST_CHECK_EQUAL(0.5_s, 500_ms); |
| 112 | |
| 113 | BOOST_CHECK_EQUAL(1_ms, 1000_us); |
| 114 | BOOST_CHECK_EQUAL(0.5_ms, 500_us); |
| 115 | |
| 116 | BOOST_CHECK_EQUAL(1_us, 1000_ns); |
| 117 | BOOST_CHECK_EQUAL(0.5_us, 500_ns); |
| 118 | |
| 119 | BOOST_CHECK_EQUAL(1_ns, nanoseconds(1)); |
| 120 | BOOST_CHECK_EQUAL(5.5_ns, 0.0055_us); |
| 121 | } |
| 122 | |
Alexander Afanasyev | e66040e | 2018-01-25 19:15:30 -0500 | [diff] [blame] | 123 | BOOST_AUTO_TEST_CASE(Year2038) |
| 124 | { |
| 125 | auto year2042 = fromIsoString("20420101T000001.042000"); |
| 126 | auto year2010 = fromIsoString("20100101T000001.042000"); |
| 127 | |
| 128 | BOOST_CHECK_EQUAL(to_string(year2010), "1262304001042000000 nanoseconds since Jan 1, 1970"); |
| 129 | BOOST_CHECK_EQUAL(to_string(year2042), "2272147201042000000 nanoseconds since Jan 1, 1970"); |
| 130 | BOOST_CHECK_GT(year2042, year2010); |
| 131 | } |
| 132 | |
Junxiao Shi | 5537d13 | 2016-08-05 03:48:01 +0000 | [diff] [blame] | 133 | BOOST_AUTO_TEST_SUITE_END() // TestTime |
| 134 | BOOST_AUTO_TEST_SUITE_END() // Util |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 135 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 136 | } // namespace tests |
Alexander Afanasyev | f837917 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 137 | } // namespace time |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 138 | } // namespace ndn |