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

Generated by: LCOV version 1.10