LCOV - code coverage report
Current view: top level - sw/source/core/fields - flddat.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 86 118 72.9 %
Date: 2015-06-13 12:38:46 Functions: 14 18 77.8 %
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 <tools/datetime.hxx>
      21             : #include <svl/zforlist.hxx>
      22             : #include <com/sun/star/util/DateTime.hpp>
      23             : #include <doc.hxx>
      24             : #include <fldbas.hxx>
      25             : #include <flddat.hxx>
      26             : #include <unofldmid.h>
      27             : 
      28             : using namespace ::com::sun::star;
      29             : 
      30        2958 : SwDateTimeFieldType::SwDateTimeFieldType(SwDoc* pInitDoc)
      31        2958 :     : SwValueFieldType( pInitDoc, RES_DATETIMEFLD )
      32        2958 : {}
      33             : 
      34           0 : SwFieldType* SwDateTimeFieldType::Copy() const
      35             : {
      36           0 :     SwDateTimeFieldType *pTmp = new SwDateTimeFieldType(GetDoc());
      37           0 :     return pTmp;
      38             : }
      39             : 
      40         124 : SwDateTimeField::SwDateTimeField(SwDateTimeFieldType* pInitType, sal_uInt16 nSub, sal_uLong nFormat, sal_uInt16 nLng)
      41             :     : SwValueField(pInitType, nFormat, nLng, 0.0),
      42             :     nSubType(nSub),
      43         124 :     nOffset(0)
      44             : {
      45         124 :     if (!nFormat)
      46             :     {
      47           1 :         SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
      48           1 :         if (nSubType & DATEFLD)
      49           0 :             ChangeFormat(pFormatter->GetFormatIndex(NF_DATE_SYSTEM_SHORT, GetLanguage()));
      50             :         else
      51           1 :             ChangeFormat(pFormatter->GetFormatIndex(NF_TIME_HHMMSS, GetLanguage()));
      52             :     }
      53         124 :     if (IsFixed())
      54             :     {
      55          57 :         DateTime aDateTime( DateTime::SYSTEM );
      56          57 :         SetDateTime(aDateTime);
      57             :     }
      58         124 : }
      59             : 
      60          47 : OUString SwDateTimeField::Expand() const
      61             : {
      62             :     double fVal;
      63             : 
      64          47 :     if (!(IsFixed()))
      65             :     {
      66          28 :         DateTime aDateTime( DateTime::SYSTEM );
      67          28 :         fVal = GetDateTime(GetDoc(), aDateTime);
      68             :     }
      69             :     else
      70          19 :         fVal = GetValue();
      71             : 
      72          47 :     if (nOffset)
      73           0 :         fVal += (double)(nOffset * 60L) / 86400.0;
      74             : 
      75          47 :     return ExpandValue(fVal, GetFormat(), GetLanguage());
      76             : }
      77             : 
      78         104 : SwField* SwDateTimeField::Copy() const
      79             : {
      80             :     SwDateTimeField *pTmp =
      81         104 :         new SwDateTimeField(static_cast<SwDateTimeFieldType*>(GetTyp()), nSubType,
      82         104 :                                             GetFormat(), GetLanguage());
      83             : 
      84         104 :     pTmp->SetValue(GetValue());
      85         104 :     pTmp->SetOffset(nOffset);
      86         104 :     pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
      87             : 
      88         104 :     return pTmp;
      89             : }
      90             : 
      91         450 : sal_uInt16 SwDateTimeField::GetSubType() const
      92             : {
      93         450 :     return nSubType;
      94             : }
      95             : 
      96           0 : void SwDateTimeField::SetSubType(sal_uInt16 nType)
      97             : {
      98           0 :     nSubType = nType;
      99           0 : }
     100             : 
     101           0 : void SwDateTimeField::SetPar2(const OUString& rStr)
     102             : {
     103           0 :     nOffset = rStr.toInt32();
     104           0 : }
     105             : 
     106           0 : OUString SwDateTimeField::GetPar2() const
     107             : {
     108           0 :     if (nOffset)
     109           0 :         return OUString::number(nOffset);
     110           0 :     return OUString();
     111             : }
     112             : 
     113          66 : void SwDateTimeField::SetDateTime(const DateTime& rDT)
     114             : {
     115          66 :     SetValue(GetDateTime(GetDoc(), rDT));
     116          66 : }
     117             : 
     118         218 : double SwDateTimeField::GetDateTime(SwDoc* pDoc, const DateTime& rDT)
     119             : {
     120         218 :     SvNumberFormatter* pFormatter = pDoc->GetNumberFormatter();
     121         218 :     Date* pNullDate = pFormatter->GetNullDate();
     122             : 
     123         218 :     double fResult = rDT - DateTime(*pNullDate);
     124             : 
     125         218 :     return fResult;
     126             : }
     127             : 
     128         133 : double SwDateTimeField::GetValue() const
     129             : {
     130         133 :     if (IsFixed())
     131          71 :         return SwValueField::GetValue();
     132             :     else
     133          62 :         return GetDateTime(GetDoc(), DateTime( DateTime::SYSTEM ));
     134             : }
     135             : 
     136           5 : Date SwDateTimeField::GetDate(bool bUseOffset) const
     137             : {
     138           5 :     SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
     139           5 :     Date* pNullDate = pFormatter->GetNullDate();
     140             : 
     141           5 :     long nVal = static_cast<long>( GetValue() );
     142             : 
     143           5 :     if (bUseOffset && nOffset)
     144           0 :         nVal += nOffset / 60 / 24;
     145             : 
     146           5 :     Date aDate = *pNullDate + nVal;
     147             : 
     148           5 :     return aDate;
     149             : }
     150             : 
     151           5 : tools::Time SwDateTimeField::GetTime(bool bUseOffset) const
     152             : {
     153             :     double fDummy;
     154           5 :     double fFract = modf(GetValue(), &fDummy);
     155           5 :     DateTime aDT((long)fDummy, 0);
     156           5 :     aDT += fFract;
     157           5 :     if (bUseOffset)
     158           0 :          aDT += tools::Time(0, nOffset);
     159           5 :     return static_cast<tools::Time>(aDT);
     160             : }
     161             : 
     162          32 : bool SwDateTimeField::QueryValue( uno::Any& rVal, sal_uInt16 nWhichId ) const
     163             : {
     164          32 :     switch( nWhichId )
     165             :     {
     166             :     case FIELD_PROP_BOOL1:
     167           5 :         rVal <<= IsFixed();
     168           5 :         break;
     169             :     case FIELD_PROP_BOOL2:
     170           6 :         rVal <<= IsDate();
     171           6 :         break;
     172             :     case FIELD_PROP_FORMAT:
     173           7 :         rVal <<= (sal_Int32)GetFormat();
     174           7 :         break;
     175             :     case FIELD_PROP_SUBTYPE:
     176           3 :         rVal <<= (sal_Int32)nOffset;
     177           3 :         break;
     178             :     case FIELD_PROP_DATE_TIME:
     179             :         {
     180           5 :             DateTime aDateTime(GetDate(), GetTime());
     181           5 :             rVal <<= aDateTime.GetUNODateTime();
     182             :         }
     183           5 :         break;
     184             :     default:
     185           6 :         return SwField::QueryValue(rVal, nWhichId);
     186             :     }
     187          26 :     return true;
     188             : }
     189             : 
     190           9 : bool SwDateTimeField::PutValue( const uno::Any& rVal, sal_uInt16 nWhichId )
     191             : {
     192           9 :     sal_Int32 nTmp = 0;
     193           9 :     switch( nWhichId )
     194             :     {
     195             :     case FIELD_PROP_BOOL1:
     196           0 :         if(*static_cast<sal_Bool const *>(rVal.getValue()))
     197           0 :             nSubType |= FIXEDFLD;
     198             :         else
     199           0 :             nSubType &= ~FIXEDFLD;
     200           0 :         break;
     201             :     case FIELD_PROP_BOOL2:
     202           0 :         nSubType &=  ~(DATEFLD|TIMEFLD);
     203           0 :         nSubType |= *static_cast<sal_Bool const *>(rVal.getValue()) ? DATEFLD : TIMEFLD;
     204           0 :         break;
     205             :     case FIELD_PROP_FORMAT:
     206           0 :         rVal >>= nTmp;
     207           0 :         ChangeFormat(nTmp);
     208           0 :         break;
     209             :     case FIELD_PROP_SUBTYPE:
     210           0 :         rVal >>= nTmp;
     211           0 :         nOffset = nTmp;
     212           0 :         break;
     213             :     case FIELD_PROP_DATE_TIME:
     214             :         {
     215           9 :             util::DateTime aDateTimeValue;
     216           9 :             if(!(rVal >>= aDateTimeValue))
     217           0 :                 return false;
     218           9 :             DateTime aDateTime( DateTime::EMPTY );
     219           9 :             aDateTime.SetNanoSec(aDateTimeValue.NanoSeconds);
     220           9 :             aDateTime.SetSec(aDateTimeValue.Seconds);
     221           9 :             aDateTime.SetMin(aDateTimeValue.Minutes);
     222           9 :             aDateTime.SetHour(aDateTimeValue.Hours);
     223           9 :             aDateTime.SetDay(aDateTimeValue.Day);
     224           9 :             aDateTime.SetMonth(aDateTimeValue.Month);
     225           9 :             aDateTime.SetYear(aDateTimeValue.Year);
     226           9 :             SetDateTime(aDateTime);
     227             :         }
     228           9 :         break;
     229             :         default:
     230           0 :             return SwField::PutValue(rVal, nWhichId);
     231             :     }
     232           9 :     return true;
     233         177 : }
     234             : 
     235             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11