Boost logo

Boost Users :

From: Gerardo Lamastra (gerardo.lamastra_at_[hidden])
Date: 2007-07-18 17:53:13


Hello everybody,

I've seen that boost::posix_time supports a conversion function
time_t from_time_t(ptime t)
I've also seen on the mailing list, that something related to
this issue has already been discussed. However, I've not found
enough information (browsing ML and bug support) to understand
if this issue is considered closed, or if anyone has never run
into similar problems:

We're using this function, and a counterpart ptime to_time_t(time_t)
function because our application interacts with a MySQL db where date
information is stored as unix timestamp. While processing the data,
we run in a problem (I won't say 'bug') because MySQL and boost give
different meaning to unix timestamp.
For example, timestamp 1184795149 is 2007-Jul-18 23:45:49 (local time)
for MySQL and it is 2007-Jul-18 21:45:49 (local time) for boost.
The correct value is the first one.
My current timezone offset is 2 hours, so you can see that we are
missing UTC here.

Checking the code, I've seen that the function basically converts
by adding the number of second to 1/1/1970 0:0:0; however, it should
be 1/1/1970 0:0:0 UTC, which is not.
Jeff Garland touches this issue in this post:

http://aspn.activestate.com/ASPN/Mail/Message/1758411

where he encourages C++ developer to move away from the time_t
interface; however, often, for legacy reason, this may not be possible
(as in our application).

In order to comply with other implementation, we suggest to modify the
function as follows:

// The Boost Actual Implementation
ptime boost_from_time_t(std::time_t t)
{
    ptime start(boost::gregorian::date(1970,1,1));
    return start + seconds(static_cast<long>(t));
}

// Our 'old' function, written to match the previous one
time_t boost_to_time_t(ptime t)
{
    static const ptime THE_EPOCH(boost::gregorian::date(1970,1,1));
    return time_period(THE_EPOCH, t).length().total_seconds();
}

// The implementations that takes into account UTC
ptime my_from_time_t(std::time_t t)
{
    static const time_duration OFFSET =
        second_clock::local_time() - second_clock::universal_time();
    static const ptime THE_EPOCH(boost::gregorian::date(1970,1,1),
OFFSET);
    return THE_EPOCH + seconds(static_cast<long>(t));
}

time_t my_to_time_t(ptime t)
{
    static const time_duration OFFSET =
        second_clock::local_time() - second_clock::universal_time();
    static const ptime THE_EPOCH(boost::gregorian::date(1970,1,1),
OFFSET);
    return time_period(THE_EPOCH, t).length().total_seconds();
}

That's all. Thanks a lot for any response.

-Gerardo
--------------------------------------------------------------------

CONFIDENTIALITY NOTICE

This message and its attachments are addressed solely to the persons above and may contain confidential information. If you have received the message in error, be informed that any use of the content hereof is prohibited. Please return it immediately to the sender and delete the message. Should you have any questions, please contact us by replying to webmaster_at_telecomitalia.it.

        Thank you

                                        www.telecomitalia.it

--------------------------------------------------------------------
                        


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