|
Boost Users : |
From: Michal Lijowski (michal_at_[hidden])
Date: 2005-09-16 10:11:57
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/Fedora/RPMS.
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
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