1#ifndef OSMIUM_OSM_TIMESTAMP_HPP
2#define OSMIUM_OSM_TIMESTAMP_HPP
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 std::array<int, 12> mon_lengths = {{
84 31, 29, 31, 30, 31, 30,
85 31, 31, 30, 31, 30, 31
88 if (str[ 0] >=
'0' && str[ 0] <=
'9' &&
89 str[ 1] >=
'0' && str[ 1] <=
'9' &&
90 str[ 2] >=
'0' && str[ 2] <=
'9' &&
91 str[ 3] >=
'0' && str[ 3] <=
'9' &&
93 str[ 5] >=
'0' && str[ 5] <=
'9' &&
94 str[ 6] >=
'0' && str[ 6] <=
'9' &&
96 str[ 8] >=
'0' && str[ 8] <=
'9' &&
97 str[ 9] >=
'0' && str[ 9] <=
'9' &&
99 str[11] >=
'0' && str[11] <=
'9' &&
100 str[12] >=
'0' && str[12] <=
'9' &&
102 str[14] >=
'0' && str[14] <=
'9' &&
103 str[15] >=
'0' && str[15] <=
'9' &&
105 str[17] >=
'0' && str[17] <=
'9' &&
106 str[18] >=
'0' && str[18] <=
'9' &&
109 tm.tm_year = (str[ 0] -
'0') * 1000 +
110 (str[ 1] -
'0') * 100 +
111 (str[ 2] -
'0') * 10 +
112 (str[ 3] -
'0') - 1900;
113 tm.tm_mon = (str[ 5] -
'0') * 10 + (str[ 6] -
'0') - 1;
114 tm.tm_mday = (str[ 8] -
'0') * 10 + (str[ 9] -
'0');
115 tm.tm_hour = (str[11] -
'0') * 10 + (str[12] -
'0');
116 tm.tm_min = (str[14] -
'0') * 10 + (str[15] -
'0');
117 tm.tm_sec = (str[17] -
'0') * 10 + (str[18] -
'0');
121 if (tm.tm_year >= 0 &&
122 tm.tm_mon >= 0 && tm.tm_mon <= 11 &&
123 tm.tm_mday >= 1 && tm.tm_mday <= mon_lengths[tm.tm_mon] &&
124 tm.tm_hour >= 0 && tm.tm_hour <= 23 &&
125 tm.tm_min >= 0 && tm.tm_min <= 59 &&
126 tm.tm_sec >= 0 && tm.tm_sec <= 60) {
130 return _mkgmtime(&tm);
134 throw std::invalid_argument{std::string{
"can not parse timestamp: '"} + str +
"'"};
158 assert(result !=
nullptr);
164 detail::add_4digit_int_to_string(tm.tm_year + 1900, s);
166 detail::add_2digit_int_to_string(tm.tm_mon + 1, s);
168 detail::add_2digit_int_to_string(tm.tm_mday, s);
170 detail::add_2digit_int_to_string(tm.tm_hour, s);
172 detail::add_2digit_int_to_string(tm.tm_min, s);
174 detail::add_2digit_int_to_string(tm.tm_sec, s);
194 template <typename T, typename
std::enable_if<
std::is_integral<T>::value,
int>::
type = 0>
228 explicit constexpr operator bool() const noexcept {
238 explicit constexpr operator uint32_t() const noexcept {
243 explicit constexpr operator uint64_t() const noexcept {
247 template <
typename T>
252 template <
typename T>
300 return {std::numeric_limits<uint32_t>::max()};
303 template <
typename TChar,
typename TTraits>
304 inline std::basic_ostream<TChar, TTraits>&
operator<<(std::basic_ostream<TChar, TTraits>& out, Timestamp timestamp) {
305 out << timestamp.to_iso();
310 return uint32_t(lhs) == uint32_t(rhs);
314 return !(lhs == rhs);
318 return uint32_t(lhs) < uint32_t(rhs);
Definition: timestamp.hpp:146
void to_iso_str(std::string &s) const
Definition: timestamp.hpp:150
std::string to_iso() const
Definition: timestamp.hpp:262
constexpr time_t seconds_since_epoch() const noexcept
Explicit conversion into time_t.
Definition: timestamp.hpp:233
Timestamp(const std::string ×tamp)
Definition: timestamp.hpp:215
constexpr Timestamp() noexcept=default
uint32_t m_timestamp
Definition: timestamp.hpp:148
bool valid() const noexcept
Definition: timestamp.hpp:223
void operator+=(T time_difference) noexcept
Definition: timestamp.hpp:248
void operator-=(T time_difference) noexcept
Definition: timestamp.hpp:253
Timestamp(const char *timestamp)
Definition: timestamp.hpp:205
std::string to_iso_all() const
Definition: timestamp.hpp:277
type
Definition: entity_bits.hpp:63
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
constexpr Timestamp end_of_time() noexcept
Definition: timestamp.hpp:299
bool operator==(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:440
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:459
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:455
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:463
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:444
constexpr Timestamp start_of_time() noexcept
Definition: timestamp.hpp:291
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const item_type item_type)
Definition: item_type.hpp:187
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:451
Definition: location.hpp:555