I have made some progress, but am now getting unexpected results:

I have the following in my class:

    date_facet* fmt;
    mutable std::stringstream desig_stream;

and I do the following in my constructor:

    fmt(new date_facet);
    desig_stream.imbue(std::locale(std::locale::classic(), fmt));

at some point in my code I call

    fmt->format("%b %y"); // Mmm yy

and in my class's operator<<(ostream&...) I do the following:

    e.desig_stream.str("");
    e.desig_stream << e.desig;
    os << e.desig_stream.str();

because of the fact that I set the format to "%b %y", I would expect to see "Dec 10", instead, I'm getting "2010-Dec-01 00:00:00"

What must I change for my stringstream / date_facet objects to work together such that I can output a ptime object to a stream and obtain the expected format "%b %y"?

TIA
Steve

 
On 15 June 2010 14:38, Steve Lorimer <steve.lorimer@gmail.com> wrote:
Hi

I have a an object which contains a ptime and an int mask. The mask tells me that regardless of the value stored inside the ptime object, I should display the ptime as such.

If you look at the code below (set_fmt function), I set the format of a date_facet object according to some bitmasks. 

I now want to use this date_facet together with my ptime to output the ptime value to an outputstream?
I don't want it to change the outputstream's settings for ALL ptime values, just for this single instance

All the examples use a date_facet allocated on the heap, and use std::cout.imbue(...), whereas I'm looking for functionality similar to strftime... is this possible?

eg:

class expiration
{
    enum expiry_mask
    {
        expiry_null   = 0x00,
        expiry_year   = 0x01,
        expiry_month  = 0x02,
        expiry_day    = 0x04,
        expiry_hour   = 0x08,
        expiry_minute = 0x10,
        expiry_second = 0x20,
        expiry_date   = 0x07, // yyyy/mm/dd
        expiry_time   = 0x38, // hh:nn:ss
        expiry_all    = 0x3f  // yyyy/mm/dd hh:nn:ss
    };

boost::posix_time::ptime desig;
int32_t desig_mask;
boost::gregorian::date_facet fmt;

public:
void expiration::set_fmt(const int32_t mask)
{
    switch(mask & expiry_date) // strip off time part of mask
    {
        case expiry_null:
            fmt.format("");
            break;
        case expiry_year | expiry_month | expiry_day: // display as "dd Mmm yy"
            fmt.format("%d %b %y");
            break;
        case expiry_year | expiry_month: // display as "Mmm yy"
            fmt.format("%b %y");
            break;
        case expiry_year: // display as "yy"
            fmt.format("%y");
            break;
        default:
            break;
    }
}

... some other stuff here ...

};

Thanks
Steve