Libosmium  2.5.4
Fast and flexible C++ library for working with OpenStreetMap data
timestamp.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_OSM_TIMESTAMP_HPP
2 #define OSMIUM_OSM_TIMESTAMP_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2015 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <cstdint>
37 #include <ctime>
38 #include <iosfwd>
39 #include <limits>
40 #include <stdexcept>
41 #include <string>
42 
44 #include <osmium/util/minmax.hpp> // IWYU pragma: keep
45 
46 namespace osmium {
47 
55  class Timestamp {
56 
57  // length of ISO timestamp string yyyy-mm-ddThh:mm:ssZ\0
58  static constexpr int timestamp_length = 20 + 1;
59 
60  // The timestamp format for OSM timestamps in strftime(3) format.
61  // This is the ISO-Format "yyyy-mm-ddThh:mm:ssZ".
62  static const char* timestamp_format() {
63  static const char f[timestamp_length] = "%Y-%m-%dT%H:%M:%SZ";
64  return f;
65  }
66 
67  uint32_t m_timestamp;
68 
69  public:
70 
74  constexpr Timestamp() noexcept :
75  m_timestamp(0) {
76  }
77 
87  template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
88  constexpr Timestamp(T timestamp) noexcept :
89  m_timestamp(uint32_t(timestamp)) {
90  }
91 
98  explicit Timestamp(const char* timestamp) {
99 #ifndef _WIN32
100  struct tm tm {
101  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
102  };
103  if (strptime(timestamp, timestamp_format(), &tm) == nullptr) {
104  throw std::invalid_argument("can't parse timestamp");
105  }
106  m_timestamp = static_cast<uint32_t>(timegm(&tm));
107 #else
108  struct tm tm;
109  int n = sscanf(timestamp, "%4d-%2d-%2dT%2d:%2d:%2dZ", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
110  if (n != 6) {
111  throw std::invalid_argument("can't parse timestamp");
112  }
113  tm.tm_year -= 1900;
114  tm.tm_mon--;
115  tm.tm_wday = 0;
116  tm.tm_yday = 0;
117  tm.tm_isdst = 0;
118  m_timestamp = static_cast<uint32_t>(_mkgmtime(&tm));
119 #endif
120  }
121 
128  explicit Timestamp(const std::string& timestamp) :
129  Timestamp(timestamp.c_str()) {
130  }
131 
136  bool valid() const noexcept {
137  return m_timestamp != 0;
138  }
139 
141  explicit constexpr operator bool() const noexcept {
142  return m_timestamp != 0;
143  }
144 
146  constexpr time_t seconds_since_epoch() const noexcept {
147  return time_t(m_timestamp);
148  }
149 
151  explicit constexpr operator uint32_t() const noexcept {
152  return uint32_t(m_timestamp);
153  }
154 
156  explicit constexpr operator uint64_t() const noexcept {
157  return uint64_t(m_timestamp);
158  }
159 
165  OSMIUM_DEPRECATED constexpr operator time_t() const noexcept {
166  return static_cast<time_t>(m_timestamp);
167  }
168 
169  template <typename T>
170  void operator+=(T time_difference) noexcept {
171  m_timestamp += time_difference;
172  }
173 
174  template <typename T>
175  void operator-=(T time_difference) noexcept {
176  m_timestamp -= time_difference;
177  }
178 
183  std::string to_iso() const {
184  std::string s;
185 
186  if (m_timestamp != 0) {
187  struct tm tm;
188  time_t sse = seconds_since_epoch();
189 #ifndef _MSC_VER
190  gmtime_r(&sse, &tm);
191 #else
192  gmtime_s(&tm, &sse);
193 #endif
194 
195  s.resize(timestamp_length);
196  /* This const_cast is ok, because we know we have enough space
197  in the string for the format we are using (well at least until
198  the year will have 5 digits). And by setting the size
199  afterwards from the result of strftime we make sure thats set
200  right, too. */
201  s.resize(strftime(const_cast<char*>(s.c_str()), timestamp_length, timestamp_format(), &tm));
202  }
203 
204  return s;
205  }
206 
207  }; // class Timestamp
208 
213  inline constexpr Timestamp start_of_time() noexcept {
214  return Timestamp(1);
215  }
216 
221  inline constexpr Timestamp end_of_time() noexcept {
222  return Timestamp(std::numeric_limits<uint32_t>::max());
223  }
224 
225  template <typename TChar, typename TTraits>
226  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, Timestamp timestamp) {
227  out << timestamp.to_iso();
228  return out;
229  }
230 
231  inline bool operator==(const Timestamp& lhs, const Timestamp& rhs) noexcept {
232  return uint32_t(lhs) == uint32_t(rhs);
233  }
234 
235  inline bool operator!=(const Timestamp& lhs, const Timestamp& rhs) noexcept {
236  return !(lhs == rhs);
237  }
238 
239  inline bool operator<(const Timestamp& lhs, const Timestamp& rhs) noexcept {
240  return uint32_t(lhs) < uint32_t(rhs);
241  }
242 
243  inline bool operator>(const Timestamp& lhs, const Timestamp& rhs) noexcept {
244  return rhs < lhs;
245  }
246 
247  inline bool operator<=(const Timestamp& lhs, const Timestamp& rhs) noexcept {
248  return ! (rhs < lhs);
249  }
250 
251  inline bool operator>=(const Timestamp& lhs, const Timestamp& rhs) noexcept {
252  return ! (lhs < rhs);
253  }
254 
255  template <>
256  inline osmium::Timestamp min_op_start_value<osmium::Timestamp>() {
257  return end_of_time();
258  }
259 
260  template <>
261  inline osmium::Timestamp max_op_start_value<osmium::Timestamp>() {
262  return start_of_time();
263  }
264 
265 } // namespace osmium
266 
267 #endif // OSMIUM_OSM_TIMESTAMP_HPP
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
type
Definition: entity_bits.hpp:60
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:446
constexpr Timestamp start_of_time() noexcept
Definition: timestamp.hpp:213
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:219
static constexpr int timestamp_length
Definition: timestamp.hpp:58
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:438
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
Definition: timestamp.hpp:55
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:450
void operator-=(T time_difference) noexcept
Definition: timestamp.hpp:175
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:442
constexpr Timestamp() noexcept
Definition: timestamp.hpp:74
Timestamp(const std::string &timestamp)
Definition: timestamp.hpp:128
void operator+=(T time_difference) noexcept
Definition: timestamp.hpp:170
constexpr Timestamp(T timestamp) noexcept
Definition: timestamp.hpp:88
std::string to_iso() const
Definition: timestamp.hpp:183
constexpr Timestamp end_of_time() noexcept
Definition: timestamp.hpp:221
Timestamp(const char *timestamp)
Definition: timestamp.hpp:98
uint32_t m_timestamp
Definition: timestamp.hpp:67
bool valid() const noexcept
Definition: timestamp.hpp:136
constexpr time_t seconds_since_epoch() const noexcept
Explicit conversion into time_t.
Definition: timestamp.hpp:146
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:431
static const char * timestamp_format()
Definition: timestamp.hpp:62