Libosmium  2.7.2
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2016 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 <cassert>
37 #include <cstdint>
38 #include <ctime>
39 #include <iosfwd>
40 #include <limits>
41 #include <stdexcept>
42 #include <string>
43 
45 #include <osmium/util/minmax.hpp> // IWYU pragma: keep
46 
47 namespace osmium {
48 
56  class Timestamp {
57 
58  // length of ISO timestamp string yyyy-mm-ddThh:mm:ssZ\0
59  static constexpr int timestamp_length = 20 + 1;
60 
61  // The timestamp format for OSM timestamps in strftime(3) format.
62  // This is the ISO-Format "yyyy-mm-ddThh:mm:ssZ".
63  static const char* timestamp_format() {
64  static const char f[timestamp_length] = "%Y-%m-%dT%H:%M:%SZ";
65  return f;
66  }
67 
68  uint32_t m_timestamp;
69 
70  public:
71 
75  constexpr Timestamp() noexcept :
76  m_timestamp(0) {
77  }
78 
88  template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
89  constexpr Timestamp(T timestamp) noexcept :
90  m_timestamp(uint32_t(timestamp)) {
91  }
92 
99  explicit Timestamp(const char* timestamp) {
100 #ifndef _WIN32
101  struct tm tm {
102  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
103  };
104  if (strptime(timestamp, timestamp_format(), &tm) == nullptr) {
105  throw std::invalid_argument("can't parse timestamp");
106  }
107  m_timestamp = static_cast<uint32_t>(timegm(&tm));
108 #else
109  struct tm tm;
110  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);
111  if (n != 6) {
112  throw std::invalid_argument("can't parse timestamp");
113  }
114  tm.tm_year -= 1900;
115  tm.tm_mon--;
116  tm.tm_wday = 0;
117  tm.tm_yday = 0;
118  tm.tm_isdst = 0;
119  m_timestamp = static_cast<uint32_t>(_mkgmtime(&tm));
120 #endif
121  }
122 
129  explicit Timestamp(const std::string& timestamp) :
130  Timestamp(timestamp.c_str()) {
131  }
132 
137  bool valid() const noexcept {
138  return m_timestamp != 0;
139  }
140 
142  explicit constexpr operator bool() const noexcept {
143  return m_timestamp != 0;
144  }
145 
147  constexpr time_t seconds_since_epoch() const noexcept {
148  return time_t(m_timestamp);
149  }
150 
152  explicit constexpr operator uint32_t() const noexcept {
153  return uint32_t(m_timestamp);
154  }
155 
157  explicit constexpr operator uint64_t() const noexcept {
158  return uint64_t(m_timestamp);
159  }
160 
166  OSMIUM_DEPRECATED constexpr operator time_t() const noexcept {
167  return static_cast<time_t>(m_timestamp);
168  }
169 
170  template <typename T>
171  void operator+=(T time_difference) noexcept {
172  m_timestamp += time_difference;
173  }
174 
175  template <typename T>
176  void operator-=(T time_difference) noexcept {
177  m_timestamp -= time_difference;
178  }
179 
185  std::string to_iso() const {
186  std::string s;
187 
188  if (m_timestamp != 0) {
189  struct tm tm;
190  time_t sse = seconds_since_epoch();
191 #ifndef NDEBUG
192  auto result =
193 #endif
194 #ifndef _MSC_VER
195  gmtime_r(&sse, &tm);
196  assert(result != nullptr);
197 #else
198  gmtime_s(&tm, &sse);
199  assert(result == 0);
200 #endif
201 
202  s.resize(timestamp_length);
203  /* This const_cast is ok, because we know we have enough space
204  in the string for the format we are using (well at least until
205  the year will have 5 digits). And by setting the size
206  afterwards from the result of strftime we make sure thats set
207  right, too. */
208  s.resize(strftime(const_cast<char*>(s.c_str()), timestamp_length, timestamp_format(), &tm));
209  }
210 
211  return s;
212  }
213 
214  }; // class Timestamp
215 
220  inline constexpr Timestamp start_of_time() noexcept {
221  return Timestamp(1);
222  }
223 
228  inline constexpr Timestamp end_of_time() noexcept {
229  return Timestamp(std::numeric_limits<uint32_t>::max());
230  }
231 
232  template <typename TChar, typename TTraits>
233  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, Timestamp timestamp) {
234  out << timestamp.to_iso();
235  return out;
236  }
237 
238  inline bool operator==(const Timestamp& lhs, const Timestamp& rhs) noexcept {
239  return uint32_t(lhs) == uint32_t(rhs);
240  }
241 
242  inline bool operator!=(const Timestamp& lhs, const Timestamp& rhs) noexcept {
243  return !(lhs == rhs);
244  }
245 
246  inline bool operator<(const Timestamp& lhs, const Timestamp& rhs) noexcept {
247  return uint32_t(lhs) < uint32_t(rhs);
248  }
249 
250  inline bool operator>(const Timestamp& lhs, const Timestamp& rhs) noexcept {
251  return rhs < lhs;
252  }
253 
254  inline bool operator<=(const Timestamp& lhs, const Timestamp& rhs) noexcept {
255  return ! (rhs < lhs);
256  }
257 
258  inline bool operator>=(const Timestamp& lhs, const Timestamp& rhs) noexcept {
259  return ! (lhs < rhs);
260  }
261 
262  template <>
263  inline osmium::Timestamp min_op_start_value<osmium::Timestamp>() {
264  return end_of_time();
265  }
266 
267  template <>
268  inline osmium::Timestamp max_op_start_value<osmium::Timestamp>() {
269  return start_of_time();
270  }
271 
272 } // namespace osmium
273 
274 #endif // OSMIUM_OSM_TIMESTAMP_HPP
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
type
Definition: entity_bits.hpp:63
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:446
constexpr Timestamp start_of_time() noexcept
Definition: timestamp.hpp:220
constexpr time_t seconds_since_epoch() const noexcept
Explicit conversion into time_t.
Definition: timestamp.hpp:147
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:222
static constexpr int timestamp_length
Definition: timestamp.hpp:59
bool valid() const noexcept
Definition: timestamp.hpp:137
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:438
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
Definition: timestamp.hpp:56
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:450
void operator-=(T time_difference) noexcept
Definition: timestamp.hpp:176
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:442
constexpr Timestamp() noexcept
Definition: timestamp.hpp:75
Timestamp(const std::string &timestamp)
Definition: timestamp.hpp:129
void operator+=(T time_difference) noexcept
Definition: timestamp.hpp:171
constexpr Timestamp(T timestamp) noexcept
Definition: timestamp.hpp:89
std::string to_iso() const
Definition: timestamp.hpp:185
constexpr Timestamp end_of_time() noexcept
Definition: timestamp.hpp:228
Timestamp(const char *timestamp)
Definition: timestamp.hpp:99
uint32_t m_timestamp
Definition: timestamp.hpp:68
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:431
static const char * timestamp_format()
Definition: timestamp.hpp:63