
I just started with date_time library using boost-1.33.0-3 on FC4 and gcc 4.0.1 20050727. It is rpm version downloaded from http://download.fedora.redhat.com/pub/fedora/linux/core/development/i386/Fed.... I copied some lines of code from boost_date_time.pdf to get familiar with capabilities of this library and I encountered some problems. Here is a modified code taken from page 3 of the manual /* The following is a simple example that shows conversion of dates * to and from a std::string. ** Expected output: * 2001-Oct-09 * 2001-10-09 * Tuesday October 9, 2001 * An expected exception is next: * Exception: Month number is out of range 1..12 */ #include "boost/date_time/gregorian/gregorian.hpp" #include "boost/date_time/posix_time/posix_time.hpp" #include "boost/date_time/posix_time/posix_time_duration.hpp" #include "boost/date_time/local_time/local_time.hpp" #include <iostream> #include <string> #include <sstream> using namespace boost::gregorian; using namespace boost::local_time; using namespace boost::posix_time; using namespace std; int main() { try { //input streaming stringstream ss("2004-Jan-1"); date d; ss >> d; cout << " d " << d << endl; ptime t0; ss >> t0; cout << " t0 " << t0 << endl; date d1(1999, Jan, 1); ss >> d1; cout << " d1 " << d1 << endl; ptime t2(d1, minutes(0)); cout << " t2 " << t2 << endl; ss >> t2; cout << " t2 " << t2 << endl; date d3(2002,Feb,1); //an arbitrary date // the line below compiles with error: ‘nanosec’ was not declared in this scope // ptime t1(d3, hours(5)+nanosec(100)); //date + time of day offset ptime t1(d3, hours(5)); cout << " d3 " << d3 << endl; ptime t3 = t1 - minutes(4)+seconds(2); ptime now = second_clock::local_time(); //use the clock date today = now.date(); //Get the date part out of the time date tomorrow = today + date_duration(1); ptime tomorrow_start(tomorrow); //midnight // std::stringstream ss1("2004-Jan-1 05:21:33.20"); stringstream ss1("2004-Jan-1"); ss1 >> t2; cout << " t2 " << t2 << endl; //starting at current time iterator adds by one hour time_iterator titr(now,hours(1)); for (; titr < tomorrow_start; ++titr) { cout << (*titr) << std::endl; } } catch(std::exception& e) { std::cout << " Exception: " << e.what() << std::endl; } return 0; } And output from this program is d not-a-date-time t0 not-a-date-time d1 1999-Jan-01 t2 1999-Jan-01 00:00:00 t2 1999-Jan-01 00:00:00 d3 2002-Feb-01 t2 2004-Jan-01 00:00:00 2005-Sep-16 08:58:09 2005-Sep-16 09:58:09 2005-Sep-16 10:58:09 2005-Sep-16 11:58:09 2005-Sep-16 12:58:09 2005-Sep-16 13:58:09 2005-Sep-16 14:58:09 2005-Sep-16 15:58:09 2005-Sep-16 16:58:09 2005-Sep-16 17:58:09 2005-Sep-16 18:58:09 2005-Sep-16 19:58:09 2005-Sep-16 20:58:09 2005-Sep-16 21:58:09 2005-Sep-16 22:58:09 2005-Sep-16 23:58:09 My question is why d and t0 are not-a-date-time? Is this expected? Also the line ptime t1(d3, hours(5)+nanosec(100)); //date + time of day offset compiles with error: ‘nanosec’ was not declared in this scope Thanks, Michal

"Michal Lijowski"
I just started with date_time library using boost-1.33.0-3 on FC4 and gcc 4.0.1 20050727.
It is rpm version downloaded from http://download.fedora.redhat.com/pub/fedora/linux/core/development/i386/Fed....
I copied some lines of code from boost_date_time.pdf to get familiar with capabilities of this library and I encountered some problems.
Here is a modified code taken from page 3 of the manual
...snip...
My question is why d and t0 are not-a-date-time? Is this expected?
Yes, this is expected. The default constructors for date & ptime initialize to not_a_date_time.
Also the line
ptime t1(d3, hours(5)+nanosec(100)); //date + time of day offset
compiles with error: 'nanosec' was not declared in this scope
date_time can be compiled with either microsecond resolution or nanosecond resolution. If microsecond resolution (the default) was used, the nanosec code won't be available. To enable nanosecond resolution, simply define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG. Bart

Is the only way to initialize date and ptime using strings like date d(from_simple_string(s)); NOT stringstreams stringstream ss("2004-Jan-1"); date; ss >> d; Michal On Fri, 2005-09-16 at 16:36 -0700, Bart wrote:
"Michal Lijowski"
wrote in message news:1126883517.24859.2.camel@ml-cvu.wustl.edu... I just started with date_time library using boost-1.33.0-3 on FC4 and gcc 4.0.1 20050727.
It is rpm version downloaded from http://download.fedora.redhat.com/pub/fedora/linux/core/development/i386/Fed....
I copied some lines of code from boost_date_time.pdf to get familiar with capabilities of this library and I encountered some problems.
Here is a modified code taken from page 3 of the manual
...snip...
My question is why d and t0 are not-a-date-time? Is this expected?
Yes, this is expected. The default constructors for date & ptime initialize to not_a_date_time.
Also the line
ptime t1(d3, hours(5)+nanosec(100)); //date + time of day offset
compiles with error: 'nanosec' was not declared in this scope
date_time can be compiled with either microsecond resolution or nanosecond resolution. If microsecond resolution (the default) was used, the nanosec code won't be available.
To enable nanosecond resolution, simply define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG.
Bart
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Tue, 20 Sep 2005 11:41:33 -0500, Michal Lijowski wrote:
Is the only way to initialize date and ptime using strings like date d(from_simple_string(s));
NOT stringstreams
stringstream ss("2004-Jan-1"); date; ss >> d;
Michal
Using boost 1.33 you can use either method. If you use the stringstream method, you do not have to link with the library. The to_*_string & from_*_string functions require linking. In the following example, the date will first be initialized to not_a_date_time, and then set to 2004-Jan-01. date d; // d == not_a_date_time std::stringstream ss("2004-Jan-01"); ss >> d; // d == 2004-Jan-01 Also note, the default format for streaming in dates it "%Y-%b-%d". The %d flag requires two digits, so "2004-Jan-1" will not parse correctly and leave it set to not_a_date_time. It is possible to "turn-on" exceptions to catch these kinds of errors. See: http://tinyurl.com/8qzu4 for details. Bart
participants (2)
-
Bart
-
Michal Lijowski