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