Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
time.cpp
Go to the documentation of this file.
1#include <fc/time.hpp>
2#include <fc/mock_time.hpp>
3#include <fc/variant.hpp>
4#include <boost/chrono/system_clocks.hpp>
5#include <boost/date_time/posix_time/posix_time.hpp>
6#include <sstream>
7#include <fc/string.hpp>
9
10namespace fc {
11
12 namespace bch = boost::chrono;
13
15 {
18 }
19 return time_point( microseconds( bch::duration_cast<bch::microseconds>( bch::system_clock::now().time_since_epoch() ).count() ) );
20 }
21
23 {
24 const auto ptime = boost::posix_time::from_time_t( time_t( sec_since_epoch() ) );
25 return boost::posix_time::to_iso_string( ptime );
26 }
27
29 {
30 const auto ptime = boost::posix_time::from_time_t( time_t( sec_since_epoch() ) );
31 return boost::posix_time::to_iso_extended_string( ptime );
32 }
33
34 time_point_sec::operator fc::string()const
35 {
36 return this->to_iso_string();
37 }
38
40 { try {
41 static boost::posix_time::ptime epoch = boost::posix_time::from_time_t( 0 );
42 boost::posix_time::ptime pt;
43 if( s.size() >= 5 && s.at( 4 ) == '-' ) // http://en.wikipedia.org/wiki/ISO_8601
44 pt = boost::date_time::parse_delimited_time<boost::posix_time::ptime>( s, 'T' );
45 else
46 pt = boost::posix_time::from_iso_string( s );
47 return fc::time_point_sec( (pt - epoch).total_seconds() );
48 } FC_RETHROW_EXCEPTIONS( warn, "unable to convert ISO-formatted string to fc::time_point_sec" ) }
49
50 time_point::operator fc::string()const
51 {
52 auto count = elapsed.count();
53 if (count >= 0) {
54 uint64_t secs = (uint64_t)count / 1000000ULL;
55 uint64_t msec = ((uint64_t)count % 1000000ULL) / 1000ULL;
56 string padded_ms = to_string((uint64_t)(msec + 1000ULL)).substr(1);
57 const auto ptime = boost::posix_time::from_time_t(time_t(secs));
58 return boost::posix_time::to_iso_extended_string(ptime) + "." + padded_ms;
59 } else {
60 // negative time_points serialized as "durations" in the ISO form with boost
61 // this is not very human readable but fits the precedent set by the above
62 auto as_duration = boost::posix_time::microseconds(count);
63 return boost::posix_time::to_iso_string(as_duration);
64 }
65 }
66
68 { try {
69 auto dot = s.find( '.' );
70 if( dot == std::string::npos )
72 else {
73 auto ms = s.substr( dot );
74 ms[0] = '1';
75 while( ms.size() < 4 ) ms.push_back('0');
77 }
78 } FC_RETHROW_EXCEPTIONS( warn, "unable to convert ISO-formatted string to fc::time_point" ) }
79
80 void to_variant( const fc::time_point& t, variant& v ) {
81 v = fc::string( t );
82 }
86 void to_variant( const fc::time_point_sec& t, variant& v ) {
87 v = fc::string( t );
88 }
92
93 // inspired by show_date_relative() in git's date.c
95 const time_point_sec& relative_to_time /* = fc::time_point::now() */,
96 const std::string& default_ago /* = " ago" */) {
97
98
99 string ago = default_ago;
100 int32_t seconds_ago = relative_to_time.sec_since_epoch() - event_time.sec_since_epoch();
101 if (seconds_ago < 0)
102 {
103 ago = " in the future";
104 seconds_ago = -seconds_ago;
105 }
106 std::stringstream result;
107 if (seconds_ago < 90)
108 {
109 result << seconds_ago << " second" << (seconds_ago > 1 ? "s" : "") << ago;
110 return result.str();
111 }
112 uint32_t minutes_ago = (seconds_ago + 30) / 60;
113 if (minutes_ago < 90)
114 {
115 result << minutes_ago << " minute" << (minutes_ago > 1 ? "s" : "") << ago;
116 return result.str();
117 }
118 uint32_t hours_ago = (minutes_ago + 30) / 60;
119 if (hours_ago < 90)
120 {
121 result << hours_ago << " hour" << (hours_ago > 1 ? "s" : "") << ago;
122 return result.str();
123 }
124 uint32_t days_ago = (hours_ago + 12) / 24;
125 if (days_ago < 90)
126 {
127 result << days_ago << " day" << (days_ago > 1 ? "s" : "") << ago;
128 return result.str();
129 }
130 uint32_t weeks_ago = (days_ago + 3) / 7;
131 if (weeks_ago < 70)
132 {
133 result << weeks_ago << " week" << (weeks_ago > 1 ? "s" : "") << ago;
134 return result.str();
135 }
136 uint32_t months_ago = (days_ago + 15) / 30;
137 if (months_ago < 12)
138 {
139 result << months_ago << " month" << (months_ago > 1 ? "s" : "") << ago;
140 return result.str();
141 }
142 uint32_t years_ago = days_ago / 365;
143 result << years_ago << " year" << (months_ago > 1 ? "s" : "");
144 if (months_ago < 12 * 5)
145 {
146 uint32_t leftover_days = days_ago - (years_ago * 365);
147 uint32_t leftover_months = (leftover_days + 15) / 30;
148 if (leftover_months)
149 result << leftover_months << " month" << (months_ago > 1 ? "s" : "");
150 }
151 result << ago;
152 return result.str();
153 }
155 const time_point& relative_to_time /* = fc::time_point::now() */,
156 const std::string& ago /* = " ago" */) {
157 return get_approximate_relative_time_string(time_point_sec(event_time), time_point_sec(relative_to_time), ago);
158 }
159
160 void to_variant( const microseconds& input_microseconds, variant& output_variant )
161 {
162 output_variant = input_microseconds.count();
163 }
164 void from_variant( const variant& input_variant, microseconds& output_microseconds )
165 {
166 output_microseconds = microseconds(input_variant.as_int64());
167 }
168
169} //namespace fc
constexpr int64_t count() const
Definition time.hpp:26
static fc::time_point fc_now()
Definition mock_time.cpp:29
static bool is_set()
Definition mock_time.hpp:23
fc::string to_iso_string() const
Definition time.cpp:28
constexpr uint32_t sec_since_epoch() const
Definition time.hpp:88
static time_point_sec from_iso_string(const fc::string &s)
Definition time.cpp:39
fc::string to_non_delimited_iso_string() const
Definition time.cpp:22
static time_point now()
Definition time.cpp:14
constexpr time_point(microseconds e=microseconds())
Definition time.hpp:44
constexpr const microseconds & time_since_epoch() const
Definition time.hpp:52
static time_point from_iso_string(const fc::string &s)
Definition time.cpp:67
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
int64_t as_int64() const
Definition variant.cpp:377
string as_string() const
Definition variant.cpp:469
Defines exception's used by fc.
#define UNLIKELY(x)
#define FC_RETHROW_EXCEPTIONS(LOG_LEVEL, FORMAT,...)
Catchs all exception's, std::exceptions, and ... and rethrows them after appending the provided log m...
int * count
namespace sysio::chain
Definition authority.cpp:3
std::string string
Definition string.hpp:10
fc::string to_string(double)
Definition string.cpp:131
constexpr microseconds milliseconds(int64_t s)
Definition time.hpp:33
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:94
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
int64_t to_int64(const fc::string &)
Definition string.cpp:92
unsigned int uint32_t
Definition stdint.h:126
signed int int32_t
Definition stdint.h:123
unsigned __int64 uint64_t
Definition stdint.h:136
char * s