blob: 92cedceec36716e6b648fc38498e12a681f78e1d [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyeve2e3ca52014-01-03 13:59: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 Afanasyeve2e3ca52014-01-03 13:59:07 -080020 */
21
22#ifndef NDN_TIME_HPP
23#define NDN_TIME_HPP
24
Alexander Afanasyev19508852014-01-29 01:01:51 -080025#include "../common.hpp"
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070026#include <boost/chrono.hpp>
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080027
28namespace ndn {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080029namespace time {
30
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070031using boost::chrono::duration;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080032
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070033typedef duration<boost::int_least32_t, boost::ratio<86400> > days;
34using boost::chrono::hours;
35using boost::chrono::minutes;
36using boost::chrono::seconds;
37
38using boost::chrono::milliseconds;
39using boost::chrono::microseconds;
40using boost::chrono::nanoseconds;
41
42using boost::chrono::duration_cast;
43
44/**
45 * \brief System clock
46 *
47 * System clock represents the system-wide real time wall clock.
48 *
49 * It may not be monotonic: on most systems, the system time can be
50 * adjusted at any moment. It is the only clock that has the ability
51 * to be displayed and converted to/from UNIX timestamp.
52 *
53 * To get current TimePoint:
54 *
55 * <code>
56 * system_clock::TimePoint now = system_clock::now();
57 * </code>
58 *
59 * To convert TimePoint to/from UNIX timestamp:
60 *
61 * <code>
62 * system_clock::TimePoint time = ...;
63 * uint64_t timestampInMilliseconds = toUnixTimestamp(time).count();
64 * system_clock::TimePoint time2 = fromUnixTimestamp(time::milliseconds(timestampInMilliseconds));
65 * </code>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080066 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070067class system_clock : public boost::chrono::system_clock
Alexander Afanasyevf6468892014-01-29 01:04:14 -080068{
69public:
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070070 typedef time_point TimePoint;
71 typedef duration Duration;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080072
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070073 // /// \brief Get current TimePoint
74 // TimePoint
75 // now();
76}; // class system_clock
Alexander Afanasyevf6468892014-01-29 01:04:14 -080077
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070078/**
79 * \brief Steady clock
80 *
81 * Steady clock represents a monotonic clock. The time points of this
82 * clock cannot decrease as physical time moves forward. This clock is
83 * not related to wall clock time, and is best suitable for measuring
84 * intervals.
85 *
86 * Note that on OS X platform this defaults to system clock and is not
87 * truly monotonic. Refer to https://svn.boost.org/trac/boost/ticket/7719)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080088 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070089class steady_clock : public
90#ifdef __APPLE__
91// steady_clock may go backwards on OS X platforms, so use system_clock
92// instead
93 boost::chrono::system_clock
94#else
95 boost::chrono::steady_clock
96#endif
Alexander Afanasyevf6468892014-01-29 01:04:14 -080097{
98public:
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070099 typedef time_point TimePoint;
100 typedef duration Duration;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800101
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700102 // /// \brief Get current TimePoint
103 // TimePoint
104 // now();
105}; // class steady_clock
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800106
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800107
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700108/**
109 * \brief Get system_clock::TimePoint representing UNIX time epoch (00:00:00 on Jan 1, 1970)
110 */
111inline const system_clock::TimePoint&
112getUnixEpoch()
Yingdi Yuf2a82092014-02-03 16:49:15 -0800113{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700114 static system_clock::TimePoint epoch = system_clock::from_time_t(0);
115 return epoch;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800116}
117
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800118/**
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700119 * \brief Convert system_clock::TimePoint to UNIX timestamp
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800120 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700121inline milliseconds
122toUnixTimestamp(const system_clock::TimePoint& point)
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800123{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700124 return duration_cast<milliseconds>(point - getUnixEpoch());
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800125}
126
127/**
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700128 * \brief Convert UNIX timestamp to system_clock::TimePoint
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800129 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700130inline system_clock::TimePoint
131fromUnixTimestamp(const milliseconds& duration)
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800132{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700133 return getUnixEpoch() + duration;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800134}
135
136/**
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700137 * \brief Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
138 *
139 * If timePoint contains doesn't contain fractional seconds the
140 * output format is YYYYMMDDTHHMMSS
141 *
142 * Examples:
143 *
144 * - with fractional nanoseconds: 20020131T100001,123456789
145 * - with fractional microseconds: 20020131T100001,123456
146 * - with fractional milliseconds: 20020131T100001,123
147 * - without fractional seconds: 20020131T100001
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800148 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700149std::string
150toIsoString(const system_clock::TimePoint& timePoint);
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800151
152/**
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700153 * \brief Convert from the ISO string (YYYYMMDDTHHMMSS,fffffffff) representation
154 * to the internal time format
155 *
156 * Examples of accepted ISO strings:
157 *
158 * - with fractional nanoseconds: 20020131T100001,123456789
159 * - with fractional microseconds: 20020131T100001,123456
160 * - with fractional milliseconds: 20020131T100001,123
161 * - without fractional seconds: 20020131T100001
162 *
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800163 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700164system_clock::TimePoint
165fromIsoString(const std::string& isoString);
Alexander Afanasyev53af7a12014-04-10 23:35:28 -0700166
167/**
168 * \brief Convert time point to string with specified format
169 *
170 * By default, `%Y-%m-%d %H:%M:%S` is used, producing dates like
171 * `2014-04-10 22:51:00`
172 *
173 * \param timePoint time point of system_clock
174 * \param format desired output format (default: `%Y-%m-%d %H:%M:%S`)
175 * \param locale desired locale (default: "C" locale)
176 *
177 * \sa http://www.boost.org/doc/libs/1_48_0/doc/html/date_time/date_time_io.html#date_time.format_flags
178 * described possible formatting flags
179 **/
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700180std::string
Alexander Afanasyev53af7a12014-04-10 23:35:28 -0700181toString(const system_clock::TimePoint& timePoint,
182 const std::string& format = "%Y-%m-%d %H:%M:%S",
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700183 const std::locale& locale = std::locale("C"));
Alexander Afanasyev53af7a12014-04-10 23:35:28 -0700184
185/**
186 * \brief Convert from string of specified format into time point
187 *
188 * By default, `%Y-%m-%d %H:%M:%S` is used, accepting dates like
189 * `2014-04-10 22:51:00`
190 *
191 * \param formattedTimePoint string representing time point
192 * \param format input output format (default: `%Y-%m-%d %H:%M:%S`)
193 * \param locale input locale (default: "C" locale)
194 *
195 * \sa http://www.boost.org/doc/libs/1_48_0/doc/html/date_time/date_time_io.html#date_time.format_flags
196 * described possible formatting flags
197 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700198system_clock::TimePoint
Alexander Afanasyev53af7a12014-04-10 23:35:28 -0700199fromString(const std::string& formattedTimePoint,
200 const std::string& format = "%Y-%m-%d %H:%M:%S",
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700201 const std::locale& locale = std::locale("C"));
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800202
203} // namespace time
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -0800204} // namespace ndn
205
206#endif // NDN_TIME_HPP