Libosmium  2.1.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 
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  template <typename T>
117  void operator+=(T time_difference) noexcept {
118  m_timestamp += time_difference;
119  }
120 
121  template <typename T>
122  void operator-=(T time_difference) noexcept {
123  m_timestamp -= time_difference;
124  }
125 
129  std::string to_iso() const {
130  std::string s;
131 
132  if (m_timestamp != 0) {
133  struct tm tm;
134  time_t sse = seconds_since_epoch();
135 #ifndef _MSC_VER
136  gmtime_r(&sse, &tm);
137 #else
138  gmtime_s(&tm, &sse);
139 #endif
140 
141  s.resize(timestamp_length);
142  /* This const_cast is ok, because we know we have enough space
143  in the string for the format we are using (well at least until
144  the year will have 5 digits). And by setting the size
145  afterwards from the result of strftime we make sure thats set
146  right, too. */
147  s.resize(strftime(const_cast<char*>(s.c_str()), timestamp_length, timestamp_format(), &tm));
148  }
149 
150  return s;
151  }
152 
153  }; // class Timestamp
154 
156  return Timestamp(1);
157  }
158 
160  return Timestamp(std::numeric_limits<time_t>::max());
161  }
162 
163  template <typename TChar, typename TTraits>
164  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, Timestamp timestamp) {
165  out << timestamp.to_iso();
166  return out;
167  }
168 
169 } // namespace osmium
170 
171 #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:155
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:159
void operator-=(T time_difference) noexcept
Definition: timestamp.hpp:122
#define OSMIUM_CONSTEXPR
Definition: compatibility.hpp:43
constexpr Timestamp() noexcept
Definition: timestamp.hpp:70
void operator+=(T time_difference) noexcept
Definition: timestamp.hpp:117
std::string to_iso() const
Definition: timestamp.hpp:129
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