2007-04-05 00:27:36 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2007, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_TIME_HPP_INCLUDED
|
|
|
|
#define TORRENT_TIME_HPP_INCLUDED
|
|
|
|
|
2007-04-10 09:52:58 +02:00
|
|
|
#include <ctime>
|
|
|
|
|
2007-04-09 22:18:54 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2007-04-10 09:52:58 +02:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
inline char const* time_now_string()
|
|
|
|
{
|
|
|
|
time_t t = std::time(0);
|
2007-04-14 22:12:15 +02:00
|
|
|
tm* timeinfo = std::localtime(&t);
|
|
|
|
static char str[200];
|
|
|
|
std::strftime(str, 200, "%b %d %X", timeinfo);
|
|
|
|
return str;
|
2007-04-10 09:52:58 +02:00
|
|
|
}
|
2007-09-29 22:35:55 +02:00
|
|
|
|
2007-10-01 04:21:58 +02:00
|
|
|
std::string log_time();
|
2007-04-10 09:52:58 +02:00
|
|
|
}
|
|
|
|
|
2007-06-15 01:15:30 +02:00
|
|
|
#if (!defined (__MACH__) && !defined (_WIN32) && (!defined(_POSIX_MONOTONIC_CLOCK) \
|
|
|
|
|| _POSIX_MONOTONIC_CLOCK < 0)) || defined (TORRENT_USE_BOOST_DATE_TIME)
|
2007-04-05 00:27:36 +02:00
|
|
|
|
2007-04-10 11:11:32 +02:00
|
|
|
#include <boost/date_time/posix_time/posix_time_types.hpp>
|
2007-09-10 08:12:41 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2007-04-05 00:27:36 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
typedef boost::posix_time::ptime ptime;
|
|
|
|
typedef boost::posix_time::time_duration time_duration;
|
|
|
|
inline ptime time_now()
|
|
|
|
{ return boost::posix_time::microsec_clock::universal_time(); }
|
|
|
|
inline ptime min_time()
|
|
|
|
{ return boost::posix_time::ptime(boost::posix_time::min_date_time); }
|
|
|
|
inline ptime max_time()
|
|
|
|
{ return boost::posix_time::ptime(boost::posix_time::max_date_time); }
|
|
|
|
inline time_duration seconds(int s) { return boost::posix_time::seconds(s); }
|
|
|
|
inline time_duration milliseconds(int s) { return boost::posix_time::milliseconds(s); }
|
|
|
|
inline time_duration microsec(int s) { return boost::posix_time::microsec(s); }
|
|
|
|
inline time_duration minutes(int s) { return boost::posix_time::minutes(s); }
|
|
|
|
inline time_duration hours(int s) { return boost::posix_time::hours(s); }
|
|
|
|
|
|
|
|
inline int total_seconds(time_duration td)
|
|
|
|
{ return td.total_seconds(); }
|
|
|
|
inline int total_milliseconds(time_duration td)
|
|
|
|
{ return td.total_milliseconds(); }
|
|
|
|
inline boost::int64_t total_microseconds(time_duration td)
|
|
|
|
{ return td.total_microseconds(); }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <asio/time_traits.hpp>
|
|
|
|
#include <boost/cstdint.hpp>
|
2007-09-10 08:12:41 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2007-04-05 00:27:36 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
// libtorrent time_duration type
|
|
|
|
struct time_duration
|
|
|
|
{
|
|
|
|
time_duration() {}
|
2007-05-12 20:24:14 +02:00
|
|
|
time_duration operator/(int rhs) const { return time_duration(diff / rhs); }
|
2007-04-05 00:27:36 +02:00
|
|
|
explicit time_duration(boost::int64_t d) : diff(d) {}
|
2007-11-25 09:18:57 +01:00
|
|
|
time_duration& operator-=(time_duration const& c) { diff -= c.diff; return *this; }
|
2008-04-16 08:31:05 +02:00
|
|
|
time_duration& operator+=(time_duration const& c) { diff += c.diff; return *this; }
|
2007-12-04 03:53:10 +01:00
|
|
|
time_duration operator+(time_duration const& c) { return time_duration(diff + c.diff); }
|
2007-04-05 00:27:36 +02:00
|
|
|
boost::int64_t diff;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool is_negative(time_duration dt) { return dt.diff < 0; }
|
|
|
|
inline bool operator<(time_duration lhs, time_duration rhs)
|
|
|
|
{ return lhs.diff < rhs.diff; }
|
2007-06-15 01:15:30 +02:00
|
|
|
inline bool operator<=(time_duration lhs, time_duration rhs)
|
|
|
|
{ return lhs.diff <= rhs.diff; }
|
2007-04-05 00:27:36 +02:00
|
|
|
inline bool operator>(time_duration lhs, time_duration rhs)
|
|
|
|
{ return lhs.diff > rhs.diff; }
|
2007-06-15 01:15:30 +02:00
|
|
|
inline bool operator>=(time_duration lhs, time_duration rhs)
|
|
|
|
{ return lhs.diff >= rhs.diff; }
|
2007-04-05 00:27:36 +02:00
|
|
|
|
|
|
|
// libtorrent time type
|
|
|
|
struct ptime
|
|
|
|
{
|
|
|
|
ptime() {}
|
|
|
|
explicit ptime(boost::int64_t t): time(t) {}
|
|
|
|
boost::int64_t time;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool operator>(ptime lhs, ptime rhs)
|
|
|
|
{ return lhs.time > rhs.time; }
|
|
|
|
inline bool operator>=(ptime lhs, ptime rhs)
|
|
|
|
{ return lhs.time >= rhs.time; }
|
|
|
|
inline bool operator<=(ptime lhs, ptime rhs)
|
|
|
|
{ return lhs.time <= rhs.time; }
|
|
|
|
inline bool operator<(ptime lhs, ptime rhs)
|
|
|
|
{ return lhs.time < rhs.time; }
|
|
|
|
inline bool operator!=(ptime lhs, ptime rhs)
|
|
|
|
{ return lhs.time != rhs.time;}
|
|
|
|
inline bool operator==(ptime lhs, ptime rhs)
|
|
|
|
{ return lhs.time == rhs.time;}
|
|
|
|
inline time_duration operator-(ptime lhs, ptime rhs)
|
|
|
|
{ return time_duration(lhs.time - rhs.time); }
|
|
|
|
inline ptime operator+(ptime lhs, time_duration rhs)
|
|
|
|
{ return ptime(lhs.time + rhs.diff); }
|
|
|
|
inline ptime operator+(time_duration lhs, ptime rhs)
|
|
|
|
{ return ptime(rhs.time + lhs.diff); }
|
|
|
|
inline ptime operator-(ptime lhs, time_duration rhs)
|
|
|
|
{ return ptime(lhs.time - rhs.diff); }
|
|
|
|
|
|
|
|
ptime time_now();
|
|
|
|
inline ptime min_time() { return ptime(0); }
|
2007-04-05 03:08:21 +02:00
|
|
|
inline ptime max_time() { return ptime((std::numeric_limits<boost::int64_t>::max)()); }
|
2007-04-05 00:27:36 +02:00
|
|
|
int total_seconds(time_duration td);
|
|
|
|
int total_milliseconds(time_duration td);
|
|
|
|
boost::int64_t total_microseconds(time_duration td);
|
|
|
|
}
|
|
|
|
|
|
|
|
// asio time_traits
|
|
|
|
namespace asio
|
|
|
|
{
|
|
|
|
template<>
|
|
|
|
struct time_traits<libtorrent::ptime>
|
|
|
|
{
|
|
|
|
typedef libtorrent::ptime time_type;
|
|
|
|
typedef libtorrent::time_duration duration_type;
|
|
|
|
static time_type now()
|
|
|
|
{ return time_type(libtorrent::time_now()); }
|
|
|
|
static time_type add(time_type t, duration_type d)
|
|
|
|
{ return time_type(t.time + d.diff);}
|
|
|
|
static duration_type subtract(time_type t1, time_type t2)
|
|
|
|
{ return duration_type(t1 - t2); }
|
|
|
|
static bool less_than(time_type t1, time_type t2)
|
|
|
|
{ return t1 < t2; }
|
|
|
|
static boost::posix_time::time_duration to_posix_duration(
|
|
|
|
duration_type d)
|
|
|
|
{ return boost::posix_time::microseconds(libtorrent::total_microseconds(d)); }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__MACH__)
|
|
|
|
|
|
|
|
#include <mach/mach_time.h>
|
|
|
|
#include <boost/cstdint.hpp>
|
2007-09-10 08:12:41 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2007-04-05 00:27:36 +02:00
|
|
|
|
|
|
|
// high precision timer for darwin intel and ppc
|
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
namespace aux
|
|
|
|
{
|
|
|
|
inline boost::int64_t absolutetime_to_microseconds(boost::int64_t at)
|
|
|
|
{
|
|
|
|
static mach_timebase_info_data_t timebase_info = {0,0};
|
|
|
|
if (timebase_info.denom == 0)
|
|
|
|
mach_timebase_info(&timebase_info);
|
|
|
|
// make sure we don't overflow
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT((at >= 0 && at >= at / 1000 * timebase_info.numer / timebase_info.denom)
|
2007-04-05 00:27:36 +02:00
|
|
|
|| (at < 0 && at < at / 1000 * timebase_info.numer / timebase_info.denom));
|
|
|
|
return at / 1000 * timebase_info.numer / timebase_info.denom;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline boost::int64_t microseconds_to_absolutetime(boost::int64_t ms)
|
|
|
|
{
|
|
|
|
static mach_timebase_info_data_t timebase_info = {0,0};
|
|
|
|
if (timebase_info.denom == 0)
|
|
|
|
{
|
|
|
|
mach_timebase_info(&timebase_info);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(timebase_info.numer > 0);
|
|
|
|
TORRENT_ASSERT(timebase_info.denom > 0);
|
2007-04-05 00:27:36 +02:00
|
|
|
}
|
|
|
|
// make sure we don't overflow
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT((ms >= 0 && ms <= ms * timebase_info.denom / timebase_info.numer * 1000)
|
2007-04-05 00:27:36 +02:00
|
|
|
|| (ms < 0 && ms > ms * timebase_info.denom / timebase_info.numer * 1000));
|
|
|
|
return ms * timebase_info.denom / timebase_info.numer * 1000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int total_seconds(time_duration td)
|
|
|
|
{
|
|
|
|
return aux::absolutetime_to_microseconds(td.diff)
|
|
|
|
/ 1000000;
|
|
|
|
}
|
|
|
|
inline int total_milliseconds(time_duration td)
|
|
|
|
{
|
|
|
|
return aux::absolutetime_to_microseconds(td.diff)
|
|
|
|
/ 1000;
|
|
|
|
}
|
|
|
|
inline boost::int64_t total_microseconds(time_duration td)
|
|
|
|
{
|
|
|
|
return aux::absolutetime_to_microseconds(td.diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ptime time_now() { return ptime(mach_absolute_time()); }
|
|
|
|
|
|
|
|
inline time_duration microsec(boost::int64_t s)
|
|
|
|
{
|
|
|
|
return time_duration(aux::microseconds_to_absolutetime(s));
|
|
|
|
}
|
|
|
|
inline time_duration milliseconds(boost::int64_t s)
|
|
|
|
{
|
|
|
|
return time_duration(aux::microseconds_to_absolutetime(s * 1000));
|
|
|
|
}
|
|
|
|
inline time_duration seconds(boost::int64_t s)
|
|
|
|
{
|
|
|
|
return time_duration(aux::microseconds_to_absolutetime(s * 1000000));
|
|
|
|
}
|
|
|
|
inline time_duration minutes(boost::int64_t s)
|
|
|
|
{
|
|
|
|
return time_duration(aux::microseconds_to_absolutetime(s * 1000000 * 60));
|
|
|
|
}
|
|
|
|
inline time_duration hours(boost::int64_t s)
|
|
|
|
{
|
|
|
|
return time_duration(aux::microseconds_to_absolutetime(s * 1000000 * 60 * 60));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
|
2007-04-05 03:08:21 +02:00
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
2007-04-05 00:27:36 +02:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2007-04-05 03:08:21 +02:00
|
|
|
#endif
|
2007-04-05 00:27:36 +02:00
|
|
|
#include <Windows.h>
|
2007-09-10 08:12:41 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2007-04-05 00:27:36 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
namespace aux
|
|
|
|
{
|
|
|
|
inline boost::int64_t performance_counter_to_microseconds(boost::int64_t pc)
|
|
|
|
{
|
|
|
|
static LARGE_INTEGER performace_counter_frequency = {0,0};
|
|
|
|
if (performace_counter_frequency.QuadPart == 0)
|
|
|
|
QueryPerformanceFrequency(&performace_counter_frequency);
|
|
|
|
|
2007-04-26 23:33:07 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// make sure we don't overflow
|
|
|
|
boost::int64_t ret = (pc * 1000 / performace_counter_frequency.QuadPart) * 1000;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT((pc >= 0 && pc >= ret) || (pc < 0 && pc < ret));
|
2007-04-26 23:33:07 +02:00
|
|
|
#endif
|
|
|
|
return (pc * 1000 / performace_counter_frequency.QuadPart) * 1000;
|
2007-04-05 00:27:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline boost::int64_t microseconds_to_performance_counter(boost::int64_t ms)
|
|
|
|
{
|
|
|
|
static LARGE_INTEGER performace_counter_frequency = {0,0};
|
|
|
|
if (performace_counter_frequency.QuadPart == 0)
|
|
|
|
QueryPerformanceFrequency(&performace_counter_frequency);
|
2007-04-26 23:33:07 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// make sure we don't overflow
|
|
|
|
boost::int64_t ret = (ms / 1000) * performace_counter_frequency.QuadPart / 1000;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT((ms >= 0 && ms <= ret)
|
2007-04-26 23:33:07 +02:00
|
|
|
|| (ms < 0 && ms > ret));
|
|
|
|
#endif
|
|
|
|
return (ms / 1000) * performace_counter_frequency.QuadPart / 1000;
|
2007-04-05 00:27:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int total_seconds(time_duration td)
|
|
|
|
{
|
2008-04-05 23:18:27 +02:00
|
|
|
return int(aux::performance_counter_to_microseconds(td.diff)
|
|
|
|
/ 1000000);
|
2007-04-05 00:27:36 +02:00
|
|
|
}
|
|
|
|
inline int total_milliseconds(time_duration td)
|
|
|
|
{
|
2008-04-05 23:18:27 +02:00
|
|
|
return int(aux::performance_counter_to_microseconds(td.diff)
|
|
|
|
/ 1000);
|
2007-04-05 00:27:36 +02:00
|
|
|
}
|
|
|
|
inline boost::int64_t total_microseconds(time_duration td)
|
|
|
|
{
|
|
|
|
return aux::performance_counter_to_microseconds(td.diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ptime time_now()
|
|
|
|
{
|
|
|
|
LARGE_INTEGER now;
|
|
|
|
QueryPerformanceCounter(&now);
|
|
|
|
return ptime(now.QuadPart);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline time_duration microsec(boost::int64_t s)
|
|
|
|
{
|
|
|
|
return time_duration(aux::microseconds_to_performance_counter(s));
|
|
|
|
}
|
|
|
|
inline time_duration milliseconds(boost::int64_t s)
|
|
|
|
{
|
|
|
|
return time_duration(aux::microseconds_to_performance_counter(
|
|
|
|
s * 1000));
|
|
|
|
}
|
|
|
|
inline time_duration seconds(boost::int64_t s)
|
|
|
|
{
|
|
|
|
return time_duration(aux::microseconds_to_performance_counter(
|
|
|
|
s * 1000000));
|
|
|
|
}
|
|
|
|
inline time_duration minutes(boost::int64_t s)
|
|
|
|
{
|
|
|
|
return time_duration(aux::microseconds_to_performance_counter(
|
|
|
|
s * 1000000 * 60));
|
|
|
|
}
|
|
|
|
inline time_duration hours(boost::int64_t s)
|
|
|
|
{
|
|
|
|
return time_duration(aux::microseconds_to_performance_counter(
|
|
|
|
s * 1000000 * 60 * 60));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-04-09 22:18:54 +02:00
|
|
|
#elif defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
|
|
|
|
|
|
|
|
#include <time.h>
|
2007-09-10 08:12:41 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2007-04-09 22:18:54 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
inline int total_seconds(time_duration td)
|
|
|
|
{
|
|
|
|
return td.diff / 1000000;
|
|
|
|
}
|
|
|
|
inline int total_milliseconds(time_duration td)
|
|
|
|
{
|
|
|
|
return td.diff / 1000;
|
|
|
|
}
|
|
|
|
inline boost::int64_t total_microseconds(time_duration td)
|
|
|
|
{
|
|
|
|
return td.diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ptime time_now()
|
|
|
|
{
|
|
|
|
timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
2007-04-12 09:02:17 +02:00
|
|
|
return ptime(boost::int64_t(ts.tv_sec) * 1000000 + ts.tv_nsec / 1000);
|
2007-04-09 22:18:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline time_duration microsec(boost::int64_t s)
|
|
|
|
{
|
|
|
|
return time_duration(s);
|
|
|
|
}
|
|
|
|
inline time_duration milliseconds(boost::int64_t s)
|
|
|
|
{
|
|
|
|
return time_duration(s * 1000);
|
|
|
|
}
|
|
|
|
inline time_duration seconds(boost::int64_t s)
|
|
|
|
{
|
|
|
|
return time_duration(s * 1000000);
|
|
|
|
}
|
|
|
|
inline time_duration minutes(boost::int64_t s)
|
|
|
|
{
|
|
|
|
return time_duration(s * 1000000 * 60);
|
|
|
|
}
|
|
|
|
inline time_duration hours(boost::int64_t s)
|
|
|
|
{
|
|
|
|
return time_duration(s * 1000000 * 60 * 60);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
2007-09-29 18:14:03 +02:00
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
#endif
|
2007-09-10 08:12:41 +02:00
|
|
|
|