LCOV - code coverage report
Current view: top level - libreoffice/unotools/source/i18n - calendarwrapper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 74 147 50.3 %
Date: 2012-12-27 Functions: 19 27 70.4 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
      21             : #include <rtl/strbuf.hxx>
      22             : #include <tools/string.hxx>
      23             : #include <tools/debug.hxx>
      24             : #include <unotools/calendarwrapper.hxx>
      25             : #include <instance.hxx>
      26             : #include <com/sun/star/i18n/CalendarFieldIndex.hpp>
      27             : #include <com/sun/star/i18n/LocaleCalendar.hpp>
      28             : 
      29             : using namespace ::com::sun::star;
      30             : using namespace ::com::sun::star::i18n;
      31             : using namespace ::com::sun::star::uno;
      32             : 
      33             : 
      34             : const double MILLISECONDS_PER_DAY = 1000.0 * 60.0 * 60.0 * 24.0;
      35             : 
      36             : 
      37          32 : CalendarWrapper::CalendarWrapper(
      38             :             const Reference< uno::XComponentContext > & rxContext
      39             :             )
      40             :         :
      41          32 :         aEpochStart( Date( 1, 1, 1970 ) )
      42             : {
      43          32 :     xC = LocaleCalendar::create(rxContext);
      44          32 : }
      45             : 
      46          18 : CalendarWrapper::~CalendarWrapper()
      47             : {
      48          18 : }
      49             : 
      50             : 
      51          32 : void CalendarWrapper::loadDefaultCalendar( const ::com::sun::star::lang::Locale& rLocale )
      52             : {
      53             :     try
      54             :     {
      55          32 :         if ( xC.is() )
      56          32 :             xC->loadDefaultCalendar( rLocale );
      57             :     }
      58           0 :     catch (const Exception& e)
      59             :     {
      60             :         SAL_WARN( "unotools.i18n", "loadDefaultCalendar: Exception caught " << e.Message );
      61             :     }
      62          32 : }
      63             : 
      64             : 
      65           0 : void CalendarWrapper::loadCalendar( const ::rtl::OUString& rUniqueID, const ::com::sun::star::lang::Locale& rLocale )
      66             : {
      67             :     try
      68             :     {
      69           0 :         if ( xC.is() )
      70           0 :             xC->loadCalendar( rUniqueID, rLocale );
      71             :     }
      72           0 :     catch (const Exception& e)
      73             :     {
      74             :         SAL_WARN( "unotools.i18n", "loadCalendar: Exception caught requested: "
      75             :             << rUniqueID << "   Locale: " << rLocale.Language << "_" << rLocale.Country << " " << e.Message );
      76             :     }
      77           0 : }
      78             : 
      79             : 
      80           0 : ::com::sun::star::uno::Sequence< ::rtl::OUString > CalendarWrapper::getAllCalendars( const ::com::sun::star::lang::Locale& rLocale ) const
      81             : {
      82             :     try
      83             :     {
      84           0 :         if ( xC.is() )
      85           0 :             return xC->getAllCalendars( rLocale );
      86             :     }
      87           0 :     catch (const Exception& e)
      88             :     {
      89             :         SAL_WARN( "unotools.i18n", "getAllCalendars: Exception caught " << e.Message );
      90             :     }
      91             : 
      92           0 :     return ::com::sun::star::uno::Sequence< ::rtl::OUString > (0);
      93             : }
      94             : 
      95             : 
      96         188 : ::rtl::OUString CalendarWrapper::getUniqueID() const
      97             : {
      98             :     try
      99             :     {
     100         188 :         if ( xC.is() )
     101         188 :             return xC->getUniqueID();
     102             :     }
     103           0 :     catch (const Exception& e)
     104             :     {
     105             :         SAL_WARN( "unotools.i18n", "getUniqueID: Exception caught " << e.Message );
     106             :     }
     107           0 :     return ::rtl::OUString();
     108             : }
     109             : 
     110             : 
     111           0 : void CalendarWrapper::setDateTime( double nTimeInDays )
     112             : {
     113             :     try
     114             :     {
     115           0 :         if ( xC.is() )
     116           0 :             xC->setDateTime( nTimeInDays );
     117             :     }
     118           0 :     catch (const Exception& e)
     119             :     {
     120             :         SAL_WARN( "unotools.i18n", "setDateTime: Exception caught " << e.Message );
     121             :     }
     122           0 : }
     123             : 
     124             : 
     125           0 : double CalendarWrapper::getDateTime() const
     126             : {
     127             :     try
     128             :     {
     129           0 :         if ( xC.is() )
     130           0 :             return xC->getDateTime();
     131             :     }
     132           0 :     catch (const Exception& e)
     133             :     {
     134             :         SAL_WARN( "unotools.i18n", "getDateTime: Exception caught " << e.Message );
     135             :     }
     136           0 :     return 0.0;
     137             : }
     138             : 
     139             : 
     140        1288 : sal_Int32 CalendarWrapper::getCombinedOffsetInMillis(
     141             :         sal_Int16 nParentFieldIndex, sal_Int16 nChildFieldIndex ) const
     142             : {
     143        1288 :     sal_Int32 nOffset = 0;
     144             :     try
     145             :     {
     146        1288 :         if ( xC.is() )
     147             :         {
     148        1288 :             nOffset = static_cast<sal_Int32>( xC->getValue( nParentFieldIndex )) * 60000;
     149        1288 :             sal_Int16 nSecondMillis = xC->getValue( nChildFieldIndex );
     150        1288 :             if (nOffset < 0)
     151           0 :                 nOffset -= static_cast<sal_uInt16>( nSecondMillis);
     152             :             else
     153        1288 :                 nOffset += static_cast<sal_uInt16>( nSecondMillis);
     154             :         }
     155             :     }
     156           0 :     catch (const Exception& e)
     157             :     {
     158             :         SAL_WARN( "unotools.i18n", "setLocalDateTime: Exception caught " << e.Message );
     159             :     }
     160        1288 :     return nOffset;
     161             : }
     162             : 
     163             : 
     164         644 : sal_Int32 CalendarWrapper::getZoneOffsetInMillis() const
     165             : {
     166             :     return getCombinedOffsetInMillis( CalendarFieldIndex::ZONE_OFFSET,
     167         644 :             CalendarFieldIndex::ZONE_OFFSET_SECOND_MILLIS);
     168             : }
     169             : 
     170             : 
     171         644 : sal_Int32 CalendarWrapper::getDSTOffsetInMillis() const
     172             : {
     173             :     return getCombinedOffsetInMillis( CalendarFieldIndex::DST_OFFSET,
     174         644 :             CalendarFieldIndex::DST_OFFSET_SECOND_MILLIS);
     175             : }
     176             : 
     177             : 
     178         292 : void CalendarWrapper::setLocalDateTime( double nTimeInDays )
     179             : {
     180             :     try
     181             :     {
     182         292 :         if ( xC.is() )
     183             :         {
     184             :             // First set a nearby value to obtain the timezone and DST offset.
     185             :             // This is necessary to let ICU choose the corresponding
     186             :             // OlsonTimeZone transitions. Since ICU incorporates also
     187             :             // historical data even the timezone may differ for different
     188             :             // dates! (Which was the cause for #i76623# when the timezone of a
     189             :             // previously set date was used.) Timezone may also include
     190             :             // seconds, so use milliseconds field as well.
     191         292 :             xC->setDateTime( nTimeInDays );
     192         292 :             sal_Int32 nZone1 = getZoneOffsetInMillis();
     193         292 :             sal_Int32 nDST1  = getDSTOffsetInMillis();
     194         292 :             double nLoc = nTimeInDays - (double)(nZone1 + nDST1) / MILLISECONDS_PER_DAY;
     195         292 :             xC->setDateTime( nLoc );
     196         292 :             sal_Int32 nZone2 = getZoneOffsetInMillis();
     197         292 :             sal_Int32 nDST2  = getDSTOffsetInMillis();
     198             :             // If DSTs differ after calculation, we crossed boundaries. Do it
     199             :             // again, this time using the DST corrected initial value for the
     200             :             // real local time.
     201             :             // See also localtime/gmtime conversion pitfalls at
     202             :             // http://www.erack.de/download/timetest.c
     203         292 :             if ( nDST1 != nDST2 )
     204             :             {
     205           0 :                 nLoc = nTimeInDays - (double)(nZone2 + nDST2) / MILLISECONDS_PER_DAY;
     206           0 :                 xC->setDateTime( nLoc );
     207             :                 // #i17222# If the DST onset rule says to switch from 00:00 to
     208             :                 // 01:00 and we tried to set onsetDay 00:00 with DST, the
     209             :                 // result was onsetDay-1 23:00 and no DST, which is not what we
     210             :                 // want. So once again without DST, resulting in onsetDay
     211             :                 // 01:00 and DST. Yes, this seems to be weird, but logically
     212             :                 // correct.
     213           0 :                 sal_Int32 nDST3 = getDSTOffsetInMillis();
     214           0 :                 if ( nDST2 != nDST3 && !nDST3 )
     215             :                 {
     216           0 :                     nLoc = nTimeInDays - (double)(nZone2 + nDST3) / MILLISECONDS_PER_DAY;
     217           0 :                     xC->setDateTime( nLoc );
     218             :                 }
     219             :             }
     220             :         }
     221             :     }
     222           0 :     catch (const Exception& e)
     223             :     {
     224             :         SAL_WARN( "unotools.i18n",  "setLocalDateTime: Exception caught " << e.Message );
     225             :     }
     226         292 : }
     227             : 
     228             : 
     229          60 : double CalendarWrapper::getLocalDateTime() const
     230             : {
     231             :     try
     232             :     {
     233          60 :         if ( xC.is() )
     234             :         {
     235          60 :             double nTimeInDays = xC->getDateTime();
     236          60 :             sal_Int32 nZone = getZoneOffsetInMillis();
     237          60 :             sal_Int32 nDST = getDSTOffsetInMillis();
     238          60 :             nTimeInDays += (double)(nZone + nDST) / MILLISECONDS_PER_DAY;
     239          60 :             return nTimeInDays;
     240             :         }
     241             :     }
     242           0 :     catch (const Exception& e)
     243             :     {
     244             :         SAL_WARN( "unotools.i18n",  "getLocalDateTime: Exception caught " << e.Message );
     245             :     }
     246           0 :     return 0.0;
     247             : }
     248             : 
     249             : 
     250         240 : void CalendarWrapper::setValue( sal_Int16 nFieldIndex, sal_Int16 nValue )
     251             : {
     252             :     try
     253             :     {
     254         240 :         if ( xC.is() )
     255         240 :             xC->setValue( nFieldIndex, nValue );
     256             :     }
     257           0 :     catch (const Exception& e)
     258             :     {
     259             :         SAL_WARN( "unotools.i18n",  "setValue: Exception caught " << e.Message );
     260             :     }
     261         240 : }
     262             : 
     263             : 
     264          60 : sal_Bool CalendarWrapper::isValid() const
     265             : {
     266             :     try
     267             :     {
     268          60 :         if ( xC.is() )
     269          60 :             return xC->isValid();
     270             :     }
     271           0 :     catch (const Exception& e)
     272             :     {
     273             :         SAL_WARN( "unotools.i18n",  "isValue: Exception caught " << e.Message );
     274             :     }
     275           0 :     return sal_False;
     276             : }
     277             : 
     278             : 
     279           0 : sal_Int16 CalendarWrapper::getValue( sal_Int16 nFieldIndex ) const
     280             : {
     281             :     try
     282             :     {
     283           0 :         if ( xC.is() )
     284           0 :             return xC->getValue( nFieldIndex );
     285             :     }
     286           0 :     catch (const Exception& e)
     287             :     {
     288             :         SAL_WARN( "unotools.i18n", "getValue: Exception caught " << e.Message );
     289             :     }
     290           0 :     return 0;
     291             : }
     292             : 
     293             : 
     294           0 : void CalendarWrapper::addValue( sal_Int16 nFieldIndex, sal_Int32 nAmount )
     295             : {
     296             :     try
     297             :     {
     298           0 :         if ( xC.is() )
     299           0 :             xC->addValue( nFieldIndex, nAmount );
     300             :     }
     301           0 :     catch (const Exception& e)
     302             :     {
     303             :         SAL_WARN( "unotools.i18n", "addValue: Exception caught " << e.Message );
     304             :     }
     305           0 : }
     306             : 
     307             : 
     308           0 : sal_Int16 CalendarWrapper::getFirstDayOfWeek() const
     309             : {
     310             :     try
     311             :     {
     312           0 :         if ( xC.is() )
     313           0 :             return xC->getFirstDayOfWeek();
     314             :     }
     315           0 :     catch (const Exception& e)
     316             :     {
     317             :         SAL_WARN( "unotools.i18n", "getFirstDayOfWeek: Exception caught " << e.Message );
     318             :     }
     319           0 :     return 0;
     320             : }
     321             : 
     322             : 
     323         291 : sal_Int16 CalendarWrapper::getNumberOfMonthsInYear() const
     324             : {
     325             :     try
     326             :     {
     327         291 :         if ( xC.is() )
     328         291 :             return xC->getNumberOfMonthsInYear();
     329             :     }
     330           0 :     catch (const Exception& e)
     331             :     {
     332             :         SAL_WARN( "unotools.i18n", "getNumberOfMonthsInYear: Exception caught " << e.Message );
     333             :     }
     334           0 :     return 0;
     335             : }
     336             : 
     337             : 
     338         111 : sal_Int16 CalendarWrapper::getNumberOfDaysInWeek() const
     339             : {
     340             :     try
     341             :     {
     342         111 :         if ( xC.is() )
     343         111 :             return xC->getNumberOfDaysInWeek();
     344             :     }
     345           0 :     catch (const Exception& e)
     346             :     {
     347             :         SAL_WARN( "unotools.i18n", "getNumberOfDaysInWeek: Exception caught " << e.Message );
     348             :     }
     349           0 :     return 0;
     350             : }
     351             : 
     352             : 
     353          20 : ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::CalendarItem2 > CalendarWrapper::getMonths() const
     354             : {
     355             :     try
     356             :     {
     357          20 :         if ( xC.is() )
     358          20 :             return xC->getMonths2();
     359             :     }
     360           0 :     catch (const Exception& e)
     361             :     {
     362             :         SAL_WARN( "unotools.i18n", "getMonths: Exception caught " << e.Message );
     363             :     }
     364           0 :     return ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::CalendarItem2 > (0);
     365             : }
     366             : 
     367             : 
     368          20 : ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::CalendarItem2 > CalendarWrapper::getDays() const
     369             : {
     370             :     try
     371             :     {
     372          20 :         if ( xC.is() )
     373          20 :             return xC->getDays2();
     374             :     }
     375           0 :     catch (const Exception& e)
     376             :     {
     377             :         SAL_WARN( "unotools.i18n", "getDays: Exception caught " << e.Message );
     378             :     }
     379           0 :     return ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::CalendarItem2 > (0);
     380             : }
     381             : 
     382             : 
     383         165 : String CalendarWrapper::getDisplayName( sal_Int16 nCalendarDisplayIndex, sal_Int16 nIdx, sal_Int16 nNameType ) const
     384             : {
     385             :     try
     386             :     {
     387         165 :         if ( xC.is() )
     388         165 :             return xC->getDisplayName( nCalendarDisplayIndex, nIdx, nNameType );
     389             :     }
     390           0 :     catch (const Exception& e)
     391             :     {
     392             :         SAL_WARN( "unotools.i18n", "getDisplayName: Exception caught " << e.Message );
     393             :     }
     394           0 :     return String();
     395             : }
     396             : 
     397             : 
     398             : // --- XExtendedCalendar -----------------------------------------------------
     399             : 
     400         280 : String CalendarWrapper::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode ) const
     401             : {
     402             :     try
     403             :     {
     404         280 :         if ( xC.is() )
     405         280 :             return xC->getDisplayString( nCalendarDisplayCode, nNativeNumberMode );
     406             :     }
     407           0 :     catch (const Exception& e)
     408             :     {
     409             :         SAL_WARN( "unotools.i18n", "getDisplayString: Exception caught " << e.Message );
     410             :     }
     411           0 :     return String();
     412             : }
     413             : 
     414             : 
     415             : // --- XCalendar3 ------------------------------------------------------------
     416             : 
     417           0 : ::com::sun::star::i18n::Calendar2 CalendarWrapper::getLoadedCalendar() const
     418             : {
     419             :     try
     420             :     {
     421           0 :         if ( xC.is() )
     422           0 :             return xC->getLoadedCalendar2();
     423             :     }
     424           0 :     catch (const Exception& e)
     425             :     {
     426             :         SAL_WARN( "unotools.i18n", "getLoadedCalendar2: Exception caught " << e.Message );
     427             :     }
     428           0 :     return ::com::sun::star::i18n::Calendar2();
     429             : }
     430             : 
     431             : 
     432          20 : ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::CalendarItem2 > CalendarWrapper::getGenitiveMonths() const
     433             : {
     434             :     try
     435             :     {
     436          20 :         if ( xC.is() )
     437          20 :             return xC->getGenitiveMonths2();
     438             :     }
     439           0 :     catch (const Exception& e)
     440             :     {
     441             :         SAL_WARN( "unotools.i18n", "getGenitiveMonths: Exception caught " << e.Message );
     442             :     }
     443           0 :     return ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::CalendarItem2 > (0);
     444             : }
     445             : 
     446             : 
     447          20 : ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::CalendarItem2 > CalendarWrapper::getPartitiveMonths() const
     448             : {
     449             :     try
     450             :     {
     451          20 :         if ( xC.is() )
     452          20 :             return xC->getPartitiveMonths2();
     453             :     }
     454           0 :     catch (const Exception& e)
     455             :     {
     456             :         SAL_WARN( "unotools.i18n", "getPartitiveMonths: Exception caught " << e.Message );
     457             :     }
     458           0 :     return ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::CalendarItem2 > (0);
     459             : }
     460             : 
     461             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10