1 // std::time_get, std::time_put implementation, DragonFly version -*- C++ -*-
2 
3 // Copyright (C) 2001-2018 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 #include <xlocale.h>
39 
40 namespace std _GLIBCXX_VISIBILITY(default)
41 {
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43 
44   template<>
45     void
46     __timepunct<char>::
47     _M_put(char* __s, size_t __maxlen, const char* __format,
48 	   const tm* __tm) const throw()
49     {
50       const size_t __len = strftime_l(__s, __maxlen, __format, __tm,
51 				      (locale_t)_M_c_locale_timepunct);
52       // Make sure __s is null terminated.
53       if (__len == 0)
54 	__s[0] = '\0';
55     }
56 
57   template<>
58     void
59     __timepunct<char>::_M_initialize_timepunct(__c_locale __cloc)
60     {
61       if (!_M_data)
62 	_M_data = new __timepunct_cache<char>;
63 
64       if (!__cloc)
65 	{
66 	  // "C" locale
67 	  _M_c_locale_timepunct = _S_get_c_locale();
68 
69 	  _M_data->_M_date_format = "%m/%d/%y";
70 	  _M_data->_M_date_era_format = "%m/%d/%y";
71 	  _M_data->_M_time_format = "%H:%M:%S";
72 	  _M_data->_M_time_era_format = "%H:%M:%S";
73 	  _M_data->_M_date_time_format = "";
74 	  _M_data->_M_date_time_era_format = "";
75 	  _M_data->_M_am = "AM";
76 	  _M_data->_M_pm = "PM";
77 	  _M_data->_M_am_pm_format = "";
78 
79 	  // Day names, starting with "C"'s Sunday.
80 	  _M_data->_M_day1 = "Sunday";
81 	  _M_data->_M_day2 = "Monday";
82 	  _M_data->_M_day3 = "Tuesday";
83 	  _M_data->_M_day4 = "Wednesday";
84 	  _M_data->_M_day5 = "Thursday";
85 	  _M_data->_M_day6 = "Friday";
86 	  _M_data->_M_day7 = "Saturday";
87 
88 	  // Abbreviated day names, starting with "C"'s Sun.
89 	  _M_data->_M_aday1 = "Sun";
90 	  _M_data->_M_aday2 = "Mon";
91 	  _M_data->_M_aday3 = "Tue";
92 	  _M_data->_M_aday4 = "Wed";
93 	  _M_data->_M_aday5 = "Thu";
94 	  _M_data->_M_aday6 = "Fri";
95 	  _M_data->_M_aday7 = "Sat";
96 
97 	  // Month names, starting with "C"'s January.
98 	  _M_data->_M_month01 = "January";
99 	  _M_data->_M_month02 = "February";
100 	  _M_data->_M_month03 = "March";
101 	  _M_data->_M_month04 = "April";
102 	  _M_data->_M_month05 = "May";
103 	  _M_data->_M_month06 = "June";
104 	  _M_data->_M_month07 = "July";
105 	  _M_data->_M_month08 = "August";
106 	  _M_data->_M_month09 = "September";
107 	  _M_data->_M_month10 = "October";
108 	  _M_data->_M_month11 = "November";
109 	  _M_data->_M_month12 = "December";
110 
111 	  // Abbreviated month names, starting with "C"'s Jan.
112 	  _M_data->_M_amonth01 = "Jan";
113 	  _M_data->_M_amonth02 = "Feb";
114 	  _M_data->_M_amonth03 = "Mar";
115 	  _M_data->_M_amonth04 = "Apr";
116 	  _M_data->_M_amonth05 = "May";
117 	  _M_data->_M_amonth06 = "Jun";
118 	  _M_data->_M_amonth07 = "Jul";
119 	  _M_data->_M_amonth08 = "Aug";
120 	  _M_data->_M_amonth09 = "Sep";
121 	  _M_data->_M_amonth10 = "Oct";
122 	  _M_data->_M_amonth11 = "Nov";
123 	  _M_data->_M_amonth12 = "Dec";
124 	}
125       else
126 	{
127 	  _M_c_locale_timepunct = _S_clone_c_locale(__cloc);
128 
129 	  _M_data->_M_date_format = nl_langinfo_l(D_FMT, (locale_t)__cloc);
130 	  _M_data->_M_date_era_format = nl_langinfo_l(ERA_D_FMT,
131 	  					      (locale_t)__cloc);
132 	  _M_data->_M_time_format = nl_langinfo_l(T_FMT, (locale_t)__cloc);
133 	  _M_data->_M_time_era_format = nl_langinfo_l(ERA_T_FMT,
134 						      (locale_t)__cloc);
135 	  _M_data->_M_date_time_format = nl_langinfo_l(D_T_FMT,
136 						       (locale_t)__cloc);
137 	  _M_data->_M_date_time_era_format = nl_langinfo_l(ERA_D_T_FMT,
138 	  						   (locale_t)__cloc);
139 	  _M_data->_M_am = nl_langinfo_l(AM_STR, (locale_t)__cloc);
140 	  _M_data->_M_pm = nl_langinfo_l(PM_STR, (locale_t)__cloc);
141 	  _M_data->_M_am_pm_format = nl_langinfo_l(T_FMT_AMPM,
142 	  					   (locale_t)__cloc);
143 
144 	  // Day names, starting with "C"'s Sunday.
145 	  _M_data->_M_day1 = nl_langinfo_l(DAY_1, (locale_t)__cloc);
146 	  _M_data->_M_day2 = nl_langinfo_l(DAY_2, (locale_t)__cloc);
147 	  _M_data->_M_day3 = nl_langinfo_l(DAY_3, (locale_t)__cloc);
148 	  _M_data->_M_day4 = nl_langinfo_l(DAY_4, (locale_t)__cloc);
149 	  _M_data->_M_day5 = nl_langinfo_l(DAY_5, (locale_t)__cloc);
150 	  _M_data->_M_day6 = nl_langinfo_l(DAY_6, (locale_t)__cloc);
151 	  _M_data->_M_day7 = nl_langinfo_l(DAY_7, (locale_t)__cloc);
152 
153 	  // Abbreviated day names, starting with "C"'s Sun.
154 	  _M_data->_M_aday1 = nl_langinfo_l(ABDAY_1, (locale_t)__cloc);
155 	  _M_data->_M_aday2 = nl_langinfo_l(ABDAY_2, (locale_t)__cloc);
156 	  _M_data->_M_aday3 = nl_langinfo_l(ABDAY_3, (locale_t)__cloc);
157 	  _M_data->_M_aday4 = nl_langinfo_l(ABDAY_4, (locale_t)__cloc);
158 	  _M_data->_M_aday5 = nl_langinfo_l(ABDAY_5, (locale_t)__cloc);
159 	  _M_data->_M_aday6 = nl_langinfo_l(ABDAY_6, (locale_t)__cloc);
160 	  _M_data->_M_aday7 = nl_langinfo_l(ABDAY_7, (locale_t)__cloc);
161 
162 	  // Month names, starting with "C"'s January.
163 	  _M_data->_M_month01 = nl_langinfo_l(MON_1, (locale_t)__cloc);
164 	  _M_data->_M_month02 = nl_langinfo_l(MON_2, (locale_t)__cloc);
165 	  _M_data->_M_month03 = nl_langinfo_l(MON_3, (locale_t)__cloc);
166 	  _M_data->_M_month04 = nl_langinfo_l(MON_4, (locale_t)__cloc);
167 	  _M_data->_M_month05 = nl_langinfo_l(MON_5, (locale_t)__cloc);
168 	  _M_data->_M_month06 = nl_langinfo_l(MON_6, (locale_t)__cloc);
169 	  _M_data->_M_month07 = nl_langinfo_l(MON_7, (locale_t)__cloc);
170 	  _M_data->_M_month08 = nl_langinfo_l(MON_8, (locale_t)__cloc);
171 	  _M_data->_M_month09 = nl_langinfo_l(MON_9, (locale_t)__cloc);
172 	  _M_data->_M_month10 = nl_langinfo_l(MON_10, (locale_t)__cloc);
173 	  _M_data->_M_month11 = nl_langinfo_l(MON_11, (locale_t)__cloc);
174 	  _M_data->_M_month12 = nl_langinfo_l(MON_12, (locale_t)__cloc);
175 
176 	  // Abbreviated month names, starting with "C"'s Jan.
177 	  _M_data->_M_amonth01 = nl_langinfo_l(ABMON_1, (locale_t)__cloc);
178 	  _M_data->_M_amonth02 = nl_langinfo_l(ABMON_2, (locale_t)__cloc);
179 	  _M_data->_M_amonth03 = nl_langinfo_l(ABMON_3, (locale_t)__cloc);
180 	  _M_data->_M_amonth04 = nl_langinfo_l(ABMON_4, (locale_t)__cloc);
181 	  _M_data->_M_amonth05 = nl_langinfo_l(ABMON_5, (locale_t)__cloc);
182 	  _M_data->_M_amonth06 = nl_langinfo_l(ABMON_6, (locale_t)__cloc);
183 	  _M_data->_M_amonth07 = nl_langinfo_l(ABMON_7, (locale_t)__cloc);
184 	  _M_data->_M_amonth08 = nl_langinfo_l(ABMON_8, (locale_t)__cloc);
185 	  _M_data->_M_amonth09 = nl_langinfo_l(ABMON_9, (locale_t)__cloc);
186 	  _M_data->_M_amonth10 = nl_langinfo_l(ABMON_10, (locale_t)__cloc);
187 	  _M_data->_M_amonth11 = nl_langinfo_l(ABMON_11, (locale_t)__cloc);
188 	  _M_data->_M_amonth12 = nl_langinfo_l(ABMON_12, (locale_t)__cloc);
189 	}
190     }
191 
192 #ifdef _GLIBCXX_USE_WCHAR_T
193   template<>
194     void
195     __timepunct<wchar_t>::
196     _M_put(wchar_t* __s, size_t __maxlen, const wchar_t* __format,
197 	   const tm* __tm) const throw()
198     {
199       const size_t __len = wcsftime_l(__s, __maxlen, __format, __tm,
200 				      (locale_t)_M_c_locale_timepunct);
201       // Make sure __s is null terminated.
202       if (__len == 0)
203 	__s[0] = L'\0';
204     }
205 
206 #define WIDE_LANGINFO(M,FMT) \
207 	fmtlen = mbstowcs_l (holder, nl_langinfo_l(FMT, (locale_t)__cloc), \
208 		128, (locale_t)__cloc); \
209 	langstring = new wchar_t[fmtlen + 1]; \
210 	wcsncpy (langstring, holder, fmtlen); \
211 	langstring[fmtlen] = L'\0'; \
212 	_M_data->M = langstring;
213 
214   template<>
215     void
216     __timepunct<wchar_t>::_M_initialize_timepunct(__c_locale __cloc)
217     {
218       if (!_M_data)
219 	_M_data = new __timepunct_cache<wchar_t>;
220 
221       if (!__cloc)
222 	{
223 	  // "C" locale
224 	  _M_c_locale_timepunct = _S_get_c_locale();
225 
226 	  _M_data->_M_date_format = L"%m/%d/%y";
227 	  _M_data->_M_date_era_format = L"%m/%d/%y";
228 	  _M_data->_M_time_format = L"%H:%M:%S";
229 	  _M_data->_M_time_era_format = L"%H:%M:%S";
230 	  _M_data->_M_date_time_format = L"";
231 	  _M_data->_M_date_time_era_format = L"";
232 	  _M_data->_M_am = L"AM";
233 	  _M_data->_M_pm = L"PM";
234 	  _M_data->_M_am_pm_format = L"";
235 
236 	  // Day names, starting with "C"'s Sunday.
237 	  _M_data->_M_day1 = L"Sunday";
238 	  _M_data->_M_day2 = L"Monday";
239 	  _M_data->_M_day3 = L"Tuesday";
240 	  _M_data->_M_day4 = L"Wednesday";
241 	  _M_data->_M_day5 = L"Thursday";
242 	  _M_data->_M_day6 = L"Friday";
243 	  _M_data->_M_day7 = L"Saturday";
244 
245 	  // Abbreviated day names, starting with "C"'s Sun.
246 	  _M_data->_M_aday1 = L"Sun";
247 	  _M_data->_M_aday2 = L"Mon";
248 	  _M_data->_M_aday3 = L"Tue";
249 	  _M_data->_M_aday4 = L"Wed";
250 	  _M_data->_M_aday5 = L"Thu";
251 	  _M_data->_M_aday6 = L"Fri";
252 	  _M_data->_M_aday7 = L"Sat";
253 
254 	  // Month names, starting with "C"'s January.
255 	  _M_data->_M_month01 = L"January";
256 	  _M_data->_M_month02 = L"February";
257 	  _M_data->_M_month03 = L"March";
258 	  _M_data->_M_month04 = L"April";
259 	  _M_data->_M_month05 = L"May";
260 	  _M_data->_M_month06 = L"June";
261 	  _M_data->_M_month07 = L"July";
262 	  _M_data->_M_month08 = L"August";
263 	  _M_data->_M_month09 = L"September";
264 	  _M_data->_M_month10 = L"October";
265 	  _M_data->_M_month11 = L"November";
266 	  _M_data->_M_month12 = L"December";
267 
268 	  // Abbreviated month names, starting with "C"'s Jan.
269 	  _M_data->_M_amonth01 = L"Jan";
270 	  _M_data->_M_amonth02 = L"Feb";
271 	  _M_data->_M_amonth03 = L"Mar";
272 	  _M_data->_M_amonth04 = L"Apr";
273 	  _M_data->_M_amonth05 = L"May";
274 	  _M_data->_M_amonth06 = L"Jun";
275 	  _M_data->_M_amonth07 = L"Jul";
276 	  _M_data->_M_amonth08 = L"Aug";
277 	  _M_data->_M_amonth09 = L"Sep";
278 	  _M_data->_M_amonth10 = L"Oct";
279 	  _M_data->_M_amonth11 = L"Nov";
280 	  _M_data->_M_amonth12 = L"Dec";
281 	}
282       else
283 	{
284 	  wchar_t *langstring = 0;
285 	  wchar_t holder[128];
286 	  size_t fmtlen;
287 
288 	  _M_c_locale_timepunct = _S_clone_c_locale(__cloc);
289 
290 	  WIDE_LANGINFO(_M_date_format, D_FMT)
291 	  WIDE_LANGINFO(_M_date_era_format, ERA_D_FMT)
292 	  WIDE_LANGINFO(_M_time_format, T_FMT)
293 	  WIDE_LANGINFO(_M_time_era_format, ERA_T_FMT)
294 	  WIDE_LANGINFO(_M_date_time_format, D_T_FMT)
295 	  WIDE_LANGINFO(_M_date_time_era_format, ERA_D_T_FMT)
296 	  WIDE_LANGINFO(_M_am, AM_STR)
297 	  WIDE_LANGINFO(_M_pm, PM_STR)
298 	  WIDE_LANGINFO(_M_am_pm_format, T_FMT_AMPM)
299 
300 	  // Day names, starting with "C"'s Sunday.
301 	  WIDE_LANGINFO(_M_day1, DAY_1)
302 	  WIDE_LANGINFO(_M_day2, DAY_2)
303 	  WIDE_LANGINFO(_M_day3, DAY_3)
304 	  WIDE_LANGINFO(_M_day4, DAY_4)
305 	  WIDE_LANGINFO(_M_day5, DAY_5)
306 	  WIDE_LANGINFO(_M_day6, DAY_6)
307 	  WIDE_LANGINFO(_M_day7, DAY_7)
308 
309 	  // Abbreviated day names, starting with "C"'s Sun.
310 	  WIDE_LANGINFO(_M_aday1, ABDAY_1)
311 	  WIDE_LANGINFO(_M_aday2, ABDAY_2)
312 	  WIDE_LANGINFO(_M_aday3, ABDAY_3)
313 	  WIDE_LANGINFO(_M_aday4, ABDAY_4)
314 	  WIDE_LANGINFO(_M_aday5, ABDAY_5)
315 	  WIDE_LANGINFO(_M_aday6, ABDAY_6)
316 	  WIDE_LANGINFO(_M_aday7, ABDAY_7)
317 
318 	  // Month names, starting with "C"'s January.
319 	  WIDE_LANGINFO(_M_month01, MON_1)
320 	  WIDE_LANGINFO(_M_month02, MON_2)
321 	  WIDE_LANGINFO(_M_month03, MON_3)
322 	  WIDE_LANGINFO(_M_month04, MON_4)
323 	  WIDE_LANGINFO(_M_month05, MON_5)
324 	  WIDE_LANGINFO(_M_month06, MON_6)
325 	  WIDE_LANGINFO(_M_month07, MON_7)
326 	  WIDE_LANGINFO(_M_month08, MON_8)
327 	  WIDE_LANGINFO(_M_month09, MON_9)
328 	  WIDE_LANGINFO(_M_month10, MON_10)
329 	  WIDE_LANGINFO(_M_month11, MON_11)
330 	  WIDE_LANGINFO(_M_month12, MON_12)
331 
332 	  // Abbreviated month names, starting with "C"'s Jan.
333 	  WIDE_LANGINFO(_M_amonth01, ABMON_1)
334 	  WIDE_LANGINFO(_M_amonth02, ABMON_2)
335 	  WIDE_LANGINFO(_M_amonth03, ABMON_3)
336 	  WIDE_LANGINFO(_M_amonth04, ABMON_4)
337 	  WIDE_LANGINFO(_M_amonth05, ABMON_5)
338 	  WIDE_LANGINFO(_M_amonth06, ABMON_6)
339 	  WIDE_LANGINFO(_M_amonth07, ABMON_7)
340 	  WIDE_LANGINFO(_M_amonth08, ABMON_8)
341 	  WIDE_LANGINFO(_M_amonth09, ABMON_9)
342 	  WIDE_LANGINFO(_M_amonth10, ABMON_10)
343 	  WIDE_LANGINFO(_M_amonth11, ABMON_11)
344 	  WIDE_LANGINFO(_M_amonth12, ABMON_12)
345 	}
346     }
347 
348   template<>
349     __timepunct<wchar_t>::~__timepunct()
350     {
351       delete [] _M_data->_M_date_format;
352       delete [] _M_data->_M_date_era_format;
353       delete [] _M_data->_M_time_format;
354       delete [] _M_data->_M_time_era_format;
355       delete [] _M_data->_M_date_time_format;
356       delete [] _M_data->_M_date_time_era_format;
357       delete [] _M_data->_M_am;
358       delete [] _M_data->_M_pm;
359       delete [] _M_data->_M_am_pm_format;
360       delete [] _M_data->_M_day1;
361       delete [] _M_data->_M_day2;
362       delete [] _M_data->_M_day3;
363       delete [] _M_data->_M_day4;
364       delete [] _M_data->_M_day5;
365       delete [] _M_data->_M_day6;
366       delete [] _M_data->_M_day7;
367       delete [] _M_data->_M_aday1;
368       delete [] _M_data->_M_aday2;
369       delete [] _M_data->_M_aday3;
370       delete [] _M_data->_M_aday4;
371       delete [] _M_data->_M_aday5;
372       delete [] _M_data->_M_aday6;
373       delete [] _M_data->_M_aday7;
374       delete [] _M_data->_M_month01;
375       delete [] _M_data->_M_month02;
376       delete [] _M_data->_M_month03;
377       delete [] _M_data->_M_month04;
378       delete [] _M_data->_M_month05;
379       delete [] _M_data->_M_month06;
380       delete [] _M_data->_M_month07;
381       delete [] _M_data->_M_month08;
382       delete [] _M_data->_M_month09;
383       delete [] _M_data->_M_month10;
384       delete [] _M_data->_M_month11;
385       delete [] _M_data->_M_month12;
386       delete [] _M_data->_M_amonth01;
387       delete [] _M_data->_M_amonth02;
388       delete [] _M_data->_M_amonth03;
389       delete [] _M_data->_M_amonth04;
390       delete [] _M_data->_M_amonth05;
391       delete [] _M_data->_M_amonth06;
392       delete [] _M_data->_M_amonth07;
393       delete [] _M_data->_M_amonth08;
394       delete [] _M_data->_M_amonth09;
395       delete [] _M_data->_M_amonth10;
396       delete [] _M_data->_M_amonth11;
397       delete [] _M_data->_M_amonth12;
398       delete _M_data;
399     }
400 
401 #endif
402 
403 _GLIBCXX_END_NAMESPACE_VERSION
404 } // namespace
405