KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
2.9KB

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