Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first implementation of datedif #178

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions main/formula/inc/formula/compiler.hrc
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,11 @@
#define SC_OPCODE_BITXOR 406
#define SC_OPCODE_BITLSHIFT 407
#define SC_OPCODE_BITRSHIFT 408
#define SC_OPCODE_DATEDIF 409

#define SC_OPCODE_STOP_2_PAR 409
#define SC_OPCODE_STOP_2_PAR 410

#define SC_OPCODE_LAST_OPCODE_ID 408 /* last OpCode */
#define SC_OPCODE_LAST_OPCODE_ID 409 /* last OpCode */

/*** Interna ***/
#define SC_OPCODE_INTERNAL_BEGIN 9999
Expand Down
4 changes: 4 additions & 0 deletions main/formula/source/core/resource/core_resource.src
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ Resource RID_STRLIST_FUNCTION_NAMES_ENGLISH_ODFF
String SC_OPCODE_BITXOR { Text= "BITXOR" ; };
String SC_OPCODE_BITLSHIFT { Text= "BITLSHIFT" ; };
String SC_OPCODE_BITRSHIFT { Text= "BITRSHIFT" ; };
String SC_OPCODE_DATEDIF { Text = "DATEDIF" ; };

/* BEGIN defined ERROR.TYPE() values. */
String SC_OPCODE_ERROR_NULL { Text = "#NULL!" ; };
Expand Down Expand Up @@ -696,6 +697,7 @@ Resource RID_STRLIST_FUNCTION_NAMES_ENGLISH
String SC_OPCODE_BITXOR { Text = "BITXOR" ; };
String SC_OPCODE_BITLSHIFT { Text= "BITLSHIFT" ; };
String SC_OPCODE_BITRSHIFT { Text= "BITRSHIFT" ; };
String SC_OPCODE_DATEDIF { Text = "DATEDIF" ; };

/* BEGIN defined ERROR.TYPE() values. */
String SC_OPCODE_ERROR_NULL { Text = "#NULL!" ; };
Expand Down Expand Up @@ -1940,6 +1942,8 @@ Resource RID_STRLIST_FUNCTION_NAMES
{
Text [ en-US ] = "BITRSHIFT";
};
String SC_OPCODE_DATEDIF { Text [ en-US ] = "DATEDIF" ; };

/* BEGIN defined ERROR.TYPE() values. */
/* ERROR.TYPE( #NULL! ) == 1 */
String SC_OPCODE_ERROR_NULL
Expand Down
1 change: 1 addition & 0 deletions main/sc/inc/helpids.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@
#define HID_DAI_FUNC_DIFFWEEKS "SC_HID_DAI_FUNC_DIFFWEEKS"
#define HID_DAI_FUNC_DIFFYEARS "SC_HID_DAI_FUNC_DIFFYEARS"
#define HID_DAI_FUNC_ROT13 "SC_HID_DAI_FUNC_ROT13"
#define HID_FUNC_DATEDIF "SC_HID_FUNC_DATEDIF"

#define HID_SCPAGE_OPREDLINBE_FT_CONTENT "SC_HID_SCPAGE_OPREDLINBE_FT_CONTENT"
#define HID_MN_FORMAT_STYLE "SC_HID_MN_FORMAT_STYLE"
Expand Down
1 change: 1 addition & 0 deletions main/sc/source/core/inc/interpre.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ void ScBase();
void ScDecimal();
void ScConvert();
void ScEuroConvert();
void ScDateDif();

//----------------------- Finanzfunktionen ------------------------------------
void ScNPV();
Expand Down
82 changes: 82 additions & 0 deletions main/sc/source/core/tool/interpr2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,88 @@ void ScInterpreter::ScNPV()
}
}

