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 <iso8601_converter.hxx>
25 #include <utilities.hxx>
30 #include <rtl/character.hxx>
32 /* Converts ISO 8601 compliant date/time
33 representation to the representation
34 conforming to the current locale
36 std::wstring
iso8601_date_to_local_date(const std::wstring
& isoDate
)
38 ::std::wstring
ws8601DateTime(isoDate
);
40 // Get rid of the optional milliseconds part if it exists.
41 // Function accepts date/time as a combined date/time string in extended ISO8601 format,
42 // which is yyyy-mm-ddThh:mm:ss[.mmm]. Last part is the optional "fraction of second" part,
43 // that's why we cut off at 19.
44 if (ws8601DateTime
.length() > 19)
46 ws8601DateTime
.erase(19, ::std::basic_string
<char>::npos
);
49 if ( ws8601DateTime
.length() == 19 )
51 std::string asDateTime
= WStringToString( ws8601DateTime
);
53 DateTime
.wYear
= static_cast<unsigned short>(strtol( asDateTime
.substr( 0, 4 ).c_str(), nullptr, 10 ));
54 DateTime
.wMonth
= static_cast<unsigned short>(strtol( asDateTime
.substr( 5, 2 ).c_str(), nullptr, 10 ));
55 DateTime
.wDayOfWeek
= 0;
56 DateTime
.wDay
= static_cast<unsigned short>(strtol( asDateTime
.substr( 8, 2 ).c_str(), nullptr, 10 ));
57 DateTime
.wHour
= static_cast<unsigned short>(strtol( asDateTime
.substr( 11,2 ).c_str(), nullptr, 10 ));
58 DateTime
.wMinute
= static_cast<unsigned short>(strtol( asDateTime
.substr( 14,2 ).c_str(), nullptr, 10 ));
59 DateTime
.wSecond
= static_cast<unsigned short>(strtol( asDateTime
.substr( 17,2 ).c_str(), nullptr, 10 ));
60 DateTime
.wMilliseconds
= 0;
62 //get Date info from structure
63 WCHAR DateBuffer
[ MAX_PATH
];
64 int DateSize
= GetDateFormatW(
65 LOCALE_SYSTEM_DEFAULT
,
73 ws8601DateTime
.assign(DateBuffer
);
75 ws8601DateTime
= StringToWString( asDateTime
);
77 //get Time info from structure
78 WCHAR TimeBuffer
[ MAX_PATH
];
80 int TimeSize
= GetTimeFormatW(
81 LOCALE_SYSTEM_DEFAULT
,
90 ws8601DateTime
.append(L
" ");
91 ws8601DateTime
.append(TimeBuffer
);
94 ws8601DateTime
= StringToWString( asDateTime
);
97 return ws8601DateTime
;
101 /* Converts ISO 8601 conform duration
102 representation to the representation
103 conforming to the current locale
105 Expect format PTnHnMnS according to
106 ISO 8601 where n is arbitrary number
110 std::wstring
iso8601_duration_to_local_duration(const std::wstring
& iso8601duration
)
114 std::wstring minutes
;
115 std::wstring seconds
;
119 for (const auto& w_ch
: iso8601duration
)
121 if (rtl::isAsciiDigit(w_ch
)) // wchar_t is unsigned under MSVC
127 if (w_ch
== L
'D' || w_ch
== L
'd')
129 else if (w_ch
== L
'H' || w_ch
== L
'h')
131 else if (w_ch
== L
'M' || w_ch
== L
'm')
133 else if (w_ch
== L
'S' || w_ch
== L
's')
140 if (days
.length() > 0)
142 int h
= (_wtoi(days
.c_str()) * 24) + _wtoi(hours
.c_str());
148 std::wostringstream oss
;
149 oss
<< std::setw(2) << std::setfill(wchar_t('0')) << hours
<< L
":" <<
150 std::setw(2) << std::setfill(wchar_t('0')) << minutes
<< L
":" <<
151 std::setw(2) << std::setfill(wchar_t('0')) << seconds
;
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */