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 37 bool DateHelper::IsInSameYear( const Date& rD1, const Date& rD2 ) 38 { 39 return rD1.GetYear() == rD2.GetYear(); 40 } 41 bool DateHelper::IsInSameMonth( const Date& rD1, const Date& rD2 ) 42 { 43 return (rD1.GetYear() == rD2.GetYear()) 44 && (rD1.GetMonth() == rD2.GetMonth()); 45 } 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 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 78 Date DateHelper::GetDateSomeYearsAway( const Date& rD, long nYearDistance ) 79 { 80 Date aRet(rD); 81 aRet.SetYear( static_cast<sal_uInt16>(rD.GetYear()+nYearDistance) ); 82 while(!aRet.IsValid()) 83 aRet--; 84 return aRet; 85 } 86 87 bool DateHelper::IsLessThanOneMonthAway( const Date& rD1, const Date& rD2 ) 88 { 89 Date aDMin( DateHelper::GetDateSomeMonthsAway( rD1, -1 ) ); 90 Date aDMax( DateHelper::GetDateSomeMonthsAway( rD1, 1 ) ); 91 92 if( rD2 > aDMin && rD2 < aDMax ) 93 return true; 94 return false; 95 } 96 97 bool DateHelper::IsLessThanOneYearAway( const Date& rD1, const Date& rD2 ) 98 { 99 Date aDMin( DateHelper::GetDateSomeYearsAway( rD1, -1 ) ); 100 Date aDMax( DateHelper::GetDateSomeYearsAway( rD1, 1 ) ); 101 102 if( rD2 > aDMin && rD2 < aDMax ) 103 return true; 104 return false; 105 } 106 107 double DateHelper::RasterizeDateValue( double fValue, const Date& rNullDate, long TimeResolution ) 108 { 109 Date aDate(rNullDate); aDate += static_cast<long>(::rtl::math::approxFloor(fValue)); 110 switch(TimeResolution) 111 { 112 case ::com::sun::star::chart::TimeUnit::DAY: 113 break; 114 case ::com::sun::star::chart::TimeUnit::YEAR: 115 aDate.SetMonth(1); 116 aDate.SetDay(1); 117 break; 118 case ::com::sun::star::chart::TimeUnit::MONTH: 119 default: 120 aDate.SetDay(1); 121 break; 122 } 123 return aDate - rNullDate; 124 } 125 126 //............................................................................. 127 } //namespace chart 128 //............................................................................. 129