2017-09-08 10:43:43 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_UTIL_DATE_H
|
|
|
|
|
|
|
|
namespace ircd
|
|
|
|
{
|
2017-10-03 12:56:06 +02:00
|
|
|
using std::chrono::hours;
|
2017-09-20 06:39:53 +02:00
|
|
|
using std::chrono::seconds;
|
|
|
|
using std::chrono::milliseconds;
|
|
|
|
using std::chrono::microseconds;
|
|
|
|
using std::chrono::nanoseconds;
|
|
|
|
using std::chrono::duration_cast;
|
|
|
|
using std::chrono::system_clock;
|
|
|
|
using std::chrono::steady_clock;
|
2017-09-30 07:57:29 +02:00
|
|
|
using std::chrono::high_resolution_clock;
|
2017-09-20 06:39:53 +02:00
|
|
|
using std::chrono::time_point;
|
2017-10-01 09:15:01 +02:00
|
|
|
using namespace std::literals::chrono_literals;
|
2017-09-20 06:39:53 +02:00
|
|
|
|
2017-09-08 10:43:43 +02:00
|
|
|
using microtime_t = std::pair<time_t, int32_t>;
|
2017-09-20 06:39:53 +02:00
|
|
|
using steady_point = time_point<steady_clock>;
|
|
|
|
using system_point = time_point<system_clock>;
|
|
|
|
|
2017-09-08 10:43:43 +02:00
|
|
|
microtime_t microtime();
|
2017-10-12 02:47:03 +02:00
|
|
|
string_view microtime(const mutable_buffer &);
|
2017-09-08 10:43:43 +02:00
|
|
|
std::ostream &operator<<(std::ostream &, const microtime_t &);
|
|
|
|
|
2017-09-20 06:39:53 +02:00
|
|
|
template<class unit = seconds> unit now();
|
|
|
|
template<> steady_point now();
|
|
|
|
template<> system_point now();
|
|
|
|
|
2017-09-30 07:57:29 +02:00
|
|
|
template<class unit = seconds> time_t &time(time_t &ref);
|
2017-09-20 06:39:53 +02:00
|
|
|
template<class unit = seconds> time_t time();
|
2017-09-30 07:57:29 +02:00
|
|
|
template<class unit = seconds> time_t time(time_t *const &ptr);
|
2017-10-12 02:47:03 +02:00
|
|
|
|
|
|
|
const char *const rfc7231_fmt
|
|
|
|
{
|
|
|
|
"%a, %d %b %Y %T %z"
|
|
|
|
};
|
|
|
|
|
|
|
|
IRCD_OVERLOAD(localtime)
|
|
|
|
string_view timef(const mutable_buffer &out, const struct tm &tm, const char *const &fmt = rfc7231_fmt);
|
|
|
|
string_view timef(const mutable_buffer &out, const time_t &epoch, const char *const &fmt = rfc7231_fmt);
|
|
|
|
string_view timef(const mutable_buffer &out, const time_t &epoch, localtime_t, const char *const &fmt = rfc7231_fmt);
|
|
|
|
string_view timef(const mutable_buffer &out, localtime_t, const char *const &fmt = rfc7231_fmt);
|
|
|
|
string_view timef(const mutable_buffer &out, const char *const &fmt = rfc7231_fmt);
|
|
|
|
template<class... args> std::string timestr(args&&...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class... args>
|
|
|
|
std::string
|
|
|
|
ircd::timestr(args&&... a)
|
|
|
|
{
|
|
|
|
std::string ret(128, char{});
|
|
|
|
const mutable_buffer buf
|
|
|
|
{
|
|
|
|
const_cast<char *>(ret.data()), ret.size()
|
|
|
|
};
|
|
|
|
|
|
|
|
ret.resize(timef(buf, std::forward<args>(a)...).size());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::string_view
|
|
|
|
ircd::timef(const mutable_buffer &out,
|
|
|
|
const char *const &fmt)
|
|
|
|
{
|
|
|
|
const auto epoch{time()};
|
|
|
|
return timef(out, epoch, fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::string_view
|
|
|
|
ircd::timef(const mutable_buffer &out,
|
|
|
|
localtime_t,
|
|
|
|
const char *const &fmt)
|
|
|
|
{
|
|
|
|
const auto epoch{time()};
|
|
|
|
return timef(out, epoch, localtime, fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::string_view
|
|
|
|
ircd::timef(const mutable_buffer &out,
|
|
|
|
const time_t &epoch,
|
|
|
|
localtime_t,
|
|
|
|
const char *const &fmt)
|
|
|
|
{
|
|
|
|
struct tm tm;
|
|
|
|
localtime_r(&epoch, &tm);
|
|
|
|
return timef(out, tm, fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::string_view
|
|
|
|
ircd::timef(const mutable_buffer &out,
|
|
|
|
const time_t &epoch,
|
|
|
|
const char *const &fmt)
|
|
|
|
{
|
|
|
|
struct tm tm;
|
|
|
|
gmtime_r(&epoch, &tm);
|
|
|
|
return timef(out, tm, fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::string_view
|
|
|
|
ircd::timef(const mutable_buffer &out,
|
|
|
|
const struct tm &tm,
|
|
|
|
const char *const &fmt)
|
|
|
|
{
|
|
|
|
const auto len
|
|
|
|
{
|
|
|
|
strftime(data(out), size(out), fmt, &tm)
|
|
|
|
};
|
|
|
|
|
|
|
|
return { data(out), len };
|
2017-09-30 07:57:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class unit>
|
|
|
|
time_t
|
|
|
|
ircd::time(time_t *const &ptr)
|
|
|
|
{
|
|
|
|
time_t buf, &ret{ptr? *ptr : buf};
|
|
|
|
return time<unit>(ret);
|
2017-09-08 10:43:43 +02:00
|
|
|
}
|
|
|
|
|
2017-09-20 06:39:53 +02:00
|
|
|
template<class unit>
|
2017-09-08 10:43:43 +02:00
|
|
|
time_t
|
|
|
|
ircd::time()
|
|
|
|
{
|
2017-09-30 07:57:29 +02:00
|
|
|
time_t ret;
|
|
|
|
return time<unit>(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class unit>
|
|
|
|
time_t &
|
|
|
|
ircd::time(time_t &ref)
|
|
|
|
{
|
|
|
|
ref = duration_cast<unit>(system_clock::now().time_since_epoch()).count();
|
|
|
|
return ref;
|
2017-09-20 06:39:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> inline
|
|
|
|
ircd::steady_point
|
|
|
|
ircd::now()
|
|
|
|
{
|
|
|
|
return steady_clock::now();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> inline
|
|
|
|
ircd::system_point
|
|
|
|
ircd::now()
|
|
|
|
{
|
|
|
|
return system_clock::now();
|
2017-09-08 10:43:43 +02:00
|
|
|
}
|
|
|
|
|
2017-09-20 06:39:53 +02:00
|
|
|
template<class unit>
|
|
|
|
unit
|
2017-09-08 10:43:43 +02:00
|
|
|
ircd::now()
|
|
|
|
{
|
|
|
|
const auto now
|
|
|
|
{
|
2017-09-20 06:39:53 +02:00
|
|
|
steady_clock::now()
|
2017-09-08 10:43:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto tse
|
|
|
|
{
|
|
|
|
now.time_since_epoch()
|
|
|
|
};
|
|
|
|
|
2017-09-20 06:39:53 +02:00
|
|
|
return std::chrono::duration_cast<unit>(tse);
|
2017-09-08 10:43:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline std::ostream &
|
|
|
|
ircd::operator<<(std::ostream &s, const microtime_t &t)
|
|
|
|
{
|
|
|
|
char buf[64];
|
2017-10-12 02:47:03 +02:00
|
|
|
s << microtime(buf);
|
2017-09-08 10:43:43 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:47:03 +02:00
|
|
|
inline ircd::string_view
|
|
|
|
ircd::microtime(const mutable_buffer &buf)
|
2017-09-08 10:43:43 +02:00
|
|
|
{
|
2017-10-12 02:47:03 +02:00
|
|
|
const auto mt{microtime()};
|
|
|
|
const auto length
|
|
|
|
{
|
|
|
|
snprintf(data(buf), size(buf), "%zd.%06d", mt.first, mt.second)
|
|
|
|
};
|
|
|
|
|
|
|
|
return string_view
|
|
|
|
{
|
|
|
|
data(buf), size_t(length)
|
|
|
|
};
|
2017-09-08 10:43:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::microtime_t
|
|
|
|
ircd::microtime()
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
2017-10-17 09:43:25 +02:00
|
|
|
syscall(::gettimeofday, &tv, nullptr);
|
2017-09-08 10:43:43 +02:00
|
|
|
return { tv.tv_sec, tv.tv_usec };
|
|
|
|
}
|