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