Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
time_get.cpp
Go to the documentation of this file.
1
29// NOTE: A SunPro bug requires time.h preceed locale. PR #26255
30#include <ctime> // for struct tm
31#include <locale> // for locale, time_get
32#include <sstream> // for stringstream
33#include <iostream> // for cout, endl
34
35
36// Print out a tm struct value in one atomic operation
37std::ostream& operator<< (std::ostream &os, const std::tm &t)
38{
39 std::stringstream strm;
40
41 strm << "Daylight Savings = " << t.tm_isdst
42 << "\nDay of year = " << t.tm_yday
43 << "\nDay of week = " << t.tm_wday
44 << "\nYear = " << t.tm_year
45 << "\nMonth = " << t.tm_mon
46 << "\nDay of month = " << t.tm_mday
47 << "\nHour = " << t.tm_hour
48 << "\nMinute = " << t.tm_min
49 << "\nSecond = " << t.tm_sec
50 << '\n';
51
52 // guard for thread safety and output synchronization
53 const std::ostream::sentry guard (os);
54
55 if (guard)
56 os.rdbuf ()->sputn (strm.str ().c_str (), strm.str ().size ());
57 else
58 os.setstate (os.failbit);
59
60 return os;
61}
62
63
64int main ()
65{
66 typedef std::istreambuf_iterator<char, std::char_traits<char> > Iter;
67
68 // time struct to parse date into
69 static std::tm timeb; // zero initialized
70
71 // Unused, required by time_get
72 std::ios_base::iostate state;
73
74 // Stream object to read from
75 std::istringstream ins ("");
76
77 // Iterators into the stream object
78 Iter begin (ins);
79 Iter end;
80
81 const std::locale loc ("C");
82
83 // Get a reference to the time_get facet in locale loc.
84 const std::time_get<char, Iter> &tg =
85 std::use_facet<std::time_get<char, Iter> >(loc);
86
87 // Display time_base::dateorder value.
88 std::cout << "time_base::dateorder == " << tg.date_order () << ".\n";
89
90 // Insert date string into stream.
91 ins.str ("04/07/69");
92
93 // get_date from the stream and output tm contents.
94 tg.get_date (begin, end, ins, state, &timeb);
95 std::cout << "Date: Apr 7 1969\n" << timeb << std::endl;
96
97 // Insert weekday string into stream.
98 ins.str ("Monday");
99
100 // get_weekday from the stream and output tm contents.
101 tg.get_weekday (begin, end, ins, state, &timeb);
102 std::cout << "Weekday: Monday\n" << timeb << std::endl;
103
104 // Insert time string into stream.
105 ins.str ("06:47:32");
106
107 // get_time from the stream and output tm contents.
108 tg.get_time (begin, end, ins, state, &timeb);
109 std::cout << "Time: 06:47:32\n" << timeb << std::endl;
110
111 return 0;
112}
#define t
Definition 17.names.cpp:136
#define os
Definition 17.names.cpp:511
#define loc
Definition 17.names.cpp:492
#define strm
Definition 17.names.cpp:549
const int end
std::ostream_iterator< int, char, std::char_traits< char > > Iter
Definition alg7.cpp:64
std::ostream & operator<<(std::ostream &os, const std::tm &t)
Definition time_get.cpp:37
int main()
Definition time_get.cpp:64