LibreOffice Module sw (master)  1
flddat.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 <sal/config.h>
21 
22 #include <o3tl/any.hxx>
23 #include <o3tl/temporary.hxx>
24 #include <tools/datetime.hxx>
25 #include <svl/numformat.hxx>
26 #include <svl/zforlist.hxx>
27 #include <com/sun/star/util/DateTime.hpp>
28 #include <doc.hxx>
29 #include <fldbas.hxx>
30 #include <flddat.hxx>
31 #include <unofldmid.h>
32 
33 using namespace ::com::sun::star;
34 
36  : SwValueFieldType( pInitDoc, SwFieldIds::DateTime )
37 {}
38 
39 std::unique_ptr<SwFieldType> SwDateTimeFieldType::Copy() const
40 {
41  return std::make_unique<SwDateTimeFieldType>(GetDoc());
42 }
43 
45  : SwValueField(pInitType, nFormat, nLng, 0.0),
46  m_nSubType(nSub),
47  m_nOffset(0)
48 {
49  if (!nFormat)
50  {
51  SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
52  if (m_nSubType & DATEFLD)
54  else
56  }
57  if (IsFixed())
58  {
59  DateTime aDateTime( DateTime::SYSTEM );
60  SetDateTime(aDateTime);
61  }
62 }
63 
64 OUString SwDateTimeField::ExpandImpl(SwRootFrame const*const) const
65 {
66  double fVal;
67 
68  if (!(IsFixed()))
69  {
70  DateTime aDateTime( DateTime::SYSTEM );
71  fVal = GetDateTime(*GetDoc(), aDateTime);
72  }
73  else
74  fVal = GetValue();
75 
76  if (m_nOffset)
77  fVal += m_nOffset * ( 60 / 86400.0 );
78 
79  return ExpandValue(fVal, GetFormat(), GetLanguage());
80 }
81 
82 std::unique_ptr<SwField> SwDateTimeField::Copy() const
83 {
84  std::unique_ptr<SwDateTimeField> pTmp(
85  new SwDateTimeField(static_cast<SwDateTimeFieldType*>(GetTyp()), m_nSubType,
86  GetFormat(), GetLanguage()) );
87 
88  pTmp->SetValue(GetValue());
89  pTmp->SetOffset(m_nOffset);
90  pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
91 
92  return std::unique_ptr<SwField>(pTmp.release());
93 }
94 
95 sal_uInt16 SwDateTimeField::GetSubType() const
96 {
97  return m_nSubType;
98 }
99 
100 void SwDateTimeField::SetSubType(sal_uInt16 nType)
101 {
102  m_nSubType = nType;
103 }
104 
105 void SwDateTimeField::SetPar2(const OUString& rStr)
106 {
107  m_nOffset = rStr.toInt32();
108 }
109 
110 OUString SwDateTimeField::GetPar2() const
111 {
112  if (m_nOffset)
113  return OUString::number(m_nOffset);
114  return OUString();
115 }
116 
118 {
119  SetValue(GetDateTime(*GetDoc(), rDT));
120 }
121 
122 double SwDateTimeField::GetDateTime(SwDoc& rDoc, const DateTime& rDT)
123 {
124  SvNumberFormatter* pFormatter = rDoc.GetNumberFormatter();
125  const Date& rNullDate = pFormatter->GetNullDate();
126 
127  double fResult = rDT - DateTime(rNullDate);
128 
129  return fResult;
130 }
131 
133 {
134  if (IsFixed())
135  return SwValueField::GetValue();
136  else
138 }
139 
141 {
142  SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
143  const Date& rNullDate = pFormatter->GetNullDate();
144 
145  tools::Long nVal = static_cast<tools::Long>( GetValue() );
146 
147  Date aDate = rNullDate + nVal;
148 
149  return aDate;
150 }
151 
153 {
154  double fFract = modf(GetValue(), &o3tl::temporary(double()));
155  DateTime aDT( DateTime::EMPTY );
156  aDT.AddTime(fFract);
157  return static_cast<tools::Time>(aDT);
158 }
159 
160 bool SwDateTimeField::QueryValue( uno::Any& rVal, sal_uInt16 nWhichId ) const
161 {
162  switch( nWhichId )
163  {
164  case FIELD_PROP_BOOL1:
165  rVal <<= IsFixed();
166  break;
167  case FIELD_PROP_BOOL2:
168  rVal <<= (m_nSubType & DATEFLD) != 0;
169  break;
170  case FIELD_PROP_FORMAT:
171  rVal <<= static_cast<sal_Int32>(GetFormat());
172  break;
173  case FIELD_PROP_SUBTYPE:
174  rVal <<= static_cast<sal_Int32>(m_nOffset);
175  break;
177  {
178  DateTime aDateTime(GetDate(), GetTime());
179  rVal <<= aDateTime.GetUNODateTime();
180  }
181  break;
182  default:
183  return SwField::QueryValue(rVal, nWhichId);
184  }
185  return true;
186 }
187 
188 bool SwDateTimeField::PutValue( const uno::Any& rVal, sal_uInt16 nWhichId )
189 {
190  sal_Int32 nTmp = 0;
191  switch( nWhichId )
192  {
193  case FIELD_PROP_BOOL1:
194  if(*o3tl::doAccess<bool>(rVal))
195  m_nSubType |= FIXEDFLD;
196  else
197  m_nSubType &= ~FIXEDFLD;
198  break;
199  case FIELD_PROP_BOOL2:
200  m_nSubType &= ~(DATEFLD|TIMEFLD);
201  m_nSubType |= *o3tl::doAccess<bool>(rVal) ? DATEFLD : TIMEFLD;
202  break;
203  case FIELD_PROP_FORMAT:
204  rVal >>= nTmp;
205  ChangeFormat(nTmp);
206  break;
207  case FIELD_PROP_SUBTYPE:
208  rVal >>= nTmp;
209  m_nOffset = nTmp;
210  break;
212  {
213  util::DateTime aDateTimeValue;
214  if(!(rVal >>= aDateTimeValue))
215  return false;
216  DateTime aDateTime( DateTime::EMPTY );
217  aDateTime.SetNanoSec(aDateTimeValue.NanoSeconds);
218  aDateTime.SetSec(aDateTimeValue.Seconds);
219  aDateTime.SetMin(aDateTimeValue.Minutes);
220  aDateTime.SetHour(aDateTimeValue.Hours);
221  aDateTime.SetDay(aDateTimeValue.Day);
222  aDateTime.SetMonth(aDateTimeValue.Month);
223  aDateTime.SetYear(aDateTimeValue.Year);
224  SetDateTime(aDateTime);
225  }
226  break;
227  default:
228  return SwField::PutValue(rVal, nWhichId);
229  }
230  return true;
231 }
232 
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual double GetValue() const override
Definition: flddat.cxx:132
virtual void SetSubType(sal_uInt16 nSub) override
Definition: flddat.cxx:100
#define FIELD_PROP_DATE_TIME
Definition: unofldmid.h:38
LanguageType GetLanguage() const
Language at field position.
Definition: fldbas.hxx:402
void SetSec(sal_uInt16 nNewSec)
sal_uInt32 GetFormat() const
Query parameters for dialog and for BASIC.
Definition: fldbas.hxx:397
SvNumberFormatter * GetNumberFormatter(bool bCreate=true)
Definition: doc.hxx:1411
sal_uInt32 GetFormatIndex(NfIndexTableOffset, LanguageType eLnge=LANGUAGE_DONTKNOW)
virtual OUString GetPar2() const override
Definition: flddat.cxx:110
sal_uIntPtr sal_uLong
long Long
virtual sal_uInt16 GetSubType() const override
Definition: flddat.cxx:95
bool IsFixed() const
Definition: fldbas.cxx:433
Definition: doc.hxx:188
static double GetDateTime(SwDoc &rDoc, const DateTime &rDT)
Definition: flddat.cxx:122
SwDoc * GetDoc() const
Definition: fldbas.hxx:453
The root element of a Writer document layout.
Definition: rootfrm.hxx:82
#define FIELD_PROP_SUBTYPE
Definition: unofldmid.h:27
void SetDateTime(const DateTime &rDT)
Definition: flddat.cxx:117
virtual std::unique_ptr< SwField > Copy() const override
Definition: flddat.cxx:82
sal_uInt16 m_nSubType
Definition: flddat.hxx:47
#define FIELD_PROP_FORMAT
Definition: unofldmid.h:26
void SetHour(sal_uInt16 nNewHour)
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhichId)
Definition: fldbas.cxx:359
virtual std::unique_ptr< SwFieldType > Copy() const override
Definition: flddat.cxx:39
virtual void SetPar2(const OUString &rStr) override
Definition: flddat.cxx:105
void SetYear(sal_Int16 nNewYear)
NF_TIME_HHMMSS
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nWhichId) const
Definition: fldbas.cxx:346
css::util::DateTime GetUNODateTime() const
void SetDay(sal_uInt16 nNewDay)
#define FIELD_PROP_BOOL2
Definition: unofldmid.h:29
SwFieldIds
Definition: fldbas.hxx:44
Date GetDate() const
Definition: flddat.cxx:140
SwDoc * GetDoc() const
Definition: fldbas.hxx:419
bool IsAutomaticLanguage() const
Definition: fldbas.hxx:379
tools::Time GetTime() const
Definition: flddat.cxx:152
SwFieldType * GetTyp() const
Definition: fldbas.hxx:392
constexpr T & temporary(T &&x)
SwDateTimeField(SwDateTimeFieldType *pType, sal_uInt16 nSubType=DATEFLD, sal_uLong nFormat=0, LanguageType nLng=LANGUAGE_SYSTEM)
Definition: flddat.cxx:44
#define FIELD_PROP_BOOL1
Definition: unofldmid.h:28
Fields containing values that have to be formatted via number formatter.
Definition: fldbas.hxx:408
NF_DATE_SYSTEM_SHORT
void AddTime(double fTimeInDays)
virtual OUString ExpandImpl(SwRootFrame const *pLayout) const override
Definition: flddat.cxx:64
SwDateTimeFieldType(SwDoc *pDoc)
Definition: flddat.cxx:35
void SetNanoSec(sal_uInt32 nNewNanoSec)
void SetMin(sal_uInt16 nNewMin)
void ChangeFormat(sal_uInt32 n)
Definition: fldbas.cxx:428
tools::Long m_nOffset
Definition: flddat.hxx:48
virtual void SetValue(const double &rVal)
Definition: fldbas.cxx:731
const Date & GetNullDate() const
OUString ExpandValue(const double &rVal, sal_uInt32 nFormat, LanguageType nLng) const
Definition: fldbas.hxx:460
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nMId) override
Definition: flddat.cxx:188
virtual double GetValue() const
Definition: fldbas.cxx:726
void SetMonth(sal_uInt16 nNewMonth)
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nMId) const override
Definition: flddat.cxx:160