Libosmium  2.15.1
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 (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2019 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 
37 #include <osmium/util/minmax.hpp> // IWYU pragma: keep
38 
39 #include <cassert>
40 #include <cstdint>
41 #include <ctime>
42 #include <iosfwd>
43 #include <limits>
44 #include <stdexcept>
45 #include <string>
46 #include <type_traits>
47 
48 namespace osmium {
49 
50  namespace detail {
51 
52  inline void add_2digit_int_to_string(int value, std::string& out) {
53  assert(value >= 0 && value <= 99);
54  if (value > 9) {
55  const int dec = value / 10;
56  out += static_cast<char>('0' + dec);
57  value -= dec * 10;
58  } else {
59  out += '0';
60  }
61  out += static_cast<char>('0' + value);
62  }
63 
64  inline void add_4digit_int_to_string(int value, std::string& out) {
65  assert(value >= 1000 && value <= 9999);
66 
67  const int dec1 = value / 1000;
68  out += static_cast<char>('0' + dec1);
69  value -= dec1 * 1000;
70 
71  const int dec2 = value / 100;
72  out += static_cast<char>('0' + dec2);
73  value -= dec2 * 100;
74 
75  const int dec3 = value / 10;
76  out += static_cast<char>('0' + dec3);
77  value -= dec3 * 10;
78 
79  out += static_cast<char>('0' + value);
80  }
81 
82  inline time_t parse_timestamp(const char* str) {
83  static const int mon_lengths[] = {
84  31, 29, 31, 30, 31, 30,
85  31, 31, 30, 31, 30, 31
86  };
87  if (str[ 0] >= '0' && str[ 0] <= '9' &&
88  str[ 1] >= '0' && str[ 1] <= '9' &&
89  str[ 2] >= '0' && str[ 2] <= '9' &&
90  str[ 3] >= '0' && str[ 3] <= '9' &&
91  str[ 4] == '-' &&
92  str[ 5] >= '0' && str[ 5] <= '9' &&
93  str[ 6] >= '0' && str[ 6] <= '9' &&
94  str[ 7] == '-' &&
95  str[ 8] >= '0' && str[ 8] <= '9' &&
96  str[ 9] >= '0' && str[ 9] <= '9' &&
97  str[10] == 'T' &&
98  str[11] >= '0' && str[11] <= '9' &&
99  str[12] >= '0' && str[12] <= '9' &&
100  str[13] == ':' &&
101  str[14] >= '0' && str[14] <= '9' &&
102  str[15] >= '0' && str[15] <= '9' &&
103  str[16] == ':' &&
104  str[17] >= '0' && str[17] <= '9' &&
105  str[18] >= '0' && str[18] <= '9' &&
106  str[19] == 'Z') {
107  std::tm tm; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
108  tm.tm_year = (str[ 0] - '0') * 1000 +
109  (str[ 1] - '0') * 100 +
110  (str[ 2] - '0') * 10 +
111  (str[ 3] - '0') - 1900;
112  tm.tm_mon = (str[ 5] - '0') * 10 + (str[ 6] - '0') - 1;
113  tm.tm_mday = (str[ 8] - '0') * 10 + (str[ 9] - '0');
114  tm.tm_hour = (str[11] - '0') * 10 + (str[12] - '0');
115  tm.tm_min = (str[14] - '0') * 10 + (str[15] - '0');
116  tm.tm_sec = (str[17] - '0') * 10 + (str[18] - '0');
117  tm.tm_wday = 0;
118  tm.tm_yday = 0;
119  tm.tm_isdst = 0;
120  if (tm.tm_year >= 0 &&
121  tm.tm_mon >= 0 && tm.tm_mon <= 11 &&
122  tm.tm_mday >= 1 && tm.tm_mday <= mon_lengths[tm.tm_mon] &&
123  tm.tm_hour >= 0 && tm.tm_hour <= 23 &&
124  tm.tm_min >= 0 && tm.tm_min <= 59 &&
125  tm.tm_sec >= 0 && tm.tm_sec <= 60) {
126 #ifndef _WIN32
127  return timegm(&tm);
128 #else
129  return _mkgmtime(&tm);
130 #endif
131  }
132  }
133  throw std::invalid_argument{"can not parse timestamp"};
134  }
135 
136  } // namespace detail
137 
145  class Timestamp {
146 
147  uint32_t m_timestamp = 0;
148 
149  void to_iso_str(std::string& s) const {
150  std::tm tm; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
151  time_t sse = seconds_since_epoch();
152 #ifndef NDEBUG
153  auto result =
154 #endif
155 #ifndef _WIN32
156  gmtime_r(&sse, &tm);
157  assert(result != nullptr);
158 #else
159  gmtime_s(&tm, &sse);
160  assert(result == 0);
161 #endif
162 
163  detail::add_4digit_int_to_string(tm.tm_year + 1900, s);
164  s += '-';
165  detail::add_2digit_int_to_string(tm.tm_mon + 1, s);
166  s += '-';
167  detail::add_2digit_int_to_string(tm.tm_mday, s);
168  s += 'T';
169  detail::add_2digit_int_to_string(tm.tm_hour, s);
170  s += ':';
171  detail::add_2digit_int_to_string(tm.tm_min, s);
172  s += ':';
173  detail::add_2digit_int_to_string(tm.tm_sec, s);
174  s += 'Z';
175  }
176 
177  public:
178 
182  constexpr Timestamp() noexcept = default;
183 
193  template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
194  constexpr Timestamp(T timestamp) noexcept : // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
195  m_timestamp(uint32_t(timestamp)) {
196  }
197 
204  explicit Timestamp(const char* timestamp) {
205  m_timestamp = static_cast<uint32_t>(detail::parse_timestamp(timestamp));
206  }
207 
214  explicit Timestamp(const std::string& timestamp) :
215  Timestamp(timestamp.c_str()) {
216  }
217 
222  bool valid() const noexcept {
223  return m_timestamp != 0;
224  }
225 
227  explicit constexpr operator bool() const noexcept {
228  return m_timestamp != 0;
229  }
230 
232  constexpr time_t seconds_since_epoch() const noexcept {
233  return time_t(m_timestamp);
234  }
235 
237  explicit constexpr operator uint32_t() const noexcept {
238  return uint32_t(m_timestamp);
239  }
240 
242  explicit constexpr operator uint64_t() const noexcept {
243  return uint64_t(m_timestamp);
244  }
245 
251  OSMIUM_DEPRECATED constexpr operator time_t() const noexcept { // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
252  return static_cast<time_t>(m_timestamp);
253  }
254 
255  template <typename T>
256  void operator+=(T time_difference) noexcept {
257  m_timestamp += time_difference;
258  }
259 
260  template <typename T>
261  void operator-=(T time_difference) noexcept {
262  m_timestamp -= time_difference;
263  }
264 
270  std::string to_iso() const {
271  std::string s;
272 
273  if (m_timestamp != 0) {
274  to_iso_str(s);
275  }
276 
277  return s;
278  }
279 
285  std::string to_iso_all() const {
286  std::string s;
287 
288  to_iso_str(s);
289 
290  return s;
291  }
292 
293  }; // class Timestamp
294 
299  inline constexpr Timestamp start_of_time() noexcept {
300  return {1};
301  }
302 
307  inline constexpr Timestamp end_of_time() noexcept {
308  return {std::numeric_limits<uint32_t>::max()};
309  }
310 
311  template <typename TChar, typename TTraits>
312  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, Timestamp timestamp) {
313  out << timestamp.to_iso();
314  return out;
315  }
316 
317  inline bool operator==(const Timestamp& lhs, const Timestamp& rhs) noexcept {
318  return uint32_t(lhs) == uint32_t(rhs);
319  }
320 
321  inline bool operator!=(const Timestamp& lhs, const Timestamp& rhs) noexcept {
322  return !(lhs == rhs);
323  }
324 
325  inline bool operator<(const Timestamp& lhs, const Timestamp& rhs) noexcept {
326  return uint32_t(lhs) < uint32_t(rhs);
327  }
328 
329  inline bool operator>(const Timestamp& lhs, const Timestamp& rhs) noexcept {
330  return rhs < lhs;
331  }
332 
333  inline bool operator<=(const Timestamp& lhs, const Timestamp& rhs) noexcept {
334  return !(rhs < lhs);
335  }
336 
337  inline bool operator>=(const Timestamp& lhs, const Timestamp& rhs) noexcept {
338  return !(lhs < rhs);
339  }
340 
341  template <>
342  inline osmium::Timestamp min_op_start_value<osmium::Timestamp>() {
343  return end_of_time();
344  }
345 
346  template <>
347  inline osmium::Timestamp max_op_start_value<osmium::Timestamp>() {
348  return start_of_time();
349  }
350 
351 } // namespace osmium
352 
353 #endif // OSMIUM_OSM_TIMESTAMP_HPP
osmium::Timestamp::valid
bool valid() const noexcept
Definition: timestamp.hpp:222
minmax.hpp
osmium::Timestamp::m_timestamp
uint32_t m_timestamp
Definition: timestamp.hpp:147
OSMIUM_DEPRECATED
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
osmium::Timestamp::Timestamp
Timestamp(const char *timestamp)
Definition: timestamp.hpp:204
osmium::Timestamp::to_iso_str
void to_iso_str(std::string &s) const
Definition: timestamp.hpp:149
detail
Definition: attr.hpp:333
osmium::operator<=
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:461
osmium::operator>=
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:465
osmium::operator>
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:457
osmium::Timestamp::operator+=
void operator+=(T time_difference) noexcept
Definition: timestamp.hpp:256
osmium::Timestamp::to_iso_all
std::string to_iso_all() const
Definition: timestamp.hpp:285
osmium::Timestamp::Timestamp
constexpr Timestamp() noexcept=default
compatibility.hpp
osmium
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
osmium::start_of_time
constexpr Timestamp start_of_time() noexcept
Definition: timestamp.hpp:299
osmium::operator==
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:212
osmium::Timestamp::seconds_since_epoch
constexpr time_t seconds_since_epoch() const noexcept
Explicit conversion into time_t.
Definition: timestamp.hpp:232
osmium::operator<
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:453
osmium::Timestamp
Definition: timestamp.hpp:145
osmium::Timestamp::Timestamp
Timestamp(const std::string &timestamp)
Definition: timestamp.hpp:214
osmium::Timestamp::operator-=
void operator-=(T time_difference) noexcept
Definition: timestamp.hpp:261
osmium::operator<<
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const osmium::Box &box)
Definition: box.hpp:224
std
Definition: location.hpp:550
osmium::Timestamp::to_iso
std::string to_iso() const
Definition: timestamp.hpp:270
osmium::osm_entity_bits::type
type
Definition: entity_bits.hpp:63
osmium::end_of_time
constexpr Timestamp end_of_time() noexcept
Definition: timestamp.hpp:307
osmium::operator!=
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:446