Boost logo

Boost :

Subject: [boost] Boost.DateTime: "logical error"
From: Шевченко Денис (boost_at_[hidden])
Date: 2011-11-06 11:55:11


Hi all!

I found "logical error" in Boost.DateTime.

Example:

int main() {
     using namespace boost::posix_time;
     ptime tm( time_from_string( "2011-01-01 00:00:00" ) );
     std::cout << tm << std::endl;
}

Output is ok:

2011-Jan-01 00:00:00

But if we don't specify the time:

int main() {
     using namespace boost::posix_time;
     ptime tm( time_from_string( "2011-01-01" ) );
     std::cout << tm << std::endl;
}

Output (on my computer) is strange:

2011-Mar-25 19:01:01

There is no any exception, but just... garbage.

Moreover:

int main() {
     using namespace boost::posix_time;
     ptime tm( from_iso_string( "20110101T000000" ) );
     std::cout << tm << std::endl;
}

Output is ok:

2011-Jan-01 00:00:00

But if we don't specify the time:

int main() {
     using namespace boost::posix_time;
     ptime tm( from_iso_string( "20110101" ) );
     std::cout << tm << std::endl;
}

Output is also strange:

2011-Jan-01 20:11:01.100000

There is no any exception too, but just garbage.

I think it is "logical error". If we define just date (i.e. particular day
of particular month of particular year) but don't specify the time
explicitly, it's logical to assume we had in mind the beginning of this
day, i.e. 00:00:00. So I think in functions time_from_string() and
from_iso_string() must exists checking of separator's existence (space or
'T'), and if it's not - time must be 00:00:00.

Yes, we can create correct ptime object with "zero" time using
gregorian::date, for example:

int main() {
     using namespace boost::posix_time;
     ptime tm( boost::gregorian::date( 2011, 1, 1 ) );
     std::cout << tm << std::endl;
}

In this case we have correct "zero" time:

2011-Jan-01 00:00:00

But it's important that if functions time_from_string() and
from_iso_string() _DO_NOT_ALLOW_ define date without explicit time - we
must get exception, but not a random garbage.

- Denis


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk