LibreOffice Module xmloff (master) 1
vcl_date_handler.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 "vcl_date_handler.hxx"
21
22#include <rtl/ustrbuf.hxx>
23
24#include <com/sun/star/util/DateTime.hpp>
25#include <com/sun/star/util/Date.hpp>
26
28
29#include <osl/diagnose.h>
30#include <tools/date.hxx>
31
32namespace xmloff
33{
34
35 using ::com::sun::star::uno::Any;
36 using ::com::sun::star::util::DateTime;
37 using ::com::sun::star::util::Date;
38
39 //= VCLDateHandler
41 {
42 }
43
44 OUString VCLDateHandler::getAttributeValue( const Any& i_propertyValue ) const
45 {
46 Date aDate;
47 OSL_VERIFY( i_propertyValue >>= aDate );
48
49 DateTime aDateTime; // default-inited to 0
50 aDateTime.Day = aDate.Day;
51 aDateTime.Month = aDate.Month;
52 aDateTime.Year = aDate.Year;
53
54 OUStringBuffer aBuffer;
55 ::sax::Converter::convertDateTime( aBuffer, aDateTime, nullptr );
56 return aBuffer.makeStringAndClear();
57 }
58
59 bool VCLDateHandler::getPropertyValues( const OUString& i_attributeValue, PropertyValues& o_propertyValues ) const
60 {
61 DateTime aDateTime;
62 Date aDate;
63 if (::sax::Converter::parseDateTime( aDateTime, i_attributeValue ))
64 {
65 aDate.Day = aDateTime.Day;
66 aDate.Month = aDateTime.Month;
67 aDate.Year = aDateTime.Year;
68 }
69 else
70 {
71 // compatibility format, before we wrote those values in XML-schema compatible form
72 sal_Int32 nVCLDate(0);
73 if (!::sax::Converter::convertNumber(nVCLDate, i_attributeValue))
74 {
75 OSL_ENSURE( false, "VCLDateHandler::getPropertyValues: unknown date format (no XML-schema date, no legacy integer)!" );
76 return false;
77 }
78 aDate = ::Date(nVCLDate).GetUNODate();
79 }
80
81 const Any aPropertyValue( aDate );
82
83 OSL_ENSURE( o_propertyValues.size() == 1, "VCLDateHandler::getPropertyValues: date strings represent exactly one property - not more, not less!" );
84 for ( auto& prop : o_propertyValues )
85 {
86 prop.second = aPropertyValue;
87 }
88 return true;
89 }
90
91} // namespace xmloff
92
93/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::util::Date GetUNODate() const
static void convertDateTime(OUStringBuffer &rBuffer, const css::util::DateTime &rDateTime, sal_Int16 const *pTimeZoneOffset, bool bAddTimeIf0AM=false)
static bool parseDateTime(css::util::DateTime &rDateTime, std::u16string_view rString)
static bool convertNumber(sal_Int32 &rValue, std::u16string_view aString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32)
virtual bool getPropertyValues(const OUString &i_attributeValue, PropertyValues &o_propertyValues) const override
retrieves the values of the properties controlled by an XML attributed, described by a given attribut...
virtual OUString getAttributeValue(const css::uno::Any &i_propertyValue) const override
is a convenience method for XML attributes whose value comprises of only one UNO API property
::std::map< PropertyId, css::uno::Any > PropertyValues
std::unique_ptr< char[]> aBuffer