Use ISO standard delimited timestamp string serialization
This commit is contained in:
parent
42ff2b10de
commit
f948bd759d
3 changed files with 67 additions and 62 deletions
|
|
@ -47,8 +47,8 @@ namespace fc {
|
||||||
static time_point now();
|
static time_point now();
|
||||||
static time_point maximum() { return time_point( microseconds::maximum() ); }
|
static time_point maximum() { return time_point( microseconds::maximum() ); }
|
||||||
static time_point min() { return time_point(); }
|
static time_point min() { return time_point(); }
|
||||||
operator fc::string()const;
|
|
||||||
|
|
||||||
|
operator fc::string()const;
|
||||||
static time_point from_iso_string( const fc::string& s );
|
static time_point from_iso_string( const fc::string& s );
|
||||||
|
|
||||||
const microseconds& time_since_epoch()const { return elapsed; }
|
const microseconds& time_since_epoch()const { return elapsed; }
|
||||||
|
|
@ -109,8 +109,11 @@ namespace fc {
|
||||||
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_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); }
|
friend microseconds operator - ( const time_point& t, const time_point_sec& m ) { return time_point(t) - time_point(m); }
|
||||||
|
|
||||||
|
fc::string to_non_delimited_iso_string()const;
|
||||||
fc::string to_iso_string()const;
|
fc::string to_iso_string()const;
|
||||||
fc::string to_iso_extended_string()const;
|
|
||||||
|
operator fc::string()const;
|
||||||
|
static time_point_sec from_iso_string( const fc::string& s );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint32_t utc_seconds;
|
uint32_t utc_seconds;
|
||||||
|
|
|
||||||
|
|
@ -34,16 +34,6 @@ namespace fc {
|
||||||
return time_point_sec( (uint32_t)(file_number * interval_seconds) );
|
return time_point_sec( (uint32_t)(file_number * interval_seconds) );
|
||||||
}
|
}
|
||||||
|
|
||||||
string timestamp_to_string( const time_point_sec& timestamp )
|
|
||||||
{
|
|
||||||
return timestamp.to_iso_string();
|
|
||||||
}
|
|
||||||
|
|
||||||
time_point_sec string_to_timestamp( const string& str )
|
|
||||||
{
|
|
||||||
return time_point::from_iso_string( str );
|
|
||||||
}
|
|
||||||
|
|
||||||
void compress_file( const fc::path& filename )
|
void compress_file( const fc::path& filename )
|
||||||
{
|
{
|
||||||
FC_ASSERT( cfg.rotate && cfg.rotation_compression );
|
FC_ASSERT( cfg.rotate && cfg.rotation_compression );
|
||||||
|
|
@ -95,7 +85,7 @@ namespace fc {
|
||||||
FC_ASSERT( cfg.rotate );
|
FC_ASSERT( cfg.rotate );
|
||||||
fc::time_point now = time_point::now();
|
fc::time_point now = time_point::now();
|
||||||
fc::time_point_sec start_time = get_file_start_time( now, cfg.rotation_interval );
|
fc::time_point_sec start_time = get_file_start_time( now, cfg.rotation_interval );
|
||||||
string timestamp_string = timestamp_to_string( start_time );
|
string timestamp_string = start_time.to_non_delimited_iso_string();
|
||||||
fc::path link_filename = cfg.filename;
|
fc::path link_filename = cfg.filename;
|
||||||
fc::path log_filename = link_filename.parent_path() / (link_filename.filename().string() + "." + timestamp_string);
|
fc::path log_filename = link_filename.parent_path() / (link_filename.filename().string() + "." + timestamp_string);
|
||||||
|
|
||||||
|
|
@ -134,7 +124,7 @@ namespace fc {
|
||||||
continue;
|
continue;
|
||||||
string current_timestamp_str = current_filename.substr(link_filename_string.size() + 1,
|
string current_timestamp_str = current_filename.substr(link_filename_string.size() + 1,
|
||||||
timestamp_string.size());
|
timestamp_string.size());
|
||||||
fc::time_point_sec current_timestamp = string_to_timestamp( current_timestamp_str );
|
fc::time_point_sec current_timestamp = fc::time_point_sec::from_iso_string( current_timestamp_str );
|
||||||
if( current_timestamp < start_time )
|
if( current_timestamp < start_time )
|
||||||
{
|
{
|
||||||
if( current_timestamp < limit_time || file_size( current_filename ) <= 0 )
|
if( current_timestamp < limit_time || file_size( current_filename ) <= 0 )
|
||||||
|
|
|
||||||
56
src/time.cpp
56
src/time.cpp
|
|
@ -8,40 +8,52 @@
|
||||||
#include <fc/exception/exception.hpp>
|
#include <fc/exception/exception.hpp>
|
||||||
|
|
||||||
namespace fc {
|
namespace fc {
|
||||||
|
|
||||||
namespace bch = boost::chrono;
|
namespace bch = boost::chrono;
|
||||||
time_point time_point::now() {
|
|
||||||
|
time_point time_point::now()
|
||||||
|
{
|
||||||
return time_point( microseconds( bch::duration_cast<bch::microseconds>( bch::system_clock::now().time_since_epoch() ).count() ) );
|
return time_point( microseconds( bch::duration_cast<bch::microseconds>( bch::system_clock::now().time_since_epoch() ).count() ) );
|
||||||
}
|
}
|
||||||
time_point::operator fc::string()const {
|
|
||||||
bch::system_clock::time_point tp;
|
|
||||||
tp += bch::microseconds( elapsed._count);
|
|
||||||
time_t tt = bch::system_clock::to_time_t(tp);
|
|
||||||
|
|
||||||
return boost::posix_time::to_iso_string( boost::posix_time::from_time_t(tt) + boost::posix_time::microseconds( elapsed._count % 1000000 ) );
|
fc::string time_point_sec::to_non_delimited_iso_string()const
|
||||||
}
|
|
||||||
time_point time_point::from_iso_string( const fc::string& s )
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
auto pt = boost::posix_time::from_iso_string(s);
|
|
||||||
//return fc::time_point(fc::seconds( (pt - boost::posix_time::from_time_t(0)).total_seconds() ));
|
|
||||||
return fc::time_point(fc::microseconds( (pt - boost::posix_time::from_time_t(0)).total_microseconds() ));
|
|
||||||
}
|
|
||||||
FC_RETHROW_EXCEPTIONS(warn, "unable to convert ISO-formatted string to fc::time_point")
|
|
||||||
}
|
|
||||||
|
|
||||||
fc::string time_point_sec::to_iso_string()const
|
|
||||||
{
|
{
|
||||||
const auto ptime = boost::posix_time::from_time_t( time_t( sec_since_epoch() ) );
|
const auto ptime = boost::posix_time::from_time_t( time_t( sec_since_epoch() ) );
|
||||||
return boost::posix_time::to_iso_string( ptime );
|
return boost::posix_time::to_iso_string( ptime );
|
||||||
}
|
}
|
||||||
|
|
||||||
fc::string time_point_sec::to_iso_extended_string()const
|
fc::string time_point_sec::to_iso_string()const
|
||||||
{
|
{
|
||||||
const auto ptime = boost::posix_time::from_time_t( time_t( sec_since_epoch() ) );
|
const auto ptime = boost::posix_time::from_time_t( time_t( sec_since_epoch() ) );
|
||||||
return boost::posix_time::to_iso_extended_string( ptime );
|
return boost::posix_time::to_iso_extended_string( ptime );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
time_point_sec::operator fc::string()const
|
||||||
|
{
|
||||||
|
return this->to_iso_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
time_point_sec time_point_sec::from_iso_string( const fc::string& s )
|
||||||
|
{ try {
|
||||||
|
static boost::posix_time::ptime epoch = boost::posix_time::from_time_t( 0 );
|
||||||
|
boost::posix_time::ptime pt;
|
||||||
|
if( s.size() >= 5 && s.at( 4 ) == '-' ) // http://en.wikipedia.org/wiki/ISO_8601
|
||||||
|
pt = boost::date_time::parse_delimited_time<boost::posix_time::ptime>( s, 'T' );
|
||||||
|
else
|
||||||
|
pt = boost::posix_time::from_iso_string( s );
|
||||||
|
return fc::time_point_sec( (pt - epoch).total_seconds() );
|
||||||
|
} FC_RETHROW_EXCEPTIONS( warn, "unable to convert ISO-formatted string to fc::time_point_sec" ) }
|
||||||
|
|
||||||
|
time_point::operator fc::string()const
|
||||||
|
{
|
||||||
|
return fc::string( time_point_sec( *this ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
time_point time_point::from_iso_string( const fc::string& s )
|
||||||
|
{ try {
|
||||||
|
return time_point( time_point_sec::from_iso_string( s ) );
|
||||||
|
} FC_RETHROW_EXCEPTIONS( warn, "unable to convert ISO-formatted string to fc::time_point" ) }
|
||||||
|
|
||||||
void to_variant( const fc::time_point& t, variant& v ) {
|
void to_variant( const fc::time_point& t, variant& v ) {
|
||||||
v = fc::string( t );
|
v = fc::string( t );
|
||||||
}
|
}
|
||||||
|
|
@ -49,10 +61,10 @@ namespace fc {
|
||||||
t = fc::time_point::from_iso_string( v.as_string() );
|
t = fc::time_point::from_iso_string( v.as_string() );
|
||||||
}
|
}
|
||||||
void to_variant( const fc::time_point_sec& t, variant& v ) {
|
void to_variant( const fc::time_point_sec& t, variant& v ) {
|
||||||
v = fc::string(fc::time_point(t));
|
v = fc::string( t );
|
||||||
}
|
}
|
||||||
void from_variant( const fc::variant& v, fc::time_point_sec& t ) {
|
void from_variant( const fc::variant& v, fc::time_point_sec& t ) {
|
||||||
t = fc::time_point::from_iso_string(v.as_string());
|
t = fc::time_point_sec::from_iso_string( v.as_string() );
|
||||||
}
|
}
|
||||||
|
|
||||||
// inspired by show_date_relative() in git's date.c
|
// inspired by show_date_relative() in git's date.c
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue