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