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