void ScInterpreter::ScDateDif()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScDateDif");
if ( MustHaveParamCount( GetByte(), 3 ) )
{
String ReturnFormat = GetString();
double StartDate = GetDouble();
Date tmpDate = *( pFormatter->GetNullDate() );
tmpDate += (long) ::rtl::math::approxFloor( tmpDate );
sal_Int32 StartDays = tmpDate.GetDay();
sal_Int32 StartMonth = tmpDate.GetMonth();
sal_Int32 StartYear = tmpDate.GetYear();

double EndDate = GetDouble();
tmpDate = *( pFormatter->GetNullDate() );
tmpDate += (long) ::rtl::math::approxFloor( EndDate );
sal_Int32 EndDays = tmpDate.GetDay();
sal_Int32 EndMonth = tmpDate.GetMonth();
sal_Int32 EndYear = tmpDate.GetYear();


switch(ReturnFormat.ToLowerAscii()) {
case d:
// return number of days
PushDouble( EndDate - StartDate);
break;
case m:
// return number of months
sal_Int32 diffMonth = EndMonth - StartMonth + 12 * (EndYear - StartYear);
if (StartDate > EndDate && StartDays > EndDays )
{
diffMonth -= 1;
}
else if ( EndDays > StartDays )
{
diffMonth += 1;
}
PushInt(diffMonth);
break;
case ym:
// return number of months, ignoring years
sal_Int32 diffMonth = EndMonth - StartMonth;
if (StartDate > EndDate && StartDays > EndDays )
{
diffMonth -= 1;
}
else if ( EndMonth > StartMonth && StartDays > EndDays )
{
diffMonth += 1;
}
PushInt(diffMonth);
break;
case md:
// return number of days, ignoring months and years
tmpDate = Date(EndDays,StartMonth,StartYear)
double sameYear = double(tmpDate - *( pFormatter->GetNullDate() );
PushDouble( fabs(sameYear - StartDate));
break;
case yd:
// return number of days, ignoring years
tmpDate = Date(EndDays,EndMonth,StartYear)
double sameYear = double(tmpDate - *( pFormatter->GetNullDate() );
PushDouble( fabs(sameYear - StartDate));
break;
case y:
// return number of years
sal_Int32 diffYears = EndYear - StartYear;
if (EndYear > StartYear) {
if((EndMonth = StartMonth && StartDate >= EndDate) || (EndMonth > StartMonth))
diffYears -= 1;
}
else if ((EndMonth = StartMonth && EndDate >= StartDate) || (StartMonth > EndMonth))
diffYears += 1;
PushInt(diffYears);
break;
default:
// unsupported format
PushIllegalArgument();
}
}
}

void ScInterpreter::ScIRR()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIRR" );
Expand Down
1 change: 1 addition & 0 deletions main/sc/source/core/tool/interpr4.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3771,6 +3771,7 @@ StackVar ScInterpreter::Interpret()
case ocBitXor : ScBitXor(); break;
case ocBitLShift : ScBitLShift(); break;
case ocBitRShift : ScBitRShift(); break;
case ocDateDif : ScDateDif(); break;
case ocNone : nFuncFmtType = NUMBERFORMAT_UNDEFINED; break;
default : PushError( errUnknownOpCode); break;
}
Expand Down
46 changes: 46 additions & 0 deletions main/sc/source/ui/src/scfuncs.src
Original file line number Diff line number Diff line change
Expand Up @@ -9597,6 +9597,52 @@ Resource RID_SC_FUNCTION_DESCRIPTIONS2
};
};

Resource SC_OPCODE_GET_DATEDIF
{
String 1 // description
{
Text [ en-US ] = "Returns the number of whole days, months or years between 'start date' and 'end date'";
};
ExtraData =
{
0;
ID_FUNCTION_GRP_DATETIME;
U2S( HID_FUNC_DATEDIF );
3; 0; 0; 0;
0;
};

String 2 // name of parameter 1 DateDif
{
Text [ en-US ] = "Start date";
};

String 3 // description of parameter 1 DateDif
{
Text [ en-US ] = "The start date";
};

String 4 // name of parameter 2 DateDif
{
Text [ en-US ] = "End date";
};

String 5 // description of parameter 2 DateDif
{
Text [ en-US ] = "The end date";
};

String 6 // name of parameter 3 DateDif
{
Text [ en-US ] = "Format";
};

String 7 // description of parameter 3 DateDif
{
Text [ en-US ] = "Format of the result";
};
};

};

#if defined(U2S)
Expand Down
1 change: 1 addition & 0 deletions main/sc/util/hidother.src
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,4 @@ hidspecial HID_FUNC_BITOR { HelpID = HID_FUNC_BITOR; };
hidspecial HID_FUNC_BITXOR { HelpID = HID_FUNC_BITXOR; };
hidspecial HID_FUNC_BITLSHIFT { HelpID = HID_FUNC_BITLSHIFT; };
hidspecial HID_FUNC_BITRSHIFT { HelpID = HID_FUNC_BITRSHIFT; };
hidspecial HID_FUNC_DATEDIF { HelpID = HID_FUNC_DATEDIF; };