premiere-libtorrent/include/libtorrent/time.hpp

380 lines
11 KiB
C++
Raw Normal View History

/*
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
#include <ctime>
2008-05-20 11:45:55 +02:00
#include <boost/version.hpp>
#include "libtorrent/config.hpp"
#ifndef _WIN32
#include <unistd.h>
#endif
2009-09-17 13:22:18 +02:00
#include <algorithm>
#include <limits>
namespace libtorrent
{
inline char const* time_now_string()
{
time_t t = std::time(0);
tm* timeinfo = std::localtime(&t);
static char str[200];
std::strftime(str, 200, "%b %d %X", timeinfo);
return str;
}
2007-10-01 04:21:58 +02:00
std::string log_time();
}
#if defined TORRENT_USE_BOOST_DATE_TIME
#include <boost/date_time/posix_time/posix_time_types.hpp>
2007-09-10 08:12:41 +02:00
#include "libtorrent/assert.hpp"
namespace libtorrent
{
typedef boost::posix_time::ptime ptime;
typedef boost::posix_time::time_duration time_duration;
inline ptime time_now_hires()
{ 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 // TORRENT_USE_BOOST_DATE_TIME
#include <boost/cstdint.hpp>
2007-09-10 08:12:41 +02:00
#include "libtorrent/assert.hpp"
namespace libtorrent
{
// libtorrent time_duration type
struct time_duration
{
time_duration() {}
time_duration operator/(int rhs) const { return time_duration(diff / rhs); }
explicit time_duration(boost::int64_t d) : diff(d) {}
time_duration& operator-=(time_duration const& c) { diff -= c.diff; return *this; }
time_duration& operator+=(time_duration const& c) { diff += c.diff; return *this; }
2009-08-20 05:19:12 +02:00
time_duration& operator*=(int v) { diff *= v; return *this; }
time_duration operator+(time_duration const& c) { return time_duration(diff + c.diff); }
time_duration operator-(time_duration const& c) { return time_duration(diff - c.diff); }
boost::int64_t diff;
};
inline bool is_negative(time_duration dt) { return dt.diff < 0; }
2009-03-16 04:36:20 +01:00
inline bool operator==(time_duration lhs, time_duration rhs)
{ return lhs.diff == rhs.diff; }
inline bool operator<(time_duration lhs, time_duration rhs)
{ return lhs.diff < rhs.diff; }
inline bool operator<=(time_duration lhs, time_duration rhs)
{ return lhs.diff <= rhs.diff; }
inline bool operator>(time_duration lhs, time_duration rhs)
{ return lhs.diff > rhs.diff; }
inline bool operator>=(time_duration lhs, time_duration rhs)
{ return lhs.diff >= rhs.diff; }
inline time_duration operator*(time_duration lhs, int rhs)
2009-05-25 11:32:50 +02:00
{ return time_duration(boost::int64_t(lhs.diff * rhs)); }
inline time_duration operator*(int lhs, time_duration rhs)
2009-05-25 11:32:50 +02:00
{ return time_duration(boost::int64_t(lhs * rhs.diff)); }
// libtorrent time type
struct ptime
{
ptime() {}
explicit ptime(boost::uint64_t t): time(t) {}
ptime& operator+=(time_duration rhs) { time += rhs.diff; return *this; }
ptime& operator-=(time_duration rhs) { time -= rhs.diff; return *this; }
boost::uint64_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_hires();
inline ptime min_time() { return ptime(0); }
inline ptime max_time() { return ptime((std::numeric_limits<boost::uint64_t>::max)()); }
int total_seconds(time_duration td);
int total_milliseconds(time_duration td);
boost::int64_t total_microseconds(time_duration td);
}
#if defined TORRENT_USE_ABSOLUTE_TIME
#include <mach/mach_time.h>
#include <boost/cstdint.hpp>
2007-09-10 08:12:41 +02:00
#include "libtorrent/assert.hpp"
// high precision timer for darwin intel and ppc
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_hires()
{
static mach_timebase_info_data_t timebase_info = {0,0};
if (timebase_info.denom == 0)
mach_timebase_info(&timebase_info);
boost::uint64_t at = mach_absolute_time();
// make sure we don't overflow
TORRENT_ASSERT((at >= 0 && at >= at / 1000 * timebase_info.numer / timebase_info.denom)
|| (at < 0 && at < at / 1000 * timebase_info.numer / timebase_info.denom));
return ptime(at / 1000 * timebase_info.numer / timebase_info.denom);
}
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);
}
}
#elif defined TORRENT_USE_QUERY_PERFORMANCE_TIMER
2007-04-05 03:08:21 +02:00
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
2007-04-05 03:08:21 +02:00
#endif
2008-04-28 01:21:04 +02:00
#include <windows.h>
2007-09-10 08:12:41 +02:00
#include "libtorrent/assert.hpp"
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);
#ifdef TORRENT_DEBUG
2007-04-26 23:33:07 +02:00
// make sure we don't overflow
boost::int64_t ret = (pc * 1000 / performace_counter_frequency.QuadPart) * 1000;
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;
}
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);
#ifdef TORRENT_DEBUG
2007-04-26 23:33:07 +02:00
// make sure we don't overflow
boost::int64_t ret = (ms / 1000) * performace_counter_frequency.QuadPart / 1000;
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;
}
}
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);
}
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);
}
inline boost::int64_t total_microseconds(time_duration td)
{
return aux::performance_counter_to_microseconds(td.diff);
}
inline ptime time_now_hires()
{
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));
}
}
#elif defined TORRENT_USE_CLOCK_GETTIME
#include <time.h>
2007-09-10 08:12:41 +02:00
#include "libtorrent/assert.hpp"
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_hires()
{
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ptime(boost::uint64_t(ts.tv_sec) * 1000000 + ts.tv_nsec / 1000);
}
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);
}
}
#endif // TORRENT_USE_CLOCK_GETTIME
#endif // TORRENT_USE_BOOST_DATE_TIME
namespace libtorrent
{
namespace aux
{
extern TORRENT_EXPORT ptime g_current_time;
}
inline ptime const& time_now() { return aux::g_current_time; }
}
#endif // TORRENT_TIME_HPP_INCLUDED
2007-09-10 08:12:41 +02:00