BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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 
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
fc::time_point_sec::sec_since_epoch
uint32_t sec_since_epoch() const
Definition: time.hpp:90
fc::milliseconds
microseconds milliseconds(int64_t s)
Definition: time.hpp:35
fc::time_point_sec::operator>
friend bool operator>(const time_point_sec &a, const time_point_sec &b)
Definition: time.hpp:98
fc::time_point_sec::operator+=
time_point_sec & operator+=(uint32_t m)
Definition: time.hpp:103
fc::time_point::from_iso_string
static time_point from_iso_string(const std::string &s)
Definition: time.cpp:51
fc::time_point_sec::operator-=
time_point_sec & operator-=(uint32_t m)
Definition: time.hpp:105
fc::time_point_sec::operator-
time_point_sec operator-(uint32_t offset) const
Definition: time.hpp:108
fc::time_point::operator<=
bool operator<=(const time_point &t) const
Definition: time.hpp:59
FC_REFLECT_TYPENAME
FC_REFLECT_TYPENAME(fc::log_message)
fc::time_point::sec_since_epoch
uint32_t sec_since_epoch() const
Definition: time.hpp:55
fc::time_point_sec::from_iso_string
static time_point_sec from_iso_string(const std::string &s)
Definition: time.cpp:35
fc
Definition: api.hpp:15
fc::time_point_sec::operator<
friend bool operator<(const time_point_sec &a, const time_point_sec &b)
Definition: time.hpp:97
fc::time_point_sec::operator<=
friend bool operator<=(const time_point_sec &a, const time_point_sec &b)
Definition: time.hpp:99
fc::microseconds::operator+
friend microseconds operator+(const microseconds &l, const microseconds &r)
Definition: time.hpp:16
fc::time_point::operator+=
time_point & operator+=(const microseconds &m)
Definition: time.hpp:62
fc::time_point_sec::maximum
static time_point_sec maximum()
Definition: time.hpp:86
fc::time_point::operator-
time_point operator-(const microseconds &m) const
Definition: time.hpp:65
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
fc::microseconds::microseconds
microseconds(int64_t c=0)
Definition: time.hpp:14
reflect.hpp
Defines types and macros used to provide reflection.
fc::time_point_sec::time_point_sec
time_point_sec()
Definition: time.hpp:77
fc::time_point_sec::operator==
friend bool operator==(const time_point_sec &a, const time_point_sec &b)
Definition: time.hpp:101
fc::seconds
microseconds seconds(int64_t s)
Definition: time.hpp:34
fc::microseconds::maximum
static microseconds maximum()
Definition: time.hpp:15
fc::otime_point
fc::optional< time_point > otime_point
Definition: time.hpp:125
fc::time_point::time_since_epoch
const microseconds & time_since_epoch() const
Definition: time.hpp:54
fc::time_point_sec::operator!=
friend bool operator!=(const time_point_sec &a, const time_point_sec &b)
Definition: time.hpp:102
fc::time_point::operator!=
bool operator!=(const time_point &t) const
Definition: time.hpp:61
fc::days
microseconds days(int64_t d)
Definition: time.hpp:38
fc::microseconds::operator==
bool operator==(const microseconds &c) const
Definition: time.hpp:20
fc::microseconds::operator!=
bool operator!=(const microseconds &c) const
Definition: time.hpp:21
fc::microseconds::operator+=
microseconds & operator+=(const microseconds &c)
Definition: time.hpp:26
fc::microseconds::operator-=
microseconds & operator-=(const microseconds &c)
Definition: time.hpp:27
fc::time_point_sec
Definition: time.hpp:74
fc::time_point_sec::time_point_sec
time_point_sec(uint32_t seconds)
Definition: time.hpp:80
fc::time_point::min
static time_point min()
Definition: time.hpp:49
fc::time_point::operator<
bool operator<(const time_point &t) const
Definition: time.hpp:58
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
fc::microseconds::operator<
friend bool operator<(const microseconds &a, const microseconds &b)
Definition: time.hpp:24
fc::minutes
microseconds minutes(int64_t m)
Definition: time.hpp:36
fc::hours
microseconds hours(int64_t h)
Definition: time.hpp:37
fc::microseconds::to_seconds
int64_t to_seconds() const
Definition: time.hpp:29
fc::time_point_sec::operator>=
friend bool operator>=(const time_point_sec &a, const time_point_sec &b)
Definition: time.hpp:100
fc::time_point_sec::to_non_delimited_iso_string
std::string to_non_delimited_iso_string() const
Definition: time.cpp:18
fc::microseconds::operator-
friend microseconds operator-(const microseconds &l, const microseconds &r)
Definition: time.hpp:17
fc::time_point::time_point
time_point(microseconds e=microseconds())
Definition: time.hpp:46
fc::get_approximate_relative_time_string
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
fc::microseconds
Definition: time.hpp:12
fc::time_point::now
static time_point now()
Definition: time.cpp:13
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
fc::time_point_sec::min
static time_point_sec min()
Definition: time.hpp:87
fc::time_point
Definition: time.hpp:44
fc::time_point::maximum
static time_point maximum()
Definition: time.hpp:48
fc::time_point_sec::time_point_sec
time_point_sec(const time_point &t)
Definition: time.hpp:83
fc::microseconds::operator>=
friend bool operator>=(const microseconds &a, const microseconds &b)
Definition: time.hpp:23
fc::microseconds::count
int64_t count() const
Definition: time.hpp:28
fc::time_point_sec::operator=
time_point_sec operator=(const fc::time_point &t)
Definition: time.hpp:92
fc::time_point_sec::operator+
time_point_sec operator+(uint32_t offset) const
Definition: time.hpp:107
fc::time_point::operator>
bool operator>(const time_point &t) const
Definition: time.hpp:56
fc::time_point::operator+
time_point operator+(const microseconds &m) const
Definition: time.hpp:64
fc::time_point::operator>=
bool operator>=(const time_point &t) const
Definition: time.hpp:57
fc::optional
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
fc::microseconds::operator<=
friend bool operator<=(const microseconds &a, const microseconds &b)
Definition: time.hpp:25
fc::time_point::operator==
bool operator==(const time_point &t) const
Definition: time.hpp:60
fc::microseconds::operator>
friend bool operator>(const microseconds &a, const microseconds &b)
Definition: time.hpp:22
fc::time_point::operator-=
time_point & operator-=(const microseconds &m)
Definition: time.hpp:63
fc::time_point_sec::to_iso_string
std::string to_iso_string() const
Definition: time.cpp:24
optional.hpp