tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / shell / source / win32 / shlxthandler / util / iso8601_converter.cxx
blobc0415deb9e3030c13d1ba14b7384217eb9a5477a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
22 #include <stdlib.h>
24 #include <iso8601_converter.hxx>
25 #include <utilities.hxx>
27 #include <sstream>
28 #include <iomanip>
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 );
52 SYSTEMTIME DateTime;
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,
67 &DateTime,
68 nullptr,
69 DateBuffer,
70 MAX_PATH );
72 if ( DateSize )
73 ws8601DateTime.assign(DateBuffer);
74 else
75 ws8601DateTime = StringToWString( asDateTime );
77 //get Time info from structure
78 WCHAR TimeBuffer[ MAX_PATH ];
80 int TimeSize = GetTimeFormatW(
81 LOCALE_SYSTEM_DEFAULT,
83 &DateTime,
84 nullptr,
85 TimeBuffer,
86 MAX_PATH );
88 if ( TimeSize )
90 ws8601DateTime.append(L" ");
91 ws8601DateTime.append(TimeBuffer);
93 else
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
107 of digits
110 std::wstring iso8601_duration_to_local_duration(const std::wstring& iso8601duration)
112 std::wstring days;
113 std::wstring hours;
114 std::wstring minutes;
115 std::wstring seconds;
117 std::wstring num;
119 for (const auto& w_ch : iso8601duration)
121 if (rtl::isAsciiDigit(w_ch)) // wchar_t is unsigned under MSVC
123 num += w_ch;
125 else
127 if (w_ch == L'D' || w_ch == L'd')
128 days = num;
129 else if (w_ch == L'H' || w_ch == L'h')
130 hours = num;
131 else if (w_ch == L'M' || w_ch == L'm')
132 minutes = num;
133 else if (w_ch == L'S' || w_ch == L's')
134 seconds = num;
136 num.clear();
140 if (days.length() > 0)
142 int h = (_wtoi(days.c_str()) * 24) + _wtoi(hours.c_str());
143 wchar_t buff[10];
144 _itow(h, buff, 10);
145 hours = buff;
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;
152 return oss.str();
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */