related: tdf#162601 UNIQUE function is case-insensitive also for non ASCII
[LibreOffice.git] / sw / source / core / fields / flddat.cxx
blob264b7538f96750fe76a7d4802e50e327d98587b9
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/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>
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),
46 m_nSubType(nSub),
47 m_nOffset(0)
49 if (!nFormat)
51 SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
52 if (m_nSubType & DATEFLD)
53 ChangeFormat(pFormatter->GetFormatIndex(NF_DATE_SYSTEM_SHORT, GetLanguage()));
54 else
55 ChangeFormat(pFormatter->GetFormatIndex(NF_TIME_HHMMSS, GetLanguage()));
57 if (IsFixed())
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
71 double fVal;
73 if (!(IsFixed()))
75 DateTime aDateTime( DateTime::SYSTEM );
76 fVal = GetDateTime(*GetDoc(), aDateTime);
78 else
79 fVal = GetValue();
81 if (m_nOffset)
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
102 return m_nSubType;
105 void SwDateTimeField::SetSubType(sal_uInt16 nType)
107 m_nSubType = nType;
110 void SwDateTimeField::SetPar2(const OUString& rStr)
112 m_nOffset = rStr.toInt32();
115 OUString SwDateTimeField::GetPar2() const
117 if (m_nOffset)
118 return OUString::number(m_nOffset);
119 return OUString();
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));
134 return fResult;
137 double SwDateTimeField::GetValue() const
139 if (getenv("STABLE_FIELDS_HACK"))
141 const_cast<SwDateTimeField*>(this)->m_nSubType |= FIXEDFLD; //HACK
144 if (IsFixed())
145 return SwValueField::GetValue();
146 else
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;
159 return aDate;
162 tools::Time SwDateTimeField::GetTime() const
164 double fFract = modf(GetValue(), &o3tl::temporary(double()));
165 DateTime aDT( DateTime::EMPTY );
166 aDT.AddTime(fFract);
167 return static_cast<tools::Time>(aDT);
170 bool SwDateTimeField::QueryValue( uno::Any& rVal, sal_uInt16 nWhichId ) const
172 switch( nWhichId )
174 case FIELD_PROP_BOOL1:
175 rVal <<= IsFixed();
176 break;
177 case FIELD_PROP_BOOL2:
178 rVal <<= (m_nSubType & DATEFLD) != 0;
179 break;
180 case FIELD_PROP_FORMAT:
181 rVal <<= static_cast<sal_Int32>(GetFormat());
182 break;
183 case FIELD_PROP_SUBTYPE:
184 rVal <<= static_cast<sal_Int32>(m_nOffset);
185 break;
186 case FIELD_PROP_DATE_TIME:
188 DateTime aDateTime(GetDate(), GetTime());
189 rVal <<= aDateTime.GetUNODateTime();
191 break;
192 default:
193 return SwField::QueryValue(rVal, nWhichId);
195 return true;
198 bool SwDateTimeField::PutValue( const uno::Any& rVal, sal_uInt16 nWhichId )
200 sal_Int32 nTmp = 0;
201 switch( nWhichId )
203 case FIELD_PROP_BOOL1:
204 if(*o3tl::doAccess<bool>(rVal))
205 m_nSubType |= FIXEDFLD;
206 else
207 m_nSubType &= ~FIXEDFLD;
208 break;
209 case FIELD_PROP_BOOL2:
210 m_nSubType &= ~(DATEFLD|TIMEFLD);
211 m_nSubType |= *o3tl::doAccess<bool>(rVal) ? DATEFLD : TIMEFLD;
212 break;
213 case FIELD_PROP_FORMAT:
214 rVal >>= nTmp;
215 ChangeFormat(nTmp);
216 break;
217 case FIELD_PROP_SUBTYPE:
218 rVal >>= nTmp;
219 m_nOffset = nTmp;
220 break;
221 case FIELD_PROP_DATE_TIME:
223 util::DateTime aDateTimeValue;
224 if(!(rVal >>= aDateTimeValue))
225 return false;
226 DateTime aDateTime(aDateTimeValue);
227 SetDateTime(aDateTime);
229 break;
230 default:
231 return SwField::PutValue(rVal, nWhichId);
233 return true;
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */