1 #ifndef OSMIUM_OSM_TIMESTAMP_HPP 2 #define OSMIUM_OSM_TIMESTAMP_HPP 46 #include <type_traits> 52 inline void add_2digit_int_to_string(
int value, std::string& out) {
53 assert(value >= 0 && value <= 99);
55 const int dec = value / 10;
56 out +=
static_cast<char>(
'0' + dec);
61 out +=
static_cast<char>(
'0' + value);
64 inline void add_4digit_int_to_string(
int value, std::string& out) {
65 assert(value >= 1000 && value <= 9999);
67 const int dec1 = value / 1000;
68 out +=
static_cast<char>(
'0' + dec1);
71 const int dec2 = value / 100;
72 out +=
static_cast<char>(
'0' + dec2);
75 const int dec3 = value / 10;
76 out +=
static_cast<char>(
'0' + dec3);
79 out +=
static_cast<char>(
'0' + value);
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
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' &&
92 str[ 5] >=
'0' && str[ 5] <=
'9' &&
93 str[ 6] >=
'0' && str[ 6] <=
'9' &&
95 str[ 8] >=
'0' && str[ 8] <=
'9' &&
96 str[ 9] >=
'0' && str[ 9] <=
'9' &&
98 str[11] >=
'0' && str[11] <=
'9' &&
99 str[12] >=
'0' && str[12] <=
'9' &&
101 str[14] >=
'0' && str[14] <=
'9' &&
102 str[15] >=
'0' && str[15] <=
'9' &&
104 str[17] >=
'0' && str[17] <=
'9' &&
105 str[18] >=
'0' && str[18] <=
'9' &&
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');
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) {
129 return _mkgmtime(&tm);
133 throw std::invalid_argument{
"can not parse timestamp"};
147 uint32_t m_timestamp = 0;
151 time_t sse = seconds_since_epoch();
157 assert(result !=
nullptr);
163 detail::add_4digit_int_to_string(tm.tm_year + 1900, s);
165 detail::add_2digit_int_to_string(tm.tm_mon + 1, s);
167 detail::add_2digit_int_to_string(tm.tm_mday, s);
169 detail::add_2digit_int_to_string(tm.tm_hour, s);
171 detail::add_2digit_int_to_string(tm.tm_min, s);
173 detail::add_2digit_int_to_string(tm.tm_sec, s);
182 constexpr
Timestamp() noexcept =
default;
193 template <typename T, typename std::enable_if<std::is_integral<T>::value,
int>
::type = 0>
195 m_timestamp(uint32_t(timestamp)) {
205 m_timestamp =
static_cast<uint32_t
>(detail::parse_timestamp(timestamp));
223 return m_timestamp != 0;
227 explicit constexpr
operator bool() const noexcept {
228 return m_timestamp != 0;
233 return time_t(m_timestamp);
237 explicit constexpr
operator uint32_t() const noexcept {
238 return uint32_t(m_timestamp);
242 explicit constexpr
operator uint64_t() const noexcept {
243 return uint64_t(m_timestamp);
252 return static_cast<time_t
>(m_timestamp);
255 template <
typename T>
257 m_timestamp += time_difference;
260 template <
typename T>
262 m_timestamp -= time_difference;
273 if (m_timestamp != 0) {
308 return {std::numeric_limits<uint32_t>::max()};
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();
318 return uint32_t(lhs) == uint32_t(rhs);
322 return !(lhs == rhs);
326 return uint32_t(lhs) < uint32_t(rhs);
353 #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:461
constexpr Timestamp start_of_time() noexcept
Definition: timestamp.hpp:299
constexpr time_t seconds_since_epoch() const noexcept
Explicit conversion into time_t.
Definition: timestamp.hpp:232
void to_iso_str(std::string &s) const
Definition: timestamp.hpp:149
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:212
bool valid() const noexcept
Definition: timestamp.hpp:222
std::string to_iso() const
Definition: timestamp.hpp:270
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:453
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: timestamp.hpp:145
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:465
void operator-=(T time_difference) noexcept
Definition: timestamp.hpp:261
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:457
Timestamp(const std::string ×tamp)
Definition: timestamp.hpp:214
void operator+=(T time_difference) noexcept
Definition: timestamp.hpp:256
constexpr Timestamp(T timestamp) noexcept
Definition: timestamp.hpp:194
constexpr Timestamp end_of_time() noexcept
Definition: timestamp.hpp:307
Timestamp(const char *timestamp)
Definition: timestamp.hpp:204
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:446
std::string to_iso_all() const
Definition: timestamp.hpp:285