Money and Time objects for iostream

I was browsing <iomanip> while writing this function:
http://cplusplus.com/forum/beginner/64156/#msg347042

I created a class that simultaneously outputs to ostream and ofstream objects and I was trying to accomodate all manipulators (std::hex, std::setw(), etc)

I happened to see this:
1
2
3
4
5
template<class _Elem,	class _Traits,	class _Money> inline
basic_ostream<_Elem, _Traits>&	operator<<(basic_ostream<_Elem, _Traits>& _Ostr, const _Monobj<_Money>& _Manip)
{	// put monetary amount to output stream output stream
//...
}

and this:
1
2
3
4
5
template<class _Elem, class _Traits> inline
basic_ostream<_Elem, _Traits>& operator<<(basic_ostream<_Elem, _Traits>& _Ostr,	const _Timeobj<_Elem>& _Manip)
{	// put time information to output stream
//...
}


This implies that there is default handing for money and time objects. I've looked for documentation on this but haven't seen anything. Does anybody know what this feature is or how to use it?

I believe these objects are created with the std::get_time() or the std::get_money() functions but I don't know how to set the money structure or the format flags. Can someone point me to a reference? (Not like this-> &)

I tried this out since struct tm* looked familiar:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <ctime>
#include <iostream>
#include <iomanip>

int main()
{
	time_t rawtime;
	struct tm* timeinfo;
	
	time(&rawtime);
	timeinfo = localtime(&rawtime);
	int format = 0;

	std::cout << std::get_time(timeinfo, &format);
}


I get an error stating that no overloaded function matches ostream << _Timeobj despite what I just pasted above from <iomanip>

Last edited on
> Can someone point me to a reference?

See the examples (towards the end) in:
http://en.cppreference.com/w/cpp/io/manip/get_time
http://en.cppreference.com/w/cpp/io/manip/get_money

To understand what is going on, you might want to read this first:
http://www.angelikalanger.com/Articles/C++Report/StandardFacets/StandardFacets.html

Great references, Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <ctime>
#include <iostream>
#include <iomanip>

int main()
{
	time_t rawtime;
	struct tm* timeinfo;
	
	time(&rawtime);
	timeinfo = localtime(&rawtime);

	std::cout << std::put_time(timeinfo,"%d %b %y - %H:%M:%S") << std::endl;

	return 0;
}
10 Mar 12 - 15:22:28
Press any key to continue . . .


I'm really surprised about the amount of things added to these functions in C++11
Last edited on
> I created a class that simultaneously outputs to ostream and ofstream objects and I was trying
> to accomodate all manipulators (std::hex, std::setw(), etc)

Just dug up this home-grown teestream:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>

template < typename C, typename T = std::char_traits<C> >

struct basic_teebuf : public std::basic_streambuf<C,T>
{
    typedef std::basic_streambuf<C,T> streambuf_type ;
    typedef typename T::int_type int_type ;

    basic_teebuf( streambuf_type* buff_a, streambuf_type* buff_b )
            : first(buff_a), second(buff_b) {}

    protected:
        virtual int_type overflow( int_type c )
        {
            const int_type eof = T::eof() ;
            if( T::eq_int_type( c, eof ) ) return T::not_eof(c) ;
            else
            {
                const C ch = T::to_char_type(c) ;
                if( T::eq_int_type( first->sputc(ch), eof ) ||
                    T::eq_int_type( second->sputc(ch), eof ) )
                        return eof ;
                else return c ;
            }
        }

        virtual int sync()
        { return !first->pubsync() && !second->pubsync() ? 0 : -1 ; }

    private:
        streambuf_type* first ;
        streambuf_type* second ;
};

template < typename C, typename T = std::char_traits<C> >
struct basic_teestream : public std::basic_ostream<C,T>
{
    typedef std::basic_ostream<C,T> stream_type ;
    typedef basic_teebuf<C,T> streambuff_type ;

    basic_teestream( stream_type& first, stream_type& second )
         : stream_type(&stmbuf), stmbuf( first.rdbuf(), second.rdbuf() ) {}

    basic_teestream( streambuff_type* first, streambuff_type* second )
         : stream_type(&stmbuf), stmbuf(first,second ) {}

    ~basic_teestream() { stmbuf.pubsync() ; }

    private: streambuff_type stmbuf ;
};

typedef basic_teebuf<char> teebuf ;
typedef basic_teestream<char> teestream ;

#include <fstream>
#include <iomanip>
#include <algorithm>
#include <iterator>

int main()
{
    std::ofstream file( "out.txt" ) ;
    teestream tee( std::cout, file ) ;
    tee << std::boolalpha << ('A'==65) << std::endl ;

    std::ofstream file2( "out2.txt" ) ;
    teestream tee_3way( tee, file2 ) ;
    tee_3way << "hello world!" << std::fixed << std::setw(12) << 12.45 << '\n' ;

    int a[] = { 20, 30, 40, 50, 60 } ;
    std::ofstream file3( "out3.txt" ) ;
    teestream tee_4way( tee_3way, file3 ) ;
    tee_4way << std::hex << std::showbase ;
    std::copy( a, a+sizeof(a)/sizeof(*a),
               std::ostream_iterator<int>( tee_4way, "\n" ) ) ;
}


It is a tutorial example written a few years ago, so it is pure C++98, and it is not designed to be the most efficient implementation. But it is very flexible.
Last edited on
Topic archived. No new replies allowed.