LibreOffice Module xmloff (master)  1
vcl_time_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_time_handler.hxx"
21 
22 #include <rtl/ustrbuf.hxx>
23 
24 #include <com/sun/star/util/Duration.hpp>
25 #include <com/sun/star/util/Time.hpp>
26 
27 #include <sax/tools/converter.hxx>
28 
29 #include <osl/diagnose.h>
30 #include <tools/time.hxx>
31 
32 namespace xmloff
33 {
34 
35  using ::com::sun::star::uno::Any;
36  using ::com::sun::star::uno::makeAny;
37  using ::com::sun::star::util::Duration;
38  using ::com::sun::star::util::Time;
39 
40  //= VCLTimeHandler
42  {
43  }
44 
45  OUString VCLTimeHandler::getAttributeValue( const Any& i_propertyValue ) const
46  {
47  css::util::Time aTime;
48  OSL_VERIFY( i_propertyValue >>= aTime );
49 
50  Duration aDuration; // default-inited to 0
51  aDuration.Hours = aTime.Hours;
52  aDuration.Minutes = aTime.Minutes;
53  aDuration.Seconds = aTime.Seconds;
54  aDuration.NanoSeconds = aTime.NanoSeconds;
55 
56  OUStringBuffer aBuffer;
57  ::sax::Converter::convertDuration( aBuffer, aDuration );
58  return aBuffer.makeStringAndClear();
59  }
60 
61  bool VCLTimeHandler::getPropertyValues( const OUString& i_attributeValue, PropertyValues& o_propertyValues ) const
62  {
63  Duration aDuration;
64  css::util::Time aTime;
65  if (::sax::Converter::convertDuration( aDuration, i_attributeValue ))
66  {
67  aTime = Time(aDuration.NanoSeconds, aDuration.Seconds,
68  aDuration.Minutes, aDuration.Hours,
69  false);
70  }
71  else
72  {
73  // compatibility format, before we wrote those values in XML-schema compatible form
74  sal_Int64 nVCLTime(0);
75  if (!::sax::Converter::convertNumber64(nVCLTime, i_attributeValue))
76  {
77  OSL_ENSURE( false, "VCLTimeHandler::getPropertyValues: unknown time format (no XML-schema time, no legacy integer)!" );
78  return false;
79  }
80  // legacy integer was in centiseconds
81  nVCLTime *= ::tools::Time::nanoPerCenti;
82  aTime = ::tools::Time(nVCLTime).GetUNOTime();
83  }
84 
85  const Any aPropertyValue( makeAny( aTime ) );
86 
87  OSL_ENSURE( o_propertyValues.size() == 1, "VCLTimeHandler::getPropertyValues: time strings represent exactly one property - not more, not less!" );
88  for ( auto& prop : o_propertyValues )
89  {
90  prop.second = aPropertyValue;
91  }
92  return true;
93  }
94 
95 } // namespace xmloff
96 
97 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::std::map< PropertyId, css::uno::Any > PropertyValues
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 ...
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...
css::util::Time GetUNOTime() const
static bool convertNumber64(sal_Int64 &rValue, std::u16string_view aString, sal_Int64 nMin=SAL_MIN_INT64, sal_Int64 nMax=SAL_MAX_INT64)
static const sal_Int64 nanoPerCenti
static void convertDuration(OUStringBuffer &rBuffer, const double fTime)
std::unique_ptr< char[]> aBuffer
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)