1b0d29bc4SBrooks Davis // Copyright 2010 The Kyua Authors.
2b0d29bc4SBrooks Davis // All rights reserved.
3b0d29bc4SBrooks Davis //
4b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without
5b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are
6b0d29bc4SBrooks Davis // met:
7b0d29bc4SBrooks Davis //
8b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright
9b0d29bc4SBrooks Davis // notice, this list of conditions and the following disclaimer.
10b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11b0d29bc4SBrooks Davis // notice, this list of conditions and the following disclaimer in the
12b0d29bc4SBrooks Davis // documentation and/or other materials provided with the distribution.
13b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14b0d29bc4SBrooks Davis // may be used to endorse or promote products derived from this software
15b0d29bc4SBrooks Davis // without specific prior written permission.
16b0d29bc4SBrooks Davis //
17b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28b0d29bc4SBrooks Davis
29b0d29bc4SBrooks Davis #include "utils/datetime.hpp"
30b0d29bc4SBrooks Davis
31b0d29bc4SBrooks Davis extern "C" {
32b0d29bc4SBrooks Davis #include <sys/time.h>
33b0d29bc4SBrooks Davis
34b0d29bc4SBrooks Davis #include <time.h>
35b0d29bc4SBrooks Davis }
36b0d29bc4SBrooks Davis
37b0d29bc4SBrooks Davis #include <stdexcept>
38b0d29bc4SBrooks Davis
39b0d29bc4SBrooks Davis #include "utils/format/macros.hpp"
40b0d29bc4SBrooks Davis #include "utils/optional.ipp"
41b0d29bc4SBrooks Davis #include "utils/noncopyable.hpp"
42b0d29bc4SBrooks Davis #include "utils/sanity.hpp"
43b0d29bc4SBrooks Davis
44b0d29bc4SBrooks Davis namespace datetime = utils::datetime;
45b0d29bc4SBrooks Davis
46b0d29bc4SBrooks Davis using utils::none;
47b0d29bc4SBrooks Davis using utils::optional;
48b0d29bc4SBrooks Davis
49b0d29bc4SBrooks Davis
50b0d29bc4SBrooks Davis namespace {
51b0d29bc4SBrooks Davis
52b0d29bc4SBrooks Davis
53b0d29bc4SBrooks Davis /// Fake value for the current time.
54b0d29bc4SBrooks Davis static optional< datetime::timestamp > mock_now = none;
55b0d29bc4SBrooks Davis
56b0d29bc4SBrooks Davis
57b0d29bc4SBrooks Davis } // anonymous namespace
58b0d29bc4SBrooks Davis
59b0d29bc4SBrooks Davis
60b0d29bc4SBrooks Davis /// Creates a zero time delta.
delta(void)61b0d29bc4SBrooks Davis datetime::delta::delta(void) :
62b0d29bc4SBrooks Davis seconds(0),
63b0d29bc4SBrooks Davis useconds(0)
64b0d29bc4SBrooks Davis {
65b0d29bc4SBrooks Davis }
66b0d29bc4SBrooks Davis
67b0d29bc4SBrooks Davis
68b0d29bc4SBrooks Davis /// Creates a time delta.
69b0d29bc4SBrooks Davis ///
70b0d29bc4SBrooks Davis /// \param seconds_ The seconds in the delta.
71b0d29bc4SBrooks Davis /// \param useconds_ The microseconds in the delta.
72b0d29bc4SBrooks Davis ///
73b0d29bc4SBrooks Davis /// \throw std::runtime_error If the input delta is negative.
delta(const int64_t seconds_,const unsigned long useconds_)74b0d29bc4SBrooks Davis datetime::delta::delta(const int64_t seconds_,
75b0d29bc4SBrooks Davis const unsigned long useconds_) :
76b0d29bc4SBrooks Davis seconds(seconds_),
77b0d29bc4SBrooks Davis useconds(useconds_)
78b0d29bc4SBrooks Davis {
79b0d29bc4SBrooks Davis if (seconds_ < 0) {
80b0d29bc4SBrooks Davis throw std::runtime_error(F("Negative deltas are not supported by the "
81b0d29bc4SBrooks Davis "datetime::delta class; got: %s") % (*this));
82b0d29bc4SBrooks Davis }
83b0d29bc4SBrooks Davis }
84b0d29bc4SBrooks Davis
85b0d29bc4SBrooks Davis
86b0d29bc4SBrooks Davis /// Converts a time expressed in microseconds to a delta.
87b0d29bc4SBrooks Davis ///
88b0d29bc4SBrooks Davis /// \param useconds The amount of microseconds representing the delta.
89b0d29bc4SBrooks Davis ///
90b0d29bc4SBrooks Davis /// \return A new delta object.
91b0d29bc4SBrooks Davis ///
92b0d29bc4SBrooks Davis /// \throw std::runtime_error If the input delta is negative.
93b0d29bc4SBrooks Davis datetime::delta
from_microseconds(const int64_t useconds)94b0d29bc4SBrooks Davis datetime::delta::from_microseconds(const int64_t useconds)
95b0d29bc4SBrooks Davis {
96b0d29bc4SBrooks Davis if (useconds < 0) {
97b0d29bc4SBrooks Davis throw std::runtime_error(F("Negative deltas are not supported by the "
98b0d29bc4SBrooks Davis "datetime::delta class; got: %sus") %
99b0d29bc4SBrooks Davis useconds);
100b0d29bc4SBrooks Davis }
101b0d29bc4SBrooks Davis
102b0d29bc4SBrooks Davis return delta(useconds / 1000000, useconds % 1000000);
103b0d29bc4SBrooks Davis }
104b0d29bc4SBrooks Davis
105b0d29bc4SBrooks Davis
106b0d29bc4SBrooks Davis /// Convers the delta to a flat representation expressed in microseconds.
107b0d29bc4SBrooks Davis ///
108b0d29bc4SBrooks Davis /// \return The amount of microseconds that corresponds to this delta.
109b0d29bc4SBrooks Davis int64_t
to_microseconds(void) const110b0d29bc4SBrooks Davis datetime::delta::to_microseconds(void) const
111b0d29bc4SBrooks Davis {
112b0d29bc4SBrooks Davis return seconds * 1000000 + useconds;
113b0d29bc4SBrooks Davis }
114b0d29bc4SBrooks Davis
115b0d29bc4SBrooks Davis
116b0d29bc4SBrooks Davis /// Checks if two time deltas are equal.
117b0d29bc4SBrooks Davis ///
118b0d29bc4SBrooks Davis /// \param other The object to compare to.
119b0d29bc4SBrooks Davis ///
120b0d29bc4SBrooks Davis /// \return True if the two time deltas are equals; false otherwise.
121b0d29bc4SBrooks Davis bool
operator ==(const datetime::delta & other) const122b0d29bc4SBrooks Davis datetime::delta::operator==(const datetime::delta& other) const
123b0d29bc4SBrooks Davis {
124b0d29bc4SBrooks Davis return seconds == other.seconds && useconds == other.useconds;
125b0d29bc4SBrooks Davis }
126b0d29bc4SBrooks Davis
127b0d29bc4SBrooks Davis
128b0d29bc4SBrooks Davis /// Checks if two time deltas are different.
129b0d29bc4SBrooks Davis ///
130b0d29bc4SBrooks Davis /// \param other The object to compare to.
131b0d29bc4SBrooks Davis ///
132b0d29bc4SBrooks Davis /// \return True if the two time deltas are different; false otherwise.
133b0d29bc4SBrooks Davis bool
operator !=(const datetime::delta & other) const134b0d29bc4SBrooks Davis datetime::delta::operator!=(const datetime::delta& other) const
135b0d29bc4SBrooks Davis {
136b0d29bc4SBrooks Davis return !(*this == other);
137b0d29bc4SBrooks Davis }
138b0d29bc4SBrooks Davis
139b0d29bc4SBrooks Davis
140b0d29bc4SBrooks Davis /// Checks if this time delta is shorter than another one.
141b0d29bc4SBrooks Davis ///
142b0d29bc4SBrooks Davis /// \param other The object to compare to.
143b0d29bc4SBrooks Davis ///
144b0d29bc4SBrooks Davis /// \return True if this time delta is shorter than other; false otherwise.
145b0d29bc4SBrooks Davis bool
operator <(const datetime::delta & other) const146b0d29bc4SBrooks Davis datetime::delta::operator<(const datetime::delta& other) const
147b0d29bc4SBrooks Davis {
148b0d29bc4SBrooks Davis return seconds < other.seconds ||
149b0d29bc4SBrooks Davis (seconds == other.seconds && useconds < other.useconds);
150b0d29bc4SBrooks Davis }
151b0d29bc4SBrooks Davis
152b0d29bc4SBrooks Davis
153b0d29bc4SBrooks Davis /// Checks if this time delta is shorter than or equal to another one.
154b0d29bc4SBrooks Davis ///
155b0d29bc4SBrooks Davis /// \param other The object to compare to.
156b0d29bc4SBrooks Davis ///
157b0d29bc4SBrooks Davis /// \return True if this time delta is shorter than or equal to; false
158b0d29bc4SBrooks Davis /// otherwise.
159b0d29bc4SBrooks Davis bool
operator <=(const datetime::delta & other) const160b0d29bc4SBrooks Davis datetime::delta::operator<=(const datetime::delta& other) const
161b0d29bc4SBrooks Davis {
162b0d29bc4SBrooks Davis return (*this) < other || (*this) == other;
163b0d29bc4SBrooks Davis }
164b0d29bc4SBrooks Davis
165b0d29bc4SBrooks Davis
166b0d29bc4SBrooks Davis /// Checks if this time delta is larger than another one.
167b0d29bc4SBrooks Davis ///
168b0d29bc4SBrooks Davis /// \param other The object to compare to.
169b0d29bc4SBrooks Davis ///
170b0d29bc4SBrooks Davis /// \return True if this time delta is larger than other; false otherwise.
171b0d29bc4SBrooks Davis bool
operator >(const datetime::delta & other) const172b0d29bc4SBrooks Davis datetime::delta::operator>(const datetime::delta& other) const
173b0d29bc4SBrooks Davis {
174b0d29bc4SBrooks Davis return seconds > other.seconds ||
175b0d29bc4SBrooks Davis (seconds == other.seconds && useconds > other.useconds);
176b0d29bc4SBrooks Davis }
177b0d29bc4SBrooks Davis
178b0d29bc4SBrooks Davis
179b0d29bc4SBrooks Davis /// Checks if this time delta is larger than or equal to another one.
180b0d29bc4SBrooks Davis ///
181b0d29bc4SBrooks Davis /// \param other The object to compare to.
182b0d29bc4SBrooks Davis ///
183b0d29bc4SBrooks Davis /// \return True if this time delta is larger than or equal to; false
184b0d29bc4SBrooks Davis /// otherwise.
185b0d29bc4SBrooks Davis bool
operator >=(const datetime::delta & other) const186b0d29bc4SBrooks Davis datetime::delta::operator>=(const datetime::delta& other) const
187b0d29bc4SBrooks Davis {
188b0d29bc4SBrooks Davis return (*this) > other || (*this) == other;
189b0d29bc4SBrooks Davis }
190b0d29bc4SBrooks Davis
191b0d29bc4SBrooks Davis
192b0d29bc4SBrooks Davis /// Adds a time delta to this one.
193b0d29bc4SBrooks Davis ///
194b0d29bc4SBrooks Davis /// \param other The time delta to add.
195b0d29bc4SBrooks Davis ///
196b0d29bc4SBrooks Davis /// \return The addition of this time delta with the other time delta.
197b0d29bc4SBrooks Davis datetime::delta
operator +(const datetime::delta & other) const198b0d29bc4SBrooks Davis datetime::delta::operator+(const datetime::delta& other) const
199b0d29bc4SBrooks Davis {
200b0d29bc4SBrooks Davis return delta::from_microseconds(to_microseconds() +
201b0d29bc4SBrooks Davis other.to_microseconds());
202b0d29bc4SBrooks Davis }
203b0d29bc4SBrooks Davis
204b0d29bc4SBrooks Davis
205b0d29bc4SBrooks Davis /// Adds a time delta to this one and updates this with the result.
206b0d29bc4SBrooks Davis ///
207b0d29bc4SBrooks Davis /// \param other The time delta to add.
208b0d29bc4SBrooks Davis ///
209b0d29bc4SBrooks Davis /// \return The addition of this time delta with the other time delta.
210b0d29bc4SBrooks Davis datetime::delta&
operator +=(const datetime::delta & other)211b0d29bc4SBrooks Davis datetime::delta::operator+=(const datetime::delta& other)
212b0d29bc4SBrooks Davis {
213b0d29bc4SBrooks Davis *this = *this + other;
214b0d29bc4SBrooks Davis return *this;
215b0d29bc4SBrooks Davis }
216b0d29bc4SBrooks Davis
217b0d29bc4SBrooks Davis
218b0d29bc4SBrooks Davis /// Scales this delta by a positive integral factor.
219b0d29bc4SBrooks Davis ///
220b0d29bc4SBrooks Davis /// \param factor The scaling factor.
221b0d29bc4SBrooks Davis ///
222b0d29bc4SBrooks Davis /// \return The scaled delta.
223b0d29bc4SBrooks Davis datetime::delta
operator *(const std::size_t factor) const224b0d29bc4SBrooks Davis datetime::delta::operator*(const std::size_t factor) const
225b0d29bc4SBrooks Davis {
226b0d29bc4SBrooks Davis return delta::from_microseconds(to_microseconds() * factor);
227b0d29bc4SBrooks Davis }
228b0d29bc4SBrooks Davis
229b0d29bc4SBrooks Davis
230b0d29bc4SBrooks Davis /// Scales this delta by and updates this delta with the result.
231b0d29bc4SBrooks Davis ///
232b0d29bc4SBrooks Davis /// \param factor The scaling factor.
233b0d29bc4SBrooks Davis ///
234b0d29bc4SBrooks Davis /// \return The scaled delta as a reference to the input object.
235b0d29bc4SBrooks Davis datetime::delta&
operator *=(const std::size_t factor)236b0d29bc4SBrooks Davis datetime::delta::operator*=(const std::size_t factor)
237b0d29bc4SBrooks Davis {
238b0d29bc4SBrooks Davis *this = *this * factor;
239b0d29bc4SBrooks Davis return *this;
240b0d29bc4SBrooks Davis }
241b0d29bc4SBrooks Davis
242b0d29bc4SBrooks Davis
243b0d29bc4SBrooks Davis /// Injects the object into a stream.
244b0d29bc4SBrooks Davis ///
245b0d29bc4SBrooks Davis /// \param output The stream into which to inject the object.
246b0d29bc4SBrooks Davis /// \param object The object to format.
247b0d29bc4SBrooks Davis ///
248b0d29bc4SBrooks Davis /// \return The output stream.
249b0d29bc4SBrooks Davis std::ostream&
operator <<(std::ostream & output,const delta & object)250b0d29bc4SBrooks Davis datetime::operator<<(std::ostream& output, const delta& object)
251b0d29bc4SBrooks Davis {
252b0d29bc4SBrooks Davis return (output << object.to_microseconds() << "us");
253b0d29bc4SBrooks Davis }
254b0d29bc4SBrooks Davis
255b0d29bc4SBrooks Davis
256b0d29bc4SBrooks Davis namespace utils {
257b0d29bc4SBrooks Davis namespace datetime {
258b0d29bc4SBrooks Davis
259b0d29bc4SBrooks Davis
260b0d29bc4SBrooks Davis /// Internal representation for datetime::timestamp.
261b0d29bc4SBrooks Davis struct timestamp::impl : utils::noncopyable {
262b0d29bc4SBrooks Davis /// The raw timestamp as provided by libc.
263b0d29bc4SBrooks Davis ::timeval data;
264b0d29bc4SBrooks Davis
265b0d29bc4SBrooks Davis /// Constructs an impl object from initialized data.
266b0d29bc4SBrooks Davis ///
267b0d29bc4SBrooks Davis /// \param data_ The raw timestamp to use.
implutils::datetime::timestamp::impl268b0d29bc4SBrooks Davis impl(const ::timeval& data_) : data(data_)
269b0d29bc4SBrooks Davis {
270b0d29bc4SBrooks Davis }
271b0d29bc4SBrooks Davis };
272b0d29bc4SBrooks Davis
273b0d29bc4SBrooks Davis
274b0d29bc4SBrooks Davis } // namespace datetime
275b0d29bc4SBrooks Davis } // namespace utils
276b0d29bc4SBrooks Davis
277b0d29bc4SBrooks Davis
278b0d29bc4SBrooks Davis /// Constructs a new timestamp.
279b0d29bc4SBrooks Davis ///
280b0d29bc4SBrooks Davis /// \param pimpl_ An existing impl representation.
timestamp(std::shared_ptr<impl> pimpl_)281b0d29bc4SBrooks Davis datetime::timestamp::timestamp(std::shared_ptr< impl > pimpl_) :
282b0d29bc4SBrooks Davis _pimpl(pimpl_)
283b0d29bc4SBrooks Davis {
284b0d29bc4SBrooks Davis }
285b0d29bc4SBrooks Davis
286b0d29bc4SBrooks Davis
287b0d29bc4SBrooks Davis /// Constructs a timestamp from the amount of microseconds since the epoch.
288b0d29bc4SBrooks Davis ///
289b0d29bc4SBrooks Davis /// \param value Microseconds since the epoch in UTC. Must be positive.
290b0d29bc4SBrooks Davis ///
291b0d29bc4SBrooks Davis /// \return A new timestamp.
292b0d29bc4SBrooks Davis datetime::timestamp
from_microseconds(const int64_t value)293b0d29bc4SBrooks Davis datetime::timestamp::from_microseconds(const int64_t value)
294b0d29bc4SBrooks Davis {
295b0d29bc4SBrooks Davis PRE(value >= 0);
296b0d29bc4SBrooks Davis ::timeval data;
297b0d29bc4SBrooks Davis data.tv_sec = static_cast< time_t >(value / 1000000);
298b0d29bc4SBrooks Davis data.tv_usec = static_cast< suseconds_t >(value % 1000000);
299b0d29bc4SBrooks Davis return timestamp(std::shared_ptr< impl >(new impl(data)));
300b0d29bc4SBrooks Davis }
301b0d29bc4SBrooks Davis
302b0d29bc4SBrooks Davis
303b0d29bc4SBrooks Davis /// Constructs a timestamp based on user-friendly values.
304b0d29bc4SBrooks Davis ///
305b0d29bc4SBrooks Davis /// \param year The year in the [1900,inf) range.
306b0d29bc4SBrooks Davis /// \param month The month in the [1,12] range.
307b0d29bc4SBrooks Davis /// \param day The day in the [1,30] range.
308b0d29bc4SBrooks Davis /// \param hour The hour in the [0,23] range.
309b0d29bc4SBrooks Davis /// \param minute The minute in the [0,59] range.
310b0d29bc4SBrooks Davis /// \param second The second in the [0,60] range. Yes, that is 60, which can be
311b0d29bc4SBrooks Davis /// the case on leap seconds.
312b0d29bc4SBrooks Davis /// \param microsecond The microsecond in the [0,999999] range.
313b0d29bc4SBrooks Davis ///
314b0d29bc4SBrooks Davis /// \return A new timestamp.
315b0d29bc4SBrooks Davis datetime::timestamp
from_values(const int year,const int month,const int day,const int hour,const int minute,const int second,const int microsecond)316b0d29bc4SBrooks Davis datetime::timestamp::from_values(const int year, const int month,
317b0d29bc4SBrooks Davis const int day, const int hour,
318b0d29bc4SBrooks Davis const int minute, const int second,
319b0d29bc4SBrooks Davis const int microsecond)
320b0d29bc4SBrooks Davis {
321b0d29bc4SBrooks Davis PRE(year >= 1900);
322b0d29bc4SBrooks Davis PRE(month >= 1 && month <= 12);
323b0d29bc4SBrooks Davis PRE(day >= 1 && day <= 30);
324b0d29bc4SBrooks Davis PRE(hour >= 0 && hour <= 23);
325b0d29bc4SBrooks Davis PRE(minute >= 0 && minute <= 59);
326b0d29bc4SBrooks Davis PRE(second >= 0 && second <= 60);
327b0d29bc4SBrooks Davis PRE(microsecond >= 0 && microsecond <= 999999);
328b0d29bc4SBrooks Davis
329b0d29bc4SBrooks Davis // The code below is quite convoluted. The problem is that we can't assume
330b0d29bc4SBrooks Davis // that some fields (like tm_zone) of ::tm exist, and thus we can't blindly
331b0d29bc4SBrooks Davis // set them from the code. Instead of detecting their presence in the
332b0d29bc4SBrooks Davis // configure script, we just query the current time to initialize such
333b0d29bc4SBrooks Davis // fields and then we override the ones we are interested in. (There might
334b0d29bc4SBrooks Davis // be some better way to do this, but I don't know it and the documentation
335b0d29bc4SBrooks Davis // does not shed much light into how to create your own fake date.)
336b0d29bc4SBrooks Davis
337b0d29bc4SBrooks Davis const time_t current_time = ::time(NULL);
338b0d29bc4SBrooks Davis
339b0d29bc4SBrooks Davis ::tm timedata;
340b0d29bc4SBrooks Davis if (::gmtime_r(¤t_time, &timedata) == NULL)
341b0d29bc4SBrooks Davis UNREACHABLE;
342b0d29bc4SBrooks Davis
343b0d29bc4SBrooks Davis timedata.tm_sec = second;
344b0d29bc4SBrooks Davis timedata.tm_min = minute;
345b0d29bc4SBrooks Davis timedata.tm_hour = hour;
346b0d29bc4SBrooks Davis timedata.tm_mday = day;
347b0d29bc4SBrooks Davis timedata.tm_mon = month - 1;
348b0d29bc4SBrooks Davis timedata.tm_year = year - 1900;
349b0d29bc4SBrooks Davis // Ignored: timedata.tm_wday
350b0d29bc4SBrooks Davis // Ignored: timedata.tm_yday
351b0d29bc4SBrooks Davis
352b0d29bc4SBrooks Davis ::timeval data;
353b0d29bc4SBrooks Davis data.tv_sec = ::mktime(&timedata);
354b0d29bc4SBrooks Davis data.tv_usec = static_cast< suseconds_t >(microsecond);
355b0d29bc4SBrooks Davis return timestamp(std::shared_ptr< impl >(new impl(data)));
356b0d29bc4SBrooks Davis }
357b0d29bc4SBrooks Davis
358b0d29bc4SBrooks Davis
359b0d29bc4SBrooks Davis /// Constructs a new timestamp representing the current time in UTC.
360b0d29bc4SBrooks Davis ///
361b0d29bc4SBrooks Davis /// \return A new timestamp.
362b0d29bc4SBrooks Davis datetime::timestamp
now(void)363b0d29bc4SBrooks Davis datetime::timestamp::now(void)
364b0d29bc4SBrooks Davis {
365b0d29bc4SBrooks Davis if (mock_now)
366b0d29bc4SBrooks Davis return mock_now.get();
367b0d29bc4SBrooks Davis
368b0d29bc4SBrooks Davis ::timeval data;
369b0d29bc4SBrooks Davis {
370b0d29bc4SBrooks Davis const int ret = ::gettimeofday(&data, NULL);
371b0d29bc4SBrooks Davis INV(ret != -1);
372b0d29bc4SBrooks Davis }
373b0d29bc4SBrooks Davis
374b0d29bc4SBrooks Davis return timestamp(std::shared_ptr< impl >(new impl(data)));
375b0d29bc4SBrooks Davis }
376b0d29bc4SBrooks Davis
377b0d29bc4SBrooks Davis
378b0d29bc4SBrooks Davis /// Formats a timestamp.
379b0d29bc4SBrooks Davis ///
380b0d29bc4SBrooks Davis /// \param format The format string to use as consumed by strftime(3).
381b0d29bc4SBrooks Davis ///
382b0d29bc4SBrooks Davis /// \return The formatted time.
383b0d29bc4SBrooks Davis std::string
strftime(const std::string & format) const384b0d29bc4SBrooks Davis datetime::timestamp::strftime(const std::string& format) const
385b0d29bc4SBrooks Davis {
386b0d29bc4SBrooks Davis ::tm timedata;
387b0d29bc4SBrooks Davis // This conversion to time_t is necessary because tv_sec is not guaranteed
388b0d29bc4SBrooks Davis // to be a time_t. For example, it isn't in NetBSD 5.x
389b0d29bc4SBrooks Davis ::time_t epoch_seconds;
390b0d29bc4SBrooks Davis epoch_seconds = _pimpl->data.tv_sec;
391b0d29bc4SBrooks Davis if (::gmtime_r(&epoch_seconds, &timedata) == NULL)
392b0d29bc4SBrooks Davis UNREACHABLE_MSG("gmtime_r(3) did not accept the value returned by "
393b0d29bc4SBrooks Davis "gettimeofday(2)");
394b0d29bc4SBrooks Davis
395b0d29bc4SBrooks Davis char buf[128];
396b0d29bc4SBrooks Davis if (::strftime(buf, sizeof(buf), format.c_str(), &timedata) == 0)
397b0d29bc4SBrooks Davis UNREACHABLE_MSG("Arbitrary-long format strings are unimplemented");
398b0d29bc4SBrooks Davis return buf;
399b0d29bc4SBrooks Davis }
400b0d29bc4SBrooks Davis
401b0d29bc4SBrooks Davis
402b0d29bc4SBrooks Davis /// Formats a timestamp with the ISO 8601 standard and in UTC.
403b0d29bc4SBrooks Davis ///
404b0d29bc4SBrooks Davis /// \return A string with the formatted timestamp.
405b0d29bc4SBrooks Davis std::string
to_iso8601_in_utc(void) const406b0d29bc4SBrooks Davis datetime::timestamp::to_iso8601_in_utc(void) const
407b0d29bc4SBrooks Davis {
408b0d29bc4SBrooks Davis return F("%s.%06sZ") % strftime("%Y-%m-%dT%H:%M:%S") % _pimpl->data.tv_usec;
409b0d29bc4SBrooks Davis }
410b0d29bc4SBrooks Davis
411b0d29bc4SBrooks Davis
412b0d29bc4SBrooks Davis /// Returns the number of microseconds since the epoch in UTC.
413b0d29bc4SBrooks Davis ///
414b0d29bc4SBrooks Davis /// \return A number of microseconds.
415b0d29bc4SBrooks Davis int64_t
to_microseconds(void) const416b0d29bc4SBrooks Davis datetime::timestamp::to_microseconds(void) const
417b0d29bc4SBrooks Davis {
418b0d29bc4SBrooks Davis return static_cast< int64_t >(_pimpl->data.tv_sec) * 1000000 +
419b0d29bc4SBrooks Davis _pimpl->data.tv_usec;
420b0d29bc4SBrooks Davis }
421b0d29bc4SBrooks Davis
422b0d29bc4SBrooks Davis
423b0d29bc4SBrooks Davis /// Returns the number of seconds since the epoch in UTC.
424b0d29bc4SBrooks Davis ///
425b0d29bc4SBrooks Davis /// \return A number of seconds.
426b0d29bc4SBrooks Davis int64_t
to_seconds(void) const427b0d29bc4SBrooks Davis datetime::timestamp::to_seconds(void) const
428b0d29bc4SBrooks Davis {
429b0d29bc4SBrooks Davis return static_cast< int64_t >(_pimpl->data.tv_sec);
430b0d29bc4SBrooks Davis }
431b0d29bc4SBrooks Davis
432b0d29bc4SBrooks Davis
433b0d29bc4SBrooks Davis /// Sets the current time for testing purposes.
434b0d29bc4SBrooks Davis void
set_mock_now(const int year,const int month,const int day,const int hour,const int minute,const int second,const int microsecond)435b0d29bc4SBrooks Davis datetime::set_mock_now(const int year, const int month,
436b0d29bc4SBrooks Davis const int day, const int hour,
437b0d29bc4SBrooks Davis const int minute, const int second,
438b0d29bc4SBrooks Davis const int microsecond)
439b0d29bc4SBrooks Davis {
440b0d29bc4SBrooks Davis mock_now = timestamp::from_values(year, month, day, hour, minute, second,
441b0d29bc4SBrooks Davis microsecond);
442b0d29bc4SBrooks Davis }
443b0d29bc4SBrooks Davis
444b0d29bc4SBrooks Davis
445b0d29bc4SBrooks Davis /// Sets the current time for testing purposes.
446b0d29bc4SBrooks Davis ///
447b0d29bc4SBrooks Davis /// \param mock_now_ The mock timestamp to set the time to.
448b0d29bc4SBrooks Davis void
set_mock_now(const timestamp & mock_now_)449b0d29bc4SBrooks Davis datetime::set_mock_now(const timestamp& mock_now_)
450b0d29bc4SBrooks Davis {
451b0d29bc4SBrooks Davis mock_now = mock_now_;
452b0d29bc4SBrooks Davis }
453b0d29bc4SBrooks Davis
454b0d29bc4SBrooks Davis
455b0d29bc4SBrooks Davis /// Checks if two timestamps are equal.
456b0d29bc4SBrooks Davis ///
457b0d29bc4SBrooks Davis /// \param other The object to compare to.
458b0d29bc4SBrooks Davis ///
459b0d29bc4SBrooks Davis /// \return True if the two timestamps are equals; false otherwise.
460b0d29bc4SBrooks Davis bool
operator ==(const datetime::timestamp & other) const461b0d29bc4SBrooks Davis datetime::timestamp::operator==(const datetime::timestamp& other) const
462b0d29bc4SBrooks Davis {
463b0d29bc4SBrooks Davis return _pimpl->data.tv_sec == other._pimpl->data.tv_sec &&
464b0d29bc4SBrooks Davis _pimpl->data.tv_usec == other._pimpl->data.tv_usec;
465b0d29bc4SBrooks Davis }
466b0d29bc4SBrooks Davis
467b0d29bc4SBrooks Davis
468b0d29bc4SBrooks Davis /// Checks if two timestamps are different.
469b0d29bc4SBrooks Davis ///
470b0d29bc4SBrooks Davis /// \param other The object to compare to.
471b0d29bc4SBrooks Davis ///
472b0d29bc4SBrooks Davis /// \return True if the two timestamps are different; false otherwise.
473b0d29bc4SBrooks Davis bool
operator !=(const datetime::timestamp & other) const474b0d29bc4SBrooks Davis datetime::timestamp::operator!=(const datetime::timestamp& other) const
475b0d29bc4SBrooks Davis {
476b0d29bc4SBrooks Davis return !(*this == other);
477b0d29bc4SBrooks Davis }
478b0d29bc4SBrooks Davis
479b0d29bc4SBrooks Davis
480b0d29bc4SBrooks Davis /// Checks if a timestamp is before another.
481b0d29bc4SBrooks Davis ///
482b0d29bc4SBrooks Davis /// \param other The object to compare to.
483b0d29bc4SBrooks Davis ///
484b0d29bc4SBrooks Davis /// \return True if this timestamp comes before other; false otherwise.
485b0d29bc4SBrooks Davis bool
operator <(const datetime::timestamp & other) const486b0d29bc4SBrooks Davis datetime::timestamp::operator<(const datetime::timestamp& other) const
487b0d29bc4SBrooks Davis {
488b0d29bc4SBrooks Davis return to_microseconds() < other.to_microseconds();
489b0d29bc4SBrooks Davis }
490b0d29bc4SBrooks Davis
491b0d29bc4SBrooks Davis
492b0d29bc4SBrooks Davis /// Checks if a timestamp is before or equal to another.
493b0d29bc4SBrooks Davis ///
494b0d29bc4SBrooks Davis /// \param other The object to compare to.
495b0d29bc4SBrooks Davis ///
496b0d29bc4SBrooks Davis /// \return True if this timestamp comes before other or is equal to it;
497b0d29bc4SBrooks Davis /// false otherwise.
498b0d29bc4SBrooks Davis bool
operator <=(const datetime::timestamp & other) const499b0d29bc4SBrooks Davis datetime::timestamp::operator<=(const datetime::timestamp& other) const
500b0d29bc4SBrooks Davis {
501b0d29bc4SBrooks Davis return to_microseconds() <= other.to_microseconds();
502b0d29bc4SBrooks Davis }
503b0d29bc4SBrooks Davis
504b0d29bc4SBrooks Davis
505b0d29bc4SBrooks Davis /// Checks if a timestamp is after another.
506b0d29bc4SBrooks Davis ///
507b0d29bc4SBrooks Davis /// \param other The object to compare to.
508b0d29bc4SBrooks Davis ///
509b0d29bc4SBrooks Davis /// \return True if this timestamp comes after other; false otherwise;
510b0d29bc4SBrooks Davis bool
operator >(const datetime::timestamp & other) const511b0d29bc4SBrooks Davis datetime::timestamp::operator>(const datetime::timestamp& other) const
512b0d29bc4SBrooks Davis {
513b0d29bc4SBrooks Davis return to_microseconds() > other.to_microseconds();
514b0d29bc4SBrooks Davis }
515b0d29bc4SBrooks Davis
516b0d29bc4SBrooks Davis
517b0d29bc4SBrooks Davis /// Checks if a timestamp is after or equal to another.
518b0d29bc4SBrooks Davis ///
519b0d29bc4SBrooks Davis /// \param other The object to compare to.
520b0d29bc4SBrooks Davis ///
521b0d29bc4SBrooks Davis /// \return True if this timestamp comes after other or is equal to it;
522b0d29bc4SBrooks Davis /// false otherwise.
523b0d29bc4SBrooks Davis bool
operator >=(const datetime::timestamp & other) const524b0d29bc4SBrooks Davis datetime::timestamp::operator>=(const datetime::timestamp& other) const
525b0d29bc4SBrooks Davis {
526b0d29bc4SBrooks Davis return to_microseconds() >= other.to_microseconds();
527b0d29bc4SBrooks Davis }
528b0d29bc4SBrooks Davis
529b0d29bc4SBrooks Davis
530b0d29bc4SBrooks Davis /// Calculates the addition of a delta to a timestamp.
531b0d29bc4SBrooks Davis ///
532b0d29bc4SBrooks Davis /// \param other The delta to add.
533b0d29bc4SBrooks Davis ///
534b0d29bc4SBrooks Davis /// \return A new timestamp in the future.
535b0d29bc4SBrooks Davis datetime::timestamp
operator +(const datetime::delta & other) const536b0d29bc4SBrooks Davis datetime::timestamp::operator+(const datetime::delta& other) const
537b0d29bc4SBrooks Davis {
538b0d29bc4SBrooks Davis return datetime::timestamp::from_microseconds(to_microseconds() +
539b0d29bc4SBrooks Davis other.to_microseconds());
540b0d29bc4SBrooks Davis }
541b0d29bc4SBrooks Davis
542b0d29bc4SBrooks Davis
543b0d29bc4SBrooks Davis /// Calculates the addition of a delta to this timestamp.
544b0d29bc4SBrooks Davis ///
545b0d29bc4SBrooks Davis /// \param other The delta to add.
546b0d29bc4SBrooks Davis ///
547b0d29bc4SBrooks Davis /// \return A reference to the modified timestamp.
548b0d29bc4SBrooks Davis datetime::timestamp&
operator +=(const datetime::delta & other)549b0d29bc4SBrooks Davis datetime::timestamp::operator+=(const datetime::delta& other)
550b0d29bc4SBrooks Davis {
551b0d29bc4SBrooks Davis *this = *this + other;
552b0d29bc4SBrooks Davis return *this;
553b0d29bc4SBrooks Davis }
554b0d29bc4SBrooks Davis
555b0d29bc4SBrooks Davis
556b0d29bc4SBrooks Davis /// Calculates the subtraction of a delta from a timestamp.
557b0d29bc4SBrooks Davis ///
558b0d29bc4SBrooks Davis /// \param other The delta to subtract.
559b0d29bc4SBrooks Davis ///
560b0d29bc4SBrooks Davis /// \return A new timestamp in the past.
561b0d29bc4SBrooks Davis datetime::timestamp
operator -(const datetime::delta & other) const562b0d29bc4SBrooks Davis datetime::timestamp::operator-(const datetime::delta& other) const
563b0d29bc4SBrooks Davis {
564b0d29bc4SBrooks Davis return datetime::timestamp::from_microseconds(to_microseconds() -
565b0d29bc4SBrooks Davis other.to_microseconds());
566b0d29bc4SBrooks Davis }
567b0d29bc4SBrooks Davis
568b0d29bc4SBrooks Davis
569b0d29bc4SBrooks Davis /// Calculates the subtraction of a delta from this timestamp.
570b0d29bc4SBrooks Davis ///
571b0d29bc4SBrooks Davis /// \param other The delta to subtract.
572b0d29bc4SBrooks Davis ///
573b0d29bc4SBrooks Davis /// \return A reference to the modified timestamp.
574b0d29bc4SBrooks Davis datetime::timestamp&
operator -=(const datetime::delta & other)575b0d29bc4SBrooks Davis datetime::timestamp::operator-=(const datetime::delta& other)
576b0d29bc4SBrooks Davis {
577b0d29bc4SBrooks Davis *this = *this - other;
578b0d29bc4SBrooks Davis return *this;
579b0d29bc4SBrooks Davis }
580b0d29bc4SBrooks Davis
581b0d29bc4SBrooks Davis
582b0d29bc4SBrooks Davis /// Calculates the delta between two timestamps.
583b0d29bc4SBrooks Davis ///
584b0d29bc4SBrooks Davis /// \param other The subtrahend.
585b0d29bc4SBrooks Davis ///
586b0d29bc4SBrooks Davis /// \return The difference between this object and the other object.
587b0d29bc4SBrooks Davis ///
588b0d29bc4SBrooks Davis /// \throw std::runtime_error If the subtraction would result in a negative time
589b0d29bc4SBrooks Davis /// delta, which are currently not supported.
590b0d29bc4SBrooks Davis datetime::delta
operator -(const datetime::timestamp & other) const591b0d29bc4SBrooks Davis datetime::timestamp::operator-(const datetime::timestamp& other) const
592b0d29bc4SBrooks Davis {
593*6b13d60bSMuhammad Moinur Rahman /*
594*6b13d60bSMuhammad Moinur Rahman * XXX-BD: gettimeofday isn't necessarily monotonic so return the
595*6b13d60bSMuhammad Moinur Rahman * smallest non-zero delta if time went backwards.
596*6b13d60bSMuhammad Moinur Rahman */
597*6b13d60bSMuhammad Moinur Rahman if ((*this) < other)
598*6b13d60bSMuhammad Moinur Rahman return datetime::delta::from_microseconds(1);
599b0d29bc4SBrooks Davis return datetime::delta::from_microseconds(to_microseconds() -
600b0d29bc4SBrooks Davis other.to_microseconds());
601b0d29bc4SBrooks Davis }
602b0d29bc4SBrooks Davis
603b0d29bc4SBrooks Davis
604b0d29bc4SBrooks Davis /// Injects the object into a stream.
605b0d29bc4SBrooks Davis ///
606b0d29bc4SBrooks Davis /// \param output The stream into which to inject the object.
607b0d29bc4SBrooks Davis /// \param object The object to format.
608b0d29bc4SBrooks Davis ///
609b0d29bc4SBrooks Davis /// \return The output stream.
610b0d29bc4SBrooks Davis std::ostream&
operator <<(std::ostream & output,const timestamp & object)611b0d29bc4SBrooks Davis datetime::operator<<(std::ostream& output, const timestamp& object)
612b0d29bc4SBrooks Davis {
613b0d29bc4SBrooks Davis return (output << object.to_microseconds() << "us");
614b0d29bc4SBrooks Davis }
615