blob: d3a659cd250f0500981ed8d6398aba6514584940 [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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 {
27
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070028BOOST_AUTO_TEST_SUITE(UtilTestTime)
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080029
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070030BOOST_AUTO_TEST_CASE(SystemClock)
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080031{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070032 time::system_clock::TimePoint value = time::system_clock::now();
33 time::system_clock::TimePoint referenceTime =
Alexander Afanasyev0bb9aae2014-04-21 18:08:18 -070034 time::fromUnixTimestamp(time::milliseconds(1390966967032LL));
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080035
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070036 BOOST_CHECK_GT(value, referenceTime);
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080037
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070038 BOOST_CHECK_EQUAL(time::toIsoString(referenceTime), "20140129T034247.032000");
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070039 BOOST_CHECK_EQUAL(time::toString(referenceTime), "2014-01-29 03:42:47");
40 BOOST_CHECK_EQUAL(time::toString(referenceTime), "2014-01-29 03:42:47");
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070041
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070042 // Unfortunately, not all systems has lv_LV locale installed :(
43 // BOOST_CHECK_EQUAL(time::toString(referenceTime, "%Y. gada %d. %B",
44 // std::locale("lv_LV.UTF-8")),
45 // "2014. gada 29. Janvāris");
46
47 BOOST_CHECK_EQUAL(time::toString(referenceTime, "%Y -- %d -- %B",
48 std::locale("C")),
49 "2014 -- 29 -- January");
50
51 BOOST_CHECK_EQUAL(time::fromIsoString("20140129T034247.032000"), referenceTime);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070052 BOOST_CHECK_EQUAL(time::fromIsoString("20140129T034247.032000Z"), referenceTime);
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070053 BOOST_CHECK_EQUAL(time::fromString("2014-01-29 03:42:47"),
54 time::fromUnixTimestamp(time::seconds(1390966967)));
55
56 // Unfortunately, not all systems has lv_LV locale installed :(
57 // BOOST_CHECK_EQUAL(time::fromString("2014. gada 29. Janvāris", "%Y. gada %d. %B",
58 // std::locale("lv_LV.UTF-8")),
59 // time::fromUnixTimestamp(time::seconds(1390953600)));
60
61 BOOST_CHECK_EQUAL(time::fromString("2014 -- 29 -- January", "%Y -- %d -- %B",
62 std::locale("C")),
63 time::fromUnixTimestamp(time::seconds(1390953600)));
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{
68 time::steady_clock::TimePoint oldValue = time::steady_clock::now();
69 usleep(100);
70 time::steady_clock::TimePoint newValue = time::steady_clock::now();
71
72 BOOST_CHECK_GT(newValue, oldValue);
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080073}
74
75BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080076
77} // namespace ndn