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