25#include "kspread_functions_helper.h"
27#include "valueconverter.h"
29#include <kcalendarsystem.h>
35Value func_currentDate (valVector args,
ValueCalc *calc, FuncExtra *);
36Value func_currentDateTime (valVector args,
ValueCalc *calc, FuncExtra *);
37Value func_currentTime (valVector args,
ValueCalc *calc, FuncExtra *);
39Value func_datevalue (valVector args,
ValueCalc *calc, FuncExtra *);
42Value func_dayOfYear (valVector args,
ValueCalc *calc, FuncExtra *);
45Value func_daysInMonth (valVector args,
ValueCalc *calc, FuncExtra *);
46Value func_daysInYear (valVector args,
ValueCalc *calc, FuncExtra *);
47Value func_easterSunday (valVector args,
ValueCalc *calc, FuncExtra *);
52Value func_isLeapYear (valVector args,
ValueCalc *calc, FuncExtra *);
53Value func_isoWeekNum (valVector args,
ValueCalc *calc, FuncExtra *);
57Value func_monthname (valVector args,
ValueCalc *calc, FuncExtra *);
62Value func_timevalue (valVector args,
ValueCalc *calc, FuncExtra *);
66Value func_weeksInYear (valVector args,
ValueCalc *calc, FuncExtra *);
72void RegisterDateTimeFunctions()
77 FunctionRepository* repo = FunctionRepository::self();
80 f =
new Function (
"CURRENTDATE", func_currentDate);
83 f =
new Function (
"CURRENTDATETIME", func_currentDateTime);
86 f =
new Function (
"CURRENTTIME", func_currentTime);
89 f =
new Function (
"DATE", func_date);
92 f =
new Function (
"DATEVALUE", func_datevalue);
94 f =
new Function (
"DAY", func_day);
96 f =
new Function (
"DAYNAME", func_dayname);
98 f =
new Function (
"DAYOFYEAR", func_dayOfYear);
101 f =
new Function (
"DAYS", func_days);
102 f->setParamCount (2);
104 f =
new Function (
"DAYS360", func_days360);
105 f->setParamCount (2, 3);
107 f =
new Function (
"DAYSINMONTH", func_daysInMonth);
108 f->setParamCount (2);
110 f =
new Function (
"DAYSINYEAR", func_daysInYear);
112 f =
new Function (
"EASTERSUNDAY", func_easterSunday);
114 f =
new Function (
"EDATE", func_edate);
115 f->setParamCount (2);
117 f =
new Function (
"EOMONTH", func_eomonth);
118 f->setParamCount (2);
120 f =
new Function (
"HOUR", func_hour);
121 f->setParamCount (0, 1);
123 f =
new Function (
"HOURS", func_hour);
124 f->setParamCount (0, 1);
126 f =
new Function (
"ISLEAPYEAR", func_isLeapYear);
128 f =
new Function (
"ISOWEEKNUM", func_isoWeekNum);
130 f =
new Function (
"MINUTE", func_minute);
131 f->setParamCount (0, 1);
133 f =
new Function (
"MINUTES", func_minute);
134 f->setParamCount (0, 1);
136 f =
new Function (
"MONTH", func_month);
138 f =
new Function (
"MONTHNAME", func_monthname);
140 f =
new Function (
"MONTHS", func_months);
141 f->setParamCount (3);
143 f =
new Function (
"NOW", func_currentDateTime);
144 f->setParamCount (0);
146 f =
new Function (
"SECOND", func_second);
147 f->setParamCount (0, 1);
149 f =
new Function (
"SECONDS", func_second);
150 f->setParamCount (0, 1);
152 f =
new Function (
"TIME", func_time);
153 f->setParamCount (3);
155 f =
new Function (
"TIMEVALUE", func_timevalue);
157 f =
new Function (
"TODAY", func_currentDate);
158 f->setParamCount (0);
160 f =
new Function (
"WEEKDAY", func_weekday);
161 f->setParamCount (1, 2);
163 f =
new Function (
"WEEKS", func_weeks);
164 f->setParamCount (3);
166 f =
new Function (
"WEEKSINYEAR", func_weeksInYear);
168 f =
new Function (
"YEAR", func_year);
170 f =
new Function (
"YEARS", func_years);
171 f->setParamCount (3);
178 TQDate date = calc->conv()->asDate (args[0]).
asDate();
179 int months = calc->conv()->asInteger (args[1]).
asInteger();
181 date = calc->conv()->locale()->calendar()->addMonths (date, months);
193 Value modDate = func_edate (args, calc, 0);
194 if (modDate.
isError())
return modDate;
197 TQDate date = modDate.
asDate();
198 date.setYMD (date.year(), date.month(), date.daysInMonth());
207 TQDate date1 = calc->conv()->asDate (args[0]).
asDate();
208 TQDate date2 = calc->conv()->asDate (args[1]).
asDate();
209 bool european =
false;
210 if (args.count() == 3)
211 european = calc->conv()->asBoolean (args[2]).
asBoolean();
216 bool negative =
false;
218 if (date1.daysTo( date2 ) < 0)
228 month1 = date1.month();
229 month2 = date2.month();
230 year1 = date1.year();
231 year2 = date2.year();
243 if ( month1 == 2 && month2 == 2
244 && date1.daysInMonth() == day1
245 && date2.daysInMonth() == day2 )
248 if ( month1 == 2 && date1.daysInMonth() == day1 )
251 if ( day2 == 31 && day1 >= 30 )
258 int result = ( ( year2 - year1 ) * 12 + ( month2 - month1 ) ) * 30
261 return Value (result);
267 Value v = calc->conv()->asDate (args[0]);
270 return Value (date.year ());
276 Value v = calc->conv()->asDate (args[0]);
279 return Value (date.month ());
285 Value v = calc->conv()->asDate (args[0]);
288 return Value (date.day ());
295 if (args.count() == 1)
297 Value v = calc->conv()->asTime (args[0]);
302 time = TQTime::currentTime ();
303 return Value (time.hour ());
310 if (args.count() == 1)
312 Value v = calc->conv()->asTime (args[0]);
317 time = TQTime::currentTime ();
318 return Value (time.minute ());
325 if (args.count() == 1)
327 Value v = calc->conv()->asTime (args[0]);
332 time = TQTime::currentTime ();
333 return Value (time.second ());
343 if (args.count() == 2)
344 method = calc->conv()->asInteger (args[1]).
asInteger();
346 if ( method < 1 || method > 3 )
349 int result = date.dayOfWeek();
353 else if (method == 1)
359 return Value (result);
364Value func_datevalue (valVector args,
ValueCalc *calc, FuncExtra *)
366 if (args[0].isString()) {
367 Value v = calc->conv()->asDate (args[0]);
369 return calc->conv()->asFloat (v);
376Value func_timevalue (valVector args,
ValueCalc *calc, FuncExtra *)
378 if (args[0].isString()) {
379 Value v = calc->conv()->asTime (args[0]);
381 return calc->conv()->asFloat (v);
389 TQDate date1 = calc->conv()->asDate (args[0]).
asDate();
390 TQDate date2 = calc->conv()->asDate (args[1]).
asDate();
391 if (!date1.isValid() || !date2.isValid())
394 int type = calc->conv()->asInteger (args[2]).
asInteger();
398 int years = date2.year() - date1.year();
400 if (date2.month() < date1.month())
402 else if ( (date2.month() == date1.month()) && (date2.day() < date1.day()) )
405 return Value (years);
410 if ( date1.year() == date2.year() )
413 if ( (date1.month() != 1) || (date1.day() != 1) )
414 date1.setYMD(date1.year() + 1, 1, 1);
415 date2.setYMD(date2.year(), 1, 1);
417 return Value (date2.year() - date1.year());
423 TQDate date1 = calc->conv()->asDate (args[0]).
asDate();
424 TQDate date2 = calc->conv()->asDate (args[1]).
asDate();
425 if (!date1.isValid() || !date2.isValid())
428 int type = calc->conv()->asInteger (args[2]).
asInteger();
431 int months = (date2.year() - date1.year()) * 12;
432 months += date2.month() - date1.month();
434 if (date2.day() < date1.day())
435 if (date2.day() != date2.daysInMonth())
438 return Value (months);
443 if (date1.month() == 12)
444 date1.setYMD(date1.year() + 1, 1, 1);
446 date1.setYMD(date1.year(), date1.month() + 1, 1);
447 date2.setYMD(date2.year(), date2.month(), 1);
449 int months = (date2.year() - date1.year()) * 12;
450 months += date2.month() - date1.month();
452 return Value (months);
458 TQDate date1 = calc->conv()->asDate (args[0]).
asDate();
459 TQDate date2 = calc->conv()->asDate (args[1]).
asDate();
460 if (!date1.isValid() || !date2.isValid())
463 int type = calc->conv()->asInteger (args[2]).
asInteger();
464 int days = date1.daysTo (date2);
467 return Value ((
int) (days / 7));
470 int weekStartDay = calc->conv()->locale()->weekStartDay();
472 int dow1 = date1.dayOfWeek();
473 int dow2 = date2.dayOfWeek();
475 days -= (7 + (weekStartDay % 7) - dow1);
476 days -= ((dow2 - weekStartDay) % 7);
478 return Value ((
int) (days / 7));
484 TQDate date1 = calc->conv()->asDate (args[0]).
asDate();
485 TQDate date2 = calc->conv()->asDate (args[1]).
asDate();
486 if (!date1.isValid() || !date2.isValid())
489 return Value (date1.daysTo (date2));
495 int y = calc->conv()->asInteger (args[0]).
asInteger();
496 int m = calc->conv()->asInteger (args[1]).
asInteger();
497 int d = calc->conv()->asInteger (args[2]).
asInteger();
500 if( _date.setYMD (y, m, d))
501 return Value (_date);
508 int number = calc->conv()->asInteger (args[0]).
asInteger();
510 TQString weekName = calc->conv()->locale()->calendar()->weekDayName (number);
511 if (weekName.isNull())
513 return Value (weekName);
517Value func_monthname (valVector args,
ValueCalc *calc, FuncExtra *)
519 int number = calc->conv()->asInteger (args[0]).
asInteger();
521 TQString monthName = calc->conv()->locale()->calendar()->monthName (number,
522 TQDate::currentDate().year());
523 if (monthName.isNull())
525 return Value (monthName);
531 int h = calc->conv()->asInteger (args[0]).
asInteger();
532 int m = calc->conv()->asInteger (args[1]).
asInteger();
533 int s = calc->conv()->asInteger (args[2]).
asInteger();
555 return Value (TQTime (h, m, s));
561 return Value (TQDate::currentDate ());
567 return Value (TQTime::currentTime ());
573 return Value (TQDateTime::currentDateTime ());
577Value func_dayOfYear (valVector args,
ValueCalc *calc, FuncExtra *)
579 Value date = func_date (args, calc, 0);
580 if (date.
isError())
return date;
585Value func_daysInMonth (valVector args,
ValueCalc *calc, FuncExtra *)
587 int y = calc->conv()->asInteger (args[0]).
asInteger();
588 int m = calc->conv()->asInteger (args[1]).
asInteger();
589 TQDate date (y, m, 1);
590 return Value (date.daysInMonth());
594Value func_isLeapYear (valVector args,
ValueCalc *calc, FuncExtra *)
596 int y = calc->conv()->asInteger (args[0]).
asInteger();
597 return Value (TQDate::leapYear (y));
601Value func_daysInYear (valVector args,
ValueCalc *calc, FuncExtra *)
603 int y = calc->conv()->asInteger (args[0]).
asInteger();
604 return Value (TQDate::leapYear (y) ? 366 : 365);
608Value func_weeksInYear (valVector args,
ValueCalc *calc, FuncExtra *)
610 int y = calc->conv()->asInteger (args[0]).
asInteger();
611 TQDate date (y, 12, 31);
612 return Value (date.weekNumber ());
616Value func_easterSunday (valVector args,
ValueCalc *calc, FuncExtra *)
619 int nYear = calc->conv()->asInteger (args[0]).
asInteger();
622 int B,C,D,E,F,G,H,I,K,L,M,N,O;
624 B = int(nYear / 100);
628 F = int((B + 8) / 25);
629 G = int((B - F + 1) / 3);
630 H = (19 * N + B - D - G + 15) % 30;
633 L = (32 + 2 * E + 2 * I - H - K) % 7;
634 M = int((N + 11 * H + 22 * L) / 451);
635 O = H + L - 7 * M + 114;
637 nMonth = int(O / 31);
639 return Value (TQDate (nYear, nMonth, nDay));
643Value func_isoWeekNum (valVector args,
ValueCalc *calc, FuncExtra *)
645 TQDate date = calc->conv()->asDate (args[0]).
asDate();
649 return Value (date.weekNumber());
The ValueCalc class is used to perform all sorts of calculations.
Provides a wrapper for cell value.
TQDate asDate() const
Returns the date representation of this value.
long asInteger() const
Returns the integer value of this value.
static const Value & errorVALUE()
Returns constant reference to VALUE!
bool isError() const
Returns true if this value holds error information.
TQTime asTime() const
Returns the time representation of this value.
bool asBoolean() const
Returns the boolean value of this value.