blob: 3d7160dcd00801e56bc1c69a87489f1ee4cf5a47 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -08002/**
Alexander Afanasyevf8379172017-01-11 16:56:04 -08003 * Copyright (c) 2013-2017 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
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080022#include "util/time.hpp"
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080023
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070024#include "boost-test.hpp"
25
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080026namespace ndn {
Alexander Afanasyevf8379172017-01-11 16:56:04 -080027namespace time {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080028namespace tests {
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080029
Junxiao Shi5537d132016-08-05 03:48:01 +000030BOOST_AUTO_TEST_SUITE(Util)
31BOOST_AUTO_TEST_SUITE(TestTime)
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080032
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070033BOOST_AUTO_TEST_CASE(SystemClock)
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080034{
Alexander Afanasyevf8379172017-01-11 16:56:04 -080035 system_clock::TimePoint value = system_clock::now();
36 system_clock::TimePoint referenceTime = fromUnixTimestamp(milliseconds(1390966967032LL));
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080037
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070038 BOOST_CHECK_GT(value, referenceTime);
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080039
Alexander Afanasyevf8379172017-01-11 16:56:04 -080040 BOOST_CHECK_EQUAL(toIsoString(referenceTime), "20140129T034247.032000");
41 BOOST_CHECK_EQUAL(toString(referenceTime), "2014-01-29 03:42:47");
42 BOOST_CHECK_EQUAL(toString(referenceTime), "2014-01-29 03:42:47");
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070043
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070044 // Unfortunately, not all systems has lv_LV locale installed :(
Alexander Afanasyevf8379172017-01-11 16:56:04 -080045 // BOOST_CHECK_EQUAL(toString(referenceTime, "%Y. gada %d. %B",
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070046 // std::locale("lv_LV.UTF-8")),
47 // "2014. gada 29. Janvāris");
48
Alexander Afanasyevf8379172017-01-11 16:56:04 -080049 BOOST_CHECK_EQUAL(toString(referenceTime, "%Y -- %d -- %B",
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070050 std::locale("C")),
51 "2014 -- 29 -- January");
52
Alexander Afanasyevf8379172017-01-11 16:56:04 -080053 BOOST_CHECK_EQUAL(fromIsoString("20140129T034247.032000"), referenceTime);
54 BOOST_CHECK_EQUAL(fromIsoString("20140129T034247.032000Z"), referenceTime);
55 BOOST_CHECK_EQUAL(fromString("2014-01-29 03:42:47"),
56 fromUnixTimestamp(seconds(1390966967)));
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070057
58 // Unfortunately, not all systems has lv_LV locale installed :(
Alexander Afanasyevf8379172017-01-11 16:56:04 -080059 // BOOST_CHECK_EQUAL(fromString("2014. gada 29. Janvāris", "%Y. gada %d. %B",
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070060 // std::locale("lv_LV.UTF-8")),
Alexander Afanasyevf8379172017-01-11 16:56:04 -080061 // fromUnixTimestamp(seconds(1390953600)));
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070062
Alexander Afanasyevf8379172017-01-11 16:56:04 -080063 BOOST_CHECK_EQUAL(fromString("2014 -- 29 -- January", "%Y -- %d -- %B",
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070064 std::locale("C")),
Alexander Afanasyevf8379172017-01-11 16:56:04 -080065 fromUnixTimestamp(seconds(1390953600)));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070066}
67
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070068BOOST_AUTO_TEST_CASE(SteadyClock)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069{
Alexander Afanasyevf8379172017-01-11 16:56:04 -080070 steady_clock::TimePoint oldValue = steady_clock::now();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070071 usleep(100);
Alexander Afanasyevf8379172017-01-11 16:56:04 -080072 steady_clock::TimePoint newValue = steady_clock::now();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070073
74 BOOST_CHECK_GT(newValue, oldValue);
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080075}
76
Junxiao Shi5537d132016-08-05 03:48:01 +000077BOOST_AUTO_TEST_CASE(Abs)
78{
Alexander Afanasyevf8379172017-01-11 16:56:04 -080079 BOOST_CHECK_EQUAL(abs(nanoseconds(24422)), nanoseconds(24422));
80 BOOST_CHECK_EQUAL(abs(microseconds(0)), microseconds(0));
81 BOOST_CHECK_EQUAL(abs(milliseconds(-15583)), milliseconds(15583));
82}
83
Alexander Afanasyevf8379172017-01-11 16:56:04 -080084BOOST_AUTO_TEST_CASE(LargeDates)
85{
86 system_clock::TimePoint value = fromUnixTimestamp(milliseconds(1390966967032LL));
87 BOOST_CHECK_EQUAL(toIsoString(value), "20140129T034247.032000");
88
89 value += days(365 * 100 + 25 - 1); // 36524 days
90 BOOST_CHECK_EQUAL(toIsoString(value), "21140129T034247.032000");
Junxiao Shi5537d132016-08-05 03:48:01 +000091}
92
93BOOST_AUTO_TEST_SUITE_END() // TestTime
94BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080095
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080096} // namespace tests
Alexander Afanasyevf8379172017-01-11 16:56:04 -080097} // namespace time
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080098} // namespace ndn