kspread

kspread_functions_helper.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998-2002 The KSpread Team
00003                            www.koffice.org/kspread
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <qdatetime.h>
00022 
00023 #include "kspread_functions_helper.h"
00024 
00025 /*  DISABLED - we use KCalendarSystem instead
00026 void addMonths( QDate & date, int months )
00027 {
00028   int d = date.day();
00029   int m = date.month() + months;
00030   int y = date.year();
00031 
00032   if ( m > 12 )
00033   {
00034     y += (int) ( m / 12 );
00035     m %= 12;
00036   }
00037 
00038   // e.g. 31 Feb: decrease day...
00039   while ( !QDate::isValid( y, m, d ) && d > 0 )
00040     --d;
00041 
00042   date.setYMD( y, m, d );
00043 }
00044 
00045 void subMonths( QDate & date, int months )
00046 {
00047   int d = date.day();
00048   int m = date.month() - months;
00049   int y = date.year();
00050 
00051   while ( m < 1 )
00052   {
00053     m += 12;
00054     y -= 1;
00055   }
00056 
00057   // e.g. 31 Feb: decrease day
00058   while ( !QDate::isValid( y, m, d ) && d > 0 )
00059     --d;
00060 
00061   date.setYMD( y, m, d );
00062 }
00063 
00064 */
00065 
00066 int KSpread::daysPerYear(QDate const & date, int basis)
00067 {
00068   switch( basis )
00069   {
00070    case 0:
00071     return 360;
00072 
00073    case 1:
00074     if ( QDate::leapYear( date.year() ) )
00075       return 366;
00076     return 365;
00077 
00078    case 2:
00079     return 360;
00080    case 3:
00081     return 365;
00082    case 4:
00083     return 360;
00084   }
00085 
00086   return -1;
00087 }
00088 
00089 int KSpread::daysBetweenDates(QDate const & date1, QDate const & date2, int basis)
00090 {
00091   int day1, day2, month1, month2, year1, year2;
00092   bool isLeapYear = false;
00093   int days, months, years;
00094 
00095   day1   = date1.day();
00096   month1 = date1.month();
00097   year1  = date1.year();
00098   day2   = date2.day();
00099   month2 = date2.month();
00100   year2  = date2.year();
00101 
00102   years  = year2  - year1;
00103   months = month2 - month1 + years * 12;
00104   days   = day2   - day1;
00105 
00106   isLeapYear = QDate::leapYear( year1 );
00107 
00108   switch (basis)
00109   {
00110    case 0:
00111     if ( month1 == 2 && month2 != 2 && year1 == year2 )
00112     {
00113       if ( isLeapYear )
00114         return months * 30 + days - 1;
00115       else
00116         return months * 30 + days - 2;
00117     }
00118     return months * 30 + days;
00119 
00120    case 1: // TODO: real days for difference between months!
00121     //    return ( month2 - month1 ) * 30 + years * 360 + days;
00122 
00123    case 2: // TODO: real days for difference between months!
00124     //    return ( month2 - month1 ) * 30 + years * 365 + days;
00125 
00126    case 3:
00127     return date1.daysTo( date2 );
00128 
00129    case 4:
00130     return months * 30 + days;
00131   }
00132 
00133   return -1;
00134 }
00135 
KDE Home | KDE Accessibility Home | Description of Access Keys