kspread

kspread_functions_helper.cpp
1/* This file is part of the KDE project
2 Copyright (C) 1998-2002 The KSpread Team
3 www.koffice.org/kspread
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <tqdatetime.h>
22
23#include "kspread_functions_helper.h"
24
25/* DISABLED - we use KCalendarSystem instead
26void addMonths( TQDate & date, int months )
27{
28 int d = date.day();
29 int m = date.month() + months;
30 int y = date.year();
31
32 if ( m > 12 )
33 {
34 y += (int) ( m / 12 );
35 m %= 12;
36 }
37
38 // e.g. 31 Feb: decrease day...
39 while ( !TQDate::isValid( y, m, d ) && d > 0 )
40 --d;
41
42 date.setYMD( y, m, d );
43}
44
45void subMonths( TQDate & date, int months )
46{
47 int d = date.day();
48 int m = date.month() - months;
49 int y = date.year();
50
51 while ( m < 1 )
52 {
53 m += 12;
54 y -= 1;
55 }
56
57 // e.g. 31 Feb: decrease day
58 while ( !TQDate::isValid( y, m, d ) && d > 0 )
59 --d;
60
61 date.setYMD( y, m, d );
62}
63
64*/
65
66int KSpread::daysPerYear(TQDate const & date, int basis)
67{
68 switch( basis )
69 {
70 case 0:
71 return 360;
72
73 case 1:
74 if ( TQDate::leapYear( date.year() ) )
75 return 366;
76 return 365;
77
78 case 2:
79 return 360;
80 case 3:
81 return 365;
82 case 4:
83 return 360;
84 }
85
86 return -1;
87}
88
89int KSpread::daysBetweenDates(TQDate const & date1, TQDate const & date2, int basis)
90{
91 int day1, day2, month1, month2, year1, year2;
92 bool isLeapYear = false;
93 int days, months, years;
94
95 day1 = date1.day();
96 month1 = date1.month();
97 year1 = date1.year();
98 day2 = date2.day();
99 month2 = date2.month();
100 year2 = date2.year();
101
102 years = year2 - year1;
103 months = month2 - month1 + years * 12;
104 days = day2 - day1;
105
106 isLeapYear = TQDate::leapYear( year1 );
107
108 switch (basis)
109 {
110 case 0:
111 if ( month1 == 2 && month2 != 2 && year1 == year2 )
112 {
113 if ( isLeapYear )
114 return months * 30 + days - 1;
115 else
116 return months * 30 + days - 2;
117 }
118 return months * 30 + days;
119
120 case 1: // TODO: real days for difference between months!
121 // return ( month2 - month1 ) * 30 + years * 360 + days;
122
123 case 2: // TODO: real days for difference between months!
124 // return ( month2 - month1 ) * 30 + years * 365 + days;
125
126 case 3:
127 return date1.daysTo( date2 );
128
129 case 4:
130 return months * 30 + days;
131 }
132
133 return -1;
134}
135