nss: upgrade to release 3.73
[LibreOffice.git] / sw / source / core / fields / flddat.cxx
blobdf3d15aafc49fef2488617abfcc03433910fcd61
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <o3tl/any.hxx>
23 #include <o3tl/temporary.hxx>
24 #include <tools/datetime.hxx>
25 #include <svl/zforlist.hxx>
26 #include <com/sun/star/util/DateTime.hpp>
27 #include <doc.hxx>
28 #include <fldbas.hxx>
29 #include <flddat.hxx>
30 #include <unofldmid.h>
32 using namespace ::com::sun::star;
34 SwDateTimeFieldType::SwDateTimeFieldType(SwDoc* pInitDoc)
35 : SwValueFieldType( pInitDoc, SwFieldIds::DateTime )
38 std::unique_ptr<SwFieldType> SwDateTimeFieldType::Copy() const
40 return std::make_unique<SwDateTimeFieldType>(GetDoc());
43 SwDateTimeField::SwDateTimeField(SwDateTimeFieldType* pInitType, sal_uInt16 nSub, sal_uLong nFormat, LanguageType nLng)
44 : SwValueField(pInitType, nFormat, nLng, 0.0),
45 m_nSubType(nSub),
46 m_nOffset(0)
48 if (!nFormat)
50 SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
51 if (m_nSubType & DATEFLD)
52 ChangeFormat(pFormatter->GetFormatIndex(NF_DATE_SYSTEM_SHORT, GetLanguage()));
53 else
54 ChangeFormat(pFormatter->GetFormatIndex(NF_TIME_HHMMSS, GetLanguage()));
56 if (IsFixed())
58 DateTime aDateTime( DateTime::SYSTEM );
59 SetDateTime(aDateTime);
63 OUString SwDateTimeField::ExpandImpl(SwRootFrame const*const) const
65 double fVal;
67 if (!(IsFixed()))
69 DateTime aDateTime( DateTime::SYSTEM );
70 fVal = GetDateTime(*GetDoc(), aDateTime);
72 else
73 fVal = GetValue();
75 if (m_nOffset)
76 fVal += m_nOffset * ( 60 / 86400.0 );
78 return ExpandValue(fVal, GetFormat(), GetLanguage());
81 std::unique_ptr<SwField> SwDateTimeField::Copy() const
83 std::unique_ptr<SwDateTimeField> pTmp(
84 new SwDateTimeField(static_cast<SwDateTimeFieldType*>(GetTyp()), m_nSubType,
85 GetFormat(), GetLanguage()) );
87 pTmp->SetValue(GetValue());
88 pTmp->SetOffset(m_nOffset);
89 pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
91 return std::unique_ptr<SwField>(pTmp.release());
94 sal_uInt16 SwDateTimeField::GetSubType() const
96 return m_nSubType;
99 void SwDateTimeField::SetSubType(sal_uInt16 nType)
101 m_nSubType = nType;
104 void SwDateTimeField::SetPar2(const OUString& rStr)
106 m_nOffset = rStr.toInt32();
109 OUString SwDateTimeField::GetPar2() const
111 if (m_nOffset)
112 return OUString::number(m_nOffset);
113 return OUString();
116 void SwDateTimeField::SetDateTime(const DateTime& rDT)
118 SetValue(GetDateTime(*GetDoc(), rDT));
121 double SwDateTimeField::GetDateTime(SwDoc& rDoc, const DateTime& rDT)
123 SvNumberFormatter* pFormatter = rDoc.GetNumberFormatter();
124 const Date& rNullDate = pFormatter->GetNullDate();
126 double fResult = rDT - DateTime(rNullDate);
128 return fResult;
131 double SwDateTimeField::GetValue() const
133 if (IsFixed())
134 return SwValueField::GetValue();
135 else
136 return GetDateTime(*GetDoc(), DateTime( DateTime::SYSTEM ));
139 Date SwDateTimeField::GetDate() const
141 SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
142 const Date& rNullDate = pFormatter->GetNullDate();
144 tools::Long nVal = static_cast<tools::Long>( GetValue() );
146 Date aDate = rNullDate + nVal;
148 return aDate;
151 tools::Time SwDateTimeField::GetTime() const
153 double fFract = modf(GetValue(), &o3tl::temporary(double()));
154 DateTime aDT( DateTime::EMPTY );
155 aDT.AddTime(fFract);
156 return static_cast<tools::Time>(aDT);
159 bool SwDateTimeField::QueryValue( uno::Any& rVal, sal_uInt16 nWhichId ) const
161 switch( nWhichId )
163 case FIELD_PROP_BOOL1:
164 rVal <<= IsFixed();
165 break;
166 case FIELD_PROP_BOOL2:
167 rVal <<= (m_nSubType & DATEFLD) != 0;
168 break;
169 case FIELD_PROP_FORMAT:
170 rVal <<= static_cast<sal_Int32>(GetFormat());
171 break;
172 case FIELD_PROP_SUBTYPE:
173 rVal <<= static_cast<sal_Int32>(m_nOffset);
174 break;
175 case FIELD_PROP_DATE_TIME:
177 DateTime aDateTime(GetDate(), GetTime());
178 rVal <<= aDateTime.GetUNODateTime();
180 break;
181 default:
182 return SwField::QueryValue(rVal, nWhichId);
184 return true;
187 bool SwDateTimeField::PutValue( const uno::Any& rVal, sal_uInt16 nWhichId )
189 sal_Int32 nTmp = 0;
190 switch( nWhichId )
192 case FIELD_PROP_BOOL1:
193 if(*o3tl::doAccess<bool>(rVal))
194 m_nSubType |= FIXEDFLD;
195 else
196 m_nSubType &= ~FIXEDFLD;
197 break;
198 case FIELD_PROP_BOOL2:
199 m_nSubType &= ~(DATEFLD|TIMEFLD);
200 m_nSubType |= *o3tl::doAccess<bool>(rVal) ? DATEFLD : TIMEFLD;
201 break;
202 case FIELD_PROP_FORMAT:
203 rVal >>= nTmp;
204 ChangeFormat(nTmp);
205 break;
206 case FIELD_PROP_SUBTYPE:
207 rVal >>= nTmp;
208 m_nOffset = nTmp;
209 break;
210 case FIELD_PROP_DATE_TIME:
212 util::DateTime aDateTimeValue;
213 if(!(rVal >>= aDateTimeValue))
214 return false;
215 DateTime aDateTime( DateTime::EMPTY );
216 aDateTime.SetNanoSec(aDateTimeValue.NanoSeconds);
217 aDateTime.SetSec(aDateTimeValue.Seconds);
218 aDateTime.SetMin(aDateTimeValue.Minutes);
219 aDateTime.SetHour(aDateTimeValue.Hours);
220 aDateTime.SetDay(aDateTimeValue.Day);
221 aDateTime.SetMonth(aDateTimeValue.Month);
222 aDateTime.SetYear(aDateTimeValue.Year);
223 SetDateTime(aDateTime);
225 break;
226 default:
227 return SwField::PutValue(rVal, nWhichId);
229 return true;
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */