1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_shell.hxx"
30*cdf0e10cSrcweir #include "internal/iso8601_converter.hxx"
31*cdf0e10cSrcweir #include "internal/utilities.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <sstream>
34*cdf0e10cSrcweir #include <iomanip>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir //-----------------------------------
37*cdf0e10cSrcweir /* Converts ISO 8601 conform date/time
38*cdf0e10cSrcweir    represenation to the representation
39*cdf0e10cSrcweir    conforming to the current locale
40*cdf0e10cSrcweir */
41*cdf0e10cSrcweir std::wstring iso8601_date_to_local_date(const std::wstring& isoDate )
42*cdf0e10cSrcweir {
43*cdf0e10cSrcweir     const std::wstring CONST_SPACE(L" ");
44*cdf0e10cSrcweir 	::std::wstring ws8601DateTime(isoDate);
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir     if ( ws8601DateTime.length() == 19 )
47*cdf0e10cSrcweir     {
48*cdf0e10cSrcweir         //fill in the SYSTEMTIME structure;
49*cdf0e10cSrcweir         std::string asDateTime = WStringToString( ws8601DateTime );
50*cdf0e10cSrcweir         SYSTEMTIME DateTime;
51*cdf0e10cSrcweir         DateTime.wYear         = ( unsigned short )strtol( asDateTime.substr( 0, 4 ).c_str(), NULL, 10 );
52*cdf0e10cSrcweir         DateTime.wMonth        = ( unsigned short )strtol( asDateTime.substr( 5, 2 ).c_str(), NULL, 10 );
53*cdf0e10cSrcweir         DateTime.wDayOfWeek    =  0;
54*cdf0e10cSrcweir         DateTime.wDay          = ( unsigned short )strtol( asDateTime.substr( 8, 2 ).c_str(), NULL, 10 );
55*cdf0e10cSrcweir         DateTime.wHour         = ( unsigned short )strtol( asDateTime.substr( 11,2 ).c_str(), NULL, 10 );
56*cdf0e10cSrcweir         DateTime.wMinute       = ( unsigned short )strtol( asDateTime.substr( 14,2 ).c_str(), NULL, 10 );
57*cdf0e10cSrcweir         DateTime.wSecond       = ( unsigned short )strtol( asDateTime.substr( 17,2 ).c_str(), NULL, 10 );
58*cdf0e10cSrcweir         DateTime.wMilliseconds =  0;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir         //get Date info from structure
61*cdf0e10cSrcweir         WCHAR DateBuffer[ MAX_PATH ];
62*cdf0e10cSrcweir         int DateSize = GetDateFormatW(
63*cdf0e10cSrcweir             LOCALE_SYSTEM_DEFAULT,
64*cdf0e10cSrcweir             0,
65*cdf0e10cSrcweir             &DateTime,
66*cdf0e10cSrcweir             NULL,
67*cdf0e10cSrcweir             DateBuffer,
68*cdf0e10cSrcweir             MAX_PATH );
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir         if ( DateSize )
71*cdf0e10cSrcweir             ws8601DateTime.assign(DateBuffer);
72*cdf0e10cSrcweir         else
73*cdf0e10cSrcweir             ws8601DateTime = StringToWString( asDateTime );
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir         //get Time info from structure
76*cdf0e10cSrcweir         WCHAR TimeBuffer[ MAX_PATH ];
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir         int TimeSize =  GetTimeFormatW(
79*cdf0e10cSrcweir             LOCALE_SYSTEM_DEFAULT,
80*cdf0e10cSrcweir             0,
81*cdf0e10cSrcweir             &DateTime,
82*cdf0e10cSrcweir             NULL,
83*cdf0e10cSrcweir             TimeBuffer,
84*cdf0e10cSrcweir             MAX_PATH );
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir         if ( TimeSize )
87*cdf0e10cSrcweir         {
88*cdf0e10cSrcweir             ws8601DateTime.append(L" ");
89*cdf0e10cSrcweir             ws8601DateTime.append(TimeBuffer);
90*cdf0e10cSrcweir         }
91*cdf0e10cSrcweir         else
92*cdf0e10cSrcweir             ws8601DateTime = StringToWString( asDateTime );
93*cdf0e10cSrcweir     }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     return ws8601DateTime;
96*cdf0e10cSrcweir }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir //------------------------------------
99*cdf0e10cSrcweir /* Converts ISO 8601 conform duration
100*cdf0e10cSrcweir    representation to the representation
101*cdf0e10cSrcweir    conforming to the current locale
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir    Expect format PTnHnMnS according to
104*cdf0e10cSrcweir    ISO 8601 where n is abitrary number
105*cdf0e10cSrcweir    of digits
106*cdf0e10cSrcweir */
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir std::wstring iso8601_duration_to_local_duration(const std::wstring& iso8601duration)
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir     std::wstring days;
111*cdf0e10cSrcweir     std::wstring hours;
112*cdf0e10cSrcweir     std::wstring minutes;
113*cdf0e10cSrcweir     std::wstring seconds;
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     std::wstring::const_iterator iter     = iso8601duration.begin();
116*cdf0e10cSrcweir     std::wstring::const_iterator iter_end = iso8601duration.end();
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     std::wstring num;
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir     for (/**/; iter != iter_end; ++iter)
121*cdf0e10cSrcweir     {
122*cdf0e10cSrcweir         if (isdigit(*iter))
123*cdf0e10cSrcweir         {
124*cdf0e10cSrcweir             num += *iter;
125*cdf0e10cSrcweir         }
126*cdf0e10cSrcweir         else
127*cdf0e10cSrcweir         {
128*cdf0e10cSrcweir             if (*iter == L'D' || *iter == L'd')
129*cdf0e10cSrcweir                 days = num;
130*cdf0e10cSrcweir             else if (*iter == L'H' || *iter == L'h')
131*cdf0e10cSrcweir                 hours = num;
132*cdf0e10cSrcweir             else if (*iter == L'M' || *iter == L'm')
133*cdf0e10cSrcweir                 minutes = num;
134*cdf0e10cSrcweir             else if (*iter == L'S' || *iter == L's')
135*cdf0e10cSrcweir                 seconds = num;
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir             num.clear();
138*cdf0e10cSrcweir         }
139*cdf0e10cSrcweir     }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir     if (days.length() > 0)
142*cdf0e10cSrcweir     {
143*cdf0e10cSrcweir         int h = ((_wtoi(days.c_str()) * 24) + _wtoi(hours.c_str()));
144*cdf0e10cSrcweir         wchar_t buff[10];
145*cdf0e10cSrcweir         _itow(h, buff, 10);
146*cdf0e10cSrcweir         hours = buff;
147*cdf0e10cSrcweir     }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir #if defined(_MSC_VER) //&& defined(_M_X64)
150*cdf0e10cSrcweir     std::wostringstream oss;
151*cdf0e10cSrcweir     oss << std::setw(2) << std::setfill(wchar_t('0')) << hours   << L":" <<
152*cdf0e10cSrcweir            std::setw(2) << std::setfill(wchar_t('0')) << minutes << L":" <<
153*cdf0e10cSrcweir            std::setw(2) << std::setfill(wchar_t('0')) << seconds;
154*cdf0e10cSrcweir     return oss.str();
155*cdf0e10cSrcweir #elif defined( __MINGW32__ )
156*cdf0e10cSrcweir #define ADD_AS_PREFILLED( st, out ) \
157*cdf0e10cSrcweir     if ( st.length() == 0 ) \
158*cdf0e10cSrcweir         out += L"00"; \
159*cdf0e10cSrcweir     else if ( st.length() == 1 ) \
160*cdf0e10cSrcweir         out += L"0"; \
161*cdf0e10cSrcweir     out += st;
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     std::wstring result;
164*cdf0e10cSrcweir     ADD_AS_PREFILLED( hours, result )
165*cdf0e10cSrcweir     result += L":";
166*cdf0e10cSrcweir     ADD_AS_PREFILLED( minutes, result )
167*cdf0e10cSrcweir     result += L":";
168*cdf0e10cSrcweir     ADD_AS_PREFILLED( seconds, result )
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir     return result;
171*cdf0e10cSrcweir #undef ADD_AS_PREFILLED
172*cdf0e10cSrcweir /*
173*cdf0e10cSrcweir #else
174*cdf0e10cSrcweir     std::wostringstream oss;
175*cdf0e10cSrcweir     oss << std::setw(2) << std::setfill('0') << hours   << L":" <<
176*cdf0e10cSrcweir            std::setw(2) << std::setfill('0') << minutes << L":" <<
177*cdf0e10cSrcweir            std::setw(2) << std::setfill('0') << seconds;
178*cdf0e10cSrcweir     return oss.str();
179*cdf0e10cSrcweir */
180*cdf0e10cSrcweir #endif
181*cdf0e10cSrcweir }
182*cdf0e10cSrcweir 
183