1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_shell.hxx"
26 #include "internal/iso8601_converter.hxx"
27 #include "internal/utilities.hxx"
28 
29 #include <sstream>
30 #include <iomanip>
31 
32 //-----------------------------------
33 /* Converts ISO 8601 conform date/time
34    representation to the representation
35    conforming to the current locale
36 */
iso8601_date_to_local_date(const std::wstring & isoDate)37 std::wstring iso8601_date_to_local_date(const std::wstring& isoDate )
38 {
39 	const std::wstring CONST_SPACE(L" ");
40 	::std::wstring ws8601DateTime(isoDate);
41 
42 	if ( ws8601DateTime.length() >= 19 )
43 	{
44 		// fill in the SYSTEMTIME structure;
45 		std::string asDateTime = WStringToString( ws8601DateTime );
46 		SYSTEMTIME DateTime;
47 		DateTime.wYear			= ( unsigned short )strtol( asDateTime.substr( 0, 4 ).c_str(), NULL, 10 );
48 		DateTime.wMonth			= ( unsigned short )strtol( asDateTime.substr( 5, 2 ).c_str(), NULL, 10 );
49 		DateTime.wDayOfWeek		= 0;
50 		DateTime.wDay			= ( unsigned short )strtol( asDateTime.substr( 8, 2 ).c_str(), NULL, 10 );
51 		DateTime.wHour			= ( unsigned short )strtol( asDateTime.substr( 11,2 ).c_str(), NULL, 10 );
52 		DateTime.wMinute		= ( unsigned short )strtol( asDateTime.substr( 14,2 ).c_str(), NULL, 10 );
53 		DateTime.wSecond		= ( unsigned short )strtol( asDateTime.substr( 17,2 ).c_str(), NULL, 10 );
54 		DateTime.wMilliseconds	= 0;
55 
56 		// get Date info from structure
57 		WCHAR DateBuffer[ MAX_PATH ];
58 		int DateSize = GetDateFormatW(
59 			LOCALE_SYSTEM_DEFAULT,
60 			0,
61 			&DateTime,
62 			NULL,
63 			DateBuffer,
64 			MAX_PATH );
65 
66 		if ( DateSize )
67 			ws8601DateTime.assign(DateBuffer);
68 		else
69 			ws8601DateTime = StringToWString( asDateTime );
70 
71 		// get Time info from structure
72 		WCHAR TimeBuffer[ MAX_PATH ];
73 
74 		int TimeSize = GetTimeFormatW(
75 			LOCALE_SYSTEM_DEFAULT,
76 			0,
77 			&DateTime,
78 			NULL,
79 			TimeBuffer,
80 			MAX_PATH );
81 
82 		if ( TimeSize )
83 		{
84 			ws8601DateTime.append(L" ");
85 			ws8601DateTime.append(TimeBuffer);
86 		}
87 		else
88 			ws8601DateTime = StringToWString( asDateTime );
89 	}
90 
91 	return ws8601DateTime;
92 }
93 
94 //------------------------------------
95 /* Converts ISO 8601 conform duration
96    representation to the representation
97    conforming to the current locale
98 
99    Expect format PTnHnMnS according to
100    ISO 8601 where n is arbitrary number
101    of digits
102 */
103 
iso8601_duration_to_local_duration(const std::wstring & iso8601duration)104 std::wstring iso8601_duration_to_local_duration(const std::wstring& iso8601duration)
105 {
106 	std::wstring days;
107 	std::wstring hours;
108 	std::wstring minutes;
109 	std::wstring seconds;
110 
111 	std::wstring::const_iterator iter = iso8601duration.begin();
112 	std::wstring::const_iterator iter_end = iso8601duration.end();
113 
114 	std::wstring num;
115 
116 	for (/**/; iter != iter_end; ++iter)
117 	{
118 		if (isdigit(*iter))
119 		{
120 			num += *iter;
121 		}
122 		else
123 		{
124 			if (*iter == L'D' || *iter == L'd')
125 				days = num;
126 			else if (*iter == L'H' || *iter == L'h')
127 				hours = num;
128 			else if (*iter == L'M' || *iter == L'm')
129 				minutes = num;
130 			else if (*iter == L'S' || *iter == L's')
131 				seconds = num;
132 
133 			num.clear();
134 		}
135 	}
136 
137 	if (days.length() > 0)
138 	{
139 		int h = ((_wtoi(days.c_str()) * 24) + _wtoi(hours.c_str()));
140 		wchar_t buff[10];
141 		_itow(h, buff, 10);
142 		hours = buff;
143 	}
144 
145 #if defined(_MSC_VER) //&& defined(_M_X64)
146 	std::wostringstream oss;
147 	oss << std::setw(2) << std::setfill(wchar_t('0')) << hours   << L":" <<
148 		   std::setw(2) << std::setfill(wchar_t('0')) << minutes << L":" <<
149 		   std::setw(2) << std::setfill(wchar_t('0')) << seconds;
150 	return oss.str();
151 #elif defined( __MINGW32__ )
152 #define ADD_AS_PREFILLED( st, out ) \
153 	if ( st.length() == 0 ) \
154 		out += L"00"; \
155 	else if ( st.length() == 1 ) \
156 		out += L"0"; \
157 	out += st;
158 
159 	std::wstring result;
160 	ADD_AS_PREFILLED( hours, result )
161 	result += L":";
162 	ADD_AS_PREFILLED( minutes, result )
163 	result += L":";
164 	ADD_AS_PREFILLED( seconds, result )
165 
166 	return result;
167 #undef ADD_AS_PREFILLED
168 /*
169 #else
170 	std::wostringstream oss;
171 	oss << std::setw(2) << std::setfill('0') << hours   << L":" <<
172 		   std::setw(2) << std::setfill('0') << minutes << L":" <<
173 		   std::setw(2) << std::setfill('0') << seconds;
174 	return oss.str();
175 */
176 #endif
177 }
178 
179 /* vim: set noet sw=4 ts=4: */
180