blob: d09f3ab243de81afe2a7108dfa3dcdc0e884fbdd [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -08002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080011 */
12
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080013#include "util/time.hpp"
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080014
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070015#include "boost-test.hpp"
16
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080017namespace ndn {
18
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070019BOOST_AUTO_TEST_SUITE(UtilTestTime)
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080020
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070021BOOST_AUTO_TEST_CASE(SystemClock)
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080022{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070023 time::system_clock::TimePoint value = time::system_clock::now();
24 time::system_clock::TimePoint referenceTime =
Alexander Afanasyev0bb9aae2014-04-21 18:08:18 -070025 time::fromUnixTimestamp(time::milliseconds(1390966967032LL));
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080026
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070027 BOOST_CHECK_GT(value, referenceTime);
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080028
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070029 BOOST_CHECK_EQUAL(time::toIsoString(referenceTime), "20140129T034247.032000");
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070030 BOOST_CHECK_EQUAL(time::toString(referenceTime), "2014-01-29 03:42:47");
31 BOOST_CHECK_EQUAL(time::toString(referenceTime), "2014-01-29 03:42:47");
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070032
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070033 // Unfortunately, not all systems has lv_LV locale installed :(
34 // BOOST_CHECK_EQUAL(time::toString(referenceTime, "%Y. gada %d. %B",
35 // std::locale("lv_LV.UTF-8")),
36 // "2014. gada 29. Janvāris");
37
38 BOOST_CHECK_EQUAL(time::toString(referenceTime, "%Y -- %d -- %B",
39 std::locale("C")),
40 "2014 -- 29 -- January");
41
42 BOOST_CHECK_EQUAL(time::fromIsoString("20140129T034247.032000"), referenceTime);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070043 BOOST_CHECK_EQUAL(time::fromIsoString("20140129T034247.032000Z"), referenceTime);
Alexander Afanasyev53af7a12014-04-10 23:35:28 -070044 BOOST_CHECK_EQUAL(time::fromString("2014-01-29 03:42:47"),
45 time::fromUnixTimestamp(time::seconds(1390966967)));
46
47 // Unfortunately, not all systems has lv_LV locale installed :(
48 // BOOST_CHECK_EQUAL(time::fromString("2014. gada 29. Janvāris", "%Y. gada %d. %B",
49 // std::locale("lv_LV.UTF-8")),
50 // time::fromUnixTimestamp(time::seconds(1390953600)));
51
52 BOOST_CHECK_EQUAL(time::fromString("2014 -- 29 -- January", "%Y -- %d -- %B",
53 std::locale("C")),
54 time::fromUnixTimestamp(time::seconds(1390953600)));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070055}
56
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070057BOOST_AUTO_TEST_CASE(SteadyClock)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070058{
59 time::steady_clock::TimePoint oldValue = time::steady_clock::now();
60 usleep(100);
61 time::steady_clock::TimePoint newValue = time::steady_clock::now();
62
63 BOOST_CHECK_GT(newValue, oldValue);
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080064}
65
66BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080067
68} // namespace ndn