1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/numformat.hxx>
26 #include <svl/zforlist.hxx>
27 #include <com/sun/star/util/DateTime.hpp>
31 #include <unofldmid.h>
33 using namespace ::com::sun::star
;
35 SwDateTimeFieldType::SwDateTimeFieldType(SwDoc
* pInitDoc
)
36 : SwValueFieldType( pInitDoc
, SwFieldIds::DateTime
)
39 std::unique_ptr
<SwFieldType
> SwDateTimeFieldType::Copy() const
41 return std::make_unique
<SwDateTimeFieldType
>(GetDoc());
44 SwDateTimeField::SwDateTimeField(SwDateTimeFieldType
* pInitType
, sal_uInt16 nSub
, sal_uLong nFormat
, LanguageType nLng
)
45 : SwValueField(pInitType
, nFormat
, nLng
, 0.0),
51 SvNumberFormatter
* pFormatter
= GetDoc()->GetNumberFormatter();
52 if (m_nSubType
& DATEFLD
)
53 ChangeFormat(pFormatter
->GetFormatIndex(NF_DATE_SYSTEM_SHORT
, GetLanguage()));
55 ChangeFormat(pFormatter
->GetFormatIndex(NF_TIME_HHMMSS
, GetLanguage()));
59 DateTime
aDateTime( DateTime::SYSTEM
);
60 SetDateTime(aDateTime
);
64 OUString
SwDateTimeField::ExpandImpl(SwRootFrame
const*const) const
66 if (getenv("STABLE_FIELDS_HACK"))
68 const_cast<SwDateTimeField
*>(this)->m_nSubType
|= FIXEDFLD
; //HACK
75 DateTime
aDateTime( DateTime::SYSTEM
);
76 fVal
= GetDateTime(*GetDoc(), aDateTime
);
82 fVal
+= m_nOffset
* ( 60 / 86400.0 );
84 return ExpandValue(fVal
, GetFormat(), GetLanguage());
87 std::unique_ptr
<SwField
> SwDateTimeField::Copy() const
89 std::unique_ptr
<SwDateTimeField
> pTmp(
90 new SwDateTimeField(static_cast<SwDateTimeFieldType
*>(GetTyp()), m_nSubType
,
91 GetFormat(), GetLanguage()) );
93 pTmp
->SetValue(GetValue());
94 pTmp
->SetOffset(m_nOffset
);
95 pTmp
->SetAutomaticLanguage(IsAutomaticLanguage());
97 return std::unique_ptr
<SwField
>(pTmp
.release());
100 sal_uInt16
SwDateTimeField::GetSubType() const
105 void SwDateTimeField::SetSubType(sal_uInt16 nType
)
110 void SwDateTimeField::SetPar2(const OUString
& rStr
)
112 m_nOffset
= rStr
.toInt32();
115 OUString
SwDateTimeField::GetPar2() const
118 return OUString::number(m_nOffset
);
122 void SwDateTimeField::SetDateTime(const DateTime
& rDT
)
124 SetValue(GetDateTime(*GetDoc(), rDT
));
127 double SwDateTimeField::GetDateTime(SwDoc
& rDoc
, const DateTime
& rDT
)
129 SvNumberFormatter
* pFormatter
= rDoc
.GetNumberFormatter();
130 const Date
& rNullDate
= pFormatter
->GetNullDate();
132 double fResult
= DateTime::Sub(rDT
, DateTime(rNullDate
));
137 double SwDateTimeField::GetValue() const
139 if (getenv("STABLE_FIELDS_HACK"))
141 const_cast<SwDateTimeField
*>(this)->m_nSubType
|= FIXEDFLD
; //HACK
145 return SwValueField::GetValue();
147 return GetDateTime(*GetDoc(), DateTime( DateTime::SYSTEM
));
150 Date
SwDateTimeField::GetDate() const
152 SvNumberFormatter
* pFormatter
= GetDoc()->GetNumberFormatter();
153 const Date
& rNullDate
= pFormatter
->GetNullDate();
155 tools::Long nVal
= static_cast<tools::Long
>( GetValue() );
157 Date aDate
= rNullDate
+ nVal
;
162 tools::Time
SwDateTimeField::GetTime() const
164 double fFract
= modf(GetValue(), &o3tl::temporary(double()));
165 DateTime
aDT( DateTime::EMPTY
);
167 return static_cast<tools::Time
>(aDT
);
170 bool SwDateTimeField::QueryValue( uno::Any
& rVal
, sal_uInt16 nWhichId
) const
174 case FIELD_PROP_BOOL1
:
177 case FIELD_PROP_BOOL2
:
178 rVal
<<= (m_nSubType
& DATEFLD
) != 0;
180 case FIELD_PROP_FORMAT
:
181 rVal
<<= static_cast<sal_Int32
>(GetFormat());
183 case FIELD_PROP_SUBTYPE
:
184 rVal
<<= static_cast<sal_Int32
>(m_nOffset
);
186 case FIELD_PROP_DATE_TIME
:
188 DateTime
aDateTime(GetDate(), GetTime());
189 rVal
<<= aDateTime
.GetUNODateTime();
193 return SwField::QueryValue(rVal
, nWhichId
);
198 bool SwDateTimeField::PutValue( const uno::Any
& rVal
, sal_uInt16 nWhichId
)
203 case FIELD_PROP_BOOL1
:
204 if(*o3tl::doAccess
<bool>(rVal
))
205 m_nSubType
|= FIXEDFLD
;
207 m_nSubType
&= ~FIXEDFLD
;
209 case FIELD_PROP_BOOL2
:
210 m_nSubType
&= ~(DATEFLD
|TIMEFLD
);
211 m_nSubType
|= *o3tl::doAccess
<bool>(rVal
) ? DATEFLD
: TIMEFLD
;
213 case FIELD_PROP_FORMAT
:
217 case FIELD_PROP_SUBTYPE
:
221 case FIELD_PROP_DATE_TIME
:
223 util::DateTime aDateTimeValue
;
224 if(!(rVal
>>= aDateTimeValue
))
226 DateTime
aDateTime(aDateTimeValue
);
227 SetDateTime(aDateTime
);
231 return SwField::PutValue(rVal
, nWhichId
);
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */