1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "precompiled_xmloff.hxx"
25 
26 #include "vcl_date_handler.hxx"
27 #include "xmloff/xmluconv.hxx"
28 
29 #include <com/sun/star/util/DateTime.hpp>
30 
31 #include <tools/diagnose_ex.h>
32 #include <tools/date.hxx>
33 
34 //......................................................................................................................
35 namespace xmloff
36 {
37 //......................................................................................................................
38 
39     using ::com::sun::star::uno::Any;
40     using ::com::sun::star::uno::makeAny;
41     using ::com::sun::star::util::DateTime;
42 
43 	//==================================================================================================================
44 	//= VCLDateHandler
45 	//==================================================================================================================
46 	//------------------------------------------------------------------------------------------------------------------
VCLDateHandler()47     VCLDateHandler::VCLDateHandler()
48     {
49     }
50 
51 	//------------------------------------------------------------------------------------------------------------------
getAttributeValue(const PropertyValues &) const52     ::rtl::OUString VCLDateHandler::getAttributeValue( const PropertyValues& /*i_propertyValues*/ ) const
53     {
54         OSL_ENSURE( false, "VCLDateHandler::getAttributeValue: unexpected call!" );
55         return ::rtl::OUString();
56     }
57 
58 	//------------------------------------------------------------------------------------------------------------------
getAttributeValue(const Any & i_propertyValue) const59     ::rtl::OUString VCLDateHandler::getAttributeValue( const Any& i_propertyValue ) const
60     {
61         sal_Int32 nVCLDate(0);
62         OSL_VERIFY( i_propertyValue >>= nVCLDate );
63         ::Date aVCLDate( nVCLDate );
64 
65         DateTime aDateTime; // default-inited to 0
66         aDateTime.Day = aVCLDate.GetDay();
67         aDateTime.Month = aVCLDate.GetMonth();
68         aDateTime.Year = aVCLDate.GetYear();
69 
70         ::rtl::OUStringBuffer aBuffer;
71         SvXMLUnitConverter::convertDateTime( aBuffer, aDateTime, sal_False );
72         return aBuffer.makeStringAndClear();
73     }
74 
75 	//------------------------------------------------------------------------------------------------------------------
getPropertyValues(const::rtl::OUString i_attributeValue,PropertyValues & o_propertyValues) const76     bool VCLDateHandler::getPropertyValues( const ::rtl::OUString i_attributeValue, PropertyValues& o_propertyValues ) const
77     {
78         sal_Int32 nVCLDate(0);
79 
80         DateTime aDateTime;
81         if ( SvXMLUnitConverter::convertDateTime( aDateTime, i_attributeValue ) )
82         {
83             ::Date aVCLDate( aDateTime.Day, aDateTime.Month, aDateTime.Year );
84             nVCLDate = aVCLDate.GetDate();
85         }
86         else
87         {
88             // compatibility format, before we wrote those values in XML-schema compatible form
89 			if ( !SvXMLUnitConverter::convertNumber( nVCLDate, i_attributeValue ) )
90             {
91                 OSL_ENSURE( false, "VCLDateHandler::getPropertyValues: unknown date format (no XML-schema date, no legacy integer)!" );
92                 return false;
93             }
94         }
95 
96         const Any aPropertyValue( makeAny( nVCLDate ) );
97 
98         OSL_ENSURE( o_propertyValues.size() == 1, "VCLDateHandler::getPropertyValues: date strings represent exactly one property - not more, not less!" );
99         for (   PropertyValues::iterator prop = o_propertyValues.begin();
100                 prop != o_propertyValues.end();
101                 ++prop
102             )
103         {
104             prop->second = aPropertyValue;
105         }
106         return true;
107     }
108 
109 //......................................................................................................................
110 } // namespace xmloff
111 //......................................................................................................................
112