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