xref: /trunk/main/svl/source/items/dateitem.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*40df464eSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*40df464eSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*40df464eSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*40df464eSAndrew Rist  * distributed with this work for additional information
6*40df464eSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*40df464eSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*40df464eSAndrew Rist  * "License"); you may not use this file except in compliance
9*40df464eSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*40df464eSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*40df464eSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*40df464eSAndrew Rist  * software distributed under the License is distributed on an
15*40df464eSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*40df464eSAndrew Rist  * KIND, either express or implied.  See the License for the
17*40df464eSAndrew Rist  * specific language governing permissions and limitations
18*40df464eSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*40df464eSAndrew Rist  *************************************************************/
21*40df464eSAndrew Rist 
22*40df464eSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir // include ---------------------------------------------------------------
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #define _DATETIMEITEM_CXX
30cdf0e10cSrcweir #include <svl/dateitem.hxx>
31cdf0e10cSrcweir #include <svl/svldata.hxx>
32cdf0e10cSrcweir #include <svl/svl.hrc>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <unotools/intlwrapper.hxx>
35cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <tools/stream.hxx>
38cdf0e10cSrcweir #include <tools/debug.hxx>
39cdf0e10cSrcweir #include <tools/datetime.hxx>
40cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
41cdf0e10cSrcweir #include <com/sun/star/util/DateTime.hpp>
42cdf0e10cSrcweir #include <com/sun/star/lang/Locale.hpp>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir // STATIC DATA -----------------------------------------------------------
45cdf0e10cSrcweir 
46cdf0e10cSrcweir DBG_NAME(SfxDateTimeItem)
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 
49cdf0e10cSrcweir // -----------------------------------------------------------------------
50cdf0e10cSrcweir 
51cdf0e10cSrcweir TYPEINIT1(SfxDateTimeItem, SfxPoolItem);
52cdf0e10cSrcweir 
53cdf0e10cSrcweir // -----------------------------------------------------------------------
54cdf0e10cSrcweir 
SfxDateTimeItem(sal_uInt16 which)55cdf0e10cSrcweir SfxDateTimeItem::SfxDateTimeItem( sal_uInt16 which ) :
56cdf0e10cSrcweir     SfxPoolItem( which )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir     DBG_CTOR(SfxDateTimeItem, 0);
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir // -----------------------------------------------------------------------
62cdf0e10cSrcweir 
SfxDateTimeItem(sal_uInt16 which,const DateTime & rDT)63cdf0e10cSrcweir SfxDateTimeItem::SfxDateTimeItem( sal_uInt16 which, const DateTime& rDT ) :
64cdf0e10cSrcweir     SfxPoolItem( which ),
65cdf0e10cSrcweir     aDateTime( rDT )
66cdf0e10cSrcweir 
67cdf0e10cSrcweir {
68cdf0e10cSrcweir     DBG_CTOR(SfxDateTimeItem, 0);
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir // -----------------------------------------------------------------------
72cdf0e10cSrcweir 
SfxDateTimeItem(const SfxDateTimeItem & rItem)73cdf0e10cSrcweir SfxDateTimeItem::SfxDateTimeItem( const SfxDateTimeItem& rItem ) :
74cdf0e10cSrcweir     SfxPoolItem( rItem ),
75cdf0e10cSrcweir     aDateTime( rItem.aDateTime )
76cdf0e10cSrcweir {
77cdf0e10cSrcweir     DBG_CTOR(SfxDateTimeItem, 0);
78cdf0e10cSrcweir }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir // -----------------------------------------------------------------------
81cdf0e10cSrcweir 
operator ==(const SfxPoolItem & rItem) const82cdf0e10cSrcweir int SfxDateTimeItem::operator==( const SfxPoolItem& rItem ) const
83cdf0e10cSrcweir {
84cdf0e10cSrcweir     DBG_CHKTHIS(SfxDateTimeItem, 0);
85cdf0e10cSrcweir     DBG_ASSERT( SfxPoolItem::operator==( rItem ), "unequal type" );
86cdf0e10cSrcweir     return ( ( (SfxDateTimeItem&)rItem ).aDateTime == aDateTime );
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir // -----------------------------------------------------------------------
90cdf0e10cSrcweir 
Compare(const SfxPoolItem & rItem) const91cdf0e10cSrcweir int SfxDateTimeItem::Compare( const SfxPoolItem& rItem ) const
92cdf0e10cSrcweir {
93cdf0e10cSrcweir     DBG_CHKTHIS(SfxDateTimeItem, 0);
94cdf0e10cSrcweir     DBG_ASSERT( SfxPoolItem::operator==( rItem ), "unequal type" );
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     // da X.Compare( Y ) am String einem Compare( Y, X ) entspricht,
97cdf0e10cSrcweir     // vergleichen wir hier Y mit X
98cdf0e10cSrcweir     if ( ( (const SfxDateTimeItem&)rItem ).aDateTime < aDateTime )
99cdf0e10cSrcweir         return -1;
100cdf0e10cSrcweir     else if ( ( (const SfxDateTimeItem&)rItem ).aDateTime == aDateTime )
101cdf0e10cSrcweir         return 0;
102cdf0e10cSrcweir     else
103cdf0e10cSrcweir         return 1;
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir // -----------------------------------------------------------------------
107cdf0e10cSrcweir 
Create(SvStream & rStream,sal_uInt16) const108cdf0e10cSrcweir SfxPoolItem* SfxDateTimeItem::Create( SvStream& rStream, sal_uInt16 ) const
109cdf0e10cSrcweir {
110cdf0e10cSrcweir     DBG_CHKTHIS(SfxDateTimeItem, 0);
111cdf0e10cSrcweir     sal_uInt32 nDate = 0;
112cdf0e10cSrcweir     sal_Int32 nTime = 0;
113cdf0e10cSrcweir     rStream >> nDate;
114cdf0e10cSrcweir     rStream >> nTime;
115cdf0e10cSrcweir     DateTime aDT(nDate, nTime);
116cdf0e10cSrcweir     return new SfxDateTimeItem( Which(), aDT );
117cdf0e10cSrcweir }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir // -----------------------------------------------------------------------
120cdf0e10cSrcweir 
Store(SvStream & rStream,sal_uInt16) const121cdf0e10cSrcweir SvStream& SfxDateTimeItem::Store( SvStream& rStream, sal_uInt16 ) const
122cdf0e10cSrcweir {
123cdf0e10cSrcweir     DBG_CHKTHIS(SfxDateTimeItem, 0);
124cdf0e10cSrcweir     rStream << aDateTime.GetDate();
125cdf0e10cSrcweir     rStream << aDateTime.GetTime();
126cdf0e10cSrcweir     return rStream;
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir // -----------------------------------------------------------------------
130cdf0e10cSrcweir 
Clone(SfxItemPool *) const131cdf0e10cSrcweir SfxPoolItem* SfxDateTimeItem::Clone( SfxItemPool* ) const
132cdf0e10cSrcweir {
133cdf0e10cSrcweir     DBG_CHKTHIS(SfxDateTimeItem, 0);
134cdf0e10cSrcweir     return new SfxDateTimeItem( *this );
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir // -----------------------------------------------------------------------
138cdf0e10cSrcweir 
GetPresentation(SfxItemPresentation,SfxMapUnit,SfxMapUnit,XubString & rText,const IntlWrapper * pIntlWrapper) const139cdf0e10cSrcweir SfxItemPresentation SfxDateTimeItem::GetPresentation
140cdf0e10cSrcweir (
141cdf0e10cSrcweir     SfxItemPresentation     /*ePresentation*/,
142cdf0e10cSrcweir     SfxMapUnit              /*eCoreMetric*/,
143cdf0e10cSrcweir     SfxMapUnit              /*ePresentationMetric*/,
144cdf0e10cSrcweir     XubString&              rText,
145cdf0e10cSrcweir     const IntlWrapper *   pIntlWrapper
146cdf0e10cSrcweir )   const
147cdf0e10cSrcweir {
148cdf0e10cSrcweir     DBG_CHKTHIS(SfxDateTimeItem, 0);
149cdf0e10cSrcweir     if (aDateTime.IsValid())
150cdf0e10cSrcweir         if (pIntlWrapper)
151cdf0e10cSrcweir         {
152cdf0e10cSrcweir             rText = pIntlWrapper->getLocaleData()->getDate(aDateTime);
153cdf0e10cSrcweir             rText.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
154cdf0e10cSrcweir             rText += pIntlWrapper->getLocaleData()->getTime(aDateTime);
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir         else
157cdf0e10cSrcweir         {
158cdf0e10cSrcweir             DBG_WARNING("SfxDateTimeItem::GetPresentation():"
159cdf0e10cSrcweir                          " Using default en_US IntlWrapper");
160cdf0e10cSrcweir             const IntlWrapper aIntlWrapper(
161cdf0e10cSrcweir                 ::comphelper::getProcessServiceFactory(), LANGUAGE_ENGLISH_US );
162cdf0e10cSrcweir             rText = aIntlWrapper.getLocaleData()->getDate(aDateTime);
163cdf0e10cSrcweir             rText.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
164cdf0e10cSrcweir             rText += aIntlWrapper.getLocaleData()->getTime(aDateTime);
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir     else
167cdf0e10cSrcweir         rText.Erase();
168cdf0e10cSrcweir     return SFX_ITEM_PRESENTATION_NAMELESS;
169cdf0e10cSrcweir }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir //----------------------------------------------------------------------------
172cdf0e10cSrcweir // virtual
PutValue(const com::sun::star::uno::Any & rVal,sal_uInt8 nMemberId)173cdf0e10cSrcweir sal_Bool SfxDateTimeItem::PutValue( const com::sun::star::uno::Any& rVal,
174cdf0e10cSrcweir                                 sal_uInt8 nMemberId )
175cdf0e10cSrcweir {
176cdf0e10cSrcweir     nMemberId &= ~CONVERT_TWIPS;
177cdf0e10cSrcweir     com::sun::star::util::DateTime aValue;
178cdf0e10cSrcweir     if ( rVal >>= aValue )
179cdf0e10cSrcweir     {
180cdf0e10cSrcweir         aDateTime = DateTime( Date( aValue.Day,
181cdf0e10cSrcweir                                     aValue.Month,
182cdf0e10cSrcweir                                     aValue.Year ),
183cdf0e10cSrcweir                               Time( aValue.Hours,
184cdf0e10cSrcweir                                     aValue.Minutes,
185cdf0e10cSrcweir                                     aValue.Seconds,
186cdf0e10cSrcweir                                     aValue.HundredthSeconds ) );
187cdf0e10cSrcweir         return sal_True;
188cdf0e10cSrcweir     }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     DBG_ERROR( "SfxDateTimeItem::PutValue - Wrong type!" );
191cdf0e10cSrcweir     return sal_False;
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir //----------------------------------------------------------------------------
195cdf0e10cSrcweir // virtual
QueryValue(com::sun::star::uno::Any & rVal,sal_uInt8 nMemberId) const196cdf0e10cSrcweir sal_Bool SfxDateTimeItem::QueryValue( com::sun::star::uno::Any& rVal,
197cdf0e10cSrcweir                                   sal_uInt8 nMemberId ) const
198cdf0e10cSrcweir {
199cdf0e10cSrcweir     nMemberId &= ~CONVERT_TWIPS;
200cdf0e10cSrcweir     com::sun::star::util::DateTime aValue( aDateTime.Get100Sec(),
201cdf0e10cSrcweir                                            aDateTime.GetSec(),
202cdf0e10cSrcweir                                            aDateTime.GetMin(),
203cdf0e10cSrcweir                                            aDateTime.GetHour(),
204cdf0e10cSrcweir                                            aDateTime.GetDay(),
205cdf0e10cSrcweir                                            aDateTime.GetMonth(),
206cdf0e10cSrcweir                                            aDateTime.GetYear() );
207cdf0e10cSrcweir     rVal <<= aValue;
208cdf0e10cSrcweir     return sal_True;
209cdf0e10cSrcweir }
210cdf0e10cSrcweir 
211cdf0e10cSrcweir // -----------------------------------------------------------------------
212cdf0e10cSrcweir // -----------------------------------------------------------------------
213cdf0e10cSrcweir // -----------------------------------------------------------------------
214cdf0e10cSrcweir 
215cdf0e10cSrcweir TYPEINIT1(SfxColumnDateTimeItem, SfxDateTimeItem);
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 
SfxColumnDateTimeItem(sal_uInt16 which)218cdf0e10cSrcweir SfxColumnDateTimeItem::SfxColumnDateTimeItem( sal_uInt16 which ) :
219cdf0e10cSrcweir     SfxDateTimeItem( which )
220cdf0e10cSrcweir {}
221cdf0e10cSrcweir 
SfxColumnDateTimeItem(sal_uInt16 which,const DateTime & rDT)222cdf0e10cSrcweir SfxColumnDateTimeItem::SfxColumnDateTimeItem( sal_uInt16 which, const DateTime& rDT ) :
223cdf0e10cSrcweir     SfxDateTimeItem( which, rDT )
224cdf0e10cSrcweir {}
225cdf0e10cSrcweir 
SfxColumnDateTimeItem(const SfxDateTimeItem & rCpy)226cdf0e10cSrcweir SfxColumnDateTimeItem::SfxColumnDateTimeItem( const SfxDateTimeItem& rCpy ) :
227cdf0e10cSrcweir     SfxDateTimeItem( rCpy )
228cdf0e10cSrcweir {}
229cdf0e10cSrcweir 
Clone(SfxItemPool *) const230cdf0e10cSrcweir SfxPoolItem* SfxColumnDateTimeItem::Clone( SfxItemPool* ) const
231cdf0e10cSrcweir {
232cdf0e10cSrcweir     return new SfxColumnDateTimeItem( *this );
233cdf0e10cSrcweir }
234cdf0e10cSrcweir 
GetPresentation(SfxItemPresentation,SfxMapUnit,SfxMapUnit,XubString & rText,const IntlWrapper * pIntlWrapper) const235cdf0e10cSrcweir SfxItemPresentation SfxColumnDateTimeItem::GetPresentation
236cdf0e10cSrcweir (
237cdf0e10cSrcweir     SfxItemPresentation     /*ePresentation*/,
238cdf0e10cSrcweir     SfxMapUnit              /*eCoreMetric*/,
239cdf0e10cSrcweir     SfxMapUnit              /*ePresentationMetric*/,
240cdf0e10cSrcweir     XubString&              rText,
241cdf0e10cSrcweir     const IntlWrapper *   pIntlWrapper
242cdf0e10cSrcweir )   const
243cdf0e10cSrcweir {
244cdf0e10cSrcweir     DBG_ASSERT(pIntlWrapper,
245cdf0e10cSrcweir                "SfxColumnDateTimeItem::GetPresentation():"
246cdf0e10cSrcweir                 " Using default en_US IntlWrapper");
247cdf0e10cSrcweir 
248cdf0e10cSrcweir     ::com::sun::star::lang::Locale aLocale;
249cdf0e10cSrcweir     if (GetDateTime() == DateTime(Date(1, 2, 3), Time(3, 2, 1)))
250cdf0e10cSrcweir     {
251cdf0e10cSrcweir         rText = String(SvtSimpleResId(STR_COLUM_DT_AUTO,
252cdf0e10cSrcweir                                 pIntlWrapper ?
253cdf0e10cSrcweir                                  pIntlWrapper->getLocale() :
254cdf0e10cSrcweir                                  aLocale));
255cdf0e10cSrcweir     }
256cdf0e10cSrcweir     else if (pIntlWrapper)
257cdf0e10cSrcweir     {
258cdf0e10cSrcweir         rText = pIntlWrapper->getLocaleData()->getDate(GetDateTime());
259cdf0e10cSrcweir         rText.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
260cdf0e10cSrcweir         rText += pIntlWrapper->getLocaleData()->getTime(GetDateTime());
261cdf0e10cSrcweir     }
262cdf0e10cSrcweir     else
263cdf0e10cSrcweir     {
264cdf0e10cSrcweir         const IntlWrapper aIntlWrapper(
265cdf0e10cSrcweir             ::comphelper::getProcessServiceFactory(), LANGUAGE_ENGLISH_US );
266cdf0e10cSrcweir         rText = aIntlWrapper.getLocaleData()->getDate(GetDateTime());
267cdf0e10cSrcweir         rText.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
268cdf0e10cSrcweir         rText += aIntlWrapper.getLocaleData()->getTime(GetDateTime());
269cdf0e10cSrcweir     }
270cdf0e10cSrcweir     return SFX_ITEM_PRESENTATION_NAMELESS;
271cdf0e10cSrcweir }
272