time_members.cc revision 102782
155682Smarkm// std::time_get, std::time_put implementation, GNU version -*- C++ -*-
255682Smarkm
355682Smarkm// Copyright (C) 2001, 2002 Free Software Foundation, Inc.
455682Smarkm//
555682Smarkm// This file is part of the GNU ISO C++ Library.  This library is free
655682Smarkm// software; you can redistribute it and/or modify it under the
755682Smarkm// terms of the GNU General Public License as published by the
855682Smarkm// Free Software Foundation; either version 2, or (at your option)
955682Smarkm// any later version.
1055682Smarkm
1155682Smarkm// This library is distributed in the hope that it will be useful,
1255682Smarkm// but WITHOUT ANY WARRANTY; without even the implied warranty of
1355682Smarkm// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1455682Smarkm// GNU General Public License for more details.
1555682Smarkm
1655682Smarkm// You should have received a copy of the GNU General Public License along
1755682Smarkm// with this library; see the file COPYING.  If not, write to the Free
1855682Smarkm// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
1955682Smarkm// USA.
2055682Smarkm
2155682Smarkm// As a special exception, you may use this file as part of a free software
2255682Smarkm// library without restriction.  Specifically, if other files instantiate
2355682Smarkm// templates or use macros or inline functions from this file, or you compile
2455682Smarkm// this file and link it with other files to produce an executable, this
2555682Smarkm// file does not by itself cause the resulting executable to be covered by
2655682Smarkm// the GNU General Public License.  This exception does not however
2755682Smarkm// invalidate any other reasons why the executable file might be covered by
2855682Smarkm// the GNU General Public License.
2955682Smarkm
3055682Smarkm//
3155682Smarkm// ISO C++ 14882: 22.2.5.1.2 - time_get virtual functions
3255682Smarkm// ISO C++ 14882: 22.2.5.3.2 - time_put virtual functions
3355682Smarkm//
3455682Smarkm
3555682Smarkm// Written by Benjamin Kosnik <bkoz@redhat.com>
3655682Smarkm
3755682Smarkm#include <locale>
3855682Smarkm
3955682Smarkmnamespace std
4055682Smarkm{
4155682Smarkm  template<>
4255682Smarkm    __timepunct<char>::~__timepunct()
4355682Smarkm    {
4455682Smarkm      if (_M_c_locale_timepunct != _S_c_locale)
4555682Smarkm	_S_destroy_c_locale(_M_c_locale_timepunct);
4655682Smarkm    }
4755682Smarkm
4855682Smarkm  template<>
4955682Smarkm    void
5055682Smarkm    __timepunct<char>::
5155682Smarkm    _M_put(char* __s, size_t __maxlen, const char* __format,
5255682Smarkm	   const tm* __tm) const
5355682Smarkm    {
5455682Smarkm#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
5555682Smarkm      __strftime_l(__s, __maxlen, _M_c_locale_timepunct, __format, __tm);
5655682Smarkm#else
5755682Smarkm      char* __old = strdup(setlocale(LC_ALL, NULL));
58      setlocale(LC_ALL, _M_name_timepunct);
59      strftime(__s, __maxlen, __format, __tm);
60      setlocale(LC_ALL, __old);
61      free(__old);
62#endif
63    }
64
65  template<>
66    void
67    __timepunct<char>::_M_initialize_timepunct(__c_locale __cloc)
68    {
69      if (__cloc == _S_c_locale)
70	{
71	  // "C" locale
72	  _M_date_format = "%m/%d/%y";
73	  _M_date_era_format = "%m/%d/%y";
74	  _M_time_format = "%H:%M:%S";
75	  _M_time_era_format = "%H:%M:%S";
76	  _M_date_time_format = "";
77	  _M_date_time_era_format = "";
78	  _M_am = "AM";
79	  _M_pm = "PM";
80	  _M_am_pm_format = "";
81
82	  // Day names, starting with "C"'s Sunday.
83	  _M_day1 = "Sunday";
84	  _M_day2 = "Monday";
85	  _M_day3 = "Tuesday";
86	  _M_day4 = "Wednesday";
87	  _M_day5 = "Thursday";
88	  _M_day6 = "Friday";
89	  _M_day7 = "Saturday";
90
91	  // Abbreviated day names, starting with "C"'s Sun.
92	  _M_day_a1 = "Sun";
93	  _M_day_a2 = "Mon";
94	  _M_day_a3 = "Tue";
95	  _M_day_a4 = "Wed";
96	  _M_day_a5 = "Thu";
97	  _M_day_a6 = "Fri";
98	  _M_day_a7 = "Sat";
99
100	  // Month names, starting with "C"'s January.
101	  _M_month01 = "January";
102	  _M_month02 = "February";
103	  _M_month03 = "March";
104	  _M_month04 = "April";
105	  _M_month05 = "May";
106	  _M_month06 = "June";
107	  _M_month07 = "July";
108	  _M_month08 = "August";
109	  _M_month09 = "September";
110	  _M_month10 = "October";
111	  _M_month11 = "November";
112	  _M_month12 = "December";
113
114	  // Abbreviated month names, starting with "C"'s Jan.
115	  _M_month_a01 = "Jan";
116	  _M_month_a02 = "Feb";
117	  _M_month_a03 = "Mar";
118	  _M_month_a04 = "Apr";
119	  _M_month_a05 = "May";
120	  _M_month_a06 = "Jun";
121	  _M_month_a07 = "July";
122	  _M_month_a08 = "Aug";
123	  _M_month_a09 = "Sep";
124	  _M_month_a10 = "Oct";
125	  _M_month_a11 = "Nov";
126	  _M_month_a12 = "Dec";
127	}
128      else
129	{
130	  _M_c_locale_timepunct = _S_clone_c_locale(__cloc);
131
132	  _M_date_format = __nl_langinfo_l(D_FMT, __cloc);
133	  _M_date_era_format = __nl_langinfo_l(ERA_D_FMT, __cloc);
134	  _M_time_format = __nl_langinfo_l(T_FMT, __cloc);
135	  _M_time_era_format = __nl_langinfo_l(ERA_T_FMT, __cloc);
136	  _M_date_time_format = __nl_langinfo_l(D_T_FMT, __cloc);
137	  _M_date_time_era_format = __nl_langinfo_l(ERA_D_T_FMT, __cloc);
138	  _M_am = __nl_langinfo_l(AM_STR, __cloc);
139	  _M_pm = __nl_langinfo_l(PM_STR, __cloc);
140	  _M_am_pm_format = __nl_langinfo_l(T_FMT_AMPM, __cloc);
141
142	  // Day names, starting with "C"'s Sunday.
143	  _M_day1 = __nl_langinfo_l(DAY_1, __cloc);
144	  _M_day2 = __nl_langinfo_l(DAY_2, __cloc);
145	  _M_day3 = __nl_langinfo_l(DAY_3, __cloc);
146	  _M_day4 = __nl_langinfo_l(DAY_4, __cloc);
147	  _M_day5 = __nl_langinfo_l(DAY_5, __cloc);
148	  _M_day6 = __nl_langinfo_l(DAY_6, __cloc);
149	  _M_day7 = __nl_langinfo_l(DAY_7, __cloc);
150
151	  // Abbreviated day names, starting with "C"'s Sun.
152	  _M_day_a1 = __nl_langinfo_l(ABDAY_1, __cloc);
153	  _M_day_a2 = __nl_langinfo_l(ABDAY_2, __cloc);
154	  _M_day_a3 = __nl_langinfo_l(ABDAY_3, __cloc);
155	  _M_day_a4 = __nl_langinfo_l(ABDAY_4, __cloc);
156	  _M_day_a5 = __nl_langinfo_l(ABDAY_5, __cloc);
157	  _M_day_a6 = __nl_langinfo_l(ABDAY_6, __cloc);
158	  _M_day_a7 = __nl_langinfo_l(ABDAY_7, __cloc);
159
160	  // Month names, starting with "C"'s January.
161	  _M_month01 = __nl_langinfo_l(MON_1, __cloc);
162	  _M_month02 = __nl_langinfo_l(MON_2, __cloc);
163	  _M_month03 = __nl_langinfo_l(MON_3, __cloc);
164	  _M_month04 = __nl_langinfo_l(MON_4, __cloc);
165	  _M_month05 = __nl_langinfo_l(MON_5, __cloc);
166	  _M_month06 = __nl_langinfo_l(MON_6, __cloc);
167	  _M_month07 = __nl_langinfo_l(MON_7, __cloc);
168	  _M_month08 = __nl_langinfo_l(MON_8, __cloc);
169	  _M_month09 = __nl_langinfo_l(MON_9, __cloc);
170	  _M_month10 = __nl_langinfo_l(MON_10, __cloc);
171	  _M_month11 = __nl_langinfo_l(MON_11, __cloc);
172	  _M_month12 = __nl_langinfo_l(MON_12, __cloc);
173
174	  // Abbreviated month names, starting with "C"'s Jan.
175	  _M_month_a01 = __nl_langinfo_l(ABMON_1, __cloc);
176	  _M_month_a02 = __nl_langinfo_l(ABMON_2, __cloc);
177	  _M_month_a03 = __nl_langinfo_l(ABMON_3, __cloc);
178	  _M_month_a04 = __nl_langinfo_l(ABMON_4, __cloc);
179	  _M_month_a05 = __nl_langinfo_l(ABMON_5, __cloc);
180	  _M_month_a06 = __nl_langinfo_l(ABMON_6, __cloc);
181	  _M_month_a07 = __nl_langinfo_l(ABMON_7, __cloc);
182	  _M_month_a08 = __nl_langinfo_l(ABMON_8, __cloc);
183	  _M_month_a09 = __nl_langinfo_l(ABMON_9, __cloc);
184	  _M_month_a10 = __nl_langinfo_l(ABMON_10, __cloc);
185	  _M_month_a11 = __nl_langinfo_l(ABMON_11, __cloc);
186	  _M_month_a12 = __nl_langinfo_l(ABMON_12, __cloc);
187	}
188    }
189
190#ifdef _GLIBCPP_USE_WCHAR_T
191  template<>
192    __timepunct<wchar_t>::~__timepunct()
193    {
194      if (_M_c_locale_timepunct != _S_c_locale)
195	_S_destroy_c_locale(_M_c_locale_timepunct);
196    }
197
198  template<>
199    void
200    __timepunct<wchar_t>::
201    _M_put(wchar_t* __s, size_t __maxlen, const wchar_t* __format,
202	   const tm* __tm) const
203    {
204#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
205      __wcsftime_l(__s, __maxlen, _M_c_locale_timepunct, __format, __tm);
206#else
207      char* __old = strdup(setlocale(LC_ALL, NULL));
208      setlocale(LC_ALL, _M_name_timepunct);
209      wcsftime(__s, __maxlen, __format, __tm);
210      setlocale(LC_ALL, __old);
211      free(__old);
212#endif
213    }
214
215  template<>
216    void
217    __timepunct<wchar_t>::_M_initialize_timepunct(__c_locale __cloc)
218    {
219      if (__cloc == _S_c_locale)
220	{
221	  // "C" locale
222	  _M_date_format = L"%m/%d/%y";
223	  _M_date_era_format = L"%m/%d/%y";
224	  _M_time_format = L"%H:%M:%S";
225	  _M_time_era_format = L"%H:%M:%S";
226	  _M_date_time_format = L"";
227	  _M_date_time_era_format = L"";
228	  _M_am = L"AM";
229	  _M_pm = L"PM";
230	  _M_am_pm_format = L"";
231
232	  // Day names, starting with "C"'s Sunday.
233	  _M_day1 = L"Sunday";
234	  _M_day2 = L"Monday";
235	  _M_day3 = L"Tuesday";
236	  _M_day4 = L"Wednesday";
237	  _M_day5 = L"Thursday";
238	  _M_day6 = L"Friday";
239	  _M_day7 = L"Saturday";
240
241	  // Abbreviated day names, starting with "C"'s Sun.
242	  _M_day_a1 = L"Sun";
243	  _M_day_a2 = L"Mon";
244	  _M_day_a3 = L"Tue";
245	  _M_day_a4 = L"Wed";
246	  _M_day_a5 = L"Thu";
247	  _M_day_a6 = L"Fri";
248	  _M_day_a7 = L"Sat";
249
250	  // Month names, starting with "C"'s January.
251	  _M_month01 = L"January";
252	  _M_month02 = L"February";
253	  _M_month03 = L"March";
254	  _M_month04 = L"April";
255	  _M_month05 = L"May";
256	  _M_month06 = L"June";
257	  _M_month07 = L"July";
258	  _M_month08 = L"August";
259	  _M_month09 = L"September";
260	  _M_month10 = L"October";
261	  _M_month11 = L"November";
262	  _M_month12 = L"December";
263
264	  // Abbreviated month names, starting with "C"'s Jan.
265	  _M_month_a01 = L"Jan";
266	  _M_month_a02 = L"Feb";
267	  _M_month_a03 = L"Mar";
268	  _M_month_a04 = L"Apr";
269	  _M_month_a05 = L"May";
270	  _M_month_a06 = L"Jun";
271	  _M_month_a07 = L"July";
272	  _M_month_a08 = L"Aug";
273	  _M_month_a09 = L"Sep";
274	  _M_month_a10 = L"Oct";
275	  _M_month_a11 = L"Nov";
276	  _M_month_a12 = L"Dec";
277	}
278      else
279	{
280	  _M_c_locale_timepunct = _S_clone_c_locale(__cloc);
281
282	  _M_date_format = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WD_FMT, __cloc));
283	  _M_date_era_format = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WERA_D_FMT, __cloc));
284	  _M_time_format = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WT_FMT, __cloc));
285	  _M_time_era_format = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WERA_T_FMT, __cloc));
286	  _M_date_time_format = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WD_T_FMT, __cloc));
287	  _M_date_time_era_format = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WERA_D_T_FMT, __cloc));
288	  _M_am = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WAM_STR, __cloc));
289	  _M_pm = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WPM_STR, __cloc));
290	  _M_am_pm_format = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WT_FMT_AMPM, __cloc));
291
292	  // Day names, starting with "C"'s Sunday.
293	  _M_day1 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WDAY_1, __cloc));
294	  _M_day2 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WDAY_2, __cloc));
295	  _M_day3 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WDAY_3, __cloc));
296	  _M_day4 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WDAY_4, __cloc));
297	  _M_day5 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WDAY_5, __cloc));
298	  _M_day6 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WDAY_6, __cloc));
299	  _M_day7 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WDAY_7, __cloc));
300
301	  // Abbreviated day names, starting with "C"'s Sun.
302	  _M_day_a1 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABDAY_1, __cloc));
303	  _M_day_a2 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABDAY_2, __cloc));
304	  _M_day_a3 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABDAY_3, __cloc));
305	  _M_day_a4 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABDAY_4, __cloc));
306	  _M_day_a5 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABDAY_5, __cloc));
307	  _M_day_a6 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABDAY_6, __cloc));
308	  _M_day_a7 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABDAY_7, __cloc));
309
310	  // Month names, starting with "C"'s January.
311	  _M_month01 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WMON_1, __cloc));
312	  _M_month02 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WMON_2, __cloc));
313	  _M_month03 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WMON_3, __cloc));
314	  _M_month04 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WMON_4, __cloc));
315	  _M_month05 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WMON_5, __cloc));
316	  _M_month06 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WMON_6, __cloc));
317	  _M_month07 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WMON_7, __cloc));
318	  _M_month08 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WMON_8, __cloc));
319	  _M_month09 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WMON_9, __cloc));
320	  _M_month10 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WMON_10, __cloc));
321	  _M_month11 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WMON_11, __cloc));
322	  _M_month12 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WMON_12, __cloc));
323
324	  // Abbreviated month names, starting with "C"'s Jan.
325	  _M_month_a01 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABMON_1, __cloc));
326	  _M_month_a02 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABMON_2, __cloc));
327	  _M_month_a03 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABMON_3, __cloc));
328	  _M_month_a04 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABMON_4, __cloc));
329	  _M_month_a05 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABMON_5, __cloc));
330	  _M_month_a06 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABMON_6, __cloc));
331	  _M_month_a07 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABMON_7, __cloc));
332	  _M_month_a08 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABMON_8, __cloc));
333	  _M_month_a09 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABMON_9, __cloc));
334	  _M_month_a10 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABMON_10, __cloc));
335	  _M_month_a11 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABMON_11, __cloc));
336	  _M_month_a12 = reinterpret_cast<wchar_t*>(__nl_langinfo_l(_NL_WABMON_12, __cloc));
337	}
338    }
339#endif
340}
341