Skip to content

Commit

Permalink
Issue #48 implemented Days function
Browse files Browse the repository at this point in the history
  • Loading branch information
swmal committed Feb 29, 2020
1 parent 43c41ac commit 434095d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public BuiltInFunctions()
Functions["second"] = new Second();
Functions["weeknum"] = new Weeknum();
Functions["weekday"] = new Weekday();
Functions["days"] = new Days();
Functions["days360"] = new Days360();
Functions["yearfrac"] = new Yearfrac();
Functions["edate"] = new Edate();
Expand Down
33 changes: 33 additions & 0 deletions src/EPPlus/FormulaParsing/Excel/Functions/DateTime/Days.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/
A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************
Date Author Change
*************************************************************************************************
01/27/2020 EPPlus Software AB Initial release EPPlus 5
*************************************************************************************************/
using OfficeOpenXml.FormulaParsing.ExpressionGraph;
using System;
using System.Collections.Generic;
using System.Text;

namespace OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime
{
public class Days : ExcelFunction
{
public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
{
ValidateArguments(arguments, 2);
ValidateArguments(arguments, 2);
var numDate1 = ArgToDecimal(arguments, 0);
var numDate2 = ArgToDecimal(arguments, 1);
var endDate = System.DateTime.FromOADate(numDate1);
var startDate = System.DateTime.FromOADate(numDate2);
return CreateResult(endDate.Subtract(startDate).TotalDays, DataType.Date);
}
}
}
2 changes: 1 addition & 1 deletion src/EPPlus/FormulaParsing/Excel/Functions/Math/Odd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, P
var arg = arguments.ElementAt(0).Value;
if (!IsNumeric(arg)) return new CompileResult(eErrorType.Value);
var number = ConvertUtil.GetValueDouble(arg);
if(number % 1 > 0)
if(number % 1 != 0)
{
if (number >= 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ public void DayShouldReturnMonthOfYearWithStringParam()
Assert.AreEqual(12, result.Result);
}

[TestMethod]
public void DaysShouldReturnCorrectResultWithDateTimeTypes()
{
var d1 = new DateTime(2015, 1, 1);
var d2 = new DateTime(2015, 2, 2);
var func = new Days();
var result = func.Execute(FunctionsHelper.CreateArgs(d2, d1), _parsingContext);
Assert.AreEqual(32d, result.Result);
}

[TestMethod]
public void DaysShouldReturnCorrectResultWithDatesAsStrings()
{
var d1 = new DateTime(2015, 1, 1).ToString("yyyy-MM-dd");
var d2 = new DateTime(2015, 2, 2).ToString("yyyy-MM-dd");
var func = new Days();
var result = func.Execute(FunctionsHelper.CreateArgs(d2, d1), _parsingContext);
Assert.AreEqual(32d, result.Result);
}

[TestMethod]
public void MonthShouldReturnMonthOfYear()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ public void HourShouldReturnCorrectResultWhenParsingString()
Assert.AreEqual(10, result);
}

[TestMethod]
public void DaysShouldReturnCorrectResult()
{
var result = _parser.Parse("Days(Date(2015, 2, 2), Date(2015, 1, 1))");
Assert.AreEqual(32d, result);
}

[TestMethod]
public void Day360ShouldReturnCorrectResult()
{
Expand Down

0 comments on commit 434095d

Please sign in to comment.