Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
time_put.cpp
Go to the documentation of this file.
1
29// NOTE: due to a SunPro bug, <ctime> must preceed <locale> (see PR #26255)
30#include <ctime> // for struct tm
31#include <exception> // for exception
32#include <iostream> // for cout, endl
33#include <locale> // for locale, time_put
34#include <sstream> // for ostringstream
35
36#include <examples.h>
37
38
39int main (int argc, char *argv[])
40{
41 // zero-out all members (including any non-standard ones)
42 std::tm t = std::tm ();
43
44 // assign individual members, being careful to avoid relying
45 // on the implementation-defined layout of the struct
46 t.tm_sec = 56; // seconds
47 t.tm_min = 34; // minutes
48 t.tm_hour = 14; // hours (i.e., 2 PM)
49 t.tm_mday = 3; // day of month
50 t.tm_mon = 1; // month (February)
51 t.tm_year = 84; // years since 1900 (i.e., 1984)
52 t.tm_yday = 33; // day of the year
53 t.tm_wday = 5; // Sunday-based weekday (Friday)
54 t.tm_isdst = 0; // Daylight Saving Time
55
56 // array of all supported conversion specifiers
57 static const struct {
58 // const below commented out to avoid compiler warnings
59 // such as HP aCC's Warning 578: A class member may not
60 // be a const array since there is no way to initialize
61 // such a member.
62 /* const */ char spec [4]; // specifier
63 /* const */ char desc [40]; // description
64 } fmt[] = {
65 // plain (unmodified) conversion specifiers
66 { "%a", "abbreviated weekday name" },
67 { "%A", "full weekday name" },
68 { "%b", "abbreviated month name" },
69 { "%B", "full month name" },
70 { "%c", "date and time" },
71 { "%C", "two-digit year [00,99]" },
72 { "%d", "month number [01,31]" },
73 { "%D", "date as \"%m/%d/%y\"" },
74 { "%e", "month number [ 1,31]" },
75 { "%F", "date as \"%Y-%m-%d\"" },
76 { "%g", "week-based year [00,99]" },
77 { "%G", "week-based year" },
78 { "%h", "same as \"%b\"" },
79 { "%H", "hour [00-23]" },
80 { "%I", "hour [01-12]" },
81 { "%j", "day of the year [001,366]" },
82 { "%k", "hour [ 0,23]" },
83 { "%l", "hour [ 1,12]" },
84 { "%m", "month number [01,12]" },
85 { "%M", "minute [00,59]" },
86 { "%n", "newline character" },
87 { "%p", "a.m. or p.m. equivalent" },
88 { "%r", "AM/PM time" },
89 { "%R", "24-hour time" },
90 { "%S", "second [00,60]" },
91 { "%t", "tab character" },
92 { "%T", "time as \"%H:%M:%S\"" },
93 { "%u", "Monday-based weekday [1,7]" },
94 { "%U", "week number [00,53]" },
95 { "%V", "week numver [01,53]" },
96 { "%w", "weekday [0,6]" },
97 { "%W", "week number [00,53]" },
98 { "%x", "date" },
99 { "%X", "time" },
100 { "%y", "two-digit year [00,99]" },
101 { "%Y", "year" },
102 { "%z", "offset from UTC" },
103 { "%Z", "time zone" },
104 { "%%", "percent character" },
105
106 // modified conversion specifiers
107 { "%Ec", "" },
108 { "%EC", "" },
109 { "%Ex", "" },
110 { "%EX", "" },
111 { "%Ey", "" },
112 { "%EY", "" },
113 { "%Od", "" },
114 { "%Oe", "" },
115 { "%OH", "" },
116 { "%OI", "" },
117 { "%Om", "" },
118 { "%OM", "" },
119 { "%OS", "" },
120 { "%Ou", "" },
121 { "%OU", "" },
122 { "%OV", "" },
123 { "%Ow", "" },
124 { "%OW", "" },
125 { "%Oy", "" }
126 };
127
128 if (1 < argc) {
129 try {
130 // imbue the locale specified by the argument in cout
131 std::cout.imbue (std::locale (argv [1]));
132 }
133 catch (std::exception &ex) {
134 // print out the reason for the failure and exit
135 std::cerr << ex.what () << '\n';
136 return 1;
137 }
138 }
139
140 // retrieve the time_put facet installed in cout
141 const std::time_put<char> &tput =
142 std::use_facet<std::time_put<char> >(std::cout.getloc ());
143
144 // left-justify text
145 std::cout.setf (std::cout.left);
146
147 // print out the time value formatted according to each
148 // of the conversion specifiers defined above
149 for (std::size_t i = 0; i != sizeof fmt / sizeof *fmt; ++i) {
150
151 const char* const spec = fmt [i].spec;
152
153 std::ostringstream tmp;
154
155 // format the directive, in quotes, in a temporary stringstream
156 tmp << '"' << spec << '"';
157
158 // format the quoted directive in a field of the given width
159 std::cout << " ";
160 std::cout.width (8);
161 std::cout << tmp.str ();
162
163 // clear the temporary stringstream
164 tmp.str ("");
165
166 tmp << '"';
167 if ('n' == spec [1])
168 tmp << "\\n";
169 else if ('t' == spec [1])
170 tmp << "\\t";
171 else
172 tput.put (std::ostreambuf_iterator<char>(tmp.rdbuf ()),
173 std::cout, '\0', &t, spec,
174 spec + std::char_traits<char>::length (spec));
175
176 tmp << '"';
177
178 // format the string in a field of the given width
179 std::cout.width (32);
180 std::cout << tmp.str ();
181
182 // clear the temporary stringstream
183 tmp.str ("");
184
185 const char* const d = spec + 1 + ('E' == *spec || 'O' == *spec);
186
187 if ('c' == *d || 'r' == *d || 'x' == *d || 'X' == *d) {
188
189 if (*fmt [i].desc) {
190 // format the description of the directive
191 tmp << fmt [i].desc << " (";
192 }
193
194 tmp << "\"";
195
196 // extension of this implementation:
197 // for %c, %r, %x, and %X, write out the formatting
198 // string that the conversion specifier expands to
199 tput.put (std::ostreambuf_iterator<char>(tmp.rdbuf ()),
200 std::cout, '\0', 0, spec,
201 spec + std::char_traits<char>::length (spec));
202
203 tmp << "\"";
204
205 if (*fmt [i].desc)
206 tmp << ')';
207 }
208 else if (*fmt [i].desc) {
209 // format the description of the directive
210 tmp << fmt [i].desc;
211 }
212
213 std::cout << tmp.str () << '\n';
214 }
215
216 return 0;
217}
#define i
Definition 17.names.cpp:126
#define t
Definition 17.names.cpp:136
#define d
Definition 17.names.cpp:115
#define tmp
Definition 17.names.cpp:553
#define left
#define main