1// std::time_get, std::time_put implementation, generic version -*- C++ -*-
2
3// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010
4// Free Software Foundation, Inc.
5//
6// This file is part of the GNU ISO C++ Library.  This library is free
7// software; you can redistribute it and/or modify it under the
8// terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 3, or (at your option)
10// any later version.
11
12// This library is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16
17// Under Section 7 of GPL version 3, you are granted additional
18// permissions described in the GCC Runtime Library Exception, version
19// 3.1, as published by the Free Software Foundation.
20
21// You should have received a copy of the GNU General Public License and
22// a copy of the GCC Runtime Library Exception along with this program;
23// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24// <http://www.gnu.org/licenses/>.
25
26//
27// ISO C++ 14882: 22.2.5.1.2 - time_get virtual functions
28// ISO C++ 14882: 22.2.5.3.2 - time_put virtual functions
29//
30
31// Written by Benjamin Kosnik <bkoz@redhat.com>
32
33#include <locale>
34#include <cstdlib>
35#include <cstring>
36
37_GLIBCXX_BEGIN_NAMESPACE(std)
38
39  template<>
40    void
41    __timepunct<char>::
42    _M_put(char* __s, size_t __maxlen, const char* __format,
43	   const tm* __tm) const throw()
44    {
45      char* __old = setlocale(LC_ALL, NULL);
46      const size_t __llen = strlen(__old) + 1;
47      char* __sav = new char[__llen];
48      memcpy(__sav, __old, __llen);
49      setlocale(LC_ALL, _M_name_timepunct);
50      const size_t __len = strftime(__s, __maxlen, __format, __tm);
51      setlocale(LC_ALL, __sav);
52      delete [] __sav;
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)
61    {
62      // "C" locale.
63      if (!_M_data)
64	_M_data = new __timepunct_cache<char>;
65
66      _M_data->_M_date_format = "%m/%d/%y";
67      _M_data->_M_date_era_format = "%m/%d/%y";
68      _M_data->_M_time_format = "%H:%M:%S";
69      _M_data->_M_time_era_format = "%H:%M:%S";
70      _M_data->_M_date_time_format = "";
71      _M_data->_M_date_time_era_format = "";
72      _M_data->_M_am = "AM";
73      _M_data->_M_pm = "PM";
74      _M_data->_M_am_pm_format = "";
75
76      // Day names, starting with "C"'s Sunday.
77      _M_data->_M_day1 = "Sunday";
78      _M_data->_M_day2 = "Monday";
79      _M_data->_M_day3 = "Tuesday";
80      _M_data->_M_day4 = "Wednesday";
81      _M_data->_M_day5 = "Thursday";
82      _M_data->_M_day6 = "Friday";
83      _M_data->_M_day7 = "Saturday";
84
85      // Abbreviated day names, starting with "C"'s Sun.
86      _M_data->_M_aday1 = "Sun";
87      _M_data->_M_aday2 = "Mon";
88      _M_data->_M_aday3 = "Tue";
89      _M_data->_M_aday4 = "Wed";
90      _M_data->_M_aday5 = "Thu";
91      _M_data->_M_aday6 = "Fri";
92      _M_data->_M_aday7 = "Sat";
93
94      // Month names, starting with "C"'s January.
95      _M_data->_M_month01 = "January";
96      _M_data->_M_month02 = "February";
97      _M_data->_M_month03 = "March";
98      _M_data->_M_month04 = "April";
99      _M_data->_M_month05 = "May";
100      _M_data->_M_month06 = "June";
101      _M_data->_M_month07 = "July";
102      _M_data->_M_month08 = "August";
103      _M_data->_M_month09 = "September";
104      _M_data->_M_month10 = "October";
105      _M_data->_M_month11 = "November";
106      _M_data->_M_month12 = "December";
107
108      // Abbreviated month names, starting with "C"'s Jan.
109      _M_data->_M_amonth01 = "Jan";
110      _M_data->_M_amonth02 = "Feb";
111      _M_data->_M_amonth03 = "Mar";
112      _M_data->_M_amonth04 = "Apr";
113      _M_data->_M_amonth05 = "May";
114      _M_data->_M_amonth06 = "Jun";
115      _M_data->_M_amonth07 = "Jul";
116      _M_data->_M_amonth08 = "Aug";
117      _M_data->_M_amonth09 = "Sep";
118      _M_data->_M_amonth10 = "Oct";
119      _M_data->_M_amonth11 = "Nov";
120      _M_data->_M_amonth12 = "Dec";
121    }
122
123#ifdef _GLIBCXX_USE_WCHAR_T
124  template<>
125    void
126    __timepunct<wchar_t>::
127    _M_put(wchar_t* __s, size_t __maxlen, const wchar_t* __format,
128	   const tm* __tm) const throw()
129    {
130      char* __old = setlocale(LC_ALL, NULL);
131      const size_t __llen = strlen(__old) + 1;
132      char* __sav = new char[__llen];
133      memcpy(__sav, __old, __llen);
134      setlocale(LC_ALL, _M_name_timepunct);
135      const size_t __len = wcsftime(__s, __maxlen, __format, __tm);
136      setlocale(LC_ALL, __sav);
137      delete [] __sav;
138      // Make sure __s is null terminated.
139      if (__len == 0)
140	__s[0] = L'\0';
141    }
142
143  template<>
144    void
145    __timepunct<wchar_t>::_M_initialize_timepunct(__c_locale)
146    {
147      // "C" locale.
148      if (!_M_data)
149	_M_data = new __timepunct_cache<wchar_t>;
150
151      _M_data->_M_date_format = L"%m/%d/%y";
152      _M_data->_M_date_era_format = L"%m/%d/%y";
153      _M_data->_M_time_format = L"%H:%M:%S";
154      _M_data->_M_time_era_format = L"%H:%M:%S";
155      _M_data->_M_date_time_format = L"";
156      _M_data->_M_date_time_era_format = L"";
157      _M_data->_M_am = L"AM";
158      _M_data->_M_pm = L"PM";
159      _M_data->_M_am_pm_format = L"";
160
161      // Day names, starting with "C"'s Sunday.
162      _M_data->_M_day1 = L"Sunday";
163      _M_data->_M_day2 = L"Monday";
164      _M_data->_M_day3 = L"Tuesday";
165      _M_data->_M_day4 = L"Wednesday";
166      _M_data->_M_day5 = L"Thursday";
167      _M_data->_M_day6 = L"Friday";
168      _M_data->_M_day7 = L"Saturday";
169
170      // Abbreviated day names, starting with "C"'s Sun.
171      _M_data->_M_aday1 = L"Sun";
172      _M_data->_M_aday2 = L"Mon";
173      _M_data->_M_aday3 = L"Tue";
174      _M_data->_M_aday4 = L"Wed";
175      _M_data->_M_aday5 = L"Thu";
176      _M_data->_M_aday6 = L"Fri";
177      _M_data->_M_aday7 = L"Sat";
178
179      // Month names, starting with "C"'s January.
180      _M_data->_M_month01 = L"January";
181      _M_data->_M_month02 = L"February";
182      _M_data->_M_month03 = L"March";
183      _M_data->_M_month04 = L"April";
184      _M_data->_M_month05 = L"May";
185      _M_data->_M_month06 = L"June";
186      _M_data->_M_month07 = L"July";
187      _M_data->_M_month08 = L"August";
188      _M_data->_M_month09 = L"September";
189      _M_data->_M_month10 = L"October";
190      _M_data->_M_month11 = L"November";
191      _M_data->_M_month12 = L"December";
192
193      // Abbreviated month names, starting with "C"'s Jan.
194      _M_data->_M_amonth01 = L"Jan";
195      _M_data->_M_amonth02 = L"Feb";
196      _M_data->_M_amonth03 = L"Mar";
197      _M_data->_M_amonth04 = L"Apr";
198      _M_data->_M_amonth05 = L"May";
199      _M_data->_M_amonth06 = L"Jun";
200      _M_data->_M_amonth07 = L"Jul";
201      _M_data->_M_amonth08 = L"Aug";
202      _M_data->_M_amonth09 = L"Sep";
203      _M_data->_M_amonth10 = L"Oct";
204      _M_data->_M_amonth11 = L"Nov";
205      _M_data->_M_amonth12 = L"Dec";
206    }
207#endif
208
209_GLIBCXX_END_NAMESPACE
210