1cde9e8dcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3cde9e8dcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4cde9e8dcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5cde9e8dcSAndrew Rist  * distributed with this work for additional information
6cde9e8dcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7cde9e8dcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8cde9e8dcSAndrew Rist  * "License"); you may not use this file except in compliance
9cde9e8dcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cde9e8dcSAndrew Rist  *
11cde9e8dcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cde9e8dcSAndrew Rist  *
13cde9e8dcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14cde9e8dcSAndrew Rist  * software distributed under the License is distributed on an
15cde9e8dcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16cde9e8dcSAndrew Rist  * KIND, either express or implied.  See the License for the
17cde9e8dcSAndrew Rist  * specific language governing permissions and limitations
18cde9e8dcSAndrew Rist  * under the License.
19cde9e8dcSAndrew Rist  *
20cde9e8dcSAndrew Rist  *************************************************************/
21cde9e8dcSAndrew Rist 
22cde9e8dcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_chart2.hxx"
26cdf0e10cSrcweir #include "DateHelper.hxx"
27cdf0e10cSrcweir #include "DateScaling.hxx"
28cdf0e10cSrcweir #include <rtl/math.hxx>
29cdf0e10cSrcweir #include <com/sun/star/chart/TimeUnit.hpp>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir //.............................................................................
32cdf0e10cSrcweir namespace chart
33cdf0e10cSrcweir {
34cdf0e10cSrcweir //.............................................................................
35cdf0e10cSrcweir using namespace ::com::sun::star;
36cdf0e10cSrcweir 
IsInSameYear(const Date & rD1,const Date & rD2)37cdf0e10cSrcweir bool DateHelper::IsInSameYear( const Date& rD1, const Date& rD2 )
38cdf0e10cSrcweir {
39cdf0e10cSrcweir     return rD1.GetYear() == rD2.GetYear();
40cdf0e10cSrcweir }
IsInSameMonth(const Date & rD1,const Date & rD2)41cdf0e10cSrcweir bool DateHelper::IsInSameMonth( const Date& rD1, const Date& rD2 )
42cdf0e10cSrcweir {
43cdf0e10cSrcweir     return (rD1.GetYear() == rD2.GetYear())
44cdf0e10cSrcweir         && (rD1.GetMonth() == rD2.GetMonth());
45cdf0e10cSrcweir }
GetMonthsBetweenDates(Date aD1,Date aD2)46cdf0e10cSrcweir long DateHelper::GetMonthsBetweenDates( Date aD1, Date aD2 )
47cdf0e10cSrcweir {
48cdf0e10cSrcweir 	Date aHelp = aD1;
49cdf0e10cSrcweir 	long nSign = 1;
50cdf0e10cSrcweir 	if( aD1 < aD2 )
51cdf0e10cSrcweir 	{
52cdf0e10cSrcweir 		aD1 = aD2;
53cdf0e10cSrcweir 		aD2 = aHelp;
54cdf0e10cSrcweir 		nSign = -1;
55cdf0e10cSrcweir 	}
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 	return nSign*( ( aD1.GetMonth() - aD2.GetMonth() )
58cdf0e10cSrcweir 		+ ( aD1.GetYear() - aD2.GetYear() )*12 );
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
GetDateSomeMonthsAway(const Date & rD,long nMonthDistance)61cdf0e10cSrcweir Date DateHelper::GetDateSomeMonthsAway( const Date& rD, long nMonthDistance )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     Date aRet(rD);
64cdf0e10cSrcweir     long nMonth = rD.GetMonth()+nMonthDistance;
65cdf0e10cSrcweir 	long nNewMonth = nMonth%12;
66cdf0e10cSrcweir 	long nNewYear = rD.GetYear() + nMonth/12;
67cdf0e10cSrcweir 	if( nMonth <= 0 || !nNewMonth )
68cdf0e10cSrcweir 		nNewYear--;
69cdf0e10cSrcweir 	if( nNewMonth <= 0 )
70cdf0e10cSrcweir 		nNewMonth += 12;
71cdf0e10cSrcweir 	aRet.SetMonth( sal_uInt16(nNewMonth) );
72cdf0e10cSrcweir 	aRet.SetYear( sal_uInt16(nNewYear) );
73cdf0e10cSrcweir     while(!aRet.IsValid())
74cdf0e10cSrcweir         aRet--;
75cdf0e10cSrcweir     return aRet;
76cdf0e10cSrcweir }
77cdf0e10cSrcweir 
GetDateSomeYearsAway(const Date & rD,long nYearDistance)78cdf0e10cSrcweir Date DateHelper::GetDateSomeYearsAway( const Date& rD, long nYearDistance )
79cdf0e10cSrcweir {
80cdf0e10cSrcweir     Date aRet(rD);
81*9a1ec794SAndre Fischer     const long nFutureYear (rD.GetYear()+nYearDistance);
82*9a1ec794SAndre Fischer     aRet.SetYear(static_cast<sal_uInt16>(nFutureYear));
83*9a1ec794SAndre Fischer     if ( ! aRet.IsValid())
84*9a1ec794SAndre Fischer     {
85*9a1ec794SAndre Fischer         // The Date class has the nasty property to store years modulo
86*9a1ec794SAndre Fischer         // 10000.  In order to handle (probably invalid) very large
87*9a1ec794SAndre Fischer         // year values more gracefully than with an infinite loop we
88*9a1ec794SAndre Fischer         // check that condition and return an invalid date.
89*9a1ec794SAndre Fischer         if (nFutureYear < 10000)
90*9a1ec794SAndre Fischer         {
91*9a1ec794SAndre Fischer             while ( ! aRet.IsValid())
92*9a1ec794SAndre Fischer                 --aRet;
93*9a1ec794SAndre Fischer         }
94*9a1ec794SAndre Fischer     }
95cdf0e10cSrcweir     return aRet;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
IsLessThanOneMonthAway(const Date & rD1,const Date & rD2)98cdf0e10cSrcweir bool DateHelper::IsLessThanOneMonthAway( const Date& rD1, const Date& rD2 )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir     Date aDMin( DateHelper::GetDateSomeMonthsAway( rD1, -1 ) );
101cdf0e10cSrcweir     Date aDMax( DateHelper::GetDateSomeMonthsAway( rD1, 1 ) );
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     if( rD2 > aDMin && rD2 < aDMax )
104cdf0e10cSrcweir         return true;
105cdf0e10cSrcweir     return false;
106cdf0e10cSrcweir }
107cdf0e10cSrcweir 
IsLessThanOneYearAway(const Date & rD1,const Date & rD2)108cdf0e10cSrcweir bool DateHelper::IsLessThanOneYearAway( const Date& rD1, const Date& rD2 )
109cdf0e10cSrcweir {
110cdf0e10cSrcweir     Date aDMin( DateHelper::GetDateSomeYearsAway( rD1, -1 ) );
111cdf0e10cSrcweir     Date aDMax( DateHelper::GetDateSomeYearsAway( rD1, 1 ) );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     if( rD2 > aDMin && rD2 < aDMax )
114cdf0e10cSrcweir         return true;
115cdf0e10cSrcweir     return false;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
RasterizeDateValue(double fValue,const Date & rNullDate,long TimeResolution)118cdf0e10cSrcweir double DateHelper::RasterizeDateValue( double fValue, const Date& rNullDate, long TimeResolution )
119cdf0e10cSrcweir {
120cdf0e10cSrcweir     Date aDate(rNullDate); aDate += static_cast<long>(::rtl::math::approxFloor(fValue));
121cdf0e10cSrcweir     switch(TimeResolution)
122cdf0e10cSrcweir     {
123cdf0e10cSrcweir         case ::com::sun::star::chart::TimeUnit::DAY:
124cdf0e10cSrcweir             break;
125cdf0e10cSrcweir         case ::com::sun::star::chart::TimeUnit::YEAR:
126cdf0e10cSrcweir             aDate.SetMonth(1);
127cdf0e10cSrcweir             aDate.SetDay(1);
128cdf0e10cSrcweir             break;
129cdf0e10cSrcweir         case ::com::sun::star::chart::TimeUnit::MONTH:
130cdf0e10cSrcweir         default:
131cdf0e10cSrcweir             aDate.SetDay(1);
132cdf0e10cSrcweir             break;
133cdf0e10cSrcweir     }
134cdf0e10cSrcweir     return aDate - rNullDate;
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir //.............................................................................
138cdf0e10cSrcweir } //namespace chart
139cdf0e10cSrcweir //.............................................................................
140