1// Copyright 2010 Google Inc. 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 35extern "C" { 36#include <stdint.h> 37} 38 39#include <cstddef> 40#include <ostream> 41#include <string> 42 43#include "utils/shared_ptr.hpp" 44 45namespace utils { 46namespace datetime { 47 48 49/// Represents a time delta to describe deadlines. 50class delta { 51public: 52 /// The amount of seconds in the time delta. 53 int64_t seconds; 54 55 /// The amount of microseconds in the time delta. 56 unsigned long useconds; 57 58 delta(void); 59 delta(const int64_t, const unsigned long); 60 61 static delta from_microseconds(const int64_t); 62 int64_t to_microseconds(void) const; 63 64 bool operator==(const delta&) const; 65 bool operator!=(const delta&) const; 66 67 delta operator+(const delta&) const; 68 delta operator+=(const delta&); 69}; 70 71 72std::ostream& operator<<(std::ostream&, const delta&); 73 74 75/// Represents a fixed date/time. 76/// 77/// Timestamps are immutable objects and therefore we can simply use a shared 78/// pointer to hide the implementation type of the date. By not using an auto 79/// pointer, we don't have to worry about providing our own copy constructor and 80/// assignment opertor. 81class timestamp { 82 struct impl; 83 84 /// Pointer to the shared internal implementation. 85 std::shared_ptr< impl > _pimpl; 86 87 timestamp(std::shared_ptr< impl >); 88 89public: 90 static timestamp from_microseconds(const int64_t); 91 static timestamp from_values(const int, const int, const int, 92 const int, const int, const int, 93 const int); 94 static timestamp now(void); 95 96 std::string strftime(const std::string&) const; 97 int64_t to_microseconds(void) const; 98 int64_t to_seconds(void) const; 99 100 bool operator==(const timestamp&) const; 101 bool operator!=(const timestamp&) const; 102 103 delta operator-(const timestamp&) const; 104}; 105 106 107std::ostream& operator<<(std::ostream&, const timestamp&); 108 109 110void set_mock_now(const int, const int, const int, const int, const int, 111 const int, const int); 112 113 114} // namespace datetime 115} // namespace utils 116 117#endif // !defined(UTILS_DATETIME_HPP) 118