LibreOffice Module shell (master) 1
iso8601_converter.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <stdlib.h>
23
24#include <iso8601_converter.hxx>
25#include <utilities.hxx>
26
27#include <sstream>
28#include <iomanip>
29
30#include <rtl/character.hxx>
31
32/* Converts ISO 8601 compliant date/time
33 representation to the representation
34 conforming to the current locale
35*/
36std::wstring iso8601_date_to_local_date(const std::wstring& isoDate )
37{
38 ::std::wstring ws8601DateTime(isoDate);
39
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)
45 {
46 ws8601DateTime.erase(19, ::std::basic_string<char>::npos);
47 }
48
49 if ( ws8601DateTime.length() == 19 )
50 {
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;
61
62 //get Date info from structure
63 WCHAR DateBuffer[ MAX_PATH ];
64 int DateSize = GetDateFormatW(
65 LOCALE_SYSTEM_DEFAULT,
66 0,
67 &DateTime,
68 nullptr,
69 DateBuffer,
70 MAX_PATH );
71
72 if ( DateSize )
73 ws8601DateTime.assign(DateBuffer);
74 else
75 ws8601DateTime = StringToWString( asDateTime );
76
77 //get Time info from structure
78 WCHAR TimeBuffer[ MAX_PATH ];
79
80 int TimeSize = GetTimeFormatW(
81 LOCALE_SYSTEM_DEFAULT,
82 0,
83 &DateTime,
84 nullptr,
85 TimeBuffer,
86 MAX_PATH );
87
88 if ( TimeSize )
89 {
90 ws8601DateTime.append(L" ");
91 ws8601DateTime.append(TimeBuffer);
92 }
93 else
94 ws8601DateTime = StringToWString( asDateTime );
95 }
96
97 return ws8601DateTime;
98}
99
100
101/* Converts ISO 8601 conform duration
102 representation to the representation
103 conforming to the current locale
104
105 Expect format PTnHnMnS according to
106 ISO 8601 where n is arbitrary number
107 of digits
108*/
109
110std::wstring iso8601_duration_to_local_duration(const std::wstring& iso8601duration)
111{
112 std::wstring days;
113 std::wstring hours;
114 std::wstring minutes;
115 std::wstring seconds;
116
117 std::wstring num;
118
119 for (const auto& w_ch : iso8601duration)
120 {
121 if (rtl::isAsciiDigit(w_ch)) // wchar_t is unsigned under MSVC
122 {
123 num += w_ch;
124 }
125 else
126 {
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;
135
136 num.clear();
137 }
138 }
139
140 if (days.length() > 0)
141 {
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;
146 }
147
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();
153}
154
155/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define MAX_PATH
std::wstring iso8601_duration_to_local_duration(const std::wstring &iso8601duration)
std::wstring iso8601_date_to_local_date(const std::wstring &isoDate)
sal_Int32 h
static std::string WStringToString(const std::wstring &String, int codepage)
Definition: utilities.cxx:48
static std::wstring StringToWString(const std::string &String, int codepage)
Definition: utilities.cxx:34