2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
2019-07-15 18:47:34 +02:00
|
|
|
// Copyright (C) 2016-2019 Jason Volk <jason@zemos.net>
|
2018-02-04 03:22:01 +01:00
|
|
|
//
|
|
|
|
// 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. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-09-08 10:43:43 +02:00
|
|
|
|
|
|
|
#pragma once
|
2019-07-15 18:47:34 +02:00
|
|
|
#define HAVE_IRCD_TIMEDATE_H
|
2017-09-08 10:43:43 +02:00
|
|
|
|
|
|
|
namespace ircd
|
|
|
|
{
|
2018-11-15 03:14:08 +01:00
|
|
|
using high_resolution_point = time_point<high_resolution_clock>;
|
2017-09-20 06:39:53 +02:00
|
|
|
using steady_point = time_point<steady_clock>;
|
|
|
|
using system_point = time_point<system_clock>;
|
2018-11-15 03:14:08 +01:00
|
|
|
using microtime_t = std::pair<time_t, int32_t>;
|
2019-07-15 18:47:34 +02:00
|
|
|
IRCD_OVERLOAD(localtime)
|
2017-09-20 06:39:53 +02:00
|
|
|
|
2018-05-24 03:05:05 +02:00
|
|
|
// Standard time_point samples
|
2017-09-20 06:39:53 +02:00
|
|
|
template<class unit = seconds> unit now();
|
|
|
|
template<> steady_point now();
|
|
|
|
template<> system_point now();
|
|
|
|
|
2018-05-24 03:05:05 +02:00
|
|
|
// Standard time_point (system_clock only) directly into long integer.
|
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
|
|
|
|
2018-05-24 03:05:05 +02:00
|
|
|
// System vdso microtime suite
|
|
|
|
microtime_t microtime();
|
|
|
|
string_view microtime(const mutable_buffer &);
|
|
|
|
|
|
|
|
// System vdso formatted time suite
|
2019-07-15 18:47:34 +02:00
|
|
|
extern const char *const rfc7231_fmt;
|
2017-10-12 02:47:03 +02:00
|
|
|
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);
|
2018-04-14 04:23:48 +02:00
|
|
|
string_view timef(const mutable_buffer &out, const system_point &epoch, const char *const &fmt = rfc7231_fmt);
|
|
|
|
string_view timef(const mutable_buffer &out, const system_point &epoch, localtime_t, const char *const &fmt = rfc7231_fmt);
|
2017-10-12 02:47:03 +02:00
|
|
|
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);
|
2018-05-24 03:05:05 +02:00
|
|
|
template<size_t max = 128, class... args> std::string timestr(args&&...);
|
2018-02-13 00:04:54 +01:00
|
|
|
|
2019-04-18 04:27:26 +02:00
|
|
|
// Other tools
|
|
|
|
string_view smalldate(const mutable_buffer &buf, const time_t <ime);
|
|
|
|
|
|
|
|
// Interface conveniences.
|
2018-05-24 03:05:05 +02:00
|
|
|
std::ostream &operator<<(std::ostream &, const microtime_t &);
|
2018-02-13 00:04:54 +01:00
|
|
|
std::ostream &operator<<(std::ostream &, const system_point &);
|
2018-05-24 02:58:11 +02:00
|
|
|
template<class rep, class period> std::ostream &operator<<(std::ostream &, const duration<rep, period> &);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class rep,
|
|
|
|
class period>
|
|
|
|
std::ostream &
|
|
|
|
ircd::operator<<(std::ostream &s,
|
|
|
|
const duration<rep, period> &duration)
|
|
|
|
{
|
|
|
|
s << duration.count();
|
|
|
|
return s;
|
2018-02-13 00:04:54 +01:00
|
|
|
}
|
|
|
|
|
2018-05-24 03:05:05 +02:00
|
|
|
/// timestr() is a passthru to timef() where you don't give the first argument
|
|
|
|
/// (the mutable_buffer). Instead of supplying a buffer an allocated
|
|
|
|
/// std::string is returned with the result. By default this string's buffer
|
|
|
|
/// is sufficiently large, but may be further tuned in the template parameter.
|
|
|
|
template<size_t max,
|
|
|
|
class... args>
|
2017-10-12 02:47:03 +02:00
|
|
|
std::string
|
|
|
|
ircd::timestr(args&&... a)
|
|
|
|
{
|
2018-01-29 17:37:20 +01:00
|
|
|
return string(max, [&](const mutable_buffer &buf)
|
2017-10-12 02:47:03 +02:00
|
|
|
{
|
2018-01-29 17:37:20 +01:00
|
|
|
return timef(buf, std::forward<args>(a)...);
|
|
|
|
});
|
2017-10-12 02:47:03 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-15 18:47:34 +02:00
|
|
|
template<>
|
|
|
|
inline ircd::system_point
|
2017-09-20 06:39:53 +02:00
|
|
|
ircd::now()
|
|
|
|
{
|
2018-11-15 03:14:08 +01:00
|
|
|
return system_clock::now();
|
2017-09-20 06:39:53 +02:00
|
|
|
}
|
|
|
|
|
2019-07-15 18:47:34 +02:00
|
|
|
template<>
|
|
|
|
inline ircd::steady_point
|
2017-09-20 06:39:53 +02:00
|
|
|
ircd::now()
|
|
|
|
{
|
2018-11-15 03:14:08 +01:00
|
|
|
return steady_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
|
|
|
}
|