Line data Source code
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 <tools/datetime.hxx>
21 : #include <svl/zforlist.hxx>
22 : #include <com/sun/star/util/DateTime.hpp>
23 : #include <doc.hxx>
24 : #include <fldbas.hxx>
25 : #include <flddat.hxx>
26 : #include <unofldmid.h>
27 :
28 : using namespace ::com::sun::star;
29 :
30 0 : SwDateTimeFieldType::SwDateTimeFieldType(SwDoc* pInitDoc)
31 0 : : SwValueFieldType( pInitDoc, RES_DATETIMEFLD )
32 0 : {}
33 :
34 0 : SwFieldType* SwDateTimeFieldType::Copy() const
35 : {
36 0 : SwDateTimeFieldType *pTmp = new SwDateTimeFieldType(GetDoc());
37 0 : return pTmp;
38 : }
39 :
40 0 : SwDateTimeField::SwDateTimeField(SwDateTimeFieldType* pInitType, sal_uInt16 nSub, sal_uLong nFmt, sal_uInt16 nLng)
41 : : SwValueField(pInitType, nFmt, nLng, 0.0),
42 : nSubType(nSub),
43 0 : nOffset(0)
44 : {
45 0 : if (!nFmt)
46 : {
47 0 : SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
48 0 : if (nSubType & DATEFLD)
49 0 : ChangeFormat(pFormatter->GetFormatIndex(NF_DATE_SYSTEM_SHORT, GetLanguage()));
50 : else
51 0 : ChangeFormat(pFormatter->GetFormatIndex(NF_TIME_HHMMSS, GetLanguage()));
52 : }
53 0 : if (IsFixed())
54 : {
55 0 : DateTime aDateTime( DateTime::SYSTEM );
56 0 : SetDateTime(aDateTime);
57 : }
58 0 : }
59 :
60 0 : OUString SwDateTimeField::Expand() const
61 : {
62 : double fVal;
63 :
64 0 : if (!(IsFixed()))
65 : {
66 0 : DateTime aDateTime( DateTime::SYSTEM );
67 0 : fVal = GetDateTime(GetDoc(), aDateTime);
68 : }
69 : else
70 0 : fVal = GetValue();
71 :
72 0 : if (nOffset)
73 0 : fVal += (double)(nOffset * 60L) / 86400.0;
74 :
75 0 : return ExpandValue(fVal, GetFormat(), GetLanguage());
76 : }
77 :
78 0 : SwField* SwDateTimeField::Copy() const
79 : {
80 : SwDateTimeField *pTmp =
81 0 : new SwDateTimeField((SwDateTimeFieldType*)GetTyp(), nSubType,
82 0 : GetFormat(), GetLanguage());
83 :
84 0 : pTmp->SetValue(GetValue());
85 0 : pTmp->SetOffset(nOffset);
86 0 : pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
87 :
88 0 : return pTmp;
89 : }
90 :
91 0 : sal_uInt16 SwDateTimeField::GetSubType() const
92 : {
93 0 : return nSubType;
94 : }
95 :
96 0 : void SwDateTimeField::SetSubType(sal_uInt16 nType)
97 : {
98 0 : nSubType = nType;
99 0 : }
100 :
101 0 : void SwDateTimeField::SetPar2(const OUString& rStr)
102 : {
103 0 : nOffset = rStr.toInt32();
104 0 : }
105 :
106 0 : OUString SwDateTimeField::GetPar2() const
107 : {
108 0 : if (nOffset)
109 0 : return OUString::number(nOffset);
110 0 : return OUString();
111 : }
112 :
113 0 : void SwDateTimeField::SetDateTime(const DateTime& rDT)
114 : {
115 0 : SetValue(GetDateTime(GetDoc(), rDT));
116 0 : }
117 :
118 0 : double SwDateTimeField::GetDateTime(SwDoc* pDoc, const DateTime& rDT)
119 : {
120 0 : SvNumberFormatter* pFormatter = pDoc->GetNumberFormatter();
121 0 : Date* pNullDate = pFormatter->GetNullDate();
122 :
123 0 : double fResult = rDT - DateTime(*pNullDate);
124 :
125 0 : return fResult;
126 : }
127 :
128 0 : double SwDateTimeField::GetValue() const
129 : {
130 0 : if (IsFixed())
131 0 : return SwValueField::GetValue();
132 : else
133 0 : return GetDateTime(GetDoc(), DateTime( DateTime::SYSTEM ));
134 : }
135 :
136 0 : Date SwDateTimeField::GetDate(sal_Bool bUseOffset) const
137 : {
138 0 : SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
139 0 : Date* pNullDate = pFormatter->GetNullDate();
140 :
141 0 : long nVal = static_cast<long>( GetValue() );
142 :
143 0 : if (bUseOffset && nOffset)
144 0 : nVal += nOffset / 60 / 24;
145 :
146 0 : Date aDate = *pNullDate + nVal;
147 :
148 0 : return aDate;
149 : }
150 :
151 0 : Time SwDateTimeField::GetTime(sal_Bool bUseOffset) const
152 : {
153 : double fDummy;
154 0 : double fFract = modf(GetValue(), &fDummy);
155 0 : DateTime aDT((long)fDummy, 0);
156 0 : aDT += fFract;
157 0 : if (bUseOffset)
158 0 : aDT += Time(0, nOffset);
159 0 : return (Time)aDT;
160 : }
161 :
162 0 : bool SwDateTimeField::QueryValue( uno::Any& rVal, sal_uInt16 nWhichId ) const
163 : {
164 0 : switch( nWhichId )
165 : {
166 : case FIELD_PROP_BOOL1:
167 : {
168 0 : sal_Bool bTmp = IsFixed();
169 0 : rVal.setValue(&bTmp, ::getCppuBooleanType());
170 : }
171 0 : break;
172 : case FIELD_PROP_BOOL2:
173 : {
174 0 : sal_Bool bTmp = IsDate();
175 0 : rVal.setValue(&bTmp, ::getCppuBooleanType());
176 : }
177 0 : break;
178 : case FIELD_PROP_FORMAT:
179 0 : rVal <<= (sal_Int32)GetFormat();
180 0 : break;
181 : case FIELD_PROP_SUBTYPE:
182 0 : rVal <<= (sal_Int32)nOffset;
183 0 : break;
184 : case FIELD_PROP_DATE_TIME:
185 : {
186 0 : DateTime aDateTime(GetDate(), GetTime());
187 :
188 0 : util::DateTime DateTimeValue;
189 0 : DateTimeValue.NanoSeconds = aDateTime.GetNanoSec();
190 0 : DateTimeValue.Seconds = aDateTime.GetSec();
191 0 : DateTimeValue.Minutes = aDateTime.GetMin();
192 0 : DateTimeValue.Hours = aDateTime.GetHour();
193 0 : DateTimeValue.Day = aDateTime.GetDay();
194 0 : DateTimeValue.Month = aDateTime.GetMonth();
195 0 : DateTimeValue.Year = aDateTime.GetYear();
196 0 : rVal <<= DateTimeValue;
197 : }
198 0 : break;
199 : default:
200 0 : return SwField::QueryValue(rVal, nWhichId);
201 : }
202 0 : return true;
203 : }
204 :
205 0 : bool SwDateTimeField::PutValue( const uno::Any& rVal, sal_uInt16 nWhichId )
206 : {
207 0 : sal_Int32 nTmp = 0;
208 0 : switch( nWhichId )
209 : {
210 : case FIELD_PROP_BOOL1:
211 0 : if(*(sal_Bool*)rVal.getValue())
212 0 : nSubType |= FIXEDFLD;
213 : else
214 0 : nSubType &= ~FIXEDFLD;
215 0 : break;
216 : case FIELD_PROP_BOOL2:
217 0 : nSubType &= ~(DATEFLD|TIMEFLD);
218 0 : nSubType |= *(sal_Bool*)rVal.getValue() ? DATEFLD : TIMEFLD;
219 0 : break;
220 : case FIELD_PROP_FORMAT:
221 0 : rVal >>= nTmp;
222 0 : ChangeFormat(nTmp);
223 0 : break;
224 : case FIELD_PROP_SUBTYPE:
225 0 : rVal >>= nTmp;
226 0 : nOffset = nTmp;
227 0 : break;
228 : case FIELD_PROP_DATE_TIME:
229 : {
230 0 : util::DateTime aDateTimeValue;
231 0 : if(!(rVal >>= aDateTimeValue))
232 0 : return false;
233 0 : DateTime aDateTime( DateTime::EMPTY );
234 0 : aDateTime.SetNanoSec(aDateTimeValue.NanoSeconds);
235 0 : aDateTime.SetSec(aDateTimeValue.Seconds);
236 0 : aDateTime.SetMin(aDateTimeValue.Minutes);
237 0 : aDateTime.SetHour(aDateTimeValue.Hours);
238 0 : aDateTime.SetDay(aDateTimeValue.Day);
239 0 : aDateTime.SetMonth(aDateTimeValue.Month);
240 0 : aDateTime.SetYear(aDateTimeValue.Year);
241 0 : SetDateTime(aDateTime);
242 : }
243 0 : break;
244 : default:
245 0 : return SwField::PutValue(rVal, nWhichId);
246 : }
247 0 : return true;
248 : }
249 :
250 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|