Boost logo

Boost Users :

Subject: Re: [Boost-users] [date time] using date_facet to output to a stream once
From: Steve Lorimer (steve.lorimer_at_[hidden])
Date: 2010-06-16 12:10:36


Help please - I'm going mad!

I use a date_facet object, set it's format to "%b %y", expecting to format
my ptime object as "*Dec 1**0*". Instead, I'm getting "*2010-Dec-01 00:00:00
*" - what am I doing wrong?

My code is below:

On 15 June 2010 15:26, Steve Lorimer <steve.lorimer_at_[hidden]> wrote:

> 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 1**0*", 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_at_[hidden]> 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
>>
>
>



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