Boost logo

Boost Users :

Subject: [Boost-users] date_time question
From: Amadeus W.M. (amadeus84_at_[hidden])
Date: 2009-04-03 01:34:40


The program below reads a date from a string -> istringstream -> ptime.
The string contains comma separated fields, the first of which is a date
in the format %Y-%m-%d (without %H:%M:%S):

2009-04-03,12.34,56.78

If I define a facet with in the format %Y-%m-%d %H:%M:%S then the program
outputs

2009-Apr-03 12:34:56

i.e. it interprets the numbers following the date as %H:%M:%S even though
they are separated by commas and dots, rather than colons.

Is this normal behaviour? Would it be possible to make it leave alone the
fields following the date? That is, do not interpret 12.34,56.78 as
%H:%M:%S.

Thanks!

// g++ -g -Wall -o dateIOSimgleFmt -I/usr/local/include/boost-1_38
dateIOSingleFmt.C

#include <iostream>
#include <sstream>
#include <cstdlib>
#include <string>

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

using namespace std;
using namespace boost::posix_time;

int main(int argc, char * argv[])
{
    ptime pt(not_a_date_time);
    string datestr("2009-04-03,12.34,56.78");
    const char * format="%Y-%m-%d %H:%M:%S";

    istringstream INSTR(datestr);
    locale loc(INSTR.getloc(),new time_input_facet(format));

    INSTR.imbue(loc);
    INSTR>>pt;

    cout << pt << endl;

    return 0;
}


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