1 #ifndef OSMIUM_OSM_TIMESTAMP_HPP
2 #define OSMIUM_OSM_TIMESTAMP_HPP
43 #include <type_traits>
52 inline time_t parse_timestamp(
const char* str) {
53 static const int mon_lengths[] = {
54 31, 29, 31, 30, 31, 30,
55 31, 31, 30, 31, 30, 31
57 if (str[ 0] >=
'0' && str[ 0] <=
'9' &&
58 str[ 1] >=
'0' && str[ 1] <=
'9' &&
59 str[ 2] >=
'0' && str[ 2] <=
'9' &&
60 str[ 3] >=
'0' && str[ 3] <=
'9' &&
62 str[ 5] >=
'0' && str[ 5] <=
'9' &&
63 str[ 6] >=
'0' && str[ 6] <=
'9' &&
65 str[ 8] >=
'0' && str[ 8] <=
'9' &&
66 str[ 9] >=
'0' && str[ 9] <=
'9' &&
68 str[11] >=
'0' && str[11] <=
'9' &&
69 str[12] >=
'0' && str[12] <=
'9' &&
71 str[14] >=
'0' && str[14] <=
'9' &&
72 str[15] >=
'0' && str[15] <=
'9' &&
74 str[17] >=
'0' && str[17] <=
'9' &&
75 str[18] >=
'0' && str[18] <=
'9' &&
78 tm.tm_year = (str[ 0] -
'0') * 1000 +
79 (str[ 1] -
'0') * 100 +
80 (str[ 2] -
'0') * 10 +
81 (str[ 3] -
'0') - 1900;
82 tm.tm_mon = (str[ 5] -
'0') * 10 + (str[ 6] -
'0') - 1;
83 tm.tm_mday = (str[ 8] -
'0') * 10 + (str[ 9] -
'0');
84 tm.tm_hour = (str[11] -
'0') * 10 + (str[12] -
'0');
85 tm.tm_min = (str[14] -
'0') * 10 + (str[15] -
'0');
86 tm.tm_sec = (str[17] -
'0') * 10 + (str[18] -
'0');
90 if (tm.tm_year >= 0 &&
91 tm.tm_mon >= 0 && tm.tm_mon <= 11 &&
92 tm.tm_mday >= 1 && tm.tm_mday <= mon_lengths[tm.tm_mon] &&
93 tm.tm_hour >= 0 && tm.tm_hour <= 23 &&
94 tm.tm_min >= 0 && tm.tm_min <= 59 &&
95 tm.tm_sec >= 0 && tm.tm_sec <= 60) {
99 return _mkgmtime(&tm);
103 throw std::invalid_argument{
"can not parse timestamp"};
147 template <typename T, typename std::enable_if<std::is_integral<T>::value,
int>
::type = 0>
159 m_timestamp =
static_cast<uint32_t
>(detail::parse_timestamp(timestamp));
177 return m_timestamp != 0;
181 explicit constexpr
operator bool() const noexcept {
182 return m_timestamp != 0;
187 return time_t(m_timestamp);
191 explicit constexpr
operator uint32_t() const noexcept {
192 return uint32_t(m_timestamp);
196 explicit constexpr
operator uint64_t() const noexcept {
197 return uint64_t(m_timestamp);
209 template <
typename T>
211 m_timestamp += time_difference;
214 template <
typename T>
216 m_timestamp -= time_difference;
227 if (m_timestamp != 0) {
235 assert(result !=
nullptr);
241 s.resize(timestamp_length);
247 s.resize(strftime(const_cast<char*>(s.c_str()), timestamp_length,
timestamp_format(), &tm));
268 return Timestamp(std::numeric_limits<uint32_t>::max());
271 template <
typename TChar,
typename TTraits>
272 inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out,
Timestamp timestamp) {
273 out << timestamp.
to_iso();
278 return uint32_t(lhs) == uint32_t(rhs);
282 return !(lhs == rhs);
286 return uint32_t(lhs) < uint32_t(rhs);
294 return ! (rhs < lhs);
298 return ! (lhs < rhs);
313 #endif // OSMIUM_OSM_TIMESTAMP_HPP
static constexpr const int timestamp_length
Definition: timestamp.hpp:118
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
type
Definition: entity_bits.hpp:63
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:453
constexpr Timestamp start_of_time() noexcept
Definition: timestamp.hpp:259
constexpr time_t seconds_since_epoch() const noexcept
Explicit conversion into time_t.
Definition: timestamp.hpp:186
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:221
bool valid() const noexcept
Definition: timestamp.hpp:176
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:445
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
Definition: timestamp.hpp:115
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:457
void operator-=(T time_difference) noexcept
Definition: timestamp.hpp:215
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:449
constexpr Timestamp() noexcept
Definition: timestamp.hpp:134
Timestamp(const std::string ×tamp)
Definition: timestamp.hpp:168
void operator+=(T time_difference) noexcept
Definition: timestamp.hpp:210
constexpr Timestamp(T timestamp) noexcept
Definition: timestamp.hpp:148
std::string to_iso() const
Definition: timestamp.hpp:224
constexpr Timestamp end_of_time() noexcept
Definition: timestamp.hpp:267
Timestamp(const char *timestamp)
Definition: timestamp.hpp:158
uint32_t m_timestamp
Definition: timestamp.hpp:127
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:438
static const char * timestamp_format()
Definition: timestamp.hpp:122