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 bool fractional_seconds(
const char** s)
noexcept {
85 if (*str !=
'.' && *str !=
',') {
90 if (*str < '0' || *str >
'9') {
96 }
while (*str >=
'0' && *str <=
'9');
102 inline std::time_t parse_timestamp(
const char** s) {
103 const char* str = *s;
106 static const std::array<int, 12> mon_lengths = {{
107 31, 29, 31, 30, 31, 30,
108 31, 31, 30, 31, 30, 31
111 if (str[ 0] >=
'0' && str[ 0] <=
'9' &&
112 str[ 1] >=
'0' && str[ 1] <=
'9' &&
113 str[ 2] >=
'0' && str[ 2] <=
'9' &&
114 str[ 3] >=
'0' && str[ 3] <=
'9' &&
116 str[ 5] >=
'0' && str[ 5] <=
'9' &&
117 str[ 6] >=
'0' && str[ 6] <=
'9' &&
119 str[ 8] >=
'0' && str[ 8] <=
'9' &&
120 str[ 9] >=
'0' && str[ 9] <=
'9' &&
122 str[11] >=
'0' && str[11] <=
'9' &&
123 str[12] >=
'0' && str[12] <=
'9' &&
125 str[14] >=
'0' && str[14] <=
'9' &&
126 str[15] >=
'0' && str[15] <=
'9' &&
128 str[17] >=
'0' && str[17] <=
'9' &&
129 str[18] >=
'0' && str[18] <=
'9' &&
130 (str[19] ==
'Z' || fractional_seconds(s))) {
133 tm.tm_year = (str[ 0] -
'0') * 1000 +
134 (str[ 1] -
'0') * 100 +
135 (str[ 2] -
'0') * 10 +
136 (str[ 3] -
'0') - 1900;
137 tm.tm_mon = (str[ 5] -
'0') * 10 + (str[ 6] -
'0') - 1;
138 tm.tm_mday = (str[ 8] -
'0') * 10 + (str[ 9] -
'0');
139 tm.tm_hour = (str[11] -
'0') * 10 + (str[12] -
'0');
140 tm.tm_min = (str[14] -
'0') * 10 + (str[15] -
'0');
141 tm.tm_sec = (str[17] -
'0') * 10 + (str[18] -
'0');
145 if (tm.tm_year >= 0 &&
146 tm.tm_mon >= 0 && tm.tm_mon <= 11 &&
147 tm.tm_mday >= 1 && tm.tm_mday <= mon_lengths[tm.tm_mon] &&
148 tm.tm_hour >= 0 && tm.tm_hour <= 23 &&
149 tm.tm_min >= 0 && tm.tm_min <= 59 &&
150 tm.tm_sec >= 0 && tm.tm_sec <= 60) {
154 return _mkgmtime(&tm);
158 throw std::invalid_argument{std::string{
"can not parse timestamp: '"} + str +
"'"};
161 inline std::time_t parse_timestamp(
const char* s) {
162 const char** str = &s;
163 return parse_timestamp(str);
187 assert(result !=
nullptr);
193 detail::add_4digit_int_to_string(tm.tm_year + 1900, s);
195 detail::add_2digit_int_to_string(tm.tm_mon + 1, s);
197 detail::add_2digit_int_to_string(tm.tm_mday, s);
199 detail::add_2digit_int_to_string(tm.tm_hour, s);
201 detail::add_2digit_int_to_string(tm.tm_min, s);
203 detail::add_2digit_int_to_string(tm.tm_sec, s);
223 template <typename T, typename
std::enable_if<
std::is_integral<T>::value,
int>::
type = 0>
257 explicit constexpr operator bool() const noexcept {
267 explicit constexpr operator uint32_t() const noexcept {
272 explicit constexpr operator uint64_t() const noexcept {
276 template <
typename T>
281 template <
typename T>
329 return {std::numeric_limits<uint32_t>::max()};
332 template <
typename TChar,
typename TTraits>
333 inline std::basic_ostream<TChar, TTraits>&
operator<<(std::basic_ostream<TChar, TTraits>& out, Timestamp timestamp) {
334 out << timestamp.to_iso();
339 return static_cast<uint32_t
>(lhs) ==
static_cast<uint32_t
>(rhs);
343 return !(lhs == rhs);
347 return static_cast<uint32_t
>(lhs) <
static_cast<uint32_t
>(rhs);
Definition: timestamp.hpp:175
void to_iso_str(std::string &s) const
Definition: timestamp.hpp:179
std::string to_iso() const
Definition: timestamp.hpp:291
Timestamp(const std::string ×tamp)
Definition: timestamp.hpp:244
constexpr Timestamp() noexcept=default
uint32_t m_timestamp
Definition: timestamp.hpp:177
bool valid() const noexcept
Definition: timestamp.hpp:252
constexpr std::time_t seconds_since_epoch() const noexcept
Explicit conversion into time_t.
Definition: timestamp.hpp:262
void operator+=(T time_difference) noexcept
Definition: timestamp.hpp:277
void operator-=(T time_difference) noexcept
Definition: timestamp.hpp:282
Timestamp(const char *timestamp)
Definition: timestamp.hpp:234
std::string to_iso_all() const
Definition: timestamp.hpp:306
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:328
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:320
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