Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / xmloff / source / forms / handler / vcl_date_handler.cxx
blob4dde7c21045f4f1c1475bc82e3d5ce5498bb5f5d
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 "vcl_date_handler.hxx"
22 #include <rtl/ustrbuf.hxx>
24 #include <com/sun/star/util/DateTime.hpp>
25 #include <com/sun/star/util/Date.hpp>
27 #include <sax/tools/converter.hxx>
29 #include <tools/diagnose_ex.h>
30 #include <tools/date.hxx>
32 namespace xmloff
35 using ::com::sun::star::uno::Any;
36 using ::com::sun::star::uno::makeAny;
37 using ::com::sun::star::util::DateTime;
38 using ::com::sun::star::util::Date;
40 //= VCLDateHandler
41 VCLDateHandler::VCLDateHandler()
45 OUString VCLDateHandler::getAttributeValue( const PropertyValues& /*i_propertyValues*/ ) const
47 OSL_ENSURE( false, "VCLDateHandler::getAttributeValue: unexpected call!" );
48 return OUString();
51 OUString VCLDateHandler::getAttributeValue( const Any& i_propertyValue ) const
53 Date aDate;
54 OSL_VERIFY( i_propertyValue >>= aDate );
56 DateTime aDateTime; // default-inited to 0
57 aDateTime.Day = aDate.Day;
58 aDateTime.Month = aDate.Month;
59 aDateTime.Year = aDate.Year;
61 OUStringBuffer aBuffer;
62 ::sax::Converter::convertDateTime( aBuffer, aDateTime, nullptr );
63 return aBuffer.makeStringAndClear();
66 bool VCLDateHandler::getPropertyValues( const OUString& i_attributeValue, PropertyValues& o_propertyValues ) const
68 DateTime aDateTime;
69 Date aDate;
70 if (::sax::Converter::parseDateTime( aDateTime, nullptr, i_attributeValue ))
72 aDate.Day = aDateTime.Day;
73 aDate.Month = aDateTime.Month;
74 aDate.Year = aDateTime.Year;
76 else
78 // compatibility format, before we wrote those values in XML-schema compatible form
79 sal_Int32 nVCLDate(0);
80 if (!::sax::Converter::convertNumber(nVCLDate, i_attributeValue))
82 OSL_ENSURE( false, "VCLDateHandler::getPropertyValues: unknown date format (no XML-schema date, no legacy integer)!" );
83 return false;
85 aDate = ::Date(nVCLDate).GetUNODate();
88 const Any aPropertyValue( makeAny( aDate ) );
90 OSL_ENSURE( o_propertyValues.size() == 1, "VCLDateHandler::getPropertyValues: date strings represent exactly one property - not more, not less!" );
91 for ( PropertyValues::iterator prop = o_propertyValues.begin();
92 prop != o_propertyValues.end();
93 ++prop
96 prop->second = aPropertyValue;
98 return true;
101 } // namespace xmloff
103 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */