peerplays-fc/include/fc/time.hpp

57 lines
2.8 KiB
C++
Raw Normal View History

#pragma once
2012-09-08 02:50:37 +00:00
#include <stdint.h>
2012-10-18 02:44:21 +00:00
#include <fc/string.hpp>
#include <fc/raw.hpp>
2012-09-08 02:50:37 +00:00
namespace fc {
class microseconds {
public:
explicit microseconds( int64_t c = 0) :_count(c){}
static microseconds max() { return microseconds(0x7fffffffffffffffll); }
friend microseconds operator + (const microseconds& l, const microseconds& r ) { return microseconds(l._count+r._count); }
bool operator==(const microseconds& c)const { return _count == c._count; }
2012-09-23 06:01:27 +00:00
friend bool operator>(const microseconds& a, const microseconds& b){ return a._count > b._count; }
2012-09-21 23:31:51 +00:00
microseconds& operator+=(const microseconds& c) { _count += c._count; return *this; }
2012-09-08 02:50:37 +00:00
int64_t count()const { return _count; }
private:
friend class time_point;
int64_t _count;
};
2012-09-09 03:46:19 +00:00
inline microseconds seconds( int64_t s ) { return microseconds( s * 1000000 ); }
2012-09-09 04:25:43 +00:00
inline microseconds milliseconds( int64_t s ) { return microseconds( s * 1000 ); }
2012-09-08 02:50:37 +00:00
class time_point {
public:
explicit time_point( microseconds e = microseconds() ) :elapsed(e){}
static time_point now();
static time_point max() { return time_point( microseconds::max() ); }
static time_point min() { return time_point(); }
2012-10-18 02:44:21 +00:00
operator fc::string()const;
2012-09-08 02:50:37 +00:00
const microseconds& time_since_epoch()const { return elapsed; }
bool operator > ( const time_point& t )const { return elapsed._count > t.elapsed._count; }
bool operator < ( const time_point& t )const { return elapsed._count < t.elapsed._count; }
bool operator <=( const time_point& t )const { return elapsed._count <=t.elapsed._count; }
bool operator ==( const time_point& t )const { return elapsed._count ==t.elapsed._count; }
bool operator !=( const time_point& t )const { return elapsed._count !=t.elapsed._count; }
2012-09-21 23:31:51 +00:00
time_point& operator += ( const microseconds& m ) { elapsed+=m; return *this; }
2012-09-08 02:50:37 +00:00
friend time_point operator + ( const time_point& t, const microseconds& m ) { return time_point(t.elapsed+m); }
friend microseconds operator - ( const time_point& t, const time_point& m ) { return microseconds(t.elapsed.count() - m.elapsed.count()); }
private:
microseconds elapsed;
};
// forward declare io
class value;
void pack( fc::value& , const fc::time_point& );
void unpack( const fc::value& , fc::time_point& );
namespace raw {
template<typename Stream, typename T>
void unpack( Stream& s, fc::time_point& v );
template<typename Stream, typename T>
void pack( Stream& s, const fc::time_point& v );
}
2012-09-08 02:50:37 +00:00
}