BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
time.cpp
Go to the documentation of this file.
1 #include <fc/time.hpp>
2 #include <fc/variant.hpp>
3 #include <boost/chrono/system_clocks.hpp>
4 #include <boost/date_time/posix_time/posix_time.hpp>
5 #include <sstream>
6 #include <fc/io/sstream.hpp>
8 
9 namespace fc {
10 
11  namespace bch = boost::chrono;
12 
14  {
15  return time_point( microseconds( bch::duration_cast<bch::microseconds>( bch::system_clock::now().time_since_epoch() ).count() ) );
16  }
17 
19  {
20  const auto ptime = boost::posix_time::from_time_t( time_t( sec_since_epoch() ) );
21  return boost::posix_time::to_iso_string( ptime );
22  }
23 
24  std::string time_point_sec::to_iso_string()const
25  {
26  const auto ptime = boost::posix_time::from_time_t( time_t( sec_since_epoch() ) );
27  return boost::posix_time::to_iso_extended_string( ptime );
28  }
29 
30  time_point_sec::operator std::string()const
31  {
32  return this->to_iso_string();
33  }
34 
36  { try {
37  static boost::posix_time::ptime epoch = boost::posix_time::from_time_t( 0 );
38  boost::posix_time::ptime pt;
39  if( s.size() >= 5 && s.at( 4 ) == '-' ) // http://en.wikipedia.org/wiki/ISO_8601
40  pt = boost::date_time::parse_delimited_time<boost::posix_time::ptime>( s, 'T' );
41  else
42  pt = boost::posix_time::from_iso_string( s );
43  return fc::time_point_sec( (pt - epoch).total_seconds() );
44  } FC_RETHROW_EXCEPTIONS( warn, "unable to convert ISO-formatted string to fc::time_point_sec" ) }
45 
46  time_point::operator std::string()const
47  {
48  return std::string( time_point_sec( *this ) );
49  }
50 
51  time_point time_point::from_iso_string( const std::string& s )
52  { try {
54  } FC_RETHROW_EXCEPTIONS( warn, "unable to convert ISO-formatted string to fc::time_point" ) }
55 
56  void to_variant( const fc::time_point& t, variant& v, uint32_t max_depth ) {
57  to_variant( std::string( t ), v, max_depth );
58  }
59  void from_variant( const fc::variant& v, fc::time_point& t, uint32_t max_depth ) {
61  }
62  void to_variant( const fc::time_point_sec& t, variant& v, uint32_t max_depth ) {
63  to_variant( std::string( t ), v, max_depth );
64  }
65  void from_variant( const fc::variant& v, fc::time_point_sec& t, uint32_t max_depth ) {
67  }
68 
69  // inspired by show_date_relative() in git's date.c
71  const time_point_sec& relative_to_time /* = fc::time_point::now() */,
72  const std::string& default_ago /* = " ago" */) {
73 
74 
75  string ago = default_ago;
76  int32_t seconds_ago = relative_to_time.sec_since_epoch() - event_time.sec_since_epoch();
77  if (seconds_ago < 0)
78  {
79  ago = " in the future";
80  seconds_ago = -seconds_ago;
81  }
82  stringstream result;
83  if (seconds_ago < 90)
84  {
85  result << seconds_ago << " second" << (seconds_ago > 1 ? "s" : "") << ago;
86  return result.str();
87  }
88  uint32_t minutes_ago = (seconds_ago + 30) / 60;
89  if (minutes_ago < 90)
90  {
91  result << minutes_ago << " minute" << (minutes_ago > 1 ? "s" : "") << ago;
92  return result.str();
93  }
94  uint32_t hours_ago = (minutes_ago + 30) / 60;
95  if (hours_ago < 90)
96  {
97  result << hours_ago << " hour" << (hours_ago > 1 ? "s" : "") << ago;
98  return result.str();
99  }
100  uint32_t days_ago = (hours_ago + 12) / 24;
101  if (days_ago < 90)
102  {
103  result << days_ago << " day" << (days_ago > 1 ? "s" : "") << ago;
104  return result.str();
105  }
106  uint32_t weeks_ago = (days_ago + 3) / 7;
107  if (weeks_ago < 70)
108  {
109  result << weeks_ago << " week" << (weeks_ago > 1 ? "s" : "") << ago;
110  return result.str();
111  }
112  uint32_t months_ago = (days_ago + 15) / 30;
113  if (months_ago < 12)
114  {
115  result << months_ago << " month" << (months_ago > 1 ? "s" : "") << ago;
116  return result.str();
117  }
118  uint32_t years_ago = days_ago / 365;
119  result << years_ago << " year" << (months_ago > 1 ? "s" : "");
120  if (months_ago < 12 * 5)
121  {
122  uint32_t leftover_days = days_ago - (years_ago * 365);
123  uint32_t leftover_months = (leftover_days + 15) / 30;
124  if (leftover_months)
125  result << leftover_months << " month" << (months_ago > 1 ? "s" : "");
126  }
127  result << ago;
128  return result.str();
129  }
131  const time_point& relative_to_time /* = fc::time_point::now() */,
132  const std::string& ago /* = " ago" */) {
133  return get_approximate_relative_time_string(time_point_sec(event_time), time_point_sec(relative_to_time), ago);
134  }
135 
136  void to_variant( const microseconds& input_microseconds, variant& output_variant, uint32_t max_depth )
137  {
138  output_variant = input_microseconds.count();
139  }
140  void from_variant( const variant& input_variant, microseconds& output_microseconds, uint32_t max_depth )
141  {
142  output_microseconds = microseconds(input_variant.as_int64());
143  }
144 
145 } //namespace fc
fc::time_point_sec::sec_since_epoch
uint32_t sec_since_epoch() const
Definition: time.hpp:90
fc::time_point::from_iso_string
static time_point from_iso_string(const std::string &s)
Definition: time.cpp:51
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::variant::as_string
std::string as_string() const
Definition: variant.cpp:469
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
fc::stringstream::str
std::string str()
Definition: sstream.cpp:33
fc::time_point::time_since_epoch
const microseconds & time_since_epoch() const
Definition: time.hpp:54
sstream.hpp
fc::time_point_sec
Definition: time.hpp:74
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
fc::time_point_sec::to_non_delimited_iso_string
std::string to_non_delimited_iso_string() const
Definition: time.cpp:18
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::as_int64
int64_t as_int64() const
Definition: variant.cpp:377
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
fc::time_point
Definition: time.hpp:44
exception.hpp
Defines exception's used by fc.
FC_RETHROW_EXCEPTIONS
#define FC_RETHROW_EXCEPTIONS(LOG_LEVEL, FORMAT,...)
Catchs all exception's, std::exceptions, and ... and rethrows them after appending the provided log m...
Definition: exception.hpp:464
fc::microseconds::count
int64_t count() const
Definition: time.hpp:28
variant.hpp
time.hpp
fc::stringstream
Definition: sstream.hpp:7
fc::time_point_sec::to_iso_string
std::string to_iso_string() const
Definition: time.cpp:24