1 // Copyright 2010 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
29 /// \file utils/datetime.hpp
30 /// Provides date and time-related classes and functions.
32 #if !defined(UTILS_DATETIME_HPP)
33 #define UTILS_DATETIME_HPP
35 #include "utils/datetime_fwd.hpp"
51 /// Represents a time delta to describe deadlines.
53 /// Because we use this class to handle deadlines, we currently do not support
57 /// The amount of seconds in the time delta.
60 /// The amount of microseconds in the time delta.
61 unsigned long useconds;
64 delta(const int64_t, const unsigned long);
66 static delta from_microseconds(const int64_t);
67 int64_t to_microseconds(void) const;
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;
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);
85 std::ostream& operator<<(std::ostream&, const delta&);
88 /// Represents a fixed date/time.
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.
97 /// Pointer to the shared internal implementation.
98 std::shared_ptr< impl > _pimpl;
100 timestamp(std::shared_ptr< impl >);
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,
107 static timestamp now(void);
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;
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;
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;
129 std::ostream& operator<<(std::ostream&, const timestamp&);
132 void set_mock_now(const int, const int, const int, const int, const int,
133 const int, const int);
134 void set_mock_now(const timestamp&);
137 } // namespace datetime
140 #endif // !defined(UTILS_DATETIME_HPP)