timestamp.hpp
Go to the documentation of this file.
#ifndef OSMIUM_OSM_TIMESTAMP_HPP
#define OSMIUM_OSM_TIMESTAMP_HPP
/*
This file is part of Osmium (http://osmcode.org/libosmium).
Copyright 2013-2016 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <cassert>
#include <cstdint>
#include <ctime>
#include <iosfwd>
#include <limits>
#include <stdexcept>
#include <string>
#include <osmium/util/compatibility.hpp>
#include <osmium/util/minmax.hpp> // IWYU pragma: keep
namespace osmium {
class Timestamp {
// length of ISO timestamp string yyyy-mm-ddThh:mm:ssZ\0
static constexpr int timestamp_length = 20 + 1;
// The timestamp format for OSM timestamps in strftime(3) format.
// This is the ISO-Format "yyyy-mm-ddThh:mm:ssZ".
static const char* timestamp_format() {
static const char f[timestamp_length] = "%Y-%m-%dT%H:%M:%SZ";
return f;
}
uint32_t m_timestamp;
public:
constexpr Timestamp() noexcept :
m_timestamp(0) {
}
template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
constexpr Timestamp(T timestamp) noexcept :
m_timestamp(uint32_t(timestamp)) {
}
explicit Timestamp(const char* timestamp) {
#ifndef _WIN32
struct tm tm {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
if (strptime(timestamp, timestamp_format(), &tm) == nullptr) {
throw std::invalid_argument("can't parse timestamp");
}
m_timestamp = static_cast<uint32_t>(timegm(&tm));
#else
struct tm tm;
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);
if (n != 6) {
throw std::invalid_argument("can't parse timestamp");
}
tm.tm_year -= 1900;
tm.tm_mon--;
tm.tm_wday = 0;
tm.tm_yday = 0;
tm.tm_isdst = 0;
m_timestamp = static_cast<uint32_t>(_mkgmtime(&tm));
#endif
}
explicit Timestamp(const std::string& timestamp) :
Timestamp(timestamp.c_str()) {
}
bool valid() const noexcept {
return m_timestamp != 0;
}
explicit constexpr operator bool() const noexcept {
return m_timestamp != 0;
}
constexpr time_t seconds_since_epoch() const noexcept {
return time_t(m_timestamp);
}
explicit constexpr operator uint32_t() const noexcept {
return uint32_t(m_timestamp);
}
explicit constexpr operator uint64_t() const noexcept {
return uint64_t(m_timestamp);
}
OSMIUM_DEPRECATED constexpr operator time_t() const noexcept {
return static_cast<time_t>(m_timestamp);
}
template <typename T>
void operator+=(T time_difference) noexcept {
m_timestamp += time_difference;
}
template <typename T>
void operator-=(T time_difference) noexcept {
m_timestamp -= time_difference;
}
std::string to_iso() const {
std::string s;
if (m_timestamp != 0) {
struct tm tm;
time_t sse = seconds_since_epoch();
#ifndef NDEBUG
auto result =
#endif
#ifndef _MSC_VER
gmtime_r(&sse, &tm);
assert(result != nullptr);
#else
gmtime_s(&tm, &sse);
assert(result == 0);
#endif
s.resize(timestamp_length);
/* This const_cast is ok, because we know we have enough space
in the string for the format we are using (well at least until
the year will have 5 digits). And by setting the size
afterwards from the result of strftime we make sure thats set
right, too. */
s.resize(strftime(const_cast<char*>(s.c_str()), timestamp_length, timestamp_format(), &tm));
}
return s;
}
}; // class Timestamp
inline constexpr Timestamp start_of_time() noexcept {
return Timestamp(1);
}
inline constexpr Timestamp end_of_time() noexcept {
return Timestamp(std::numeric_limits<uint32_t>::max());
}
template <typename TChar, typename TTraits>
inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, Timestamp timestamp) {
out << timestamp.to_iso();
return out;
}
inline bool operator==(const Timestamp& lhs, const Timestamp& rhs) noexcept {
return uint32_t(lhs) == uint32_t(rhs);
}
inline bool operator!=(const Timestamp& lhs, const Timestamp& rhs) noexcept {
return !(lhs == rhs);
}
inline bool operator<(const Timestamp& lhs, const Timestamp& rhs) noexcept {
return uint32_t(lhs) < uint32_t(rhs);
}
inline bool operator>(const Timestamp& lhs, const Timestamp& rhs) noexcept {
return rhs < lhs;
}
inline bool operator<=(const Timestamp& lhs, const Timestamp& rhs) noexcept {
return ! (rhs < lhs);
}
inline bool operator>=(const Timestamp& lhs, const Timestamp& rhs) noexcept {
return ! (lhs < rhs);
}
template <>
inline osmium::Timestamp min_op_start_value<osmium::Timestamp>() {
return end_of_time();
}
template <>
inline osmium::Timestamp max_op_start_value<osmium::Timestamp>() {
return start_of_time();
}
} // namespace osmium
#endif // OSMIUM_OSM_TIMESTAMP_HPP
OSMIUM_DEPRECATED
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
osmium::osm_entity_bits::type
type
Definition: entity_bits.hpp:60
osmium::operator<=
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:446
osmium::start_of_time
constexpr Timestamp start_of_time() noexcept
Definition: timestamp.hpp:220
osmium::Timestamp::seconds_since_epoch
constexpr time_t seconds_since_epoch() const noexcept
Explicit conversion into time_t.
Definition: timestamp.hpp:147
osmium::operator==
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:222
osmium::Timestamp::timestamp_length
static constexpr int timestamp_length
Definition: timestamp.hpp:59
osmium::Timestamp::valid
bool valid() const noexcept
Definition: timestamp.hpp:137
osmium::operator<
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:438
osmium
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
osmium::Timestamp
Definition: timestamp.hpp:56
osmium::operator>=
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:450
minmax.hpp
osmium::Timestamp::operator-=
void operator-=(T time_difference) noexcept
Definition: timestamp.hpp:176
osmium::operator>
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:442
osmium::Timestamp::Timestamp
constexpr Timestamp() noexcept
Definition: timestamp.hpp:75
osmium::Timestamp::Timestamp
Timestamp(const std::string &timestamp)
Definition: timestamp.hpp:129
osmium::Timestamp::operator+=
void operator+=(T time_difference) noexcept
Definition: timestamp.hpp:171
osmium::Timestamp::Timestamp
constexpr Timestamp(T timestamp) noexcept
Definition: timestamp.hpp:89
osmium::Timestamp::to_iso
std::string to_iso() const
Definition: timestamp.hpp:185
osmium::end_of_time
constexpr Timestamp end_of_time() noexcept
Definition: timestamp.hpp:228
osmium::Timestamp::Timestamp
Timestamp(const char *timestamp)
Definition: timestamp.hpp:99
osmium::Timestamp::m_timestamp
uint32_t m_timestamp
Definition: timestamp.hpp:68
osmium::operator!=
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:431
osmium::Timestamp::timestamp_format
static const char * timestamp_format()
Definition: timestamp.hpp:63
compatibility.hpp