Boost logo

Boost Users :

Subject: [Boost-users] time_t to ptime conversion problem
From: Scott Vanderweyst (svanderw_at_[hidden])
Date: 2010-04-22 14:57:41


While doing some testing I've come across what I consider a bug in the
boost::posix::from_time_t function. I'm running this on Visual studio 2005
due to needing to deal with some legacy code.
I'm using boost v 1.39 -

Issue is that if you create a time_t representing a date after Jan 18 2038
using standard functions, when the time_t is loaded into the ptime it's
rolling back to 1901. In Visual studio 2005, time_t is 64 bit.

Example code:

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/time_parsers.hpp>
int main()
{
    struct tm timeStruct;
    boost::posix_time::ptime boostTime;
    time_t timeVal;

    memset(&timeStruct, 0, sizeof(timeStruct));
    timeStruct.tm_year = 138;
    timeStruct.tm_mon = 0;
    timeStruct.tm_mday = 18;
    timeVal = mktime(&timeStruct);
    std::cout << timeVal << std::endl;
    boostTime = boost::posix_time::from_time_t(timeVal);
    std::cout << boostTime << std::endl;

    memset(&timeStruct, 0, sizeof(timeStruct));
    timeStruct.tm_year = 138;
    timeStruct.tm_mon = 0;
    timeStruct.tm_mday = 19;
    timeVal = mktime(&timeStruct);
    std::cout << timeVal << std::endl;
    boostTime = boost::posix_time::from_time_t(timeVal);
    std::cout << boostTime << std::endl;

    return 0;
}

which outputs:
2147403600
2038-Jan-18 05:00:00
2147490000
1901-Dec-13 22:31:44

which is not quite the result I was hoping for.

Any suggestions other than not using time_t? ( It's been considered and
rejected as a possibility for now)

Thanks in advance
Scott V



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net