Boost logo

Boost Users :

Subject: [Boost-users] date_time question
From: Amadeus W.M. (amadeus84_at_[hidden])
Date: 2008-12-21 10:31:44


I want to read from stdin files that contain dates and data. The files
come from various sources so the dates are in different formats. Of
course, the problem is reading the dates. Based on the date_time examples
I put together a few lines of code using facets that I thought should
work, but it doesn't seem to. Can't tell if I'm way off, or I'm missing
something small.

#include <boost/date_time/posix_time/posix_time.hpp>

using namespace boost::posix_time;
using namespace std;

int main(int argc, char * argv[])
{
    // Create a facet with the expected format.
    time_input_facet * tfacet=new time_input_facet("%Y/%m/%d %H:%M:%S");

    ptime pt(not_a_date_time);
    cin.imbue(locale(cin.getloc(),tfacet));
    cin >> pt;
    cout << pt << endl; // pt still not_a_date_time.

    return 0;
}

Compilation:

g++ -g -Wall -o facets -I/usr/local/include/boost-1_35 -L/usr/local/lib/
facets.C -lboost_date_time-gcc43-mt-1_35

Run:

echo "12/20/2008 09:56:00" | ./facets
not-a-date-time # pt unchanged

Once I sort out how to read dates from stdin (initially with the format
hard-coded) I have to figure out how to parse the date and guess the
format internally in the program.

Please help! Thanks!


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