1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "sal/config.h"
24 #include "internal/iso8601_converter.hxx"
25 #include "internal/utilities.hxx"
31 /* Converts ISO 8601 conform date/time
32 represenation to the representation
33 conforming to the current locale
35 std::wstring
iso8601_date_to_local_date(const std::wstring
& isoDate
)
37 const std::wstring
CONST_SPACE(L
" ");
38 ::std::wstring
ws8601DateTime(isoDate
);
40 if ( ws8601DateTime
.length() == 19 )
42 std::string asDateTime
= WStringToString( ws8601DateTime
);
44 DateTime
.wYear
= ( unsigned short )strtol( asDateTime
.substr( 0, 4 ).c_str(), NULL
, 10 );
45 DateTime
.wMonth
= ( unsigned short )strtol( asDateTime
.substr( 5, 2 ).c_str(), NULL
, 10 );
46 DateTime
.wDayOfWeek
= 0;
47 DateTime
.wDay
= ( unsigned short )strtol( asDateTime
.substr( 8, 2 ).c_str(), NULL
, 10 );
48 DateTime
.wHour
= ( unsigned short )strtol( asDateTime
.substr( 11,2 ).c_str(), NULL
, 10 );
49 DateTime
.wMinute
= ( unsigned short )strtol( asDateTime
.substr( 14,2 ).c_str(), NULL
, 10 );
50 DateTime
.wSecond
= ( unsigned short )strtol( asDateTime
.substr( 17,2 ).c_str(), NULL
, 10 );
51 DateTime
.wMilliseconds
= 0;
53 //get Date info from structure
54 WCHAR DateBuffer
[ MAX_PATH
];
55 int DateSize
= GetDateFormatW(
56 LOCALE_SYSTEM_DEFAULT
,
64 ws8601DateTime
.assign(DateBuffer
);
66 ws8601DateTime
= StringToWString( asDateTime
);
68 //get Time info from structure
69 WCHAR TimeBuffer
[ MAX_PATH
];
71 int TimeSize
= GetTimeFormatW(
72 LOCALE_SYSTEM_DEFAULT
,
81 ws8601DateTime
.append(L
" ");
82 ws8601DateTime
.append(TimeBuffer
);
85 ws8601DateTime
= StringToWString( asDateTime
);
88 return ws8601DateTime
;
92 /* Converts ISO 8601 conform duration
93 representation to the representation
94 conforming to the current locale
96 Expect format PTnHnMnS according to
97 ISO 8601 where n is abitrary number
101 std::wstring
iso8601_duration_to_local_duration(const std::wstring
& iso8601duration
)
105 std::wstring minutes
;
106 std::wstring seconds
;
108 std::wstring::const_iterator iter
= iso8601duration
.begin();
109 std::wstring::const_iterator iter_end
= iso8601duration
.end();
113 for (/**/; iter
!= iter_end
; ++iter
)
121 if (*iter
== L
'D' || *iter
== L
'd')
123 else if (*iter
== L
'H' || *iter
== L
'h')
125 else if (*iter
== L
'M' || *iter
== L
'm')
127 else if (*iter
== L
'S' || *iter
== L
's')
134 if (days
.length() > 0)
136 int h
= ((_wtoi(days
.c_str()) * 24) + _wtoi(hours
.c_str()));
142 #if defined(_MSC_VER) //&& defined(_M_X64)
143 std::wostringstream oss
;
144 oss
<< std::setw(2) << std::setfill(wchar_t('0')) << hours
<< L
":" <<
145 std::setw(2) << std::setfill(wchar_t('0')) << minutes
<< L
":" <<
146 std::setw(2) << std::setfill(wchar_t('0')) << seconds
;
148 #elif defined( __MINGW32__ )
149 #define ADD_AS_PREFILLED( st, out ) \
150 if ( st.length() == 0 ) \
152 else if ( st.length() == 1 ) \
157 ADD_AS_PREFILLED( hours
, result
)
159 ADD_AS_PREFILLED( minutes
, result
)
161 ADD_AS_PREFILLED( seconds
, result
)
164 #undef ADD_AS_PREFILLED
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */