LCOV - code coverage report
Current view: top level - sw/source/core/fields - flddat.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 40 127 31.5 %
Date: 2014-04-11 Functions: 8 16 50.0 %
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        1852 : SwDateTimeFieldType::SwDateTimeFieldType(SwDoc* pInitDoc)
      31        1852 :     : SwValueFieldType( pInitDoc, RES_DATETIMEFLD )
      32        1852 : {}
      33             : 
      34           0 : SwFieldType* SwDateTimeFieldType::Copy() const
      35             : {
      36           0 :     SwDateTimeFieldType *pTmp = new SwDateTimeFieldType(GetDoc());
      37           0 :     return pTmp;
      38             : }
      39             : 
      40         111 : SwDateTimeField::SwDateTimeField(SwDateTimeFieldType* pInitType, sal_uInt16 nSub, sal_uLong nFmt, sal_uInt16 nLng)
      41             :     : SwValueField(pInitType, nFmt, nLng, 0.0),
      42             :     nSubType(nSub),
      43         111 :     nOffset(0)
      44             : {
      45         111 :     if (!nFmt)
      46             :     {
      47           8 :         SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
      48           8 :         if (nSubType & DATEFLD)
      49           6 :             ChangeFormat(pFormatter->GetFormatIndex(NF_DATE_SYSTEM_SHORT, GetLanguage()));
      50             :         else
      51           2 :             ChangeFormat(pFormatter->GetFormatIndex(NF_TIME_HHMMSS, GetLanguage()));
      52             :     }
      53         111 :     if (IsFixed())
      54             :     {
      55           0 :         DateTime aDateTime( DateTime::SYSTEM );
      56           0 :         SetDateTime(aDateTime);
      57             :     }
      58         111 : }
      59             : 
      60          55 : OUString SwDateTimeField::Expand() const
      61             : {
      62             :     double fVal;
      63             : 
      64          55 :     if (!(IsFixed()))
      65             :     {
      66          55 :         DateTime aDateTime( DateTime::SYSTEM );
      67          55 :         fVal = GetDateTime(GetDoc(), aDateTime);
      68             :     }
      69             :     else
      70           0 :         fVal = GetValue();
      71             : 
      72          55 :     if (nOffset)
      73           0 :         fVal += (double)(nOffset * 60L) / 86400.0;
      74             : 
      75          55 :     return ExpandValue(fVal, GetFormat(), GetLanguage());
      76             : }
      77             : 
      78          93 : SwField* SwDateTimeField::Copy() const
      79             : {
      80             :     SwDateTimeField *pTmp =
      81          93 :         new SwDateTimeField((SwDateTimeFieldType*)GetTyp(), nSubType,
      82          93 :                                             GetFormat(), GetLanguage());
      83             : 
      84          93 :     pTmp->SetValue(GetValue());
      85          93 :     pTmp->SetOffset(nOffset);
      86          93 :     pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
      87             : 
      88          93 :     return pTmp;
      89             : }
      90             : 
      91         420 : sal_uInt16 SwDateTimeField::GetSubType() const
      92             : {
      93         420 :     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           0 : void SwDateTimeField::SetDateTime(const DateTime& rDT)
     114             : {
     115           0 :     SetValue(GetDateTime(GetDoc(), rDT));
     116           0 : }
     117             : 
     118         158 : double SwDateTimeField::GetDateTime(SwDoc* pDoc, const DateTime& rDT)
     119             : {
     120         158 :     SvNumberFormatter* pFormatter = pDoc->GetNumberFormatter();
     121         158 :     Date* pNullDate = pFormatter->GetNullDate();
     122             : 
     123         158 :     double fResult = rDT - DateTime(*pNullDate);
     124             : 
     125         158 :     return fResult;
     126             : }
     127             : 
     128          93 : double SwDateTimeField::GetValue() const
     129             : {
     130          93 :     if (IsFixed())
     131           0 :         return SwValueField::GetValue();
     132             :     else
     133          93 :         return GetDateTime(GetDoc(), DateTime( DateTime::SYSTEM ));
     134             : }
     135             : 
     136           0 : Date SwDateTimeField::GetDate(sal_Bool bUseOffset) const
     137             : {
     138           0 :     SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
     139           0 :     Date* pNullDate = pFormatter->GetNullDate();
     140             : 
     141           0 :     long nVal = static_cast<long>( GetValue() );
     142             : 
     143           0 :     if (bUseOffset && nOffset)
     144           0 :         nVal += nOffset / 60 / 24;
     145             : 
     146           0 :     Date aDate = *pNullDate + nVal;
     147             : 
     148           0 :     return aDate;
     149             : }
     150             : 
     151           0 : Time SwDateTimeField::GetTime(sal_Bool bUseOffset) const
     152             : {
     153             :     double fDummy;
     154           0 :     double fFract = modf(GetValue(), &fDummy);
     155           0 :     DateTime aDT((long)fDummy, 0);
     156           0 :     aDT += fFract;
     157           0 :     if (bUseOffset)
     158           0 :          aDT += Time(0, nOffset);
     159           0 :     return (Time)aDT;
     160             : }
     161             : 
     162           1 : bool SwDateTimeField::QueryValue( uno::Any& rVal, sal_uInt16 nWhichId ) const
     163             : {
     164           1 :     switch( nWhichId )
     165             :     {
     166             :     case FIELD_PROP_BOOL1:
     167             :         {
     168           0 :             sal_Bool bTmp = IsFixed();
     169           0 :             rVal.setValue(&bTmp, ::getCppuBooleanType());
     170             :         }
     171           0 :         break;
     172             :     case FIELD_PROP_BOOL2:
     173             :         {
     174           0 :             sal_Bool bTmp = IsDate();
     175           0 :             rVal.setValue(&bTmp, ::getCppuBooleanType());
     176             :         }
     177           0 :         break;
     178             :     case FIELD_PROP_FORMAT:
     179           1 :         rVal <<= (sal_Int32)GetFormat();
     180           1 :         break;
     181             :     case FIELD_PROP_SUBTYPE:
     182           0 :         rVal <<= (sal_Int32)nOffset;
     183           0 :         break;
     184             :     case FIELD_PROP_DATE_TIME:
     185             :         {
     186           0 :             DateTime aDateTime(GetDate(), GetTime());
     187             : 
     188           0 :             util::DateTime DateTimeValue;
     189           0 :             DateTimeValue.NanoSeconds = aDateTime.GetNanoSec();
     190           0 :             DateTimeValue.Seconds = aDateTime.GetSec();
     191           0 :             DateTimeValue.Minutes = aDateTime.GetMin();
     192           0 :             DateTimeValue.Hours = aDateTime.GetHour();
     193           0 :             DateTimeValue.Day = aDateTime.GetDay();
     194           0 :             DateTimeValue.Month = aDateTime.GetMonth();
     195           0 :             DateTimeValue.Year = aDateTime.GetYear();
     196           0 :             rVal <<= DateTimeValue;
     197             :         }
     198           0 :         break;
     199             :     default:
     200           0 :         return SwField::QueryValue(rVal, nWhichId);
     201             :     }
     202           1 :     return true;
     203             : }
     204             : 
     205           0 : bool SwDateTimeField::PutValue( const uno::Any& rVal, sal_uInt16 nWhichId )
     206             : {
     207           0 :     sal_Int32 nTmp = 0;
     208           0 :     switch( nWhichId )
     209             :     {
     210             :     case FIELD_PROP_BOOL1:
     211           0 :         if(*(sal_Bool*)rVal.getValue())
     212           0 :             nSubType |= FIXEDFLD;
     213             :         else
     214           0 :             nSubType &= ~FIXEDFLD;
     215           0 :         break;
     216             :     case FIELD_PROP_BOOL2:
     217           0 :         nSubType &=  ~(DATEFLD|TIMEFLD);
     218           0 :         nSubType |= *(sal_Bool*)rVal.getValue() ? DATEFLD : TIMEFLD;
     219           0 :         break;
     220             :     case FIELD_PROP_FORMAT:
     221           0 :         rVal >>= nTmp;
     222           0 :         ChangeFormat(nTmp);
     223           0 :         break;
     224             :     case FIELD_PROP_SUBTYPE:
     225           0 :         rVal >>= nTmp;
     226           0 :         nOffset = nTmp;
     227           0 :         break;
     228             :     case FIELD_PROP_DATE_TIME:
     229             :         {
     230           0 :             util::DateTime aDateTimeValue;
     231           0 :             if(!(rVal >>= aDateTimeValue))
     232           0 :                 return false;
     233           0 :             DateTime aDateTime( DateTime::EMPTY );
     234           0 :             aDateTime.SetNanoSec(aDateTimeValue.NanoSeconds);
     235           0 :             aDateTime.SetSec(aDateTimeValue.Seconds);
     236           0 :             aDateTime.SetMin(aDateTimeValue.Minutes);
     237           0 :             aDateTime.SetHour(aDateTimeValue.Hours);
     238           0 :             aDateTime.SetDay(aDateTimeValue.Day);
     239           0 :             aDateTime.SetMonth(aDateTimeValue.Month);
     240           0 :             aDateTime.SetYear(aDateTimeValue.Year);
     241           0 :             SetDateTime(aDateTime);
     242             :         }
     243           0 :         break;
     244             :         default:
     245           0 :             return SwField::PutValue(rVal, nWhichId);
     246             :     }
     247           0 :     return true;
     248             : }
     249             : 
     250             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10