peerplays-fc/include/fc/time.hpp

114 lines
5.9 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/optional.hpp>
2012-09-08 02:50:37 +00:00
#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable : 4244)
#endif //// _MSC_VER
2012-09-08 02:50:37 +00:00
namespace fc {
class microseconds {
public:
explicit microseconds( int64_t c = 0) :_count(c){}
static microseconds maximum() { return microseconds(0x7fffffffffffffffll); }
2012-09-08 02:50:37 +00:00
friend microseconds operator + (const microseconds& l, const microseconds& r ) { return microseconds(l._count+r._count); }
2013-07-18 23:10:05 +00:00
friend microseconds operator - (const microseconds& l, const microseconds& r ) { return microseconds(l._count-r._count); }
2012-09-08 02:50:37 +00:00
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; }
friend bool operator>=(const microseconds& a, const microseconds& b){ return a._count >= b._count; }
2013-01-04 20:35:17 +00:00
friend bool operator<(const microseconds& a, const microseconds& b){ return a._count < b._count; }
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 ); }
inline microseconds minutes(int64_t m) { return seconds(60*m); }
inline microseconds hours(int64_t h) { return minutes(60*h); }
inline microseconds days(int64_t d) { return hours(24*d); }
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 maximum() { return time_point( microseconds::maximum() ); }
2014-02-01 13:41:02 +00:00
static time_point min() { return time_point(); }
2012-10-18 02:44:21 +00:00
operator fc::string()const;
2013-01-20 20:44:16 +00:00
static time_point from_iso_string( const fc::string& s );
2012-10-18 02:44:21 +00:00
2012-09-08 02:50:37 +00:00
const microseconds& time_since_epoch()const { return elapsed; }
2014-04-28 01:20:40 +00:00
uint32_t sec_since_epoch()const { return elapsed.count() / 1000000; }
2012-09-08 02:50:37 +00:00
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-08 02:50:37 +00:00
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; }
2014-04-28 01:20:40 +00:00
time_point& operator += ( const microseconds& m) { elapsed+=m; return *this; }
2014-02-01 13:41:02 +00:00
time_point operator + (const microseconds& m) const { return time_point(elapsed+m); }
time_point operator - (const microseconds& m) const { return time_point(elapsed-m); }
microseconds operator - (const time_point& m) const { return microseconds(elapsed.count() - m.elapsed.count()); }
2012-09-08 02:50:37 +00:00
private:
microseconds elapsed;
};
2013-07-22 18:03:34 +00:00
/**
* A lower resolution time_point accurate only to seconds from 1970
*/
class time_point_sec
{
public:
time_point_sec()
:utc_seconds(0){}
explicit time_point_sec(uint32_t seconds )
:utc_seconds(seconds){}
2013-07-23 17:00:40 +00:00
time_point_sec( const time_point& t )
2013-07-22 18:03:34 +00:00
:utc_seconds( t.time_since_epoch().count() / 1000000ll ){}
operator time_point()const { return time_point( fc::seconds( utc_seconds) ); }
2013-07-23 17:00:40 +00:00
uint32_t sec_since_epoch()const { return utc_seconds; }
2013-07-22 18:03:34 +00:00
time_point_sec operator = ( const fc::time_point& t )
{
utc_seconds = t.time_since_epoch().count() / 1000000ll;
return *this;
}
2013-08-24 00:14:46 +00:00
friend bool operator < ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds < b.utc_seconds; }
friend bool operator > ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds > b.utc_seconds; }
2013-09-06 00:38:12 +00:00
friend bool operator <= ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds <= b.utc_seconds; }
friend bool operator >= ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds >= b.utc_seconds; }
2013-08-24 00:14:46 +00:00
friend bool operator == ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds == b.utc_seconds; }
2014-04-28 01:20:40 +00:00
friend bool operator != ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds != b.utc_seconds; }
2013-08-24 00:14:46 +00:00
time_point_sec& operator += ( uint32_t m ) { utc_seconds+=m; return *this; }
2013-07-22 18:03:34 +00:00
2014-02-06 07:47:09 +00:00
friend time_point operator - ( const time_point_sec& t, const microseconds& m ) { return time_point(t) - m; }
friend microseconds operator - ( const time_point_sec& t, const time_point_sec& m ) { return time_point(t) - time_point(m); }
friend microseconds operator - ( const time_point& t, const time_point_sec& m ) { return time_point(t) - time_point(m); }
2013-07-22 18:03:34 +00:00
private:
uint32_t utc_seconds;
};
typedef fc::optional<time_point> otime_point;
/** return a human-readable approximate time, relative to now()
* e.g., "4 hours ago", "2 months ago", etc.
*/
string get_approximate_relative_time_string(const time_point_sec& event_time);
string get_approximate_relative_time_string(const time_point& event_time);
2012-09-08 02:50:37 +00:00
}
#ifdef _MSC_VER
#pragma warning (pop)
#endif /// #ifdef _MSC_VER