1// Copyright 2010 The Kyua Authors. 2// All rights reserved. 3// 4// Redistribution and use in source and binary forms, with or without 5// modification, are permitted provided that the following conditions are 6// met: 7// 8// * Redistributions of source code must retain the above copyright 9// notice, this list of conditions and the following disclaimer. 10// * Redistributions in binary form must reproduce the above copyright 11// notice, this list of conditions and the following disclaimer in the 12// documentation and/or other materials provided with the distribution. 13// * Neither the name of Google Inc. nor the names of its contributors 14// may be used to endorse or promote products derived from this software 15// without specific prior written permission. 16// 17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29/// \file utils/datetime.hpp 30/// Provides date and time-related classes and functions. 31 32#if !defined(UTILS_DATETIME_HPP) 33#define UTILS_DATETIME_HPP 34 35#include "utils/datetime_fwd.hpp" 36 37extern "C" { 38#include <stdint.h> 39} 40 41#include <cstddef> 42#include <memory> 43#include <ostream> 44#include <string> 45 46 47namespace utils { 48namespace datetime { 49 50 51/// Represents a time delta to describe deadlines. 52/// 53/// Because we use this class to handle deadlines, we currently do not support 54/// negative deltas. 55class delta { 56public: 57 /// The amount of seconds in the time delta. 58 int64_t seconds; 59 60 /// The amount of microseconds in the time delta. 61 unsigned long useconds; 62 63 delta(void); 64 delta(const int64_t, const unsigned long); 65 66 static delta from_microseconds(const int64_t); 67 int64_t to_microseconds(void) const; 68 69 bool operator==(const delta&) const; 70 bool operator!=(const delta&) const; 71 bool operator<(const delta&) const; 72 bool operator<=(const delta&) const; 73 bool operator>(const delta&) const; 74 bool operator>=(const delta&) const; 75 76 delta operator+(const delta&) const; 77 delta& operator+=(const delta&); 78 // operator- and operator-= do not exist because we do not support negative 79 // deltas. See class docstring. 80 delta operator*(const std::size_t) const; 81 delta& operator*=(const std::size_t); 82}; 83 84 85std::ostream& operator<<(std::ostream&, const delta&); 86 87 88/// Represents a fixed date/time. 89/// 90/// Timestamps are immutable objects and therefore we can simply use a shared 91/// pointer to hide the implementation type of the date. By not using an auto 92/// pointer, we don't have to worry about providing our own copy constructor and 93/// assignment opertor. 94class timestamp { 95 struct impl; 96 97 /// Pointer to the shared internal implementation. 98 std::shared_ptr< impl > _pimpl; 99 100 timestamp(std::shared_ptr< impl >); 101 102public: 103 static timestamp from_microseconds(const int64_t); 104 static timestamp from_values(const int, const int, const int, 105 const int, const int, const int, 106 const int); 107 static timestamp now(void); 108 109 std::string strftime(const std::string&) const; 110 std::string to_iso8601_in_utc(void) const; 111 int64_t to_microseconds(void) const; 112 int64_t to_seconds(void) const; 113 114 bool operator==(const timestamp&) const; 115 bool operator!=(const timestamp&) const; 116 bool operator<(const timestamp&) const; 117 bool operator<=(const timestamp&) const; 118 bool operator>(const timestamp&) const; 119 bool operator>=(const timestamp&) const; 120 121 timestamp operator+(const delta&) const; 122 timestamp& operator+=(const delta&); 123 timestamp operator-(const delta&) const; 124 timestamp& operator-=(const delta&); 125 delta operator-(const timestamp&) const; 126}; 127 128 129std::ostream& operator<<(std::ostream&, const timestamp&); 130 131 132void set_mock_now(const int, const int, const int, const int, const int, 133 const int, const int); 134void set_mock_now(const timestamp&); 135 136 137} // namespace datetime 138} // namespace utils 139 140#endif // !defined(UTILS_DATETIME_HPP) 141