blob: 98d27e8c8b6a6934de5a735be660c6d9f3c71c37 [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyeve2e3ca52014-01-03 13:59: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 Afanasyeve2e3ca52014-01-03 13:59:07 -080011 */
12
13#ifndef NDN_TIME_HPP
14#define NDN_TIME_HPP
15
Alexander Afanasyev19508852014-01-29 01:01:51 -080016#include "../common.hpp"
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070017#include <boost/chrono.hpp>
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080018
19namespace ndn {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080020namespace time {
21
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070022using boost::chrono::duration;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080023
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070024typedef duration<boost::int_least32_t, boost::ratio<86400> > days;
25using boost::chrono::hours;
26using boost::chrono::minutes;
27using boost::chrono::seconds;
28
29using boost::chrono::milliseconds;
30using boost::chrono::microseconds;
31using boost::chrono::nanoseconds;
32
33using boost::chrono::duration_cast;
34
35/**
36 * \brief System clock
37 *
38 * System clock represents the system-wide real time wall clock.
39 *
40 * It may not be monotonic: on most systems, the system time can be
41 * adjusted at any moment. It is the only clock that has the ability
42 * to be displayed and converted to/from UNIX timestamp.
43 *
44 * To get current TimePoint:
45 *
46 * <code>
47 * system_clock::TimePoint now = system_clock::now();
48 * </code>
49 *
50 * To convert TimePoint to/from UNIX timestamp:
51 *
52 * <code>
53 * system_clock::TimePoint time = ...;
54 * uint64_t timestampInMilliseconds = toUnixTimestamp(time).count();
55 * system_clock::TimePoint time2 = fromUnixTimestamp(time::milliseconds(timestampInMilliseconds));
56 * </code>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080057 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070058class system_clock : public boost::chrono::system_clock
Alexander Afanasyevf6468892014-01-29 01:04:14 -080059{
60public:
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070061 typedef time_point TimePoint;
62 typedef duration Duration;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080063
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070064 // /// \brief Get current TimePoint
65 // TimePoint
66 // now();
67}; // class system_clock
Alexander Afanasyevf6468892014-01-29 01:04:14 -080068
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069/**
70 * \brief Steady clock
71 *
72 * Steady clock represents a monotonic clock. The time points of this
73 * clock cannot decrease as physical time moves forward. This clock is
74 * not related to wall clock time, and is best suitable for measuring
75 * intervals.
76 *
77 * Note that on OS X platform this defaults to system clock and is not
78 * truly monotonic. Refer to https://svn.boost.org/trac/boost/ticket/7719)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080079 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070080class steady_clock : public
81#ifdef __APPLE__
82// steady_clock may go backwards on OS X platforms, so use system_clock
83// instead
84 boost::chrono::system_clock
85#else
86 boost::chrono::steady_clock
87#endif
Alexander Afanasyevf6468892014-01-29 01:04:14 -080088{
89public:
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070090 typedef time_point TimePoint;
91 typedef duration Duration;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080092
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070093 // /// \brief Get current TimePoint
94 // TimePoint
95 // now();
96}; // class steady_clock
Alexander Afanasyevf6468892014-01-29 01:04:14 -080097
Alexander Afanasyevf6468892014-01-29 01:04:14 -080098
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070099/**
100 * \brief Get system_clock::TimePoint representing UNIX time epoch (00:00:00 on Jan 1, 1970)
101 */
102inline const system_clock::TimePoint&
103getUnixEpoch()
Yingdi Yuf2a82092014-02-03 16:49:15 -0800104{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700105 static system_clock::TimePoint epoch = system_clock::from_time_t(0);
106 return epoch;
Yingdi Yuf2a82092014-02-03 16:49:15 -0800107}
108
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800109/**
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700110 * \brief Convert system_clock::TimePoint to UNIX timestamp
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800111 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700112inline milliseconds
113toUnixTimestamp(const system_clock::TimePoint& point)
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800114{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700115 return duration_cast<milliseconds>(point - getUnixEpoch());
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800116}
117
118/**
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700119 * \brief Convert UNIX timestamp to system_clock::TimePoint
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800120 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700121inline system_clock::TimePoint
122fromUnixTimestamp(const milliseconds& duration)
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800123{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700124 return getUnixEpoch() + duration;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800125}
126
127/**
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700128 * \brief Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
129 *
130 * If timePoint contains doesn't contain fractional seconds the
131 * output format is YYYYMMDDTHHMMSS
132 *
133 * Examples:
134 *
135 * - with fractional nanoseconds: 20020131T100001,123456789
136 * - with fractional microseconds: 20020131T100001,123456
137 * - with fractional milliseconds: 20020131T100001,123
138 * - without fractional seconds: 20020131T100001
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800139 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700140std::string
141toIsoString(const system_clock::TimePoint& timePoint);
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800142
143/**
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700144 * \brief Convert from the ISO string (YYYYMMDDTHHMMSS,fffffffff) representation
145 * to the internal time format
146 *
147 * Examples of accepted ISO strings:
148 *
149 * - with fractional nanoseconds: 20020131T100001,123456789
150 * - with fractional microseconds: 20020131T100001,123456
151 * - with fractional milliseconds: 20020131T100001,123
152 * - without fractional seconds: 20020131T100001
153 *
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800154 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700155system_clock::TimePoint
156fromIsoString(const std::string& isoString);
Alexander Afanasyev53af7a12014-04-10 23:35:28 -0700157
158/**
159 * \brief Convert time point to string with specified format
160 *
161 * By default, `%Y-%m-%d %H:%M:%S` is used, producing dates like
162 * `2014-04-10 22:51:00`
163 *
164 * \param timePoint time point of system_clock
165 * \param format desired output format (default: `%Y-%m-%d %H:%M:%S`)
166 * \param locale desired locale (default: "C" locale)
167 *
168 * \sa http://www.boost.org/doc/libs/1_48_0/doc/html/date_time/date_time_io.html#date_time.format_flags
169 * described possible formatting flags
170 **/
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700171std::string
Alexander Afanasyev53af7a12014-04-10 23:35:28 -0700172toString(const system_clock::TimePoint& timePoint,
173 const std::string& format = "%Y-%m-%d %H:%M:%S",
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700174 const std::locale& locale = std::locale("C"));
Alexander Afanasyev53af7a12014-04-10 23:35:28 -0700175
176/**
177 * \brief Convert from string of specified format into time point
178 *
179 * By default, `%Y-%m-%d %H:%M:%S` is used, accepting dates like
180 * `2014-04-10 22:51:00`
181 *
182 * \param formattedTimePoint string representing time point
183 * \param format input output format (default: `%Y-%m-%d %H:%M:%S`)
184 * \param locale input locale (default: "C" locale)
185 *
186 * \sa http://www.boost.org/doc/libs/1_48_0/doc/html/date_time/date_time_io.html#date_time.format_flags
187 * described possible formatting flags
188 */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700189system_clock::TimePoint
Alexander Afanasyev53af7a12014-04-10 23:35:28 -0700190fromString(const std::string& formattedTimePoint,
191 const std::string& format = "%Y-%m-%d %H:%M:%S",
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700192 const std::locale& locale = std::locale("C"));
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800193
194} // namespace time
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -0800195} // namespace ndn
196
197#endif // NDN_TIME_HPP