1// std::time_get, std::time_put implementation, DragonFly version -*- C++ -*-
2
3// Copyright (C) 2001-2020 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library.  This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23// <http://www.gnu.org/licenses/>.
24
25//
26// ISO C++ 14882: 22.2.5.1.2 - time_get virtual functions
27// ISO C++ 14882: 22.2.5.3.2 - time_put virtual functions
28//
29
30// Written by Benjamin Kosnik <bkoz@redhat.com>
31// Modified for DragonFly by John Marino <gnugcc@marino.st>
32
33#include <locale>
34#include <ctime>
35#include <cwchar>
36#include <stdlib.h>
37#include <langinfo.h>
38
39#include "xlocale_port.h"
40
41namespace std _GLIBCXX_VISIBILITY(default)
42{
43_GLIBCXX_BEGIN_NAMESPACE_VERSION
44
45  template<>
46    void
47    __timepunct<char>::
48    _M_put(char* __s, size_t __maxlen, const char* __format,
49	   const tm* __tm) const throw()
50    {
51      const size_t __len = strftime_l(__s, __maxlen, __format, __tm,
52				      (locale_t)_M_c_locale_timepunct);
53      // Make sure __s is null terminated.
54      if (__len == 0)
55	__s[0] = '\0';
56    }
57
58  template<>
59    void
60    __timepunct<char>::_M_initialize_timepunct(__c_locale __cloc)
61    {
62      if (!_M_data)
63	_M_data = new __timepunct_cache<char>;
64
65      if (!__cloc)
66	{
67	  // "C" locale
68	  _M_c_locale_timepunct = _S_get_c_locale();
69
70	  _M_data->_M_date_format = "%m/%d/%y";
71	  _M_data->_M_date_era_format = "%m/%d/%y";
72	  _M_data->_M_time_format = "%H:%M:%S";
73	  _M_data->_M_time_era_format = "%H:%M:%S";
74	  _M_data->_M_date_time_format = "";
75	  _M_data->_M_date_time_era_format = "";
76	  _M_data->_M_am = "AM";
77	  _M_data->_M_pm = "PM";
78	  _M_data->_M_am_pm_format = "";
79
80	  // Day names, starting with "C"'s Sunday.
81	  _M_data->_M_day1 = "Sunday";
82	  _M_data->_M_day2 = "Monday";
83	  _M_data->_M_day3 = "Tuesday";
84	  _M_data->_M_day4 = "Wednesday";
85	  _M_data->_M_day5 = "Thursday";
86	  _M_data->_M_day6 = "Friday";
87	  _M_data->_M_day7 = "Saturday";
88
89	  // Abbreviated day names, starting with "C"'s Sun.
90	  _M_data->_M_aday1 = "Sun";
91	  _M_data->_M_aday2 = "Mon";
92	  _M_data->_M_aday3 = "Tue";
93	  _M_data->_M_aday4 = "Wed";
94	  _M_data->_M_aday5 = "Thu";
95	  _M_data->_M_aday6 = "Fri";
96	  _M_data->_M_aday7 = "Sat";
97
98	  // Month names, starting with "C"'s January.
99	  _M_data->_M_month01 = "January";
100	  _M_data->_M_month02 = "February";
101	  _M_data->_M_month03 = "March";
102	  _M_data->_M_month04 = "April";
103	  _M_data->_M_month05 = "May";
104	  _M_data->_M_month06 = "June";
105	  _M_data->_M_month07 = "July";
106	  _M_data->_M_month08 = "August";
107	  _M_data->_M_month09 = "September";
108	  _M_data->_M_month10 = "October";
109	  _M_data->_M_month11 = "November";
110	  _M_data->_M_month12 = "December";
111
112	  // Abbreviated month names, starting with "C"'s Jan.
113	  _M_data->_M_amonth01 = "Jan";
114	  _M_data->_M_amonth02 = "Feb";
115	  _M_data->_M_amonth03 = "Mar";
116	  _M_data->_M_amonth04 = "Apr";
117	  _M_data->_M_amonth05 = "May";
118	  _M_data->_M_amonth06 = "Jun";
119	  _M_data->_M_amonth07 = "Jul";
120	  _M_data->_M_amonth08 = "Aug";
121	  _M_data->_M_amonth09 = "Sep";
122	  _M_data->_M_amonth10 = "Oct";
123	  _M_data->_M_amonth11 = "Nov";
124	  _M_data->_M_amonth12 = "Dec";
125	}
126      else
127	{
128	  _M_c_locale_timepunct = _S_clone_c_locale(__cloc);
129
130	  _M_data->_M_date_format = nl_langinfo_l(D_FMT, (locale_t)__cloc);
131	  _M_data->_M_date_era_format = nl_langinfo_l(ERA_D_FMT,
132	  					      (locale_t)__cloc);
133	  _M_data->_M_time_format = nl_langinfo_l(T_FMT, (locale_t)__cloc);
134	  _M_data->_M_time_era_format = nl_langinfo_l(ERA_T_FMT,
135						      (locale_t)__cloc);
136	  _M_data->_M_date_time_format = nl_langinfo_l(D_T_FMT,
137						       (locale_t)__cloc);
138	  _M_data->_M_date_time_era_format = nl_langinfo_l(ERA_D_T_FMT,
139	  						   (locale_t)__cloc);
140	  _M_data->_M_am = nl_langinfo_l(AM_STR, (locale_t)__cloc);
141	  _M_data->_M_pm = nl_langinfo_l(PM_STR, (locale_t)__cloc);
142	  _M_data->_M_am_pm_format = nl_langinfo_l(T_FMT_AMPM,
143	  					   (locale_t)__cloc);
144
145	  // Day names, starting with "C"'s Sunday.
146	  _M_data->_M_day1 = nl_langinfo_l(DAY_1, (locale_t)__cloc);
147	  _M_data->_M_day2 = nl_langinfo_l(DAY_2, (locale_t)__cloc);
148	  _M_data->_M_day3 = nl_langinfo_l(DAY_3, (locale_t)__cloc);
149	  _M_data->_M_day4 = nl_langinfo_l(DAY_4, (locale_t)__cloc);
150	  _M_data->_M_day5 = nl_langinfo_l(DAY_5, (locale_t)__cloc);
151	  _M_data->_M_day6 = nl_langinfo_l(DAY_6, (locale_t)__cloc);
152	  _M_data->_M_day7 = nl_langinfo_l(DAY_7, (locale_t)__cloc);
153
154	  // Abbreviated day names, starting with "C"'s Sun.
155	  _M_data->_M_aday1 = nl_langinfo_l(ABDAY_1, (locale_t)__cloc);
156	  _M_data->_M_aday2 = nl_langinfo_l(ABDAY_2, (locale_t)__cloc);
157	  _M_data->_M_aday3 = nl_langinfo_l(ABDAY_3, (locale_t)__cloc);
158	  _M_data->_M_aday4 = nl_langinfo_l(ABDAY_4, (locale_t)__cloc);
159	  _M_data->_M_aday5 = nl_langinfo_l(ABDAY_5, (locale_t)__cloc);
160	  _M_data->_M_aday6 = nl_langinfo_l(ABDAY_6, (locale_t)__cloc);
161	  _M_data->_M_aday7 = nl_langinfo_l(ABDAY_7, (locale_t)__cloc);
162
163	  // Month names, starting with "C"'s January.
164	  _M_data->_M_month01 = nl_langinfo_l(MON_1, (locale_t)__cloc);
165	  _M_data->_M_month02 = nl_langinfo_l(MON_2, (locale_t)__cloc);
166	  _M_data->_M_month03 = nl_langinfo_l(MON_3, (locale_t)__cloc);
167	  _M_data->_M_month04 = nl_langinfo_l(MON_4, (locale_t)__cloc);
168	  _M_data->_M_month05 = nl_langinfo_l(MON_5, (locale_t)__cloc);
169	  _M_data->_M_month06 = nl_langinfo_l(MON_6, (locale_t)__cloc);
170	  _M_data->_M_month07 = nl_langinfo_l(MON_7, (locale_t)__cloc);
171	  _M_data->_M_month08 = nl_langinfo_l(MON_8, (locale_t)__cloc);
172	  _M_data->_M_month09 = nl_langinfo_l(MON_9, (locale_t)__cloc);
173	  _M_data->_M_month10 = nl_langinfo_l(MON_10, (locale_t)__cloc);
174	  _M_data->_M_month11 = nl_langinfo_l(MON_11, (locale_t)__cloc);
175	  _M_data->_M_month12 = nl_langinfo_l(MON_12, (locale_t)__cloc);
176
177	  // Abbreviated month names, starting with "C"'s Jan.
178	  _M_data->_M_amonth01 = nl_langinfo_l(ABMON_1, (locale_t)__cloc);
179	  _M_data->_M_amonth02 = nl_langinfo_l(ABMON_2, (locale_t)__cloc);
180	  _M_data->_M_amonth03 = nl_langinfo_l(ABMON_3, (locale_t)__cloc);
181	  _M_data->_M_amonth04 = nl_langinfo_l(ABMON_4, (locale_t)__cloc);
182	  _M_data->_M_amonth05 = nl_langinfo_l(ABMON_5, (locale_t)__cloc);
183	  _M_data->_M_amonth06 = nl_langinfo_l(ABMON_6, (locale_t)__cloc);
184	  _M_data->_M_amonth07 = nl_langinfo_l(ABMON_7, (locale_t)__cloc);
185	  _M_data->_M_amonth08 = nl_langinfo_l(ABMON_8, (locale_t)__cloc);
186	  _M_data->_M_amonth09 = nl_langinfo_l(ABMON_9, (locale_t)__cloc);
187	  _M_data->_M_amonth10 = nl_langinfo_l(ABMON_10, (locale_t)__cloc);
188	  _M_data->_M_amonth11 = nl_langinfo_l(ABMON_11, (locale_t)__cloc);
189	  _M_data->_M_amonth12 = nl_langinfo_l(ABMON_12, (locale_t)__cloc);
190	}
191    }
192
193#ifdef _GLIBCXX_USE_WCHAR_T
194  template<>
195    void
196    __timepunct<wchar_t>::
197    _M_put(wchar_t* __s, size_t __maxlen, const wchar_t* __format,
198	   const tm* __tm) const throw()
199    {
200      const size_t __len = wcsftime_l(__s, __maxlen, __format, __tm,
201				      (locale_t)_M_c_locale_timepunct);
202      // Make sure __s is null terminated.
203      if (__len == 0)
204	__s[0] = L'\0';
205    }
206
207#define WIDE_LANGINFO(M,FMT) \
208	fmtlen = mbstowcs_l (holder, nl_langinfo_l(FMT, (locale_t)__cloc), \
209		128, (locale_t)__cloc); \
210	langstring = new wchar_t[fmtlen + 1]; \
211	wcsncpy (langstring, holder, fmtlen); \
212	langstring[fmtlen] = L'\0'; \
213	_M_data->M = langstring;
214
215  template<>
216    void
217    __timepunct<wchar_t>::_M_initialize_timepunct(__c_locale __cloc)
218    {
219      if (!_M_data)
220	_M_data = new __timepunct_cache<wchar_t>;
221
222      if (!__cloc)
223	{
224	  // "C" locale
225	  _M_c_locale_timepunct = _S_get_c_locale();
226
227	  _M_data->_M_date_format = L"%m/%d/%y";
228	  _M_data->_M_date_era_format = L"%m/%d/%y";
229	  _M_data->_M_time_format = L"%H:%M:%S";
230	  _M_data->_M_time_era_format = L"%H:%M:%S";
231	  _M_data->_M_date_time_format = L"";
232	  _M_data->_M_date_time_era_format = L"";
233	  _M_data->_M_am = L"AM";
234	  _M_data->_M_pm = L"PM";
235	  _M_data->_M_am_pm_format = L"";
236
237	  // Day names, starting with "C"'s Sunday.
238	  _M_data->_M_day1 = L"Sunday";
239	  _M_data->_M_day2 = L"Monday";
240	  _M_data->_M_day3 = L"Tuesday";
241	  _M_data->_M_day4 = L"Wednesday";
242	  _M_data->_M_day5 = L"Thursday";
243	  _M_data->_M_day6 = L"Friday";
244	  _M_data->_M_day7 = L"Saturday";
245
246	  // Abbreviated day names, starting with "C"'s Sun.
247	  _M_data->_M_aday1 = L"Sun";
248	  _M_data->_M_aday2 = L"Mon";
249	  _M_data->_M_aday3 = L"Tue";
250	  _M_data->_M_aday4 = L"Wed";
251	  _M_data->_M_aday5 = L"Thu";
252	  _M_data->_M_aday6 = L"Fri";
253	  _M_data->_M_aday7 = L"Sat";
254
255	  // Month names, starting with "C"'s January.
256	  _M_data->_M_month01 = L"January";
257	  _M_data->_M_month02 = L"February";
258	  _M_data->_M_month03 = L"March";
259	  _M_data->_M_month04 = L"April";
260	  _M_data->_M_month05 = L"May";
261	  _M_data->_M_month06 = L"June";
262	  _M_data->_M_month07 = L"July";
263	  _M_data->_M_month08 = L"August";
264	  _M_data->_M_month09 = L"September";
265	  _M_data->_M_month10 = L"October";
266	  _M_data->_M_month11 = L"November";
267	  _M_data->_M_month12 = L"December";
268
269	  // Abbreviated month names, starting with "C"'s Jan.
270	  _M_data->_M_amonth01 = L"Jan";
271	  _M_data->_M_amonth02 = L"Feb";
272	  _M_data->_M_amonth03 = L"Mar";
273	  _M_data->_M_amonth04 = L"Apr";
274	  _M_data->_M_amonth05 = L"May";
275	  _M_data->_M_amonth06 = L"Jun";
276	  _M_data->_M_amonth07 = L"Jul";
277	  _M_data->_M_amonth08 = L"Aug";
278	  _M_data->_M_amonth09 = L"Sep";
279	  _M_data->_M_amonth10 = L"Oct";
280	  _M_data->_M_amonth11 = L"Nov";
281	  _M_data->_M_amonth12 = L"Dec";
282	}
283      else
284	{
285	  wchar_t *langstring = 0;
286	  wchar_t holder[128];
287	  size_t fmtlen;
288
289	  _M_c_locale_timepunct = _S_clone_c_locale(__cloc);
290
291	  WIDE_LANGINFO(_M_date_format, D_FMT)
292	  WIDE_LANGINFO(_M_date_era_format, ERA_D_FMT)
293	  WIDE_LANGINFO(_M_time_format, T_FMT)
294	  WIDE_LANGINFO(_M_time_era_format, ERA_T_FMT)
295	  WIDE_LANGINFO(_M_date_time_format, D_T_FMT)
296	  WIDE_LANGINFO(_M_date_time_era_format, ERA_D_T_FMT)
297	  WIDE_LANGINFO(_M_am, AM_STR)
298	  WIDE_LANGINFO(_M_pm, PM_STR)
299	  WIDE_LANGINFO(_M_am_pm_format, T_FMT_AMPM)
300
301	  // Day names, starting with "C"'s Sunday.
302	  WIDE_LANGINFO(_M_day1, DAY_1)
303	  WIDE_LANGINFO(_M_day2, DAY_2)
304	  WIDE_LANGINFO(_M_day3, DAY_3)
305	  WIDE_LANGINFO(_M_day4, DAY_4)
306	  WIDE_LANGINFO(_M_day5, DAY_5)
307	  WIDE_LANGINFO(_M_day6, DAY_6)
308	  WIDE_LANGINFO(_M_day7, DAY_7)
309
310	  // Abbreviated day names, starting with "C"'s Sun.
311	  WIDE_LANGINFO(_M_aday1, ABDAY_1)
312	  WIDE_LANGINFO(_M_aday2, ABDAY_2)
313	  WIDE_LANGINFO(_M_aday3, ABDAY_3)
314	  WIDE_LANGINFO(_M_aday4, ABDAY_4)
315	  WIDE_LANGINFO(_M_aday5, ABDAY_5)
316	  WIDE_LANGINFO(_M_aday6, ABDAY_6)
317	  WIDE_LANGINFO(_M_aday7, ABDAY_7)
318
319	  // Month names, starting with "C"'s January.
320	  WIDE_LANGINFO(_M_month01, MON_1)
321	  WIDE_LANGINFO(_M_month02, MON_2)
322	  WIDE_LANGINFO(_M_month03, MON_3)
323	  WIDE_LANGINFO(_M_month04, MON_4)
324	  WIDE_LANGINFO(_M_month05, MON_5)
325	  WIDE_LANGINFO(_M_month06, MON_6)
326	  WIDE_LANGINFO(_M_month07, MON_7)
327	  WIDE_LANGINFO(_M_month08, MON_8)
328	  WIDE_LANGINFO(_M_month09, MON_9)
329	  WIDE_LANGINFO(_M_month10, MON_10)
330	  WIDE_LANGINFO(_M_month11, MON_11)
331	  WIDE_LANGINFO(_M_month12, MON_12)
332
333	  // Abbreviated month names, starting with "C"'s Jan.
334	  WIDE_LANGINFO(_M_amonth01, ABMON_1)
335	  WIDE_LANGINFO(_M_amonth02, ABMON_2)
336	  WIDE_LANGINFO(_M_amonth03, ABMON_3)
337	  WIDE_LANGINFO(_M_amonth04, ABMON_4)
338	  WIDE_LANGINFO(_M_amonth05, ABMON_5)
339	  WIDE_LANGINFO(_M_amonth06, ABMON_6)
340	  WIDE_LANGINFO(_M_amonth07, ABMON_7)
341	  WIDE_LANGINFO(_M_amonth08, ABMON_8)
342	  WIDE_LANGINFO(_M_amonth09, ABMON_9)
343	  WIDE_LANGINFO(_M_amonth10, ABMON_10)
344	  WIDE_LANGINFO(_M_amonth11, ABMON_11)
345	  WIDE_LANGINFO(_M_amonth12, ABMON_12)
346	}
347    }
348
349  template<>
350    __timepunct<wchar_t>::~__timepunct()
351    {
352      delete [] _M_data->_M_date_format;
353      delete [] _M_data->_M_date_era_format;
354      delete [] _M_data->_M_time_format;
355      delete [] _M_data->_M_time_era_format;
356      delete [] _M_data->_M_date_time_format;
357      delete [] _M_data->_M_date_time_era_format;
358      delete [] _M_data->_M_am;
359      delete [] _M_data->_M_pm;
360      delete [] _M_data->_M_am_pm_format;
361      delete [] _M_data->_M_day1;
362      delete [] _M_data->_M_day2;
363      delete [] _M_data->_M_day3;
364      delete [] _M_data->_M_day4;
365      delete [] _M_data->_M_day5;
366      delete [] _M_data->_M_day6;
367      delete [] _M_data->_M_day7;
368      delete [] _M_data->_M_aday1;
369      delete [] _M_data->_M_aday2;
370      delete [] _M_data->_M_aday3;
371      delete [] _M_data->_M_aday4;
372      delete [] _M_data->_M_aday5;
373      delete [] _M_data->_M_aday6;
374      delete [] _M_data->_M_aday7;
375      delete [] _M_data->_M_month01;
376      delete [] _M_data->_M_month02;
377      delete [] _M_data->_M_month03;
378      delete [] _M_data->_M_month04;
379      delete [] _M_data->_M_month05;
380      delete [] _M_data->_M_month06;
381      delete [] _M_data->_M_month07;
382      delete [] _M_data->_M_month08;
383      delete [] _M_data->_M_month09;
384      delete [] _M_data->_M_month10;
385      delete [] _M_data->_M_month11;
386      delete [] _M_data->_M_month12;
387      delete [] _M_data->_M_amonth01;
388      delete [] _M_data->_M_amonth02;
389      delete [] _M_data->_M_amonth03;
390      delete [] _M_data->_M_amonth04;
391      delete [] _M_data->_M_amonth05;
392      delete [] _M_data->_M_amonth06;
393      delete [] _M_data->_M_amonth07;
394      delete [] _M_data->_M_amonth08;
395      delete [] _M_data->_M_amonth09;
396      delete [] _M_data->_M_amonth10;
397      delete [] _M_data->_M_amonth11;
398      delete [] _M_data->_M_amonth12;
399      delete _M_data;
400    }
401
402#endif
403
404_GLIBCXX_END_NAMESPACE_VERSION
405} // namespace
406