1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: iso8601_converter.cxx,v $
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"
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
);
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
,
74 ws8601DateTime
.assign(DateBuffer
);
76 ws8601DateTime
= StringToWString( asDateTime
);
78 //get Time info from structure
79 WCHAR TimeBuffer
[ MAX_PATH
];
81 int TimeSize
= GetTimeFormatW(
82 LOCALE_SYSTEM_DEFAULT
,
91 ws8601DateTime
.append(L
" ");
92 ws8601DateTime
.append(TimeBuffer
);
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
111 std::wstring
iso8601_duration_to_local_duration(const std::wstring
& iso8601duration
)
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();
123 for (/**/; iter
!= iter_end
; ++iter
)
131 if (*iter
== L
'D' || *iter
== L
'd')
133 else if (*iter
== L
'H' || *iter
== L
'h')
135 else if (*iter
== L
'M' || *iter
== L
'm')
137 else if (*iter
== L
'S' || *iter
== L
's')
144 if (days
.length() > 0)
146 int h
= ((_wtoi(days
.c_str()) * 24) + _wtoi(hours
.c_str()));
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
;
158 #elif defined( __MINGW32__ )
159 #define ADD_AS_PREFILLED( st, out ) \
160 if ( st.length() == 0 ) \
162 else if ( st.length() == 1 ) \
167 ADD_AS_PREFILLED( hours
, result
)
169 ADD_AS_PREFILLED( minutes
, result
)
171 ADD_AS_PREFILLED( seconds
, result
)
174 #undef ADD_AS_PREFILLED
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
;