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/zforlist.hxx>
26 #include <com/sun/star/util/DateTime.hpp>
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),
50 SvNumberFormatter
* pFormatter
= GetDoc()->GetNumberFormatter();
51 if (m_nSubType
& DATEFLD
)
52 ChangeFormat(pFormatter
->GetFormatIndex(NF_DATE_SYSTEM_SHORT
, GetLanguage()));
54 ChangeFormat(pFormatter
->GetFormatIndex(NF_TIME_HHMMSS
, GetLanguage()));
58 DateTime
aDateTime( DateTime::SYSTEM
);
59 SetDateTime(aDateTime
);
63 OUString
SwDateTimeField::ExpandImpl(SwRootFrame
const*const) const
69 DateTime
aDateTime( DateTime::SYSTEM
);
70 fVal
= GetDateTime(*GetDoc(), aDateTime
);
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
99 void SwDateTimeField::SetSubType(sal_uInt16 nType
)
104 void SwDateTimeField::SetPar2(const OUString
& rStr
)
106 m_nOffset
= rStr
.toInt32();
109 OUString
SwDateTimeField::GetPar2() const
112 return OUString::number(m_nOffset
);
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
);
131 double SwDateTimeField::GetValue() const
134 return SwValueField::GetValue();
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
;
151 tools::Time
SwDateTimeField::GetTime() const
153 double fFract
= modf(GetValue(), &o3tl::temporary(double()));
154 DateTime
aDT( DateTime::EMPTY
);
156 return static_cast<tools::Time
>(aDT
);
159 bool SwDateTimeField::QueryValue( uno::Any
& rVal
, sal_uInt16 nWhichId
) const
163 case FIELD_PROP_BOOL1
:
166 case FIELD_PROP_BOOL2
:
167 rVal
<<= (m_nSubType
& DATEFLD
) != 0;
169 case FIELD_PROP_FORMAT
:
170 rVal
<<= static_cast<sal_Int32
>(GetFormat());
172 case FIELD_PROP_SUBTYPE
:
173 rVal
<<= static_cast<sal_Int32
>(m_nOffset
);
175 case FIELD_PROP_DATE_TIME
:
177 DateTime
aDateTime(GetDate(), GetTime());
178 rVal
<<= aDateTime
.GetUNODateTime();
182 return SwField::QueryValue(rVal
, nWhichId
);
187 bool SwDateTimeField::PutValue( const uno::Any
& rVal
, sal_uInt16 nWhichId
)
192 case FIELD_PROP_BOOL1
:
193 if(*o3tl::doAccess
<bool>(rVal
))
194 m_nSubType
|= FIXEDFLD
;
196 m_nSubType
&= ~FIXEDFLD
;
198 case FIELD_PROP_BOOL2
:
199 m_nSubType
&= ~(DATEFLD
|TIMEFLD
);
200 m_nSubType
|= *o3tl::doAccess
<bool>(rVal
) ? DATEFLD
: TIMEFLD
;
202 case FIELD_PROP_FORMAT
:
206 case FIELD_PROP_SUBTYPE
:
210 case FIELD_PROP_DATE_TIME
:
212 util::DateTime aDateTimeValue
;
213 if(!(rVal
>>= aDateTimeValue
))
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
);
227 return SwField::PutValue(rVal
, nWhichId
);
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */