Discussion:
boost::posix_time::from_time_t bug ???
Łukasz Majewski
2009-05-01 12:29:48 UTC
Permalink
Hello,

i think that i've found bug in date time library or maybe i made
something wrog.

SAMPLE CODE:

#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>

using namespace boost::posix_time;

int main( int argc, char** argv )
{
time_t t = time( 0 );

std::cout << "time_t: " << ctime( &t ) << std::endl;

struct tm* t2 = localtime( &t );
std::cout << "struct tm: " << asctime( t2 ) << std::endl;

ptime t3 = from_time_t( t );

std::cout << "ptime from time_t: " << to_simple_string( t3 ) <<
std::endl;

ptime t4 = ptime_from_tm( *t2 );

std::cout << "ptime from tm: " << to_simple_string( t4 ) <<
std::endl;

return 0;
}

OUTPUT:

time_t: Fri May 1 14:22:01 2009
struct tm: Fri May 1 14:22:01 2009
ptime from time_t: 2009-May-01 12:22:01 // incorrect result
ptime from tm: 2009-May-01 14:22:01
Łukasz Majewski
2009-05-01 12:31:40 UTC
Permalink
I forgot i'm using boost 1.38

W dniu 1 maja 2009 14:29 u¿ytkownik £ukasz Majewski <
Post by Łukasz Majewski
Hello,
i think that i've found bug in date time library or maybe i made
something wrog.
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
using namespace boost::posix_time;
int main( int argc, char** argv )
{
time_t t = time( 0 );
std::cout << "time_t: " << ctime( &t ) << std::endl;
struct tm* t2 = localtime( &t );
std::cout << "struct tm: " << asctime( t2 ) << std::endl;
ptime t3 = from_time_t( t );
std::cout << "ptime from time_t: " << to_simple_string( t3 ) <<
std::endl;
ptime t4 = ptime_from_tm( *t2 );
std::cout << "ptime from tm: " << to_simple_string( t4 ) <<
std::endl;
return 0;
}
time_t: Fri May 1 14:22:01 2009
struct tm: Fri May 1 14:22:01 2009
ptime from time_t: 2009-May-01 12:22:01 // incorrect result
ptime from tm: 2009-May-01 14:22:01
Igor R
2009-05-01 15:48:10 UTC
Permalink
IMHO, the output is correct.
        std::cout << "time_t: " << ctime( &t ) << std::endl;
        struct tm* t2 = localtime( &t );
        std::cout << "struct tm: " << asctime( t2 ) << std::endl;
        ptime t3 = from_time_t( t );
        std::cout << "ptime from time_t: " << to_simple_string( t3 ) <<
std::endl;
        ptime t4 = ptime_from_tm( *t2 );
Loading...