Bump version to 5.0-14
[LibreOffice.git] / shell / source / win32 / shlxthandler / util / iso8601_converter.cxx
blob537ca8d36ab19187d953831f5e6ae2077e151488
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 "internal/iso8601_converter.hxx"
25 #include "internal/utilities.hxx"
27 #include <sstream>
28 #include <iomanip>
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 );
43 SYSTEMTIME DateTime;
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,
58 &DateTime,
59 NULL,
60 DateBuffer,
61 MAX_PATH );
63 if ( DateSize )
64 ws8601DateTime.assign(DateBuffer);
65 else
66 ws8601DateTime = StringToWString( asDateTime );
68 //get Time info from structure
69 WCHAR TimeBuffer[ MAX_PATH ];
71 int TimeSize = GetTimeFormatW(
72 LOCALE_SYSTEM_DEFAULT,
74 &DateTime,
75 NULL,
76 TimeBuffer,
77 MAX_PATH );
79 if ( TimeSize )
81 ws8601DateTime.append(L" ");
82 ws8601DateTime.append(TimeBuffer);
84 else
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
98 of digits
101 std::wstring iso8601_duration_to_local_duration(const std::wstring& iso8601duration)
103 std::wstring days;
104 std::wstring hours;
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();
111 std::wstring num;
113 for (/**/; iter != iter_end; ++iter)
115 if (isdigit(*iter))
117 num += *iter;
119 else
121 if (*iter == L'D' || *iter == L'd')
122 days = num;
123 else if (*iter == L'H' || *iter == L'h')
124 hours = num;
125 else if (*iter == L'M' || *iter == L'm')
126 minutes = num;
127 else if (*iter == L'S' || *iter == L's')
128 seconds = num;
130 num.clear();
134 if (days.length() > 0)
136 int h = ((_wtoi(days.c_str()) * 24) + _wtoi(hours.c_str()));
137 wchar_t buff[10];
138 _itow(h, buff, 10);
139 hours = buff;
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;
147 return oss.str();
148 #elif defined( __MINGW32__ )
149 #define ADD_AS_PREFILLED( st, out ) \
150 if ( st.length() == 0 ) \
151 out += L"00"; \
152 else if ( st.length() == 1 ) \
153 out += L"0"; \
154 out += st;
156 std::wstring result;
157 ADD_AS_PREFILLED( hours, result )
158 result += L":";
159 ADD_AS_PREFILLED( minutes, result )
160 result += L":";
161 ADD_AS_PREFILLED( seconds, result )
163 return result;
164 #undef ADD_AS_PREFILLED
165 #endif
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */