Libosmium  2.3.0
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 
52  class Timestamp {
53 
54  // length of ISO timestamp string yyyy-mm-ddThh:mm:ssZ\0
55  static constexpr int timestamp_length = 20 + 1;
56 
61  static const char* timestamp_format() {
62  static const char f[timestamp_length] = "%Y-%m-%dT%H:%M:%SZ";
63  return f;
64  }
65 
66  uint32_t m_timestamp;
67 
68  public:
69 
70  constexpr Timestamp() noexcept :
71  m_timestamp(0) {
72  }
73 
74  // Not "explicit" so that conversions from time_t work
75  // like in node.timestamp(123);
76  constexpr Timestamp(time_t timestamp) noexcept :
77  m_timestamp(static_cast<uint32_t>(timestamp)) {
78  }
79 
84  explicit Timestamp(const char* timestamp) {
85 #ifndef _WIN32
86  struct tm tm {
87  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
88  };
89  if (strptime(timestamp, timestamp_format(), &tm) == nullptr) {
90  throw std::invalid_argument("can't parse timestamp");
91  }
92  m_timestamp = static_cast<uint32_t>(timegm(&tm));
93 #else
94  struct tm tm;
95  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);
96  if (n != 6) {
97  throw std::invalid_argument("can't parse timestamp");
98  }
99  tm.tm_year -= 1900;
100  tm.tm_mon--;
101  tm.tm_wday = 0;
102  tm.tm_yday = 0;
103  tm.tm_isdst = 0;
104  m_timestamp = static_cast<uint32_t>(_mkgmtime(&tm));
105 #endif
106  }
107 
108  constexpr time_t seconds_since_epoch() const noexcept {
109  return static_cast<time_t>(m_timestamp);
110  }
111 
112  constexpr operator time_t() const noexcept {
113  return static_cast<time_t>(m_timestamp);
114  }
115 
116  explicit constexpr operator uint32_t() const noexcept {
117  return m_timestamp;
118  }
119 
120  template <typename T>
121  void operator+=(T time_difference) noexcept {
122  m_timestamp += time_difference;
123  }
124 
125  template <typename T>
126  void operator-=(T time_difference) noexcept {
127  m_timestamp -= time_difference;
128  }
129 
133  std::string to_iso() const {
134  std::string s;
135 
136  if (m_timestamp != 0) {
137  struct tm tm;
138  time_t sse = seconds_since_epoch();
139 #ifndef _MSC_VER
140  gmtime_r(&sse, &tm);
141 #else
142  gmtime_s(&tm, &sse);
143 #endif
144 
145  s.resize(timestamp_length);
146  /* This const_cast is ok, because we know we have enough space
147  in the string for the format we are using (well at least until
148  the year will have 5 digits). And by setting the size
149  afterwards from the result of strftime we make sure thats set
150  right, too. */
151  s.resize(strftime(const_cast<char*>(s.c_str()), timestamp_length, timestamp_format(), &tm));
152  }
153 
154  return s;
155  }
156 
157  }; // class Timestamp
158 
160  return Timestamp(1);
161  }
162 
164  return Timestamp(std::numeric_limits<time_t>::max());
165  }
166 
167  template <typename TChar, typename TTraits>
168  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, Timestamp timestamp) {
169  out << timestamp.to_iso();
170  return out;
171  }
172 
173  template <>
174  inline osmium::Timestamp min_op_start_value<osmium::Timestamp>() {
175  return end_of_time();
176  }
177 
178  template <>
179  inline osmium::Timestamp max_op_start_value<osmium::Timestamp>() {
180  return start_of_time();
181  }
182 
183 } // namespace osmium
184 
185 #endif // OSMIUM_OSM_TIMESTAMP_HPP
constexpr time_t seconds_since_epoch() const noexcept
Definition: timestamp.hpp:108
static constexpr int timestamp_length
Definition: timestamp.hpp:55
OSMIUM_CONSTEXPR Timestamp start_of_time() noexcept
Definition: timestamp.hpp:159
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
Definition: timestamp.hpp:52
OSMIUM_CONSTEXPR Timestamp end_of_time() noexcept
Definition: timestamp.hpp:163
void operator-=(T time_difference) noexcept
Definition: timestamp.hpp:126
#define OSMIUM_CONSTEXPR
Definition: compatibility.hpp:43
constexpr Timestamp() noexcept
Definition: timestamp.hpp:70
void operator+=(T time_difference) noexcept
Definition: timestamp.hpp:121
std::string to_iso() const
Definition: timestamp.hpp:133
Timestamp(const char *timestamp)
Definition: timestamp.hpp:84
uint32_t m_timestamp
Definition: timestamp.hpp:66
constexpr Timestamp(time_t timestamp) noexcept
Definition: timestamp.hpp:76
static const char * timestamp_format()
Definition: timestamp.hpp:61