1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 #include "DateHelper.hxx"
31 #include "DateScaling.hxx"
32 #include <rtl/math.hxx>
33 #include <com/sun/star/chart/TimeUnit.hpp>
34 
35 //.............................................................................
36 namespace chart
37 {
38 //.............................................................................
39 using namespace ::com::sun::star;
40 
41 bool DateHelper::IsInSameYear( const Date& rD1, const Date& rD2 )
42 {
43     return rD1.GetYear() == rD2.GetYear();
44 }
45 bool DateHelper::IsInSameMonth( const Date& rD1, const Date& rD2 )
46 {
47     return (rD1.GetYear() == rD2.GetYear())
48         && (rD1.GetMonth() == rD2.GetMonth());
49 }
50 long DateHelper::GetMonthsBetweenDates( Date aD1, Date aD2 )
51 {
52 	Date aHelp = aD1;
53 	long nSign = 1;
54 	if( aD1 < aD2 )
55 	{
56 		aD1 = aD2;
57 		aD2 = aHelp;
58 		nSign = -1;
59 	}
60 
61 	return nSign*( ( aD1.GetMonth() - aD2.GetMonth() )
62 		+ ( aD1.GetYear() - aD2.GetYear() )*12 );
63 }
64 
65 Date DateHelper::GetDateSomeMonthsAway( const Date& rD, long nMonthDistance )
66 {
67     Date aRet(rD);
68     long nMonth = rD.GetMonth()+nMonthDistance;
69 	long nNewMonth = nMonth%12;
70 	long nNewYear = rD.GetYear() + nMonth/12;
71 	if( nMonth <= 0 || !nNewMonth )
72 		nNewYear--;
73 	if( nNewMonth <= 0 )
74 		nNewMonth += 12;
75 	aRet.SetMonth( sal_uInt16(nNewMonth) );
76 	aRet.SetYear( sal_uInt16(nNewYear) );
77     while(!aRet.IsValid())
78         aRet--;
79     return aRet;
80 }
81 
82 Date DateHelper::GetDateSomeYearsAway( const Date& rD, long nYearDistance )
83 {
84     Date aRet(rD);
85     aRet.SetYear( static_cast<sal_uInt16>(rD.GetYear()+nYearDistance) );
86     while(!aRet.IsValid())
87         aRet--;
88     return aRet;
89 }
90 
91 bool DateHelper::IsLessThanOneMonthAway( const Date& rD1, const Date& rD2 )
92 {
93     Date aDMin( DateHelper::GetDateSomeMonthsAway( rD1, -1 ) );
94     Date aDMax( DateHelper::GetDateSomeMonthsAway( rD1, 1 ) );
95 
96     if( rD2 > aDMin && rD2 < aDMax )
97         return true;
98     return false;
99 }
100 
101 bool DateHelper::IsLessThanOneYearAway( const Date& rD1, const Date& rD2 )
102 {
103     Date aDMin( DateHelper::GetDateSomeYearsAway( rD1, -1 ) );
104     Date aDMax( DateHelper::GetDateSomeYearsAway( rD1, 1 ) );
105 
106     if( rD2 > aDMin && rD2 < aDMax )
107         return true;
108     return false;
109 }
110 
111 double DateHelper::RasterizeDateValue( double fValue, const Date& rNullDate, long TimeResolution )
112 {
113     Date aDate(rNullDate); aDate += static_cast<long>(::rtl::math::approxFloor(fValue));
114     switch(TimeResolution)
115     {
116         case ::com::sun::star::chart::TimeUnit::DAY:
117             break;
118         case ::com::sun::star::chart::TimeUnit::YEAR:
119             aDate.SetMonth(1);
120             aDate.SetDay(1);
121             break;
122         case ::com::sun::star::chart::TimeUnit::MONTH:
123         default:
124             aDate.SetDay(1);
125             break;
126     }
127     return aDate - rNullDate;
128 }
129 
130 //.............................................................................
131 } //namespace chart
132 //.............................................................................
133