BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface software
time.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <stdint.h>
3 #include <string>
4 #include <fc/optional.hpp>
5 
6 #ifdef _MSC_VER
7  #pragma warning (push)
8  #pragma warning (disable : 4244)
9 #endif
10 
11 namespace fc {
12  class microseconds {
13  public:
14  explicit microseconds( int64_t c = 0) :_count(c){}
15  static microseconds maximum() { return microseconds(0x7fffffffffffffffll); }
16  friend microseconds operator + (const microseconds& l, const microseconds& r ) { return microseconds(l._count+r._count); }
17  friend microseconds operator - (const microseconds& l, const microseconds& r ) { return microseconds(l._count-r._count); }
18 
19 
20  bool operator==(const microseconds& c)const { return _count == c._count; }
21  bool operator!=(const microseconds& c)const { return _count != c._count; }
22  friend bool operator>(const microseconds& a, const microseconds& b){ return a._count > b._count; }
23  friend bool operator>=(const microseconds& a, const microseconds& b){ return a._count >= b._count; }
24  friend bool operator<(const microseconds& a, const microseconds& b){ return a._count < b._count; }
25  friend bool operator<=(const microseconds& a, const microseconds& b){ return a._count <= b._count; }
26  microseconds& operator+=(const microseconds& c) { _count += c._count; return *this; }
27  microseconds& operator-=(const microseconds& c) { _count -= c._count; return *this; }
28  int64_t count()const { return _count; }
29  int64_t to_seconds()const { return _count/1000000; }
30  private:
31  friend class time_point;
32  int64_t _count;
33  };
34  inline microseconds seconds( int64_t s ) { return microseconds( s * 1000000 ); }
35  inline microseconds milliseconds( int64_t s ) { return microseconds( s * 1000 ); }
36  inline microseconds minutes(int64_t m) { return seconds(60*m); }
37  inline microseconds hours(int64_t h) { return minutes(60*h); }
38  inline microseconds days(int64_t d) { return hours(24*d); }
39 
40  class variant;
41  void to_variant( const fc::microseconds&, fc::variant&, uint32_t max_depth = 1 );
42  void from_variant( const fc::variant&, fc::microseconds&, uint32_t max_depth = 1 );
43 
44  class time_point {
45  public:
46  explicit time_point( microseconds e = microseconds() ) :elapsed(e){}
47  static time_point now();
49  static time_point min() { return time_point(); }
50 
51  operator std::string()const;
52  static time_point from_iso_string( const std::string& s );
53 
54  const microseconds& time_since_epoch()const { return elapsed; }
55  uint32_t sec_since_epoch()const { return elapsed.count() / 1000000; }
56  bool operator > ( const time_point& t )const { return elapsed._count > t.elapsed._count; }
57  bool operator >=( const time_point& t )const { return elapsed._count >=t.elapsed._count; }
58  bool operator < ( const time_point& t )const { return elapsed._count < t.elapsed._count; }
59  bool operator <=( const time_point& t )const { return elapsed._count <=t.elapsed._count; }
60  bool operator ==( const time_point& t )const { return elapsed._count ==t.elapsed._count; }
61  bool operator !=( const time_point& t )const { return elapsed._count !=t.elapsed._count; }
62  time_point& operator += ( const microseconds& m) { elapsed+=m; return *this; }
63  time_point& operator -= ( const microseconds& m) { elapsed-=m; return *this; }
64  time_point operator + (const microseconds& m) const { return time_point(elapsed+m); }
65  time_point operator - (const microseconds& m) const { return time_point(elapsed-m); }
66  microseconds operator - (const time_point& m) const { return microseconds(elapsed.count() - m.elapsed.count()); }
67  private:
68  microseconds elapsed;
69  };
70 
75  {
76  public:
78  :utc_seconds(0){}
79 
80  explicit time_point_sec(uint32_t seconds )
81  :utc_seconds(seconds){}
82 
84  :utc_seconds( t.time_since_epoch().count() / 1000000ll ){}
85 
86  static time_point_sec maximum() { return time_point_sec(0xffffffff); }
87  static time_point_sec min() { return time_point_sec(0); }
88 
89  operator time_point()const { return time_point( fc::seconds( utc_seconds) ); }
90  uint32_t sec_since_epoch()const { return utc_seconds; }
91 
92  time_point_sec operator = ( const fc::time_point& t )
93  {
94  utc_seconds = t.time_since_epoch().count() / 1000000ll;
95  return *this;
96  }
97  friend bool operator < ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds < b.utc_seconds; }
98  friend bool operator > ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds > b.utc_seconds; }
99  friend bool operator <= ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds <= b.utc_seconds; }
100  friend bool operator >= ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds >= b.utc_seconds; }
101  friend bool operator == ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds == b.utc_seconds; }
102  friend bool operator != ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds != b.utc_seconds; }
103  time_point_sec& operator += ( uint32_t m ) { utc_seconds+=m; return *this; }
104  time_point_sec& operator += ( microseconds m ) { utc_seconds+=m.to_seconds(); return *this; }
105  time_point_sec& operator -= ( uint32_t m ) { utc_seconds-=m; return *this; }
106  time_point_sec& operator -= ( microseconds m ) { utc_seconds-=m.to_seconds(); return *this; }
107  time_point_sec operator +( uint32_t offset )const { return time_point_sec(utc_seconds + offset); }
108  time_point_sec operator -( uint32_t offset )const { return time_point_sec(utc_seconds - offset); }
109 
110  friend time_point operator + ( const time_point_sec& t, const microseconds& m ) { return time_point(t) + m; }
111  friend time_point operator - ( const time_point_sec& t, const microseconds& m ) { return time_point(t) - m; }
112  friend microseconds operator - ( const time_point_sec& t, const time_point_sec& m ) { return time_point(t) - time_point(m); }
113  friend microseconds operator - ( const time_point& t, const time_point_sec& m ) { return time_point(t) - time_point(m); }
114 
115  std::string to_non_delimited_iso_string()const;
116  std::string to_iso_string()const;
117 
118  operator std::string()const;
119  static time_point_sec from_iso_string( const std::string& s );
120 
121  private:
122  uint32_t utc_seconds;
123  };
124 
126 
130  std::string get_approximate_relative_time_string(const time_point_sec& event_time,
131  const time_point_sec& relative_to_time = fc::time_point::now(),
132  const std::string& ago = " ago");
133  std::string get_approximate_relative_time_string(const time_point& event_time,
134  const time_point& relative_to_time = fc::time_point::now(),
135  const std::string& ago = " ago");
136 }
137 
138 #include <fc/reflect/reflect.hpp>
142 
143 #ifdef _MSC_VER
144  #pragma warning (pop)
145 #endif
bool operator!=(const microseconds &c) const
Definition: time.hpp:21
std::string get_approximate_relative_time_string(const time_point_sec &event_time, const time_point_sec &relative_to_time=fc::time_point::now(), const std::string &ago=" ago")
Definition: time.cpp:70
int64_t to_seconds() const
Definition: time.hpp:29
uint32_t sec_since_epoch() const
Definition: time.hpp:55
microseconds minutes(int64_t m)
Definition: time.hpp:36
friend bool operator<(const microseconds &a, const microseconds &b)
Definition: time.hpp:24
uint32_t sec_since_epoch() const
Definition: time.hpp:90
microseconds milliseconds(int64_t s)
Definition: time.hpp:35
Defines types and macros used to provide reflection.
microseconds hours(int64_t h)
Definition: time.hpp:37
friend bool operator>(const microseconds &a, const microseconds &b)
Definition: time.hpp:22
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
fc::optional< time_point > otime_point
Definition: time.hpp:125
FC_REFLECT_TYPENAME(fc::log_message)
friend microseconds operator-(const microseconds &l, const microseconds &r)
Definition: time.hpp:17
microseconds days(int64_t d)
Definition: time.hpp:38
time_point_sec(uint32_t seconds)
Definition: time.hpp:80
friend class time_point
Definition: time.hpp:31
static time_point_sec maximum()
Definition: time.hpp:86
microseconds(int64_t c=0)
Definition: time.hpp:14
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
static time_point min()
Definition: time.hpp:49
microseconds seconds(int64_t s)
Definition: time.hpp:34
int64_t count() const
Definition: time.hpp:28
friend bool operator>=(const microseconds &a, const microseconds &b)
Definition: time.hpp:23
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
microseconds & operator+=(const microseconds &c)
Definition: time.hpp:26
microseconds & operator-=(const microseconds &c)
Definition: time.hpp:27
friend bool operator<=(const microseconds &a, const microseconds &b)
Definition: time.hpp:25
const microseconds & time_since_epoch() const
Definition: time.hpp:54
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
bool operator==(const microseconds &c) const
Definition: time.hpp:20
friend microseconds operator+(const microseconds &l, const microseconds &r)
Definition: time.hpp:16
Definition: api.hpp:15
static time_point_sec min()
Definition: time.hpp:87
static time_point maximum()
Definition: time.hpp:48
time_point_sec(const time_point &t)
Definition: time.hpp:83
time_point(microseconds e=microseconds())
Definition: time.hpp:46
static microseconds maximum()
Definition: time.hpp:15
static time_point now()
Definition: time.cpp:13